How to use AuthorizationServer

4 weeks ago

ihsanberahim

or how passport use AuthorizationServer

Code

Config::set('logging.channels.tinkwell', [
            'driver' => 'single',
            'path' => storage_path('logs/tinkerwell.log'),
            'level' => 'debug',
        ]);

Config::set('logging.default', 'tinkwell');

$user = User::find(265)->makeVisible(['password']);
$token = $user->tokens()->first();
$client = $token->client->makeVisible(['secret']);

\DB::beginTransaction();

$password = '$2y$10$L9u0QtN1UA4EY1emWzcNVeVv2hJVB3rycSQiK.d4v6E6V/NIbuBJC';
$tempPassword = 'anything';


$user->password = bcrypt($tempPassword);
$user->save();

$request = (new \Zend\Diactoros\ServerRequest)->withParsedBody([
            'grant_type' => 'password',
            'client_id' => $client->id,
            'client_secret' => $client->secret,
            'username' => $user->email,
  			'password' => $tempPassword,
            'scope' => implode(' ', $token->scopes),
        ]);

$server = app(League\OAuth2\Server\AuthorizationServer::class);

$response = json_decode($server->respondToAccessTokenRequest(
            $request, new Zend\Diactoros\Response
        )->getBody()->__toString(), true);

$user->password = $password;
$user->save();

\DB::rollBack();

return $response['refresh_token'];


///////////

Output

[!] Aliasing 'User' to 'App\User' for this Tinker session.
=> "def502008119abc90c37cc561878bd4f2f19540f7243ab97dba708ef92ac5240b3a9800b6fbc93b4715227fd45b6efcb2fac29769913069b366a7a58ff2ecf456fd2345584755a410c270bc4abb44b6a56cfaaf2e7676d6bf34e35ad386967f814276708278506b7ac9a44ddfce67b23a61edbdba0f959f915b7e65c109b192a8a6b6603e4b7f9e0aef258ac0cf7b9343f64e93a252d829b4290989404959949bdb2e8f0dee272d8c613d1fbd1adb5420bb80804edc36f1d33d02f474dd144119231280d7a7a04232410cdce21afe2055ad1c004a63a6c5fb5f536b4acfd09d055d5d7fdafa445b473b46831955bc3d91adbc3f88f95538c7dbafc6a537b1b08ac183cca76053c60e54264b645a677498d00061c57d2ee7fb8f2118b26b9e58884bd19b3388d352da65f579ef2db3d754c071718fc31602ba357385bcb621b1c2366b612f44910c89f724f6c88e2ddde0ca912e3d301ae68e658f4ec08eb9ec96069a71db0dd"
New course: Desktop apps with Electron