* SPDX-License-Identifier: AGPL-3.0-or-later */ namespace KTXM\ProviderImap\Smtp\Auth; /** * Supported SASL authentication mechanisms, in preference order. */ enum AuthMechanism: string { case Plain = 'PLAIN'; case Login = 'LOGIN'; /** * Select the most preferred mechanism the server advertises. * * @param list $advertised mechanisms offered by the server (uppercased) */ public static function select(array $advertised): ?self { foreach ([self::Plain, self::Login] as $mechanism) { if (in_array($mechanism->value, $advertised, true)) { return $mechanism; } } return null; } /** * The initial AUTH response for the PLAIN mechanism: * base64( authzid NUL authcid NUL passwd ). */ public static function plainToken(string $username, string $password): string { return base64_encode("\0" . $username . "\0" . $password); } }