3 namespace BookStack\Auth\Access\Oidc;
6 use BookStack\Auth\Access\LoginService;
7 use BookStack\Auth\Access\RegistrationService;
8 use BookStack\Auth\User;
9 use BookStack\Exceptions\JsonDebugException;
10 use BookStack\Exceptions\StoppedAuthenticationException;
11 use BookStack\Exceptions\UserRegistrationException;
13 use Illuminate\Support\Facades\Cache;
14 use League\OAuth2\Client\OptionProvider\HttpBasicAuthOptionProvider;
15 use League\OAuth2\Client\Provider\Exception\IdentityProviderException;
16 use Psr\Http\Client\ClientInterface as HttpClient;
21 * Class OpenIdConnectService
22 * Handles any app-specific OIDC tasks.
26 protected RegistrationService $registrationService;
27 protected LoginService $loginService;
28 protected HttpClient $httpClient;
31 * OpenIdService constructor.
33 public function __construct(RegistrationService $registrationService, LoginService $loginService, HttpClient $httpClient)
35 $this->registrationService = $registrationService;
36 $this->loginService = $loginService;
37 $this->httpClient = $httpClient;
41 * Initiate an authorization flow.
43 * @throws OidcException
45 * @return array{url: string, state: string}
47 public function login(): array
49 $settings = $this->getProviderSettings();
50 $provider = $this->getProvider($settings);
53 'url' => $provider->getAuthorizationUrl(),
54 'state' => $provider->getState(),
59 * Process the Authorization response from the authorization server and
60 * return the matching, or new if registration active, user matched to the
61 * authorization server. Throws if the user cannot be auth if not authenticated.
63 * @throws JsonDebugException
64 * @throws OidcException
65 * @throws StoppedAuthenticationException
66 * @throws IdentityProviderException
68 public function processAuthorizeResponse(?string $authorizationCode): User
70 $settings = $this->getProviderSettings();
71 $provider = $this->getProvider($settings);
73 // Try to exchange authorization code for access token
74 $accessToken = $provider->getAccessToken('authorization_code', [
75 'code' => $authorizationCode,
78 return $this->processAccessTokenCallback($accessToken, $settings);
82 * @throws OidcException
84 protected function getProviderSettings(): OidcProviderSettings
86 $config = $this->config();
87 $settings = new OidcProviderSettings([
88 'issuer' => $config['issuer'],
89 'clientId' => $config['client_id'],
90 'clientSecret' => $config['client_secret'],
91 'redirectUri' => url('/oidc/callback'),
92 'authorizationEndpoint' => $config['authorization_endpoint'],
93 'tokenEndpoint' => $config['token_endpoint'],
96 // Use keys if configured
97 if (!empty($config['jwt_public_key'])) {
98 $settings->keys = [$config['jwt_public_key']];
102 if ($config['discover'] ?? false) {
104 $settings->discoverFromIssuer($this->httpClient, Cache::store(null), 15);
105 } catch (OidcIssuerDiscoveryException $exception) {
106 throw new OidcException('OIDC Discovery Error: ' . $exception->getMessage());
110 $settings->validate();
116 * Load the underlying OpenID Connect Provider.
118 protected function getProvider(OidcProviderSettings $settings): OidcOAuthProvider
120 return new OidcOAuthProvider($settings->arrayForProvider(), [
121 'httpClient' => $this->httpClient,
122 'optionProvider' => new HttpBasicAuthOptionProvider(),
127 * Calculate the display name.
129 protected function getUserDisplayName(OidcIdToken $token, string $defaultValue): string
131 $displayNameAttr = $this->config()['display_name_claims'];
134 foreach ($displayNameAttr as $dnAttr) {
135 $dnComponent = $token->getClaim($dnAttr) ?? '';
136 if ($dnComponent !== '') {
137 $displayName[] = $dnComponent;
141 if (count($displayName) == 0) {
142 $displayName[] = $defaultValue;
145 return implode(' ', $displayName);
149 * Extract the details of a user from an ID token.
151 * @return array{name: string, email: string, external_id: string}
153 protected function getUserDetails(OidcIdToken $token): array
155 $id = $token->getClaim('sub');
158 'external_id' => $id,
159 'email' => $token->getClaim('email'),
160 'name' => $this->getUserDisplayName($token, $id),
165 * Processes a received access token for a user. Login the user when
166 * they exist, optionally registering them automatically.
168 * @throws OidcException
169 * @throws JsonDebugException
170 * @throws StoppedAuthenticationException
172 protected function processAccessTokenCallback(OidcAccessToken $accessToken, OidcProviderSettings $settings): User
174 $idTokenText = $accessToken->getIdToken();
175 $idToken = new OidcIdToken(
181 if ($this->config()['dump_user_details']) {
182 throw new JsonDebugException($idToken->getAllClaims());
186 $idToken->validate($settings->clientId);
187 } catch (OidcInvalidTokenException $exception) {
188 throw new OidcException("ID token validate failed with error: {$exception->getMessage()}");
191 $userDetails = $this->getUserDetails($idToken);
192 $isLoggedIn = auth()->check();
194 if (empty($userDetails['email'])) {
195 throw new OidcException(trans('errors.oidc_no_email_address'));
199 throw new OidcException(trans('errors.oidc_already_logged_in'));
203 $user = $this->registrationService->findOrRegister(
204 $userDetails['name'],
205 $userDetails['email'],
206 $userDetails['external_id']
208 } catch (UserRegistrationException $exception) {
209 throw new OidcException($exception->getMessage());
212 $this->loginService->login($user, 'oidc');
218 * Get the OIDC config from the application.
220 protected function config(): array
222 return config('oidc');