-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(iam): move logic from controller down to use-case
- Loading branch information
Showing
4 changed files
with
57 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 15 additions & 11 deletions
26
src/identity-and-access/use-cases/sign-in-with-oauth-provider/http.controller.e2e.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,20 @@ | ||
import { describe, expect, it } from "vitest"; | ||
|
||
describe("SignInWithOAuthProviderUseCase", () => { | ||
it("should initiate the OAuth flow by redirecting to the IDP's authorization endpoint", async () => { | ||
// Act | ||
const response = await fetch( | ||
"http://localhost:8080/identity-and-access/oauth/google" | ||
); | ||
it.each` | ||
provider | expectedRedirectUrl | ||
${"google"} | ${"https://accounts.google.com/v3/signin/identifier"} | ||
`( | ||
"should initiate the $provider OAuth flow by redirecting to the IDP's authorization endpoint", | ||
async ({ provider, expectedRedirectUrl }) => { | ||
// Act | ||
const response = await fetch( | ||
`http://localhost:8080/identity-and-access/oauth/${provider}` | ||
); | ||
|
||
// Assert | ||
expect(response.redirected).toBe(true); | ||
expect(response.url).toMatch( | ||
"https://accounts.google.com/v3/signin/identifier" | ||
); | ||
}); | ||
// Assert | ||
expect(response.redirected).toBe(true); | ||
expect(response.url).toMatch(expectedRedirectUrl); | ||
} | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
src/identity-and-access/use-cases/sign-in-with-oauth-provider/use-case.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { Injectable, NotImplementedException } from "@nestjs/common"; | ||
import { ConfigService } from "@nestjs/config"; | ||
|
||
@Injectable() | ||
export class SignInWithOAuthProviderUseCase { | ||
constructor(private readonly config: ConfigService) {} | ||
|
||
async execute(command: SignInWithOAuthProviderCommand) { | ||
if (command.provider !== "google") { | ||
throw new NotImplementedException(); | ||
} | ||
|
||
return this.handleSignInWithGoogle(); | ||
} | ||
|
||
async handleSignInWithGoogle() { | ||
const baseAuthorizationUrl = `https://accounts.google.com/o/oauth2/v2/auth`; | ||
const authorizationUrlParameters = new URLSearchParams({ | ||
client_id: await this.config.getOrThrow("OAUTH_GOOGLE_CLIENT_ID"), | ||
redirect_uri: | ||
"http://localhost:8080/identity-and-access/oauth/google/callback", | ||
response_type: "code", | ||
scope: "email", | ||
}); | ||
|
||
return { | ||
url: `${baseAuthorizationUrl}?${authorizationUrlParameters}`, | ||
}; | ||
} | ||
} | ||
|
||
type SignInWithOAuthProviderCommand = { | ||
provider: string; | ||
}; |