Skip to content

Commit

Permalink
feat(auth-google,auth-github): Allow passing a custom callbackUrl to …
Browse files Browse the repository at this point in the history
…oauth providers
  • Loading branch information
sradevski committed Jan 6, 2025
1 parent c8f9938 commit 091b1ed
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 4 deletions.
4 changes: 4 additions & 0 deletions packages/core/types/src/auth/common/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ export type AuthenticationInput = {

/**
* Body of the incoming authentication request.
*
* One of the arguments that is suggested to be treated in a standard manner is a `callbackUrl` field.
* The field specifies where the user is redirected to after a successful authentication in the case of Oauth auhentication.
* If not passed, the provider will fallback to the callbackUrl provided in the provider options.
*/
body?: Record<string, string>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,17 @@ describe("Github auth provider", () => {
})
})

it("returns a custom redirect_uri on authenticate", async () => {
const res = await githubService.authenticate({
body: { callbackUrl: "https://someotherurl.com" },
})
expect(res).toEqual({
success: true,
location:
"https://github.com/login/oauth/authorize?redirect_uri=https%3A%2F%2Fsomeotherurl.com&client_id=test&response_type=code",
})
})

it("validate callback should return an error on empty code", async () => {
const res = await githubService.validateCallback(
{
Expand Down
7 changes: 5 additions & 2 deletions packages/modules/providers/auth-github/src/services/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,10 @@ export class GithubAuthService extends AbstractAuthModuleProvider {
}
}

return this.getRedirect(this.config_)
return this.getRedirect(
this.config_.clientId,
req.body?.callbackUrl ?? this.config_.callbackUrl
)
}

async validateCallback(
Expand Down Expand Up @@ -192,7 +195,7 @@ export class GithubAuthService extends AbstractAuthModuleProvider {
}
}

private getRedirect({ clientId, callbackUrl }: LocalServiceConfig) {
private getRedirect(clientId: string, callbackUrl: string) {
const redirectUrlParam = `redirect_uri=${encodeURIComponent(callbackUrl)}`
const clientIdParam = `client_id=${clientId}`
const responseTypeParam = "response_type=code"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,17 @@ describe("Google auth provider", () => {
})
})

it("returns a custom redirect_uri on authenticate", async () => {
const res = await googleService.authenticate({
body: { callbackUrl: "https://someotherurl.com" },
})
expect(res).toEqual({
success: true,
location:
"https://accounts.google.com/o/oauth2/v2/auth?redirect_uri=https%3A%2F%2Fsomeotherurl.com&client_id=test&response_type=code&scope=email+profile+openid",
})
})

it("validate callback should return an error on empty code", async () => {
const res = await googleService.validateCallback(
{
Expand Down
7 changes: 5 additions & 2 deletions packages/modules/providers/auth-google/src/services/google.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,10 @@ export class GoogleAuthService extends AbstractAuthModuleProvider {
}
}

return this.getRedirect(this.config_)
return this.getRedirect(
this.config_.clientId,
req.body?.callbackUrl ?? this.config_.callbackUrl
)
}

async validateCallback(
Expand Down Expand Up @@ -175,7 +178,7 @@ export class GoogleAuthService extends AbstractAuthModuleProvider {
}
}

private getRedirect({ clientId, callbackUrl }: LocalServiceConfig) {
private getRedirect(clientId: string, callbackUrl: string) {
const redirectUrlParam = `redirect_uri=${encodeURIComponent(callbackUrl)}`
const clientIdParam = `client_id=${clientId}`
const responseTypeParam = "response_type=code"
Expand Down

0 comments on commit 091b1ed

Please sign in to comment.