88
99namespace Imper86 \PhpAllegroApi \Oauth ;
1010
11+ use Http \Client \Common \Exception \ClientErrorException ;
1112use Http \Client \Common \Plugin ;
1213use Http \Client \Common \Plugin \ErrorPlugin ;
1314use Imper86 \HttpClientBuilder \Builder ;
1415use Imper86 \HttpClientBuilder \BuilderInterface ;
1516use Imper86 \PhpAllegroApi \Enum \ContentType ;
1617use Imper86 \PhpAllegroApi \Enum \EndpointHost ;
1718use Imper86 \PhpAllegroApi \Enum \GrantType ;
19+ use Imper86 \PhpAllegroApi \Exceptions \AccessDeniedException ;
20+ use Imper86 \PhpAllegroApi \Exceptions \AuthorizationPendingException ;
21+ use Imper86 \PhpAllegroApi \Exceptions \SlowDownException ;
1822use Imper86 \PhpAllegroApi \Model \CredentialsInterface ;
23+ use Imper86 \PhpAllegroApi \Model \DeviceFlowAuthSession ;
1924use Imper86 \PhpAllegroApi \Model \TokenInterface ;
2025use Imper86 \PhpAllegroApi \Plugin \OauthUriPlugin ;
2126use Imper86 \PhpAllegroApi \Plugin \SandboxUriPlugin ;
27+ use Psr \Http \Client \ClientExceptionInterface ;
2228use Psr \Http \Message \RequestInterface ;
2329use Psr \Http \Message \UriInterface ;
2430
@@ -84,6 +90,50 @@ public function getAuthorizationUri(
8490 return $ uri ;
8591 }
8692
93+ public function getDeviceCode (
94+ ?array $ scope = null
95+ ): DeviceFlowAuthSession {
96+ $ query = [
97+ 'client_id ' => $ this ->credentials ->getClientId (),
98+ ];
99+
100+ if ($ scope ) {
101+ $ query ['scope ' ] = implode (' ' , $ scope );
102+ }
103+
104+ $ response = $ this ->builder ->getHttpClient ()->sendRequest ($ this ->generateRequest ($ query , '/auth/oauth/device ' ));
105+ $ body = json_decode ($ response ->getBody ()->__toString (), true );
106+
107+ return new DeviceFlowAuthSession ($ body ['device_code ' ], $ body ['expires_in ' ], $ body ['user_code ' ], $ body ['interval ' ], $ body ['verification_uri ' ], $ body ['verification_uri_complete ' ]);
108+ }
109+
110+ public function fetchTokenWithDeviceCode (string $ code ): TokenInterface
111+ {
112+ $ query = [
113+ 'grant_type ' => GrantType::DEVICE_CODE ,
114+ 'device_code ' => $ code ,
115+ ];
116+
117+ try {
118+ $ response = $ this ->builder ->getHttpClient ()->sendRequest ($ this ->generateRequest ($ query , '/auth/oauth/token ' ));
119+ } catch (ClientErrorException $ clientErrorException ) {
120+ $ response = $ clientErrorException ->getResponse ();
121+ $ body = json_decode ($ response ->getBody ()->__toString (), true );
122+
123+ if ($ body ['error ' ] == 'slow_down ' ) {
124+ throw new SlowDownException ($ body ['error_description ' ], 0 , $ clientErrorException );
125+ } elseif ($ body ['error ' ] == 'authorization_pending ' ) {
126+ throw new AuthorizationPendingException ($ body ['error_description ' ], 0 , $ clientErrorException );
127+ } elseif ($ body ['error ' ] == 'access_denied ' ) {
128+ throw new AccessDeniedException ($ body ['error_description ' ], 0 , $ clientErrorException );
129+ } else {
130+ throw $ clientErrorException ;
131+ }
132+ }
133+
134+ return $ this ->tokenFactory ->createFromResponse ($ response , GrantType::DEVICE_CODE );
135+ }
136+
87137 public function fetchTokenWithCode (string $ code ): TokenInterface
88138 {
89139 $ query = [
@@ -92,7 +142,7 @@ public function fetchTokenWithCode(string $code): TokenInterface
92142 'redirect_uri ' => $ this ->credentials ->getRedirectUri (),
93143 ];
94144
95- $ response = $ this ->builder ->getHttpClient ()->sendRequest ($ this ->generateRequest ($ query ));
145+ $ response = $ this ->builder ->getHttpClient ()->sendRequest ($ this ->generateRequest ($ query, ' /auth/oauth/token ' ));
96146
97147 return $ this ->tokenFactory ->createFromResponse ($ response , GrantType::AUTHORIZATION_CODE );
98148 }
@@ -105,15 +155,15 @@ public function fetchTokenWithRefreshToken(string $refreshToken): TokenInterface
105155 'redirect_uri ' => $ this ->credentials ->getRedirectUri (),
106156 ];
107157
108- $ response = $ this ->builder ->getHttpClient ()->sendRequest ($ this ->generateRequest ($ query ));
158+ $ response = $ this ->builder ->getHttpClient ()->sendRequest ($ this ->generateRequest ($ query, ' /auth/oauth/token ' ));
109159
110160 return $ this ->tokenFactory ->createFromResponse ($ response , GrantType::REFRESH_TOKEN );
111161 }
112162
113163 public function fetchTokenWithClientCredentials (): TokenInterface
114164 {
115165 $ query = ['grant_type ' => GrantType::CLIENT_CREDENTIALS ];
116- $ response = $ this ->builder ->getHttpClient ()->sendRequest ($ this ->generateRequest ($ query ));
166+ $ response = $ this ->builder ->getHttpClient ()->sendRequest ($ this ->generateRequest ($ query, ' /auth/oauth/token ' ));
117167
118168 return $ this ->tokenFactory ->createFromResponse ($ response , GrantType::CLIENT_CREDENTIALS );
119169 }
@@ -130,13 +180,14 @@ public function removePlugin(string $fqcn): void
130180
131181 /**
132182 * @param string[] $query
183+ * @param string $path
133184 * @return RequestInterface
134185 */
135- private function generateRequest (array $ query ): RequestInterface
186+ private function generateRequest (array $ query, string $ path ): RequestInterface
136187 {
137188 $ uri = $ this ->builder
138189 ->getUriFactory ()
139- ->createUri (' /auth/oauth/token ' )
190+ ->createUri ($ path )
140191 ->withQuery (http_build_query ($ query ));
141192 $ auth = base64_encode (
142193 sprintf (
0 commit comments