@@ -168,10 +168,9 @@ public static function create_user( $domain, $jwt, $data ) {
168168 $ headers = self ::get_info_headers ();
169169
170170 $ headers ['Authorization ' ] = "Bearer $ jwt " ;
171- $ headers ['content-type ' ] = " application/json " ;
171+ $ headers ['content-type ' ] = ' application/json ' ;
172172
173173 $ response = wp_remote_post ( $ endpoint , array (
174- 'method ' => 'POST ' ,
175174 'headers ' => $ headers ,
176175 'body ' => json_encode ( $ data )
177176 ) );
@@ -191,6 +190,34 @@ public static function create_user( $domain, $jwt, $data ) {
191190 return json_decode ( $ response ['body ' ] );
192191 }
193192
193+ public static function signup_user ( $ domain , $ data ) {
194+
195+ $ endpoint = "https:// $ domain/dbconnections/signup " ;
196+
197+ $ headers = self ::get_info_headers ();
198+
199+ $ headers ['content-type ' ] = 'application/json ' ;
200+
201+ $ response = wp_remote_post ( $ endpoint , array (
202+ 'headers ' => $ headers ,
203+ 'body ' => json_encode ( $ data )
204+ ) );
205+
206+ if ( $ response instanceof WP_Error ) {
207+ WP_Auth0_ErrorManager::insert_auth0_error ( 'WP_Auth0_Api_Client::signup_user ' , $ response );
208+ error_log ( $ response ->get_error_message () );
209+ return false ;
210+ }
211+
212+ if ( $ response ['response ' ]['code ' ] !== 200 ) {
213+ WP_Auth0_ErrorManager::insert_auth0_error ( 'WP_Auth0_Api_Client::signup_user ' , $ response ['body ' ] );
214+ error_log ( $ response ['body ' ] );
215+ return false ;
216+ }
217+
218+ return json_decode ( $ response ['body ' ] );
219+ }
220+
194221 public static function get_required_scopes () {
195222 return array (
196223 'update:clients ' ,
@@ -225,9 +252,14 @@ public static function create_client( $domain, $app_token, $name ) {
225252 home_url ( '/wp-login.php ' )
226253 ),
227254 "allowed_origins " =>array (
228- home_url ( '/wp-login.php ' ),
229- admin_url ( '/admin.php?page=wpa0-setup&step=2&profile=social ' )
255+ home_url ( '/wp-login.php ' )
256+ ),
257+ "jwt_configuration " => array (
258+ "alg " => "RS256 "
230259 ),
260+ "app_type " => "regular_web " ,
261+ "cross_origin_auth " => true ,
262+ "cross_origin_loc " => home_url ('/index.php?auth0fallback=1 ' ,'https ' ),
231263 "allowed_logout_urls " => array (
232264 $ logout_url
233265 ),
@@ -246,7 +278,21 @@ public static function create_client( $domain, $app_token, $name ) {
246278 return false ;
247279 }
248280
249- return json_decode ( $ response ['body ' ] );
281+ $ response = json_decode ( $ response ['body ' ] );
282+
283+ // Workaround: Can't add `web_origin` on create
284+ $ payload = array (
285+ "web_origins " => array (home_url ())
286+ );
287+ $ updateResponse = WP_Auth0_Api_Client::update_client ($ domain , $ app_token , $ response ->client_id , false , $ payload );
288+
289+ if ( $ updateClient instanceof WP_Error ) {
290+ WP_Auth0_ErrorManager::insert_auth0_error ( 'WP_Auth0_Api_Client::create_client ' , $ updateResponse );
291+ error_log ( $ updateResponse ->get_error_message () );
292+ return false ;
293+ }
294+
295+ return $ response ;
250296 }
251297
252298 public static function search_clients ( $ domain , $ app_token ) {
@@ -277,7 +323,7 @@ public static function search_clients( $domain, $app_token ) {
277323 return json_decode ( $ response ['body ' ] );
278324 }
279325
280- public static function update_client ( $ domain , $ app_token , $ client_id , $ sso ) {
326+ public static function update_client ( $ domain , $ app_token , $ client_id , $ sso, $ payload = array () ) {
281327
282328 $ endpoint = "https:// $ domain/api/v2/clients/ $ client_id " ;
283329
@@ -289,9 +335,7 @@ public static function update_client( $domain, $app_token, $client_id, $sso ) {
289335 $ response = wp_remote_post ( $ endpoint , array (
290336 'method ' => 'PATCH ' ,
291337 'headers ' => $ headers ,
292- 'body ' => json_encode ( array (
293- 'sso ' => $ sso ,
294- ) )
338+ 'body ' => json_encode ( array_merge (array ( 'sso ' => boolval ($ sso )), $ payload ) )
295339 ) );
296340
297341 if ( $ response instanceof WP_Error ) {
@@ -740,4 +784,53 @@ public static function update_guardian($domain, $app_token, $factor, $enabled) {
740784
741785 return json_decode ($ response ['body ' ]);
742786 }
743- }
787+
788+ protected function convertCertToPem ($ cert ) {
789+ return '-----BEGIN CERTIFICATE----- ' .PHP_EOL
790+ .chunk_split ($ cert , 64 , PHP_EOL )
791+ .'-----END CERTIFICATE----- ' .PHP_EOL ;
792+ }
793+
794+ public static function JWKfetch ($ domain ) {
795+
796+ $ a0_options = WP_Auth0_Options::Instance ();
797+
798+ $ endpoint = "https:// $ domain/.well-known/jwks.json " ;
799+
800+ $ cache_expiration = $ a0_options ->get ('cache_expiration ' );
801+
802+ if ( false === ($ secret = get_transient ('WP_Auth0_JWKS_cache ' ) ) ) {
803+
804+ $ secret = [];
805+
806+ $ response = wp_remote_get ( $ endpoint , array () );
807+
808+ if ( $ response instanceof WP_Error ) {
809+ WP_Auth0_ErrorManager::insert_auth0_error ( 'WP_Auth0_Api_Client::JWK_fetch ' , $ response );
810+ error_log ( $ response ->get_error_message () );
811+ return false ;
812+ }
813+
814+ if ( $ response ['response ' ]['code ' ] != 200 ) {
815+ WP_Auth0_ErrorManager::insert_auth0_error ( 'WP_Auth0_Api_Client::JWK_fetch ' , $ response ['body ' ] );
816+ error_log ( $ response ['body ' ] );
817+ return false ;
818+ }
819+
820+ if ( $ response ['response ' ]['code ' ] >= 300 ) return false ;
821+
822+ $ jwks = json_decode ($ response ['body ' ], true );
823+
824+ foreach ($ jwks ['keys ' ] as $ key ) {
825+ $ secret [$ key ['kid ' ]] = self ::convertCertToPem ($ key ['x5c ' ][0 ]);
826+ }
827+
828+ if ($ cache_expiration !== 0 ) {
829+ set_transient ( 'WP_Auth0_JWKS_cache ' , $ secret , $ cache_expiration * MINUTE_IN_SECONDS );
830+ }
831+
832+ }
833+
834+ return $ secret ;
835+ }
836+ }
0 commit comments