Skip to content

Commit

Permalink
Update OAuth API (#776)
Browse files Browse the repository at this point in the history
  • Loading branch information
pilcrowonpaper authored Jun 23, 2023
1 parent 151cdf5 commit c520b5d
Show file tree
Hide file tree
Showing 14 changed files with 126 additions and 114 deletions.
6 changes: 6 additions & 0 deletions .auri/$1j735d60.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
package: "@lucia-auth/oauth" # package name
type: "major" # "major", "minor", "patch"
---

Rename `providerUser` property to `<provider_name>User` (`githubUser` etc)
6 changes: 6 additions & 0 deletions .auri/$40a7umh0.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
package: "@lucia-auth/oauth" # package name
type: "major" # "major", "minor", "patch"
---

Rename `useAuth()` to `providerUserAuth()`
6 changes: 6 additions & 0 deletions .auri/$yf495wr1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
package: "@lucia-auth/oauth" # package name
type: "major" # "major", "minor", "patch"
---

Rename `tokens` property to `<provider_name>Tokens` (`githubTokens` etc)
14 changes: 4 additions & 10 deletions packages/oauth/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,14 @@ export type OAuthConfig = {
scope?: string[];
};

export type OAuthProvider<A extends Auth> = {
export type OAuthProvider = {
validateCallback: (
code: string,
...args: any[]
) => Promise<{
existingUser: LuciaUser<A> | null;
createUser: (
attributes: CreateUserAttributesParameter<A>
) => Promise<LuciaUser<A>>;
existingUser: Record<any, any> | null;
createUser: (attributes: Record<string, any>) => Promise<Record<any, any>>;
createKey: (userId: string) => Promise<Key>;
providerUser: Record<string, any>;
tokens: {
accessToken: string;
};
}>;
getAuthorizationUrl: (
redirectUri?: string
Expand All @@ -48,7 +42,7 @@ export const scope = (base: string[], config: string[] = []) => {
return [...base, ...(config ?? [])].join(" ");
};

export const useAuth = async <_Auth extends Auth>(
export const providerUserAuth = async <_Auth extends Auth>(
auth: _Auth,
providerId: string,
providerUserId: string
Expand Down
2 changes: 1 addition & 1 deletion packages/oauth/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export { generateState, OAuthRequestError, useAuth } from "./core.js";
export { generateState, OAuthRequestError, providerUserAuth } from "./core.js";

export type { OAuthProvider } from "./core.js";
22 changes: 11 additions & 11 deletions packages/oauth/src/providers/auth0.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createUrl, handleRequest, authorizationHeaders } from "../request.js";
import { scope, generateState, useAuth } from "../core.js";
import { scope, generateState, providerUserAuth } from "../core.js";

import type { Auth } from "lucia";
import type { OAuthConfig, OAuthProvider } from "../core.js";
Expand All @@ -16,7 +16,7 @@ type Config = OAuthConfig & {
};

export const auth0 = <_Auth extends Auth>(auth: _Auth, config: Config) => {
const getTokens = async (code: string) => {
const getAuth0Tokens = async (code: string) => {
const request = new Request(new URL("/oauth/token", config.appDomain), {
method: "POST",
headers: {
Expand Down Expand Up @@ -45,7 +45,7 @@ export const auth0 = <_Auth extends Auth>(auth: _Auth, config: Config) => {
};
};

const getProviderUser = async (accessToken: string) => {
const getAuth0User = async (accessToken: string) => {
const request = new Request(new URL("/userinfo", config.appDomain), {
headers: authorizationHeaders("bearer", accessToken)
});
Expand Down Expand Up @@ -83,21 +83,21 @@ export const auth0 = <_Auth extends Auth>(auth: _Auth, config: Config) => {
return [url, state] as const;
},
validateCallback: async (code: string) => {
const tokens = await getTokens(code);
const providerUser = await getProviderUser(tokens.accessToken);
const providerUserId = providerUser.id;
const providerAuthHelpersHelpers = await useAuth(
const auth0Tokens = await getAuth0Tokens(code);
const auth0User = await getAuth0User(auth0Tokens.accessToken);
const providerUserId = auth0User.id;
const auth0UserAuth = await providerUserAuth(
auth,
PROVIDER_ID,
providerUserId
);
return {
...providerAuthHelpersHelpers,
providerUser,
tokens
...auth0UserAuth,
auth0User,
auth0Tokens
};
}
} as const satisfies OAuthProvider<_Auth>;
} as const satisfies OAuthProvider;
};

type Auth0Profile = {
Expand Down
22 changes: 11 additions & 11 deletions packages/oauth/src/providers/discord.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createUrl, handleRequest, authorizationHeaders } from "../request.js";
import { useAuth, generateState, scope } from "../core.js";
import { providerUserAuth, generateState, scope } from "../core.js";

import type { Auth } from "lucia";
import type { OAuthConfig, OAuthProvider } from "../core.js";
Expand All @@ -11,7 +11,7 @@ type Config = OAuthConfig & {
const PROVIDER_ID = "discord";

export const discord = <_Auth extends Auth>(auth: _Auth, config: Config) => {
const getTokens = async (code: string) => {
const getDiscordTokens = async (code: string) => {
const request = new Request("https://discord.com/api/oauth2/token", {
method: "POST",
headers: {
Expand All @@ -38,7 +38,7 @@ export const discord = <_Auth extends Auth>(auth: _Auth, config: Config) => {
};
};

const getProviderUser = async (accessToken: string) => {
const getDiscordUser = async (accessToken: string) => {
// do not use oauth/users/@me because it ignores intents, use oauth/users/@me instead
const request = new Request("https://discord.com/api/users/@me", {
headers: authorizationHeaders("bearer", accessToken)
Expand All @@ -60,21 +60,21 @@ export const discord = <_Auth extends Auth>(auth: _Auth, config: Config) => {
return [url, state];
},
validateCallback: async (code: string) => {
const tokens = await getTokens(code);
const providerUser = await getProviderUser(tokens.accessToken);
const providerUserId = providerUser.id;
const providerAuthHelpers = await useAuth(
const discordTokens = await getDiscordTokens(code);
const discordUser = await getDiscordUser(discordTokens.accessToken);
const providerUserId = discordUser.id;
const discordUserAuth = await providerUserAuth(
auth,
PROVIDER_ID,
providerUserId
);
return {
...providerAuthHelpers,
providerUser,
tokens
...discordUserAuth,
discordUser,
discordTokens
};
}
} as const satisfies OAuthProvider<_Auth>;
} as const satisfies OAuthProvider;
};

export type DiscordUser = {
Expand Down
20 changes: 10 additions & 10 deletions packages/oauth/src/providers/facebook.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createUrl, handleRequest, authorizationHeaders } from "../request.js";
import { scope, generateState, useAuth } from "../core.js";
import { scope, generateState, providerUserAuth } from "../core.js";

import type { Auth } from "lucia";
import type { OAuthConfig, OAuthProvider } from "../core.js";
Expand All @@ -11,7 +11,7 @@ type Config = OAuthConfig & {
const PROVIDER_ID = "facebook";

export const facebook = <_Auth extends Auth>(auth: _Auth, config: Config) => {
const getTokens = async (code: string) => {
const getFacebookTokens = async (code: string) => {
const requestUrl = createUrl(
"https://graph.facebook.com/v16.0/oauth/access_token",
{
Expand All @@ -35,7 +35,7 @@ export const facebook = <_Auth extends Auth>(auth: _Auth, config: Config) => {
};
};

const getProviderUser = async (accessToken: string) => {
const getFacebookUser = async (accessToken: string) => {
const requestUrl = createUrl("https://graph.facebook.com/me", {
access_token: accessToken,
fields: ["id", "name", "picture"].join(",")
Expand All @@ -59,21 +59,21 @@ export const facebook = <_Auth extends Auth>(auth: _Auth, config: Config) => {
return [url, state] as const;
},
validateCallback: async (code: string) => {
const tokens = await getTokens(code);
const providerUser = await getProviderUser(tokens.accessToken);
const providerUserId = providerUser.id;
const providerAuthHelpers = await useAuth(
const tokens = await getFacebookTokens(code);
const facebookUser = await getFacebookUser(tokens.accessToken);
const providerUserId = facebookUser.id;
const facebookUserAuth = await providerUserAuth(
auth,
PROVIDER_ID,
providerUserId
);
return {
...providerAuthHelpers,
providerUser,
...facebookUserAuth,
facebookUser,
tokens
};
}
} as const satisfies OAuthProvider<_Auth>;
} as const satisfies OAuthProvider;
};

export type FacebookUser = {
Expand Down
22 changes: 11 additions & 11 deletions packages/oauth/src/providers/github.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createUrl, handleRequest, authorizationHeaders } from "../request.js";
import { scope, generateState, useAuth } from "../core.js";
import { scope, generateState, providerUserAuth } from "../core.js";

import type { Auth } from "lucia";
import type { OAuthConfig, OAuthProvider } from "../core.js";
Expand All @@ -23,7 +23,7 @@ type Config = OAuthConfig & {
};

export const github = <_Auth extends Auth>(auth: _Auth, config: Config) => {
const getTokens = async (code: string): Promise<Tokens> => {
const getGithubTokens = async (code: string): Promise<Tokens> => {
const requestUrl = createUrl(
"https://github.com/login/oauth/access_token",
{
Expand Down Expand Up @@ -63,7 +63,7 @@ export const github = <_Auth extends Auth>(auth: _Auth, config: Config) => {
};
};

const getProviderUser = async (accessToken: string) => {
const getGithubUser = async (accessToken: string) => {
const request = new Request("https://api.github.com/user", {
headers: authorizationHeaders("bearer", accessToken)
});
Expand All @@ -85,21 +85,21 @@ export const github = <_Auth extends Auth>(auth: _Auth, config: Config) => {
return [url, state] as const;
},
validateCallback: async (code: string) => {
const tokens = await getTokens(code);
const providerUser = await getProviderUser(tokens.accessToken);
const providerUserId = providerUser.id.toString();
const providerAuthHelpers = await useAuth(
const githubTokens = await getGithubTokens(code);
const githubUser = await getGithubUser(githubTokens.accessToken);
const providerUserId = githubUser.id.toString();
const githubUserAuth = await providerUserAuth(
auth,
PROVIDER_ID,
providerUserId
);
return {
...providerAuthHelpers,
providerUser,
tokens
...githubUserAuth,
githubUser,
githubTokens
};
}
} as const satisfies OAuthProvider<_Auth>;
} as const satisfies OAuthProvider;
};

type PublicGithubUser = {
Expand Down
22 changes: 11 additions & 11 deletions packages/oauth/src/providers/google.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createUrl, handleRequest, authorizationHeaders } from "../request.js";
import { scope, generateState, useAuth } from "../core.js";
import { scope, generateState, providerUserAuth } from "../core.js";

import type { Auth } from "lucia";
import type { OAuthConfig, OAuthProvider } from "../core.js";
Expand All @@ -11,7 +11,7 @@ type Config = OAuthConfig & {
const PROVIDER_ID = "google";

export const google = <_Auth extends Auth>(auth: _Auth, config: Config) => {
const getTokens = async (code: string) => {
const getGoogleTokens = async (code: string) => {
const requestUrl = createUrl("https://oauth2.googleapis.com/token", {
client_id: config.clientId,
client_secret: config.clientSecret,
Expand All @@ -35,7 +35,7 @@ export const google = <_Auth extends Auth>(auth: _Auth, config: Config) => {
};
};

const getProviderUser = async (accessToken: string) => {
const getGoogleUser = async (accessToken: string) => {
const request = new Request(
"https://www.googleapis.com/oauth2/v3/userinfo",
{
Expand All @@ -62,21 +62,21 @@ export const google = <_Auth extends Auth>(auth: _Auth, config: Config) => {
return [url, state] as const;
},
validateCallback: async (code: string) => {
const tokens = await getTokens(code);
const providerUser = await getProviderUser(tokens.accessToken);
const providerUserId = providerUser.sub;
const providerAuthHelpers = await useAuth(
const googleTokens = await getGoogleTokens(code);
const googleUser = await getGoogleUser(googleTokens.accessToken);
const providerUserId = googleUser.sub;
const googleUserAuth = await providerUserAuth(
auth,
PROVIDER_ID,
providerUserId
);
return {
...providerAuthHelpers,
providerUser,
tokens
...googleUserAuth,
googleUser,
googleTokens
};
}
} as const satisfies OAuthProvider<_Auth>;
} as const satisfies OAuthProvider;
};

export type GoogleUser = {
Expand Down
Loading

0 comments on commit c520b5d

Please sign in to comment.