packages/sql-semantics-mysql/src/Statement/Account/User/Identification.php

1<?php
2
3declare(strict_types=1);
4
5namespace SqlSemantics\Platform\MySql\Statement\Account\User;
6
7use SqlSemantics\Platform\MySql\Model\Contract\NameUse;
8use SqlSemantics\Platform\MySql\Model\Diagnostic\Check;
9use SqlSemantics\Platform\MySql\Model\Rendering\Output;
10use SqlSemantics\Platform\MySql\Model\Statement\Identifier\Name;
11use SqlSemantics\Platform\MySql\Model\Statement\Node;
12use SqlSemantics\Platform\MySql\Model\Statement\Snapshot;
13use SqlSemantics\Platform\MySql\Statement\Literal\Text;
14
15/**
16 * One authentication method of an account: `IDENTIFIED [WITH plugin] [BY 'password' | BY RANDOM PASSWORD | AS 'string']`.
17 *
18 * Mirrors LEX_MFA. The password or authentication string is an operand with
19 * its exact value; it is never dropped or masked. A method without a plugin
20 * uses the default authentication plugin of the server. MySQL 5.x also
21 * writes `IDENTIFIED BY PASSWORD 'hash'`.
22 * Source: https://dev.mysql.com/doc/refman/8.4/en/create-user.html#create-user-authentication.
23 *
24 * @visibility public
25 * @example Holding a plugin and its authentication string
26 *     $method = new \SqlSemantics\Platform\MySql\Statement\Account\User\Identification(new \SqlSemantics\Platform\MySql\Model\Statement\Identifier\Name('caching_sha2_password'), \SqlSemantics\Platform\MySql\Statement\Account\User\Credential::Hash, new \SqlSemantics\Platform\MySql\Statement\Literal\Text('$A$005$x'));
27 *     [$method->plugin?->value, $method->secret?->value] // => ['caching_sha2_password', '$A$005$x']
28 */
29final class Identification implements Node
30{
31    use Snapshot;
32
33    /**
34     * @param Name|null $plugin The authentication plugin, when WITH names one
35     * @param Credential $credential What the clause gives the plugin
36     * @param Text|null $secret The password, hash or authentication string, for a credential that carries one
37     */
38    public function __construct(public readonly ?Name $plugin, public readonly Credential $credential, public readonly ?Text $secret = null)
39    {
40        Check::input(($secret !== null) === $credential->written(), 'A password, hash or authentication string is written exactly when the credential carries one.');
41        Check::input($plugin !== null || ($credential !== Credential::None && $credential !== Credential::Hash), 'Only a named plugin is identified alone or by an authentication string.');
42        Check::input($plugin === null || $credential !== Credential::PasswordHash, 'BY PASSWORD names no plugin.');
43    }
44
45    /**
46     * Tells whether the method sets a password of the default plugin: `BY 'password'` or `BY RANDOM PASSWORD`.
47     */
48    public function password(): bool
49    {
50        return $this->plugin === null && ($this->credential === Credential::Password || $this->credential === Credential::RandomPassword);
51    }
52
53    /**
54     * Tells whether REPLACE 'current' may follow the method: a new password, generated or written.
55     */
56    public function replaceable(): bool
57    {
58        return $this->credential === Credential::Password || $this->password();
59    }
60
61    /**
62     * Tells whether RETAIN CURRENT PASSWORD may follow the method: one that gives the plugin a new secret.
63     */
64    public function retainable(): bool
65    {
66        return $this->credential !== Credential::None && $this->credential !== Credential::PasswordHash;
67    }
68
69    /**
70     * Tells whether the method can be the INITIAL AUTHENTICATION of a passwordless account.
71     */
72    public function initial(): bool
73    {
74        return $this->password() || $this->credential === Credential::Hash;
75    }
76
77    /**
78     * Writes the clause.
79     */
80    public function render(Output $out): void
81    {
82        $out->keyword('IDENTIFIED');
83        if ($this->plugin !== null) {
84            $out->keyword('WITH')->name($this->plugin, NameUse::Label);
85        }
86        match ($this->credential) {
87            Credential::None => null,
88            Credential::Password => $out->keyword('BY')->node($this->secret),
89            Credential::RandomPassword => $out->keyword('BY', 'RANDOM', 'PASSWORD'),
90            Credential::Hash => $out->keyword('AS')->node($this->secret),
91            Credential::PasswordHash => $out->keyword('BY', 'PASSWORD')->node($this->secret),
92        };
93    }
94}
95