]> BookStack Code Mirror - bookstack/blob - app/Access/Oidc/OidcAccessToken.php
Played around with a new app structure
[bookstack] / app / Access / Oidc / OidcAccessToken.php
1 <?php
2
3 namespace BookStack\Access\Oidc;
4
5 use InvalidArgumentException;
6 use League\OAuth2\Client\Token\AccessToken;
7
8 class OidcAccessToken extends AccessToken
9 {
10     /**
11      * Constructs an access token.
12      *
13      * @param array $options An array of options returned by the service provider
14      *                       in the access token request. The `access_token` option is required.
15      *
16      * @throws InvalidArgumentException if `access_token` is not provided in `$options`.
17      */
18     public function __construct(array $options = [])
19     {
20         parent::__construct($options);
21         $this->validate($options);
22     }
23
24     /**
25      * Validate this access token response for OIDC.
26      * As per https://openid.net/specs/openid-connect-basic-1_0.html#TokenOK.
27      */
28     private function validate(array $options): void
29     {
30         // access_token: REQUIRED. Access Token for the UserInfo Endpoint.
31         // Performed on the extended class
32
33         // token_type: REQUIRED. OAuth 2.0 Token Type value. The value MUST be Bearer, as specified in OAuth 2.0
34         // Bearer Token Usage [RFC6750], for Clients using this subset.
35         // Note that the token_type value is case-insensitive.
36         if (strtolower(($options['token_type'] ?? '')) !== 'bearer') {
37             throw new InvalidArgumentException('The response token type MUST be "Bearer"');
38         }
39
40         // id_token: REQUIRED. ID Token.
41         if (empty($options['id_token'])) {
42             throw new InvalidArgumentException('An "id_token" property must be provided');
43         }
44     }
45
46     /**
47      * Get the id token value from this access token response.
48      */
49     public function getIdToken(): string
50     {
51         return $this->getValues()['id_token'];
52     }
53 }