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: Integrate the
auth_oauth2
module with the new auth
module
- Loading branch information
1 parent
81cf4a9
commit 6c6bb47
Showing
23 changed files
with
544 additions
and
439 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
This file was deleted.
Oops, something went wrong.
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,22 @@ | ||
import { Empty, RuntimeError, ScriptContext } from "../module.gen.ts"; | ||
import { createFlowToken } from "../utils/flow.ts"; | ||
import { initFlowWithProvider } from "../utils/providers.ts"; | ||
import { Provider } from "../utils/types.ts"; | ||
|
||
export interface Request { | ||
provider: Provider; | ||
} | ||
export interface Response { | ||
urlForLoginLink: string; | ||
token: string; | ||
} | ||
|
||
export async function run( | ||
ctx: ScriptContext, | ||
req: Request, | ||
): Promise<Response> { | ||
throw new RuntimeError("todo", { statusCode: 500 }); | ||
const token = await createFlowToken(ctx, req.provider); | ||
const url = await initFlowWithProvider(ctx, token.token, req.provider); | ||
|
||
return { token: token.token, urlForLoginLink: url }; | ||
} |
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,69 @@ | ||
import { RuntimeError, ScriptContext } from "../module.gen.ts"; | ||
import { completeFlow } from "./flow.ts"; | ||
import { OAuthProvider, Provider, ProviderType } from "./types.ts"; | ||
|
||
function getAuthProviderType(provider: Provider): ProviderType { | ||
if (ProviderType.EMAIL in provider) { | ||
return ProviderType.EMAIL; | ||
} else if (ProviderType.OAUTH in provider) { | ||
console.log("Provider is oauth:", provider); | ||
return ProviderType.OAUTH; | ||
} else { | ||
throw new RuntimeError("invalid_provider"); | ||
} | ||
} | ||
|
||
export async function initFlowWithProvider( | ||
ctx: ScriptContext, | ||
flowToken: string, | ||
provider: Provider, | ||
): Promise<string> { | ||
switch (getAuthProviderType(provider)) { | ||
case ProviderType.EMAIL: | ||
throw new Error("todo"); | ||
|
||
case ProviderType.OAUTH: { | ||
const { urlForLoginLink } = await ctx.modules.authOauth2.initFlow({ | ||
flowToken, | ||
providerIdent: (provider as OAuthProvider).oauth, | ||
}); | ||
return urlForLoginLink; | ||
} | ||
} | ||
} | ||
|
||
export async function pollProvider( | ||
ctx: ScriptContext, | ||
flowToken: string, | ||
provider: Provider, | ||
): Promise<string | null> { | ||
switch (getAuthProviderType(provider)) { | ||
case ProviderType.EMAIL: | ||
throw new Error("todo"); | ||
|
||
case ProviderType.OAUTH: { | ||
const { details } = await ctx.modules.authOauth2.getLoginData({ | ||
flowToken, | ||
providerIdent: (provider as OAuthProvider).oauth, | ||
}); | ||
if (!details) return null; | ||
|
||
const identity = await ctx.db.identityOAuth.findFirst({ | ||
where: { | ||
subId: details.sub, | ||
provider: details.provider, | ||
}, | ||
}); | ||
if (!identity) throw new Error("todo"); | ||
|
||
const userToken = await completeFlow( | ||
ctx, | ||
flowToken, | ||
identity.userId, | ||
details.retainedTokenDetails, | ||
); | ||
|
||
return userToken; | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -1,11 +1,11 @@ | ||
export interface Config { | ||
providers: Record<string, ProviderEndpoints | string>; | ||
providers: Record<string, ProviderEndpoints | string>; | ||
} | ||
|
||
export interface ProviderEndpoints { | ||
authorization: string; | ||
token: string; | ||
userinfo: string; | ||
scopes: string; | ||
userinfoKey: string; | ||
authorization: string; | ||
token: string; | ||
userinfo: string; | ||
scopes: string; | ||
userinfoKey: string; | ||
} |
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,58 +1,61 @@ | ||
{ | ||
"name": "OAuth2 Authentication Provider", | ||
"description": "Authenticate users with OAuth 2.0.", | ||
"icon": "key", | ||
"tags": [ | ||
"core", | ||
"user", | ||
"auth" | ||
], | ||
"authors": [ | ||
"rivet-gg", | ||
"Skyler Calaman" | ||
], | ||
"status": "beta", | ||
"dependencies": { | ||
"rate_limit": {}, | ||
"users": {}, | ||
"tokens": {} | ||
}, | ||
"routes": { | ||
"login_link": { | ||
"name": "Login Link", | ||
"description": "Generate a login link for accessing OpenGB.", | ||
"method": "GET", | ||
"pathPrefix": "/login/" | ||
}, | ||
"login_callback": { | ||
"name": "OAuth Redirect Callback", | ||
"description": "Verify a user's OAuth login and create a session.", | ||
"method": "GET", | ||
"pathPrefix": "/callback/" | ||
} | ||
}, | ||
"scripts": {}, | ||
"errors": { | ||
"already_friends": { | ||
"name": "Already Friends" | ||
}, | ||
"friend_request_not_found": { | ||
"name": "Friend Request Not Found" | ||
}, | ||
"friend_request_already_exists": { | ||
"name": "Friend Request Already Exists" | ||
}, | ||
"not_friend_request_recipient": { | ||
"name": "Not Friend Request Recipient" | ||
}, | ||
"friend_request_already_accepted": { | ||
"name": "Friend Request Already Accepted" | ||
}, | ||
"friend_request_already_declined": { | ||
"name": "Friend Request Already Declined" | ||
}, | ||
"cannot_send_to_self": { | ||
"name": "Cannot Send to Self" | ||
} | ||
} | ||
} | ||
"name": "OAuth2 Authentication Provider", | ||
"description": "Authenticate users with OAuth 2.0.", | ||
"icon": "key", | ||
"tags": [ | ||
"core", | ||
"user", | ||
"auth" | ||
], | ||
"authors": [ | ||
"rivet-gg", | ||
"Skyler Calaman" | ||
], | ||
"status": "beta", | ||
"dependencies": { | ||
"rate_limit": {}, | ||
"users": {}, | ||
"tokens": {} | ||
}, | ||
"routes": { | ||
"login_callback": { | ||
"name": "OAuth Redirect Callback", | ||
"description": "Verify a user's OAuth login and create a session.", | ||
"method": "GET", | ||
"pathPrefix": "/callback/" | ||
} | ||
}, | ||
"scripts": { | ||
"init_flow": { | ||
"name": "Initialize Auth Flow", | ||
"description": "Update flow token for OAuth login and generate an authorization URI." | ||
}, | ||
"get_login_data": { | ||
"name": "Get Login Data", | ||
"description": "Update flow token for OAuth login and generate an authorization URI." | ||
} | ||
}, | ||
"errors": { | ||
"already_friends": { | ||
"name": "Already Friends" | ||
}, | ||
"friend_request_not_found": { | ||
"name": "Friend Request Not Found" | ||
}, | ||
"friend_request_already_exists": { | ||
"name": "Friend Request Already Exists" | ||
}, | ||
"not_friend_request_recipient": { | ||
"name": "Not Friend Request Recipient" | ||
}, | ||
"friend_request_already_accepted": { | ||
"name": "Friend Request Already Accepted" | ||
}, | ||
"friend_request_already_declined": { | ||
"name": "Friend Request Already Declined" | ||
}, | ||
"cannot_send_to_self": { | ||
"name": "Cannot Send to Self" | ||
} | ||
} | ||
} |
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 @@ | ||
export type { ProviderIdentifierDetails } from "./utils/types.ts"; |
Oops, something went wrong.