1717namespace SimpleSAML \Module \oidc \Entities ;
1818
1919use DateTimeImmutable ;
20+ use Lcobucci \JWT \Configuration ;
2021use Lcobucci \JWT \Token ;
22+ use League \OAuth2 \Server \CryptKey ;
2123use League \OAuth2 \Server \Entities \ClientEntityInterface as OAuth2ClientEntityInterface ;
2224use League \OAuth2 \Server \Entities \Traits \AccessTokenTrait ;
2325use League \OAuth2 \Server \Entities \Traits \EntityTrait ;
2426use League \OAuth2 \Server \Entities \Traits \TokenEntityTrait ;
2527use PDO ;
2628use SimpleSAML \Module \oidc \Entities \Interfaces \AccessTokenEntityInterface ;
27- use SimpleSAML \Module \oidc \Entities \Interfaces \ClientEntityInterface ;
2829use SimpleSAML \Module \oidc \Entities \Interfaces \EntityStringRepresentationInterface ;
2930use SimpleSAML \Module \oidc \Entities \Traits \AssociateWithAuthCodeTrait ;
3031use SimpleSAML \Module \oidc \Entities \Traits \RevokeTokenTrait ;
31- use SimpleSAML \Module \oidc \Server \Exceptions \OidcServerException ;
3232use SimpleSAML \Module \oidc \Services \JsonWebTokenBuilderService ;
33- use SimpleSAML \Module \oidc \Utils \TimestampGenerator ;
3433use Stringable ;
3534
3635/**
@@ -57,86 +56,35 @@ class AccessTokenEntity implements AccessTokenEntityInterface, EntityStringRepre
5756 protected array $ requestedClaims ;
5857
5958 /**
60- * Constructor.
61- */
62- private function __construct ()
63- {
64- }
65-
66- /**
67- * Create new Access Token from data.
68- *
6959 * @param \League\OAuth2\Server\Entities\ScopeEntityInterface[] $scopes
7060 */
71- public static function fromData (
61+ public function __construct (
62+ string $ id ,
7263 OAuth2ClientEntityInterface $ clientEntity ,
7364 array $ scopes ,
65+ DateTimeImmutable $ expiryDateTime ,
66+ CryptKey $ privateKey ,
67+ protected JsonWebTokenBuilderService $ jsonWebTokenBuilderService ,
7468 int |string $ userIdentifier = null ,
7569 string $ authCodeId = null ,
7670 array $ requestedClaims = null ,
77- ): self {
78- $ accessToken = new self ();
79-
80- $ accessToken ->setClient ($ clientEntity );
81- $ accessToken ->setUserIdentifier ($ userIdentifier );
82- $ accessToken ->setAuthCodeId ($ authCodeId );
71+ bool $ isRevoked = false ,
72+ Configuration $ jwtConfiguration = null ,
73+ ) {
74+ $ this ->setIdentifier ($ id );
75+ $ this ->setClient ($ clientEntity );
8376 foreach ($ scopes as $ scope ) {
84- $ accessToken ->addScope ($ scope );
77+ $ this ->addScope ($ scope );
8578 }
86- $ accessToken ->setRequestedClaims ($ requestedClaims ?? []);
87-
88- return $ accessToken ;
89- }
90-
91- /**
92- * @throws \Exception
93- * @throws \JsonException
94- * @throws \SimpleSAML\Module\oidc\Server\Exceptions\OidcServerException
95- */
96- public static function fromState (array $ state ): self
97- {
98- $ accessToken = new self ();
99-
100- if (
101- !is_string ($ state ['scopes ' ]) ||
102- !is_string ($ state ['id ' ]) ||
103- !is_string ($ state ['expires_at ' ]) ||
104- !is_a ($ state ['client ' ], ClientEntityInterface::class)
105- ) {
106- throw OidcServerException::serverError ('Invalid Access Token Entity state ' );
107- }
108-
109- $ stateScopes = json_decode ($ state ['scopes ' ], true , 512 , JSON_THROW_ON_ERROR );
110- if (!is_array ($ stateScopes )) {
111- throw OidcServerException::serverError ('Invalid Access Token Entity state: scopes ' );
79+ $ this ->setExpiryDateTime ($ expiryDateTime );
80+ $ this ->setPrivateKey ($ privateKey );
81+ $ this ->setUserIdentifier ($ userIdentifier );
82+ $ this ->setAuthCodeId ($ authCodeId );
83+ $ this ->setRequestedClaims ($ requestedClaims ?? []);
84+ if ($ isRevoked ) {
85+ $ this ->revoke ();
11286 }
113-
114- /** @psalm-var string $scope */
115- $ scopes = array_map (fn (string $ scope ) => ScopeEntity::fromData ($ scope ), $ stateScopes );
116-
117- $ accessToken ->identifier = $ state ['id ' ];
118- $ accessToken ->scopes = $ scopes ;
119- // TODO mivanci move to new 'utcImmutable' method in TimestampGenerator.
120- $ accessToken ->expiryDateTime = DateTimeImmutable::createFromMutable (
121- TimestampGenerator::utc ($ state ['expires_at ' ]),
122- );
123- $ accessToken ->userIdentifier = empty ($ state ['user_id ' ]) ? null : (string )$ state ['user_id ' ];
124- $ accessToken ->client = $ state ['client ' ];
125- $ accessToken ->isRevoked = (bool ) $ state ['is_revoked ' ];
126- $ accessToken ->authCodeId = empty ($ state ['auth_code_id ' ]) ? null : (string )$ state ['auth_code_id ' ];
127-
128- $ stateRequestedClaims = json_decode (
129- empty ($ state ['requested_claims ' ]) ? '[] ' : (string )$ state ['requested_claims ' ],
130- true ,
131- 512 ,
132- JSON_THROW_ON_ERROR ,
133- );
134- if (!is_array ($ stateRequestedClaims )) {
135- throw OidcServerException::serverError ('Invalid Access Token Entity state: requested claims ' );
136- }
137- $ accessToken ->requestedClaims = $ stateRequestedClaims ;
138-
139- return $ accessToken ;
87+ $ jwtConfiguration !== null ? $ this ->jwtConfiguration = $ jwtConfiguration : $ this ->initJwtConfiguration ();
14088 }
14189
14290 /**
@@ -199,9 +147,8 @@ public function toString(): ?string
199147 */
200148 protected function convertToJWT (): Token
201149 {
202- $ jwtBuilderService = new JsonWebTokenBuilderService ();
203150 /** @psalm-suppress ArgumentTypeCoercion */
204- $ jwtBuilder = $ jwtBuilderService ->getProtocolJwtBuilder ()
151+ $ jwtBuilder = $ this -> jsonWebTokenBuilderService ->getProtocolJwtBuilder ()
205152 ->permittedFor ($ this ->getClient ()->getIdentifier ())
206153 ->identifiedBy ((string )$ this ->getIdentifier ())
207154 ->issuedAt (new DateTimeImmutable ())
@@ -210,6 +157,6 @@ protected function convertToJWT(): Token
210157 ->relatedTo ((string ) $ this ->getUserIdentifier ())
211158 ->withClaim ('scopes ' , $ this ->getScopes ());
212159
213- return $ jwtBuilderService ->getSignedProtocolJwt ($ jwtBuilder );
160+ return $ this -> jsonWebTokenBuilderService ->getSignedProtocolJwt ($ jwtBuilder );
214161 }
215162}
0 commit comments