|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace SimpleSAML\OpenID\Core; |
| 6 | + |
| 7 | +use SimpleSAML\OpenID\Codebooks\ClaimsEnum; |
| 8 | +use SimpleSAML\OpenID\Codebooks\UriPattern; |
| 9 | +use SimpleSAML\OpenID\Exceptions\LogoutTokenException; |
| 10 | +use SimpleSAML\OpenID\Jws\ParsedJws; |
| 11 | + |
| 12 | +/** |
| 13 | + * Logout Token abstraction from |
| 14 | + * https://openid.net/specs/openid-connect-backchannel-1_0.html#LogoutToken |
| 15 | + */ |
| 16 | +class LogoutToken extends ParsedJws |
| 17 | +{ |
| 18 | + public function getIssuer(): string |
| 19 | + { |
| 20 | + // REQUIRED. Issuer Identifier, as specified in Section 2 of |
| 21 | + // [OpenID.Core]. |
| 22 | + $iss = parent::getIssuer() ?? throw new LogoutTokenException('No Issuer claim found.'); |
| 23 | + |
| 24 | + // We will leave the possibility of http usage for local testing purposes. |
| 25 | + return $this->helpers->type()->enforceUri($iss, 'Issuer claim', UriPattern::HttpNoQueryNoFragment->value); |
| 26 | + } |
| 27 | + |
| 28 | + |
| 29 | + public function getAudience(): array |
| 30 | + { |
| 31 | + // REQUIRED. Audience(s), as specified in Section 2 of [OpenID.Core]. |
| 32 | + return parent::getAudience() ?? throw new LogoutTokenException('No Audience claim found.'); |
| 33 | + } |
| 34 | + |
| 35 | + |
| 36 | + public function getIssuedAt(): int |
| 37 | + { |
| 38 | + // REQUIRED. Issued at time, as specified in Section 2 of [OpenID.Core]. |
| 39 | + return parent::getIssuedAt() ?? throw new LogoutTokenException('No Issued At claim found.'); |
| 40 | + } |
| 41 | + |
| 42 | + |
| 43 | + public function getExpirationTime(): int |
| 44 | + { |
| 45 | + // REQUIRED. Expiration time, as specified in Section 2 of |
| 46 | + // [OpenID.Core]. |
| 47 | + return parent::getExpirationTime() ?? throw new LogoutTokenException('No Expiration Time claim found.'); |
| 48 | + } |
| 49 | + |
| 50 | + |
| 51 | + public function getJwtId(): string |
| 52 | + { |
| 53 | + // REQUIRED. Unique identifier for the token, as specified in Section 9 |
| 54 | + // of [OpenID.Core]. |
| 55 | + return parent::getJwtId() ?? throw new LogoutTokenException('No JWT ID claim found.'); |
| 56 | + } |
| 57 | + |
| 58 | + |
| 59 | + /** |
| 60 | + * @throws \SimpleSAML\OpenID\Exceptions\JwsException |
| 61 | + * @throws \SimpleSAML\OpenID\Exceptions\LogoutTokenException |
| 62 | + * @return mixed[] |
| 63 | + */ |
| 64 | + public function getEvents(): array |
| 65 | + { |
| 66 | + // REQUIRED. Claim whose value is a JSON object containing the member |
| 67 | + // name http://schemas.openid.net/event/backchannel-logout. This |
| 68 | + // declares that the JWT is a Logout Token. The corresponding member |
| 69 | + // value MUST be a JSON object and SHOULD be the empty JSON object {}. |
| 70 | + $events = $this->getPayloadClaim(ClaimsEnum::Events->value); |
| 71 | + |
| 72 | + if (is_null($events)) { |
| 73 | + throw new LogoutTokenException('No Events claim found.'); |
| 74 | + } |
| 75 | + |
| 76 | + if ( |
| 77 | + (!is_array($events)) || |
| 78 | + (!array_key_exists('http://schemas.openid.net/event/backchannel-logout', $events)) |
| 79 | + ) { |
| 80 | + throw new LogoutTokenException('Malformed events claim.'); |
| 81 | + } |
| 82 | + |
| 83 | + return $events; |
| 84 | + } |
| 85 | + |
| 86 | + |
| 87 | + /** |
| 88 | + * @return ?non-empty-string |
| 89 | + * @throws \SimpleSAML\OpenID\Exceptions\JwsException |
| 90 | + * @throws \SimpleSAML\OpenID\Exceptions\InvalidValueException |
| 91 | + */ |
| 92 | + public function getSessionId(): ?string |
| 93 | + { |
| 94 | + // OPTIONAL. Session ID - String identifier for a Session. This |
| 95 | + // represents a Session of a User Agent or device for a logged-in |
| 96 | + // End-User at an RP. Different sid values are used to identify |
| 97 | + // distinct sessions at an OP. The sid value need only be unique in |
| 98 | + // the context of a particular issuer. Its contents are opaque to the |
| 99 | + // RP. Its syntax is the same as an OAuth 2.0 Client Identifier. |
| 100 | + |
| 101 | + $sid = $this->getPayloadClaim(ClaimsEnum::Sid->value); |
| 102 | + |
| 103 | + if (is_null($sid)) { |
| 104 | + return null; |
| 105 | + } |
| 106 | + |
| 107 | + return $this->helpers->type()->ensureNonEmptyString($sid); |
| 108 | + } |
| 109 | + |
| 110 | + |
| 111 | + /** |
| 112 | + * @throws \SimpleSAML\OpenID\Exceptions\JwsException |
| 113 | + * @throws \SimpleSAML\OpenID\Exceptions\InvalidValueException |
| 114 | + */ |
| 115 | + public function getNonce(): null |
| 116 | + { |
| 117 | + $nonce = $this->getPayloadClaim(ClaimsEnum::Nonce->value); |
| 118 | + |
| 119 | + if (!is_null($nonce)) { |
| 120 | + throw new LogoutTokenException('Nonce claim is forbidden in Logout Token.'); |
| 121 | + } |
| 122 | + |
| 123 | + return null; |
| 124 | + } |
| 125 | + |
| 126 | + |
| 127 | + /** |
| 128 | + * @throws \SimpleSAML\OpenID\Exceptions\LogoutTokenException |
| 129 | + * @throws \SimpleSAML\OpenID\Exceptions\InvalidValueException |
| 130 | + * @throws \SimpleSAML\OpenID\Exceptions\JwsException |
| 131 | + */ |
| 132 | + protected function validateSubOrSidOrBoth(): void |
| 133 | + { |
| 134 | + if ( |
| 135 | + is_null($this->getSubject()) && |
| 136 | + is_null($this->getSessionId()) |
| 137 | + ) { |
| 138 | + throw new LogoutTokenException('Missing Subject and Session ID claim in Logout Token.'); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + |
| 143 | + /** |
| 144 | + * @throws \SimpleSAML\OpenID\Exceptions\JwsException |
| 145 | + */ |
| 146 | + protected function validate(): void |
| 147 | + { |
| 148 | + $this->validateByCallbacks( |
| 149 | + $this->getIssuer(...), |
| 150 | + $this->getAudience(...), |
| 151 | + $this->getIssuedAt(...), |
| 152 | + $this->getExpirationTime(...), |
| 153 | + $this->getJwtId(...), |
| 154 | + $this->getEvents(...), |
| 155 | + $this->getSessionId(...), |
| 156 | + $this->getNonce(...), |
| 157 | + $this->validateSubOrSidOrBoth(...), |
| 158 | + $this->getAlgorithm(...), |
| 159 | + ); |
| 160 | + } |
| 161 | +} |
0 commit comments