i want to create controller that will act same like the laravel passport /oauth/token controller. how can i do that? (i want to send all the parameters that i send to the oauth/token and check them).
i know that i can create a token like that:
User::find('1')->createToken('token')->accessToken;
but i want to set the oauth_client and check all of this:
grant_type
grant_type
client_secret
username
password
thanks!
To create and sign your own tokens you can extend Passport's Token class and add a convertToJWT
method. Here is an example of how to build your own tokens:
<?php
namespace App;
use Laravel\Passport\Passport;
use Lcobucci\JWT\Builder;
use Lcobucci\JWT\Signer\Key;
use Lcobucci\JWT\Signer\Rsa\Sha256;
use League\OAuth2\Server\CryptKey;
class CustomToken extends \Laravel\Passport\Token
{
/**
* Generate a JWT from the access token
*
* @return string
*/
public function convertToJWT()
{
$privateKey = new CryptKey(
'file://' . Passport::keyPath('oauth-private.key'),
null,
false
);
return (string) (new Builder())
->setAudience($this->client_id)
->setId($this->id, true)
->setIssuedAt(time())
->setNotBefore(time())
->setExpiration($this->expires_at->getTimestamp())
->setSubject($this->user->id)
->set('scopes', [])
->sign(new Sha256(), new Key($privateKey->getKeyPath(), $privateKey->getPassPhrase()))
->getToken();
}
}
You can then create new tokens by calling the constructors with the attributes you require. Token
inherits from Model
so the constructor takes an $attributes
array where the keys should match the column names from oauth_access_tokens
. Once you've created that token you use the convertToJWT()
method and return the token in the response:
public function myControllerFunction(Request $request) {
$token = new CustomToken([...]);
$access_token = $token->convertToJWT();
return json(["access_token" => $access_token]);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.