php league oauth2 client how to add key/values to auth and token request

62 Views Asked by At

I'm trying to add some key/values to the Auth Request and Token Request of League/oauth2-client

in Postman it looks like this:

Postman Oauth2 Authorization (Advanced)

I tried adding them as GET in the auth/token url but that did not work .. I also tried some other stuff but to no avail ..

1

There are 1 best solutions below

0
Jaxx0rr On

turns out that library is not really needed.. for auth request you can do a simple url like this:

function getRandomAuthState($length = 32) {
    return bin2hex(random_bytes($length / 2));
}

$url = $urlAuthorize;
$url .= '?state='.getRandomAuthState();
$url .= '&token_content_type=jwt';
$url .= '&response_type=code';
$url .= '&approval_prompt=auto';
$url .= '&redirect_uri='.$redirectUri;
$url .= '&client_id='.$clientId;

header('Location: ' . $url);

and for token you can use curl like this

    if (isset($_GET['code'])) {
    
        $code = $_GET['code'];
    
        $data = array(
            'client_id'=>$clientId,
            'client_secret'=>$clientSecret,
            'redirect_uri'=>$redirectUri,
            'grant_type'=>'authorization_code',
            'code'=>$code,
            'token_content_type'=>'jwt',
        );
        $body = http_build_query($data);
    
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,            $urlAccessToken );
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
        curl_setopt($ch, CURLOPT_POST,           1 );
        curl_setopt($ch, CURLOPT_POSTFIELDS,     $body ); 
        curl_setopt($ch, CURLOPT_HTTPHEADER,     array("application\/x-www-form-urlencoded")); 
        $response = curl_exec($ch);
    
        $curl_errno = curl_errno($ch);
        $curl_error = curl_error($ch);
        curl_close($ch);
    
        print_r($response);
}