@@ -52,40 +52,111 @@ class PrestaShop extends AbstractProvider
5252 */
5353 protected $ uiLocales ;
5454
55+ /**
56+ * @var WellKnown
57+ */
58+ protected $ wellKnown ;
59+
60+ /**
61+ * @var bool
62+ */
63+ protected $ verify = true ;
64+
65+ /**
66+ * @param array $options
67+ * @param array $collaborators
68+ *
69+ * @throws \Exception
70+ */
71+ public function __construct (array $ options = [], array $ collaborators = [])
72+ {
73+ parent ::__construct ($ options , $ collaborators );
74+ }
75+
76+ /**
77+ * @return string
78+ */
79+ public function getOauth2Url ()
80+ {
81+ return 'https://oauth.prestashop.com ' ;
82+ }
83+
84+ /**
85+ * @return WellKnown
86+ */
87+ public function getWellKnown ()
88+ {
89+ /* @phpstan-ignore-next-line */
90+ if (!isset ($ this ->wellKnown )) {
91+ try {
92+ $ this ->wellKnown = new WellKnown (
93+ $ this ->fetchWellKnown ($ this ->getOauth2Url (), $ this ->verify )
94+ );
95+ } catch (\Error $ e ) {
96+ } catch (\Exception $ e ) {
97+ }
98+ if (isset ($ e )) {
99+ $ this ->wellKnown = new WellKnown ();
100+ }
101+ }
102+
103+ return $ this ->wellKnown ;
104+ }
105+
106+ /**
107+ * @param string $url
108+ * @param bool $secure
109+ *
110+ * @return array
111+ *
112+ * @throws \Exception
113+ */
114+ protected function fetchWellKnown ($ url , $ secure = true )
115+ {
116+ $ wellKnownUrl = $ url ;
117+ if (strpos ($ wellKnownUrl , '/.well-known ' ) === false ) {
118+ $ wellKnownUrl = preg_replace ('/\/?$/ ' , '/.well-known/openid-configuration ' , $ wellKnownUrl );
119+ }
120+
121+ $ response = $ this ->getResponse ($ this ->getRequest ('GET ' , $ wellKnownUrl ));
122+
123+ return json_decode ($ response ->getBody (), true );
124+ }
125+
55126 /**
56127 * @return string
57128 */
58- public function getBaseAuthorizationUrl (): string
129+ public function getBaseAuthorizationUrl ()
59130 {
60- return ' https://oauth.prestashop.com/oauth2/auth ' ;
131+ return $ this -> getWellKnown ()-> authorization_endpoint ;
61132 }
62133
63134 /**
64135 * @param array $params
65136 *
66137 * @return string
67138 */
68- public function getBaseAccessTokenUrl (array $ params ): string
139+ public function getBaseAccessTokenUrl (array $ params )
69140 {
70- return ' https://oauth.prestashop.com/oauth2/token ' ;
141+ return $ this -> getWellKnown ()-> token_endpoint ;
71142 }
72143
73144 /**
74145 * @param AccessToken $token
75146 *
76147 * @return string
77148 */
78- public function getResourceOwnerDetailsUrl (AccessToken $ token ): string
149+ public function getResourceOwnerDetailsUrl (AccessToken $ token )
79150 {
80- return ' https://oauth.prestashop.com/userinfo ' ;
151+ return $ this -> getWellKnown ()-> userinfo_endpoint ;
81152 }
82153
83154 /**
84155 * @param array $options
85156 *
86157 * @return string[]
87158 */
88- protected function getAuthorizationParameters (array $ options ): array
159+ protected function getAuthorizationParameters (array $ options )
89160 {
90161 if (empty ($ options ['prompt ' ]) && $ this ->prompt ) {
91162 $ options ['prompt ' ] = $ this ->prompt ;
@@ -107,15 +178,15 @@ protected function getAuthorizationParameters(array $options): array
107178 /**
108179 * @return string[]
109180 */
110- public function getDefaultScopes (): array
181+ public function getDefaultScopes ()
111182 {
112183 return ['openid ' , 'offline_access ' ];
113184 }
114185
115186 /**
116187 * @return string
117188 */
118- protected function getScopeSeparator (): string
189+ protected function getScopeSeparator ()
119190 {
120191 return ' ' ;
121192 }
@@ -128,13 +199,13 @@ protected function getScopeSeparator(): string
128199 *
129200 * @throws IdentityProviderException
130201 */
131- protected function checkResponse (ResponseInterface $ response , $ data ): void
202+ protected function checkResponse (ResponseInterface $ response , $ data )
132203 {
133204 if ($ response ->getStatusCode () !== 200 ) {
134205 $ errorDescription = '' ;
135206 $ error = '' ;
136207 if (\is_array ($ data ) && !empty ($ data )) {
137- $ errorDescription = $ data ['error_description ' ] ?? $ data ['message ' ];
208+ $ errorDescription = isset ( $ data ['error_description ' ]) ? $ data [ ' error_description ' ] : $ data ['message ' ];
138209 $ error = $ data ['error ' ];
139210 }
140211 throw new IdentityProviderException (sprintf ('%d - %s: %s ' , $ response ->getStatusCode (), $ error , $ errorDescription ), $ response ->getStatusCode (), $ data );
@@ -147,7 +218,7 @@ protected function checkResponse(ResponseInterface $response, $data): void
147218 *
148219 * @return PrestaShopUser
149220 */
150- protected function createResourceOwner (array $ response , AccessToken $ token ): PrestaShopUser
221+ protected function createResourceOwner (array $ response , AccessToken $ token )
151222 {
152223 return new PrestaShopUser ($ response );
153224 }
@@ -159,7 +230,7 @@ protected function createResourceOwner(array $response, AccessToken $token): Pre
159230 *
160231 * @return PrestaShopUser
161232 */
162- public function getResourceOwner (AccessToken $ token ): PrestaShopUser
233+ public function getResourceOwner (AccessToken $ token )
163234 {
164235 /** @var PrestaShopUser $resourceOwner */
165236 $ resourceOwner = parent ::getResourceOwner ($ token );
0 commit comments