| 37 | | class AuthEntraId extends Auth implements AuthDriverInterface |
| 38 | | { |
| 39 | | private string $oAuthVerifier = ''; |
| 40 | | |
| 41 | | private string $oAuthChallenge; |
| 42 | | |
| 43 | | private const string ENTRAID_CHALLENGE_METHOD = 'S256'; |
| 44 | | |
| 45 | | private const string ENTRAID_LOGOUT_URL = 'https://login.microsoftonline.com/common/wsfederation?wa=wsignout1.0'; |
| 46 | | |
| 47 | | |
| 48 | | |
| 49 | | |
| 50 | | public function __construct( |
| 51 | | Configuration $configuration, |
| 52 | | private readonly OAuth $oAuth, |
| 53 | | ) { |
| 54 | | $this->configuration = $configuration; |
| 55 | | |
| 56 | | parent::__construct($configuration); |
| 57 | | } |
| 58 | | |
| 59 | | |
| 60 | | |
| 61 | | |
| 62 | | |
| 63 | | public function create(string $login, #[SensitiveParameter] string $password, string $domain = ''): mixed |
| 64 | | { |
| 65 | | $result = false; |
| 66 | | $user = new User($this->configuration); |
| 67 | | |
| 68 | | try { |
| 69 | | $result = $user->createUser($login, '', $domain); |
| 70 | | } catch (\Exception $exception) { |
| 71 | | $this->configuration->getLogger()->info($exception->getMessage()); |
| 72 | | } |
| 73 | | |
| 74 | | $user->setStatus('active'); |
| 75 | | $user->setAuthSource(AuthenticationSourceType::AUTH_AZURE->value); |
| 76 | | |
| 77 | | |
| 78 | | $user->setUserData([ |
| 79 | | 'display_name' => $this->oAuth->getName(), |
| 80 | | 'email' => $this->oAuth->getMail(), |
| 81 | | ]); |
| 82 | | |
| 83 | | return $result; |
| 84 | | } |
| 85 | | |
| 86 | | |
| 87 | | |
| 88 | | |
| 89 | | public function update(string $login, #[SensitiveParameter] string $password): bool |
| 90 | | { |
| 91 | | return true; |
| 92 | | } |
| 93 | | |
| 94 | | |
| 95 | | |
| 96 | | |
| 97 | | public function delete(string $login): bool |
| 98 | | { |
| 99 | | return true; |
| 100 | | } |
| 101 | | |
| 102 | | |
| 103 | | |
| 104 | | |
| 105 | | |
| 106 | | public function checkCredentials( |
| 107 | | string $login, |
| 108 | | #[SensitiveParameter] |
| 109 | | string $password, |
| 110 | | ?array $optionalData = [], |
| 111 | | ): bool { |
| 112 | | $this->create($login, ''); |
| 113 | | return true; |
| 114 | | } |
| 115 | | |
| 116 | | |
| 117 | | |
| 118 | | |
| 119 | | public function isValidLogin(string $login, ?array $optionalData = []): int |
| 120 | | { |
| 121 | | if ($login === $this->oAuth->getMail()) { |
| 122 | | return 1; |
| 123 | | } |
| 124 | | |
| 125 | | return 0; |
| 126 | | } |
| 127 | | |
| 128 | | |
| 129 | | |
| 130 | | |
| 131 | | |
| 132 | | |
| 133 | | public function authorize(): RedirectResponse |
| 134 | | { |
| 135 | | $this->createOAuthChallenge(); |
| 136 | | $this->oAuth->getEntraIdSession()->setCurrentSessionKey(); |
| 137 | | $this->oAuth->getEntraIdSession()->set(EntraIdSession::ENTRA_ID_OAUTH_VERIFIER, $this->oAuthVerifier); |
| 138 | | $this->oAuth->getEntraIdSession()->setCookie( |
| 139 | | EntraIdSession::ENTRA_ID_OAUTH_VERIFIER, |
| 140 | | $this->oAuthVerifier, |
| 141 | | 7200, |
| 142 | | false, |
| 143 | | ); |
| 144 | | |
| 145 | | $oAuthURL = sprintf( |
| 146 | | 'https://login.microsoftonline.com/%s/oauth2/v2.0/authorize' |
| 147 | | . '?response_type=code&client_id=%s&redirect_uri=%s&scope=%s&code_challenge=%s&code_challenge_method=%s', |
| 148 | | AAD_OAUTH_TENANTID, |
| 149 | | AAD_OAUTH_CLIENTID, |
| 150 | | urlencode($this->configuration->getDefaultUrl() . 'services/azure/callback.php'), |
| 151 | | AAD_OAUTH_SCOPE, |
| 152 | | $this->oAuthChallenge, |
| 153 | | self::ENTRAID_CHALLENGE_METHOD, |
| 154 | | ); |
| 155 | | |
| 156 | | return new RedirectResponse($oAuthURL); |
| 157 | | } |
| 158 | | |
| 159 | | |
| 160 | | |
| 161 | | |
| 162 | | |
| 163 | | public function logout(): RedirectResponse |
| 164 | | { |
| 165 | | return new RedirectResponse(self::ENTRAID_LOGOUT_URL); |
| 166 | | } |
| 167 | | |
| 168 | | |
| 169 | | |
| 170 | | |
| 171 | | |
| 172 | | |
| 173 | | |
| 174 | | private function createOAuthChallenge(): void |
| 175 | | { |
| 176 | | $verifier = $this->oAuthVerifier; |
| 177 | | |
| 178 | | if ($this->oAuthVerifier === '' || $this->oAuthVerifier === '0') { |
| 179 | | $chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-._~'; |
| 180 | | $charLen = strlen($chars) - 1; |
| 181 | | $verifier = ''; |
| 182 | | |
| 183 | | for ($i = 0; $i < 128; ++$i) { |
| 184 | | $verifier .= $chars[random_int(0, $charLen)]; |
| 185 | | } |
| 186 | | |
| 187 | | $this->oAuthVerifier = $verifier; |
| 188 | | } |
| 189 | | |
| 190 | | $this->oAuthChallenge = str_replace( |
| 191 | | search: '=', |
| 192 | | replace: '', |
| 193 | | subject: strtr(string: base64_encode(pack('H*', hash('sha256', $verifier))), from: '+/', to: '-_'), |
| 194 | | ); |
| 195 | | } |
| 196 | | } |