feat(auth-google,auth-github): Allow passing a custom callbackUrl to … (#10829)

* feat(auth-google,auth-github): Allow passing a custom callbackUrl to oauth providers

* feat: Add state management in auth providers

* chore: Changes based on PR review
This commit is contained in:
Stevche Radevski
2025-01-06 17:33:29 +01:00
committed by GitHub
parent 9490c265b2
commit fde73dbfae
8 changed files with 289 additions and 68 deletions
@@ -28,6 +28,22 @@ const encodedIdToken = generateJwtToken(sampleIdPayload, {
})
const baseUrl = "https://someurl.com"
const callbackUrl = encodeURIComponent(
"https://someurl.com/auth/google/callback"
)
let state = {}
const defaultSpies = {
retrieve: jest.fn(),
create: jest.fn(),
update: jest.fn(),
setState: jest.fn().mockImplementation((key, value) => {
state[key] = value
}),
getState: jest.fn().mockImplementation((key) => {
return Promise.resolve(state[key])
}),
}
// This is just a network-layer mocking, it doesn't start an actual server
const server = setupServer(
@@ -37,7 +53,7 @@ const server = setupServer(
const url = request.url
if (
url ===
"https://oauth2.googleapis.com/token?client_id=test&client_secret=test&code=invalid-code&redirect_uri=https%3A%2F%2Fsomeurl.com%2Fauth%2Fgoogle%2Fcallback&grant_type=authorization_code"
`https://oauth2.googleapis.com/token?client_id=test&client_secret=test&code=invalid-code&redirect_uri=${callbackUrl}&grant_type=authorization_code`
) {
return new HttpResponse(null, {
status: 401,
@@ -47,7 +63,7 @@ const server = setupServer(
if (
url ===
"https://oauth2.googleapis.com/token?client_id=test&client_secret=test&code=valid-code&redirect_uri=https%3A%2F%2Fsomeurl.com%2Fauth%2Fgoogle%2Fcallback&grant_type=authorization_code"
`https://oauth2.googleapis.com/token?client_id=test&client_secret=test&code=valid-code&redirect_uri=${callbackUrl}&grant_type=authorization_code`
) {
return new HttpResponse(
JSON.stringify({
@@ -90,6 +106,7 @@ describe("Google auth provider", () => {
afterEach(() => {
server.resetHandlers()
jest.restoreAllMocks()
state = {}
})
afterAll(() => server.close())
@@ -109,11 +126,27 @@ describe("Google auth provider", () => {
})
it("returns a redirect URL on authenticate", async () => {
const res = await googleService.authenticate({})
const res = await googleService.authenticate({}, defaultSpies)
expect(res).toEqual({
success: true,
location:
"https://accounts.google.com/o/oauth2/v2/auth?redirect_uri=https%3A%2F%2Fsomeurl.com%2Fauth%2Fgoogle%2Fcallback&client_id=test&response_type=code&scope=email+profile+openid",
location: `https://accounts.google.com/o/oauth2/v2/auth?redirect_uri=${callbackUrl}&client_id=test&response_type=code&scope=email+profile+openid&state=${
Object.keys(state)[0]
}`,
})
})
it("returns a custom redirect_uri on authenticate", async () => {
const res = await googleService.authenticate(
{
body: { callback_url: "https://someotherurl.com" },
},
defaultSpies
)
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&state=${
Object.keys(state)[0]
}`,
})
})
@@ -122,7 +155,7 @@ describe("Google auth provider", () => {
{
query: {},
},
{} as any
defaultSpies
)
expect(res).toEqual({
success: false,
@@ -130,14 +163,52 @@ describe("Google auth provider", () => {
})
})
it("validate callback should return an error on missing state", async () => {
const res = await googleService.validateCallback(
{
query: {
code: "valid-code",
},
},
defaultSpies
)
expect(res).toEqual({
success: false,
error: "No state provided, or session expired",
})
})
it("validate callback should return an error on expired/invalid state", async () => {
const res = await googleService.validateCallback(
{
query: {
code: "valid-code",
state: "somekey",
},
},
defaultSpies
)
expect(res).toEqual({
success: false,
error: "No state provided, or session expired",
})
})
it("validate callback should return on a missing access token for code", async () => {
state = {
somekey: {
callback_url: callbackUrl,
},
}
const res = await googleService.validateCallback(
{
query: {
code: "invalid-code",
state: "somekey",
},
},
{} as any
defaultSpies
)
expect(res).toEqual({
@@ -148,6 +219,7 @@ describe("Google auth provider", () => {
it("validate callback should return successfully on a correct code for a new user", async () => {
const authServiceSpies = {
...defaultSpies,
retrieve: jest.fn().mockImplementation(() => {
throw new MedusaError(MedusaError.Types.NOT_FOUND, "Not found")
}),
@@ -166,10 +238,17 @@ describe("Google auth provider", () => {
}),
}
state = {
somekey: {
callback_url: callbackUrl,
},
}
const res = await googleService.validateCallback(
{
query: {
code: "valid-code",
state: "somekey",
},
},
authServiceSpies
@@ -190,6 +269,7 @@ describe("Google auth provider", () => {
it("validate callback should return successfully on a correct code for an existing user", async () => {
const authServiceSpies = {
...defaultSpies,
retrieve: jest.fn().mockImplementation(() => {
return {
provider_identities: [
@@ -208,10 +288,17 @@ describe("Google auth provider", () => {
}),
}
state = {
somekey: {
callback_url: callbackUrl,
},
}
const res = await googleService.validateCallback(
{
query: {
code: "valid-code",
state: "somekey",
},
},
authServiceSpies