id_token = $id_token; $this->key = $opts->get_client_secret_as_key(); $this->algorithm = $opts->get_client_signing_algorithm(); $this->issuer = 'https://' . $opts->get_auth_domain() . '/'; $this->audience = $opts->get( 'client_id' ); JWT::$leeway = absint( apply_filters( 'auth0_jwt_leeway', \JWT::$leeway ) ); } /** * Decodes a JWT string into a PHP object. * * @param bool $validate_nonce Validate the ID token nonce. * * @return object * * @throws WP_Auth0_InvalidIdTokenException Provided JWT was invalid. */ public function decode( $validate_nonce = false ) { try { $payload = JWT::decode( $this->id_token, $this->key, array( $this->algorithm ) ); } catch ( Exception $e ) { throw new WP_Auth0_InvalidIdTokenException( $e->getMessage() ); } // Check if the token issuer is valid. if ( ! isset( $payload->iss ) || $payload->iss !== $this->issuer ) { throw new WP_Auth0_InvalidIdTokenException( __( 'Invalid token issuer', 'wp-auth0' ) ); } // Check if the token audience is valid. $token_audience = null; if ( isset( $payload->aud ) ) { $token_audience = is_array( $payload->aud ) ? $payload->aud : array( $payload->aud ); } if ( ! $token_audience || ! in_array( $this->audience, $token_audience ) ) { throw new WP_Auth0_InvalidIdTokenException( __( 'Invalid token audience', 'wp-auth0' ) ); } // Check if the token nonce is valid. $token_nonce = isset( $payload->nonce ) ? $payload->nonce : null; if ( $validate_nonce && ! WP_Auth0_Nonce_Handler::get_instance()->validate( $token_nonce ) ) { throw new WP_Auth0_InvalidIdTokenException( __( 'Invalid token nonce', 'wp-auth0' ) ); } return $payload; } }