packages/sql-semantics-mysql/src/Lowering/Account/IdentificationRule.php

1<?php
2
3declare(strict_types=1);
4
5namespace SqlSemantics\Platform\MySql\Lowering\Account;
6
7use SqlParser\Parser\Node;
8use SqlSemantics\Platform\MySql\Lowering\Lowering;
9use SqlSemantics\Platform\MySql\Model\Diagnostic\ImplementationGap;
10use SqlSemantics\Platform\MySql\Statement\Account\User\Credential;
11use SqlSemantics\Platform\MySql\Statement\Account\User\Identification;
12use SqlSemantics\Platform\MySql\Statement\Literal\Numeral;
13use SqlSemantics\Platform\MySql\Statement\Literal\Text;
14
15/**
16 * Lowers the authentication clauses of CREATE USER and ALTER USER (8.0+).
17 *
18 * Rule: MYSQL-ACCOUNT-IDENTIFICATION-001. Scope: identification,
19 * identified_by_password, identified_by_random_password,
20 * identified_with_plugin, identified_with_plugin_as_auth,
21 * identified_with_plugin_by_password,
22 * identified_with_plugin_by_random_password, opt_create_user_with_mfa,
23 * opt_initial_auth, opt_replace_password, opt_retain_current_password,
24 * opt_discard_old_password, factor. Each clause is one LEX_MFA: the plugin
25 * and what is given to it. Passwords and authentication strings keep their
26 * exact values. Constructs: Identification. Terminates: unit productions
27 * over strict subtrees.
28 * Source: https://dev.mysql.com/doc/refman/8.4/en/create-user.html#create-user-authentication.
29 * Status: Implemented.
30 *
31 * @visibility SqlSemantics\Platform\MySql
32 */
33final class IdentificationRule
34{
35    /**
36     * The unit productions of identification.
37     */
38    private const FORWARD = [
39        'identification: identified_by_password' => true, 'identification: identified_by_random_password' => true, 'identification: identified_with_plugin' => true,
40        'identification: identified_with_plugin_as_auth' => true, 'identification: identified_with_plugin_by_password' => true,
41        'identification: identified_with_plugin_by_random_password' => true,
42    ];
43
44    /**
45     * @param Lowering $lowering The lowering this rule belongs to
46     */
47    public function __construct(private readonly Lowering $lowering)
48    {
49    }
50
51    /**
52     * Lowers an identification or one of the identified_* rules.
53     *
54     * @throws ImplementationGap When the production has no rule
55     */
56    public function identification(Node $identification): Identification
57    {
58        $form = $this->lowering->form($identification);
59        if ($form->node->name === 'identification') {
60            if (!isset(self::FORWARD[$form->signature])) {
61                throw ImplementationGap::production($form);
62            }
63            $form = $this->lowering->form($form->node(0));
64        }
65        $names = $this->lowering->names;
66        $literals = $this->lowering->literals;
67
68        return match ($form->signature) {
69            'identified_by_password: IDENTIFIED_SYM BY TEXT_STRING_password' => new Identification(null, Credential::Password, $literals->text($form->node(2))),
70            'identified_by_random_password: IDENTIFIED_SYM BY RANDOM_SYM PASSWORD' => new Identification(null, Credential::RandomPassword),
71            'identified_with_plugin: IDENTIFIED_SYM WITH ident_or_text' => new Identification($names->identifier($form->node(2)), Credential::None),
72            'identified_with_plugin_as_auth: IDENTIFIED_SYM WITH ident_or_text AS TEXT_STRING_hash' => new Identification($names->identifier($form->node(2)), Credential::Hash, $literals->text($form->node(4))),
73            'identified_with_plugin_by_password: IDENTIFIED_SYM WITH ident_or_text BY TEXT_STRING_password' => new Identification($names->identifier($form->node(2)), Credential::Password, $literals->text($form->node(4))),
74            'identified_with_plugin_by_random_password: IDENTIFIED_SYM WITH ident_or_text BY RANDOM_SYM PASSWORD' => new Identification($names->identifier($form->node(2)), Credential::RandomPassword),
75            default => throw ImplementationGap::production($form),
76        };
77    }
78
79    /**
80     * Lowers the further factors of CREATE USER; an absent clause is empty.
81     *
82     * @return list<Identification>
83     * @throws ImplementationGap When the production has no rule
84     */
85    public function factors(Node $factors): array
86    {
87        $form = $this->lowering->form($factors);
88
89        return match ($form->signature) {
90            'opt_create_user_with_mfa:' => [],
91            'opt_create_user_with_mfa: AND_SYM identification' => [$this->identification($form->node(1))],
92            'opt_create_user_with_mfa: AND_SYM identification AND_SYM identification' => [$this->identification($form->node(1)), $this->identification($form->node(3))],
93            default => throw ImplementationGap::production($form),
94        };
95    }
96
97    /**
98     * Lowers the INITIAL AUTHENTICATION clause of a passwordless account.
99     *
100     * @throws ImplementationGap When the production has no rule
101     */
102    public function initial(Node $initial): Identification
103    {
104        $form = $this->lowering->form($initial);
105        if (!in_array($form->signature, [
106            'opt_initial_auth: INITIAL_SYM AUTHENTICATION_SYM identified_by_random_password',
107            'opt_initial_auth: INITIAL_SYM AUTHENTICATION_SYM identified_with_plugin_as_auth',
108            'opt_initial_auth: INITIAL_SYM AUTHENTICATION_SYM identified_by_password',
109        ], true)) {
110            throw ImplementationGap::production($form);
111        }
112
113        return $this->identification($form->node(2));
114    }
115
116    /**
117     * Lowers an opt_replace_password; an absent clause is null.
118     *
119     * @throws ImplementationGap When the production has no rule
120     */
121    public function replace(Node $replace): ?Text
122    {
123        $form = $this->lowering->form($replace);
124
125        return match ($form->signature) {
126            'opt_replace_password:' => null,
127            'opt_replace_password: REPLACE_SYM TEXT_STRING_password' => $this->lowering->literals->text($form->node(1)),
128            default => throw ImplementationGap::production($form),
129        };
130    }
131
132    /**
133     * Lowers an opt_retain_current_password.
134     *
135     * @throws ImplementationGap When the production has no rule
136     */
137    public function retain(Node $retain): bool
138    {
139        $form = $this->lowering->form($retain);
140
141        return match ($form->signature) {
142            'opt_retain_current_password:' => false,
143            'opt_retain_current_password: RETAIN_SYM CURRENT_SYM PASSWORD' => true,
144            default => throw ImplementationGap::production($form),
145        };
146    }
147
148    /**
149     * Lowers an opt_discard_old_password.
150     *
151     * @throws ImplementationGap When the production has no rule
152     */
153    public function discard(Node $discard): bool
154    {
155        $form = $this->lowering->form($discard);
156
157        return match ($form->signature) {
158            'opt_discard_old_password:' => false,
159            'opt_discard_old_password: DISCARD_SYM OLD_SYM PASSWORD' => true,
160            default => throw ImplementationGap::production($form),
161        };
162    }
163
164    /**
165     * Lowers a factor: the number before FACTOR, as written.
166     *
167     * @throws ImplementationGap When the production has no rule
168     */
169    public function factor(Node $factor): Numeral
170    {
171        $form = $this->lowering->form($factor);
172        if ($form->signature !== 'factor: NUM FACTOR_SYM') {
173            throw ImplementationGap::production($form);
174        }
175
176        return $this->lowering->numbers->token($form->token(0));
177    }
178}
179