1313 * source code.
1414 */
1515
16+ use Exception ;
17+ use JsonException ;
1618use League \OAuth2 \Client \Token \AccessToken ;
1719use Neos \Flow \Tests \UnitTestCase ;
1820use Neos \Flow \Utility \Algorithms ;
@@ -29,37 +31,41 @@ public function correctConstructorArguments(): array
2931 '3d47f0eafd6a8b49e32b55103d817b6e4ef489e7 ' ,
3032 'myService ' ,
3133 'ac36cGG4d2Cef1DeuevA7T1u7V4WOUI14 ' ,
32- 'CMc4EHfyMPLw}Tua%rnyxCnrTWMuX3 ' ,
3334 'authorization_code ' ,
3435 'profile oidc '
3536 ]
3637 ];
3738 }
3839
3940 /**
40- * @param string $expectedAuthorizationId
41+ * @param string $authorizationId
4142 * @param string $serviceName
4243 * @param string $clientId
43- * @param string $clientSecret
4444 * @param string $grantType
4545 * @param string $scope
4646 * @test
4747 * @dataProvider correctConstructorArguments
4848 */
49- public function constructSetsAuthorizationIdentifier (string $ expectedAuthorizationId , string $ serviceName , string $ clientId, string $ clientSecret , string $ grantType , string $ scope ): void
49+ public function constructSetsAuthorizationParameters (string $ authorizationId , string $ serviceName , string $ clientId , string $ grantType , string $ scope ): void
5050 {
51- $ authorization = new Authorization ($ serviceName , $ clientId , $ grantType , $ scope );
52- self ::assertSame ($ expectedAuthorizationId , $ authorization ->getAuthorizationId ());
51+ $ authorization = new Authorization ($ authorizationId , $ serviceName , $ clientId , $ grantType , $ scope );
52+ self ::assertSame ($ authorizationId , $ authorization ->getAuthorizationId ());
53+ self ::assertSame ($ serviceName , $ authorization ->getServiceName ());
54+ self ::assertSame ($ clientId , $ authorization ->getClientId ());
55+ self ::assertSame ($ grantType , $ authorization ->getGrantType ());
56+ self ::assertSame ($ scope , $ authorization ->getScope ());
5357 }
5458
5559 /**
5660 * @test
61+ * @throws JsonException
62+ * @throws Exception
5763 */
5864 public function getAccessTokenReturnsClonedObject (): void
5965 {
6066 $ accessToken = $ this ->createValidAccessToken ();
6167
62- $ authorization = new Authorization ('service ' , 'clientId ' ,Authorization::GRANT_AUTHORIZATION_CODE , 'profile ' );
68+ $ authorization = new Authorization ('3d47f0eafd6a8b49e32b55103d817b6e4ef489e7 ' , ' service ' , 'clientId ' ,Authorization::GRANT_AUTHORIZATION_CODE , 'profile ' );
6369 $ authorization ->setAccessToken ($ accessToken );
6470 $ retrievedAccessToken = $ authorization ->getAccessToken ();
6571
@@ -69,52 +75,110 @@ public function getAccessTokenReturnsClonedObject(): void
6975
7076 /**
7177 * @test
78+ * @throws JsonException
79+ * @throws Exception
7280 */
7381 public function getSerializedAccessTokenReturnsCorrectJsonString (): void
7482 {
7583 $ accessToken = $ this ->createValidAccessToken ();
7684
77- $ authorization = new Authorization ('service ' , 'clientId ' , Authorization::GRANT_AUTHORIZATION_CODE , '' );
85+ $ authorization = new Authorization ('3d47f0eafd6a8b49e32b55103d817b6e4ef489e7 ' , ' service ' , 'clientId ' , Authorization::GRANT_AUTHORIZATION_CODE , '' );
7886 $ authorization ->setAccessToken ($ accessToken );
7987
80- $ secondAccessToken = new AccessToken ($ authorization ->getSerializedAccessToken ());
88+ $ secondAccessToken = new AccessToken (json_decode ( $ authorization ->getSerializedAccessToken (), true , 512 , JSON_THROW_ON_ERROR ));
8189 $ this ->assertEquals ($ accessToken , $ secondAccessToken );
8290 }
8391
8492 /**
8593 * @test
94+ * @throws JsonException
95+ * @throws Exception
8696 */
8797 public function getAccessTokenReturnsPreviouslySetSerializedToken (): void
8898 {
8999 $ accessToken = $ this ->createValidAccessToken ();
90100
91- $ authorization = new Authorization ('service ' , 'clientId ' , Authorization::GRANT_AUTHORIZATION_CODE , '' );
92- $ authorization ->setSerializedAccessToken ($ accessToken-> jsonSerialize ( ));
101+ $ authorization = new Authorization ('3d47f0eafd6a8b49e32b55103d817b6e4ef489e7 ' , ' service ' , 'clientId ' , Authorization::GRANT_AUTHORIZATION_CODE , '' );
102+ $ authorization ->setSerializedAccessToken (json_encode ( $ accessToken, JSON_THROW_ON_ERROR , 512 ));
93103
94- $ secondAccessToken = new AccessToken ($ authorization ->getSerializedAccessToken ());
104+ $ secondAccessToken = new AccessToken (json_decode ( $ authorization ->getSerializedAccessToken (), true , 512 , JSON_THROW_ON_ERROR ));
95105 $ this ->assertEquals ($ accessToken , $ secondAccessToken );
96106 }
97107
98108 /**
99109 * @test
100110 */
101- public function calculateAuthorizationIdReturnsSha1 (): void
111+ public function generateAuthorizationIdForClientCredentialsGrantReturnsSha1 (): void
102112 {
103- $ authorizationId = Authorization::calculateAuthorizationId (
104- 'oidc_test ' , 'ac36cGG4d2Cef1DeuevA7T1u7V4WOUI14 ' , 'oidc profile ' , 'authorization_code '
113+ $ authorizationId = Authorization::generateAuthorizationIdForClientCredentialsGrant (
114+ 'oidc_test ' , 'ac36cGG4d2Cef1DeuevA7T1u7V4WOUI14 ' , 'CMc4EHfyMPLw}Tua%rnyxCnrTWMuX3 ' , 'oidc profile '
105115 );
106- self ::assertSame ('21de19f789834af6edff3346e8d9449fcd0d4dae ' , $ authorizationId );
116+ self ::assertSame ('bd55b7bc1b40d6342789c74fcc1900877b3966f4656c5d6a1c0a9111a1da02365ba9f00fcb1d058629446f7ec83d02166b0a8c271cbf1374467e7f294bb4b784 ' , $ authorizationId );
117+ }
118+
119+ /**
120+ * @test
121+ * @throws OAuthClientException
122+ *
123+ * @see https://github.com/flownative/flow-oauth2-client/issues/13
124+ */
125+ public function generateAuthorizationIdForAuthorizationCodeGrantReturnsRandomIdentifiers (): void
126+ {
127+ $ firstAuthorizationId = Authorization::generateAuthorizationIdForAuthorizationCodeGrant (
128+ 'oidc_test ' , 'test ' , 'ac36cGG4d2Cef1DeuevA7T1u7V4WOUI14 '
129+ );
130+
131+ self ::assertStringStartsWith ('oidc_test-test- ' , $ firstAuthorizationId );
132+ self ::assertStringMatchesFormat ('oidc_test-test-%x%x%x%x%x%x%x%x-%x%x%x%x-%x%x%x%x-%x%x%x%x-%x%x%x%x%x%x%x%x%x%x%x%x ' , $ firstAuthorizationId );
133+
134+ $ secondAuthorizationId = Authorization::generateAuthorizationIdForAuthorizationCodeGrant (
135+ 'oidc_test ' , 'test ' , 'ac36cGG4d2Cef1DeuevA7T1u7V4WOUI14 '
136+ );
137+
138+ self ::assertStringStartsWith ('oidc_test-test- ' , $ secondAuthorizationId );
139+ self ::assertStringMatchesFormat ('oidc_test-test-%x%x%x%x%x%x%x%x-%x%x%x%x-%x%x%x%x-%x%x%x%x-%x%x%x%x%x%x%x%x%x%x%x%x ' , $ secondAuthorizationId );
140+
141+ self ::assertNotSame ($ firstAuthorizationId , $ secondAuthorizationId );
142+ }
143+
144+ /**
145+ * @test
146+ */
147+ public function getAccessTokenReturnsNullIfNoTokenWasSet (): void
148+ {
149+ $ authorization = new Authorization ('3d47f0eafd6a8b49e32b55103d817b6e4ef489e7 ' , 'service ' , 'clientId ' , Authorization::GRANT_AUTHORIZATION_CODE , '' );
150+ self ::assertNull ($ authorization ->getAccessToken ());
151+ }
152+
153+ /**
154+ * @test
155+ */
156+ public function getAccessTokenReturnsNullIfTokenCouldNotBeUnserialized (): void
157+ {
158+ $ authorization = new Authorization ('3d47f0eafd6a8b49e32b55103d817b6e4ef489e7 ' , 'service ' , 'clientId ' , Authorization::GRANT_AUTHORIZATION_CODE , '' );
159+ $ authorization ->setSerializedAccessToken ('invalid json syntax ' );
160+ self ::assertNull ($ authorization ->getAccessToken ());
161+ }
162+
163+ /**
164+ * @test
165+ */
166+ public function getScopeReturnsScope (): void
167+ {
168+ $ authorization = new Authorization ('3d47f0eafd6a8b49e32b55103d817b6e4ef489e7 ' , 'service ' , 'clientId ' , Authorization::GRANT_AUTHORIZATION_CODE , '' );
169+ $ authorization ->setScope ('some-custom-scope ' );
170+ self ::assertSame ('some-custom-scope ' , $ authorization ->getScope ());
107171 }
108172
109173 /**
110174 * @return AccessToken
175+ * @throws Exception
111176 */
112177 private function createValidAccessToken (): AccessToken
113178 {
114- $ accessToken = new AccessToken ([
179+ return new AccessToken ([
115180 'access_token ' => Algorithms::generateRandomToken (500 ),
116181 'expires ' => time () + 3600
117182 ]);
118- return $ accessToken ;
119183 }
120184}
0 commit comments