| 
									
										
										
										
											2021-10-12 02:05:16 +08:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-18 00:56:55 +08:00
										 |  |  | namespace BookStack\Access\Oidc; | 
					
						
							| 
									
										
										
										
											2021-10-12 02:05:16 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | use InvalidArgumentException; | 
					
						
							|  |  |  | use League\OAuth2\Client\Token\AccessToken; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-13 06:04:28 +08:00
										 |  |  | class OidcAccessToken extends AccessToken | 
					
						
							| 
									
										
										
										
											2021-10-12 02:05:16 +08:00
										 |  |  | { | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Constructs an access token. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param array $options An array of options returned by the service provider | 
					
						
							| 
									
										
										
										
											2021-10-16 23:01:59 +08:00
										 |  |  |      *                       in the access token request. The `access_token` option is required. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2021-10-12 02:05:16 +08:00
										 |  |  |      * @throws InvalidArgumentException if `access_token` is not provided in `$options`. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function __construct(array $options = []) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         parent::__construct($options); | 
					
						
							|  |  |  |         $this->validate($options); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Validate this access token response for OIDC. | 
					
						
							|  |  |  |      * As per https://openid.net/specs/openid-connect-basic-1_0.html#TokenOK.
 | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     private function validate(array $options): void | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         // access_token: REQUIRED. Access Token for the UserInfo Endpoint.
 | 
					
						
							|  |  |  |         // Performed on the extended class
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // token_type: REQUIRED. OAuth 2.0 Token Type value. The value MUST be Bearer, as specified in OAuth 2.0
 | 
					
						
							|  |  |  |         // Bearer Token Usage [RFC6750], for Clients using this subset.
 | 
					
						
							|  |  |  |         // Note that the token_type value is case-insensitive.
 | 
					
						
							|  |  |  |         if (strtolower(($options['token_type'] ?? '')) !== 'bearer') { | 
					
						
							|  |  |  |             throw new InvalidArgumentException('The response token type MUST be "Bearer"'); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // id_token: REQUIRED. ID Token.
 | 
					
						
							|  |  |  |         if (empty($options['id_token'])) { | 
					
						
							|  |  |  |             throw new InvalidArgumentException('An "id_token" property must be provided'); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Get the id token value from this access token response. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function getIdToken(): string | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return $this->getValues()['id_token']; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-10-16 23:01:59 +08:00
										 |  |  | } |