Skip to content

Commit 2c6cb72

Browse files
this commit introduces improvements to the oauth2 authorization code flow test with pkce by making master user authentication and token usage explicit, adding tenant user creation and authentication, generating and validating the pkce flow with code_verifier and code_challenge, retrieving the newly created user from the database to manually create an authorization code, and simulating the authorization code exchange for an access token to fully cover the oauth2 flow
1 parent 0c0237b commit 2c6cb72

1 file changed

Lines changed: 9 additions & 2 deletions

File tree

Tests/Integration/Endpoints/ConnectEndpointTests.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public async Task WhenPostTokenWithInvalidClientSecret_ShouldReturnUnauthorized(
103103
/* arrange: create a tenant */
104104
var payload = _fixture.Build<TenantCreationScheme>()
105105
.With(tenant => tenant.Name, $"test-tenant-{Guid.NewGuid()}")
106-
.With(tenant => tenant.Description, $"test-description-{Guid.NewGuid()}.com")
106+
.With(tenant => tenant.Description, $"test-description-{Guid.NewGuid()}")
107107
.Create();
108108

109109
var httpResponse = await httpClient.PostAsJsonAsync("api/v1/tenants", payload);
@@ -201,13 +201,13 @@ public async Task WhenPostTokenWithValidAuthorizationCode_ShouldReturnAccessToke
201201
var tokenCollection = factory.Services.GetRequiredService<ITokenCollection>();
202202
var userCollection = factory.Services.GetRequiredService<IUserCollection>();
203203

204+
// arrange: authenticate as master to create tenant
204205
var masterClient = factory.HttpClient.WithTenantHeader("master");
205206
var masterCredentials = new AuthenticationCredentials
206207
{
207208
Username = "vinder.testing.user",
208209
Password = "vinder.testing.password"
209210
};
210-
211211
var authentication = await masterClient.PostAsJsonAsync("api/v1/identity/authenticate", masterCredentials);
212212
var grantedToken = await authentication.Content.ReadFromJsonAsync<AuthenticationResult>();
213213

@@ -216,6 +216,7 @@ public async Task WhenPostTokenWithValidAuthorizationCode_ShouldReturnAccessToke
216216

217217
masterClient.WithAuthorization(grantedToken.AccessToken);
218218

219+
// arrange: create tenant
219220
var payload = _fixture.Build<TenantCreationScheme>()
220221
.With(tenant => tenant.Name, $"test-tenant-{Guid.NewGuid()}")
221222
.With(tenant => tenant.Description, $"test-description-{Guid.NewGuid()}")
@@ -227,6 +228,7 @@ public async Task WhenPostTokenWithValidAuthorizationCode_ShouldReturnAccessToke
227228
Assert.NotNull(tenant);
228229
Assert.Equal(HttpStatusCode.Created, tenantResponse.StatusCode);
229230

231+
// arrange: create user for tenant
230232
var credentials = new IdentityEnrollmentCredentials
231233
{
232234
Username = $"user.{Guid.NewGuid()}@email.com",
@@ -241,6 +243,7 @@ public async Task WhenPostTokenWithValidAuthorizationCode_ShouldReturnAccessToke
241243
Assert.NotNull(identity);
242244
Assert.Equal(HttpStatusCode.Created, enrollment.StatusCode);
243245

246+
// arrange: authenticate new user
244247
var authenticationCredentials = new AuthenticationCredentials
245248
{
246249
Username = credentials.Username,
@@ -255,10 +258,12 @@ public async Task WhenPostTokenWithValidAuthorizationCode_ShouldReturnAccessToke
255258

256259
tenantClient.WithAuthorization(authenticationResult.AccessToken);
257260

261+
// arrange: generate PKCE
258262
var codeVerifier = Guid.NewGuid().ToString("N") + Guid.NewGuid().ToString("N");
259263
var codeChallenge = Application.Utilities.Base64UrlEncoder.Encode(SHA256.HashData(System.Text.Encoding.ASCII.GetBytes(codeVerifier)));
260264
var codeChallengeMethod = "S256";
261265

266+
// arrange: get user from db
262267
var filters = UserFilters.WithSpecifications()
263268
.WithUsername(credentials.Username)
264269
.Build();
@@ -269,6 +274,7 @@ public async Task WhenPostTokenWithValidAuthorizationCode_ShouldReturnAccessToke
269274
Assert.NotEmpty(users);
270275
Assert.NotNull(user);
271276

277+
// arrange: create authorization code token
272278
var authorizationCode = Guid.NewGuid().ToString("N");
273279
var token = new Domain.Aggregates.SecurityToken
274280
{
@@ -286,6 +292,7 @@ public async Task WhenPostTokenWithValidAuthorizationCode_ShouldReturnAccessToke
286292

287293
await tokenCollection.InsertAsync(token);
288294

295+
// arrange: prepare authorization_code grant request
289296
var parameters = new Dictionary<string, string>
290297
{
291298
{ "grant_type", "authorization_code" },

0 commit comments

Comments
 (0)