forked from laravel/passport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAuthorizationCodeGrantWithPkceTest.php
131 lines (103 loc) · 4.67 KB
/
AuthorizationCodeGrantWithPkceTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php
namespace Laravel\Passport\Tests\Feature;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Str;
use Laravel\Passport\Database\Factories\ClientFactory;
use Laravel\Passport\Passport;
use Orchestra\Testbench\Concerns\WithLaravelMigrations;
use Workbench\Database\Factories\UserFactory;
class AuthorizationCodeGrantWithPkceTest extends PassportTestCase
{
use WithLaravelMigrations;
protected function setUp(): void
{
PassportTestCase::setUp();
Passport::tokensCan([
'create' => 'Create',
'read' => 'Read',
'update' => 'Update',
'delete' => 'Delete',
]);
Passport::authorizationView(fn ($params) => $params);
}
public function testIssueAccessToken()
{
$client = ClientFactory::new()->asPublic()->create();
$codeVerifier = Str::random(128);
$codeChallenge = strtr(rtrim(base64_encode(hash('sha256', $codeVerifier, true)), '='), '+/', '-_');
$query = http_build_query([
'client_id' => $client->getKey(),
'redirect_uri' => $redirect = $client->redirect_uris[0],
'response_type' => 'code',
'scope' => 'create read',
'state' => $state = Str::random(40),
'code_challenge' => $codeChallenge,
'code_challenge_method' => 'S256',
]);
$user = UserFactory::new()->create();
$this->actingAs($user, 'web');
$response = $this->get('/oauth/authorize?'.$query);
$response->assertOk();
$response->assertSessionHas('authRequest');
$response->assertSessionHas('authToken');
$json = $response->json();
$this->assertEqualsCanonicalizing(['client', 'user', 'scopes', 'request', 'authToken'], array_keys($json));
$this->assertSame(collect(Passport::scopesFor(['create', 'read']))->toArray(), $json['scopes']);
$response = $this->post('/oauth/authorize', ['auth_token' => $json['authToken']]);
$response->assertRedirect();
$response->assertSessionMissing(['authRequest', 'authToken']);
$location = $response->headers->get('Location');
parse_str(parse_url($location, PHP_URL_QUERY), $params);
$this->assertStringStartsWith($redirect.'?', $location);
$this->assertSame($state, $params['state']);
$this->assertArrayHasKey('code', $params);
$response = $this->post('/oauth/token', [
'grant_type' => 'authorization_code',
'client_id' => $client->getKey(),
'redirect_uri' => $redirect,
'code' => $params['code'],
'code_verifier' => $codeVerifier,
]);
$response->assertOk();
$json = $response->json();
$this->assertArrayHasKey('access_token', $json);
$this->assertArrayHasKey('refresh_token', $json);
$this->assertSame('Bearer', $json['token_type']);
$this->assertSame(31536000, $json['expires_in']);
$refreshToken = $json['refresh_token'];
Route::get('/foo', fn (Request $request) => $request->user()->token()->toJson())
->middleware('auth:api');
$json = $this->withToken($json['access_token'], $json['token_type'])->get('/foo')->json();
$this->assertSame($client->getKey(), $json['oauth_client_id']);
$this->assertEquals($user->getAuthIdentifier(), $json['oauth_user_id']);
$this->assertSame(['create', 'read'], $json['oauth_scopes']);
$newToken = $this->post('/oauth/token', [
'grant_type' => 'refresh_token',
'client_id' => $client->getKey(),
'refresh_token' => $refreshToken,
'scope' => 'create read',
])->assertOK()->json();
$this->assertArrayHasKey('access_token', $newToken);
$this->assertArrayHasKey('refresh_token', $newToken);
$this->assertSame(31536000, $newToken['expires_in']);
$this->assertSame('Bearer', $newToken['token_type']);
}
public function testRequireCodeChallenge()
{
$client = ClientFactory::new()->asPublic()->create();
$query = http_build_query([
'client_id' => $client->getKey(),
'redirect_uri' => $client->redirect_uris[0],
'response_type' => 'code',
]);
$user = UserFactory::new()->create();
$this->actingAs($user, 'web');
$response = $this->get('/oauth/authorize?'.$query);
$response->assertStatus(400);
$json = $response->json();
$this->assertSame('invalid_request', $json['error']);
$this->assertSame('Code challenge must be provided for public clients', $json['hint']);
$this->assertArrayHasKey('error_description', $json);
}
}