This repository has been archived by the owner on Sep 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Link the
auth_oauth2
module to the auth_provider
module
- Loading branch information
1 parent
c8e4b6a
commit 0cb2af2
Showing
7 changed files
with
138 additions
and
1 deletion.
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
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
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
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
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,64 @@ | ||
import { RuntimeError, ScriptContext } from "../module.gen.ts"; | ||
|
||
export interface Request { | ||
flowToken: string; | ||
userToken: string; | ||
} | ||
|
||
export type Response = ReturnType<ScriptContext["modules"]["authProviders"]["addProviderToUser"]>; | ||
|
||
export async function run( | ||
ctx: ScriptContext, | ||
req: Request, | ||
): Promise<Response> { | ||
await ctx.modules.rateLimit.throttlePublic({}); | ||
|
||
if (!req.flowToken) throw new RuntimeError("missing_token", { statusCode: 400 }); | ||
|
||
const { tokens: [flowToken] } = await ctx.modules.tokens.fetchByToken({ tokens: [req.flowToken] }); | ||
if (!flowToken) { | ||
throw new RuntimeError("invalid_token", { statusCode: 400 }); | ||
} | ||
if (new Date(flowToken.expireAt ?? 0) < new Date()) { | ||
throw new RuntimeError("expired_token", { statusCode: 400 }); | ||
} | ||
|
||
const flowId = flowToken.meta.flowId; | ||
if (!flowId) throw new RuntimeError("invalid_token", { statusCode: 400 }); | ||
|
||
const flow = await ctx.db.loginAttempts.findFirst({ | ||
where: { | ||
id: flowId, | ||
} | ||
}); | ||
if (!flow) throw new RuntimeError("invalid_token", { statusCode: 400 }); | ||
|
||
if (!flow.identifier || !flow.tokenData) { | ||
throw new RuntimeError("flow_not_complete", { statusCode: 400 }); | ||
} | ||
|
||
await ctx.modules.users.authenticateToken({ userToken: req.userToken }); | ||
|
||
const tokenData = flow.tokenData; | ||
if (!tokenData) { | ||
throw new RuntimeError("internal_error", { statusCode: 500 }); | ||
} | ||
if (typeof tokenData !== "object") { | ||
throw new RuntimeError("internal_error", { statusCode: 500 }); | ||
} | ||
if (Array.isArray(tokenData)) { | ||
throw new RuntimeError("internal_error", { statusCode: 500 }); | ||
} | ||
|
||
return await ctx.modules.authProviders.addProviderToUser({ | ||
userToken: req.userToken, | ||
info: { | ||
providerType: "oauth2", | ||
providerId: flow.providerId, | ||
}, | ||
uniqueData: { | ||
identifier: flow.identifier, | ||
}, | ||
additionalData: tokenData, | ||
}); | ||
} |
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
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,60 @@ | ||
import { RuntimeError, ScriptContext } from "../module.gen.ts"; | ||
|
||
export interface Request { | ||
flowToken: string; | ||
} | ||
|
||
export type Response = ReturnType<ScriptContext["modules"]["authProviders"]["getOrCreateUserFromProvider"]>; | ||
|
||
export async function run( | ||
ctx: ScriptContext, | ||
req: Request, | ||
): Promise<Response> { | ||
await ctx.modules.rateLimit.throttlePublic({}); | ||
|
||
if (!req.flowToken) throw new RuntimeError("missing_token", { statusCode: 400 }); | ||
|
||
const { tokens: [flowToken] } = await ctx.modules.tokens.fetchByToken({ tokens: [req.flowToken] }); | ||
if (!flowToken) { | ||
throw new RuntimeError("invalid_token", { statusCode: 400 }); | ||
} | ||
if (new Date(flowToken.expireAt ?? 0) < new Date()) { | ||
throw new RuntimeError("expired_token", { statusCode: 400 }); | ||
} | ||
|
||
const flowId = flowToken.meta.flowId; | ||
if (!flowId) throw new RuntimeError("invalid_token", { statusCode: 400 }); | ||
|
||
const flow = await ctx.db.loginAttempts.findFirst({ | ||
where: { | ||
id: flowId, | ||
} | ||
}); | ||
if (!flow) throw new RuntimeError("invalid_token", { statusCode: 400 }); | ||
|
||
if (!flow.identifier || !flow.tokenData) { | ||
throw new RuntimeError("flow_not_complete", { statusCode: 400 }); | ||
} | ||
|
||
const tokenData = flow.tokenData; | ||
if (!tokenData) { | ||
throw new RuntimeError("internal_error", { statusCode: 500 }); | ||
} | ||
if (typeof tokenData !== "object") { | ||
throw new RuntimeError("internal_error", { statusCode: 500 }); | ||
} | ||
if (Array.isArray(tokenData)) { | ||
throw new RuntimeError("internal_error", { statusCode: 500 }); | ||
} | ||
|
||
return await ctx.modules.authProviders.getOrCreateUserFromProvider({ | ||
info: { | ||
providerType: "oauth2", | ||
providerId: flow.providerId, | ||
}, | ||
uniqueData: { | ||
identifier: flow.identifier, | ||
}, | ||
additionalData: tokenData, | ||
}); | ||
} |