Atieq ur Rehman
Atieq ur Rehman

Reputation: 75

How to extract id_token from AWS Cognito redirect_url

I am trying to use AWS Cognito hosted UI with WordPress. i am successful to load sign-in page and after login it redirects to given redirect_url along with id_token like

http://example.com/#id_token=eyJraWQiOiJvYzVvK3pwRTFrRHJFYmE0 ...

i am unable to get #id_token in my php code where i need to verify & load my local user for wordpress-php site.

any help is much appreciated

below is my plugin code

<?php

global $login_page;
$login_page = 'https://example.auth.eu-central-1.amazoncognito.com/login?response_type=token&client_id=xxxxxxxx';
$login_page .= '&redirect_uri=http://localhost/example.com';

add_action('init','goto_login_page');
add_filter( 'auth_cookie_malformed', 'check_authentication_token' );

function goto_login_page() {

    global $login_page;
    $page = basename($_SERVER['REQUEST_URI']);
    if (isset($_GET['#id_token'])) {
       $jwt = $_GET['#id_token'];
       $publicKey = '';
       $decoded = JWT::decode($jwt, $publicKey, array('RS256'));
    }
    else if( $page == "wp-login.php" && $_SERVER['REQUEST_METHOD'] == 'GET') {
        wp_redirect($login_page);
        exit;
    }
}

function check_authentication_token() {

    global $login_page;
    if (isset($_GET['#id_token'])) {
        $jwt = $_GET['#id_token'];
        $publicKey = '';
        $decoded = JWT::decode($jwt, $publicKey, array('RS256'));
        die;
    }
    else {
        var_dump("");
    }
}

i am trying to use some filters to override WordPress login stuff with AWS Cognito hosted UI

Upvotes: 2

Views: 2628

Answers (1)

Atieq ur Rehman
Atieq ur Rehman

Reputation: 75

after doing multiple experiments it turned out that i should use different approach to perform login through back-end API calls. Below are the steps to be followed

  1. user response_type=code this will response with an authorization code and the use code to get token [id_token, access_token, refresh_token] i.e.


    function get_token() {

    if(isset($_GET['code'])) {
            $data = array(
                'code' => $_GET['code'],
                'client_id' => $client_id,
                'grant_type' => 'authorization_code',
                'scope' => 'email profile openid',
                'redirect_uri' => $redirect_uri,
                'client_secret' => $client_secret
            );

            $jwt = httpPost($base_url . '/oauth2/token', $data);
            return $jwt;
        }
    }

  1. decode response to JSON json_decode($jwt)

  2. get public key like below method



    function get_public_key() {
        global $region, $pool_id;

        return json_decode(file_get_contents('https://cognito-idp.' . $region . '.amazonaws.com/' . $pool_id . '/.well-known/jwks.json'));
    }

  1. validate & extract payload using code like


    foreach ($publicKey->keys as $key) {
            try {

                $pk = JWK::parseKey($key);

                $pay_load = JWT::decode($jwt_json->id_token, $pk, array('RS256'));

                if($pay_load !== '') {
                    return  get_object_vars($pay_load);
                }
            }
            catch (Exception $e) {
                error_log($e->getMessage());
            }
        }

please note that JWK class can be taken from https://github.com/fproject/php-jwt as its not part of firebase\php-jwt

Upvotes: 3

Related Questions