Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate unique token for Client Id & Client Secret for OAuth 2 in PHP?

Tags:

php

oauth-2.0

I'm working on an OAuth Server in PHP. Currently using PHP Library http://bshaffer.github.io/oauth2-server-php-docs/ for it. Till now everything has been clear. Just want to know how to create the unique token for Client Id & Client Secret in PHP.

Now, I'm using the following piece of code for creating Client Id & Client Secret token.

$token = bin2hex(random_bytes($length));

But, a little bit confuses. whether, Is it a right way to do this thing or not? I also searched for any library to do this. But, didn't get one.

like image 772
Suresh Avatar asked Oct 30 '22 20:10

Suresh


1 Answers

For PHP 5 >= 5.3.0, PHP 7

Use openssl_random_pseudo_bytes

$client_secret = bin2hex(openssl_random_pseudo_bytes(32));

There is also an article about OAuth 2.0 Client SecretID The Client ID and Secret

For PHP 7 Use random_bytes

$client_secret = bin2hex(random_bytes(32));
like image 172
Jitendra S Avatar answered Nov 15 '22 05:11

Jitendra S