Skip to content

Commit

Permalink
Define OAuth provider object in SDK explicitly
Browse files Browse the repository at this point in the history
  • Loading branch information
infomiho committed Aug 22, 2024
1 parent 6fbeeda commit 2df9b52
Show file tree
Hide file tree
Showing 22 changed files with 170 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
{{={= =}=}}
{=# enabledProviders.isGoogleAuthEnabled =}
// PUBLIC API
export * as google from './providers/google.js';
export { google } from './providers/google.js';
{=/ enabledProviders.isGoogleAuthEnabled =}
{=# enabledProviders.isDiscordAuthEnabled =}
// PUBLIC API
export * as discord from './providers/discord.js';
export { discord } from './providers/discord.js';
{=/ enabledProviders.isDiscordAuthEnabled =}
{=# enabledProviders.isGitHubAuthEnabled =}
// PUBLIC API
export * as github from './providers/github.js';
export { github } from './providers/github.js';
{=/ enabledProviders.isGitHubAuthEnabled =}
{=# enabledProviders.isKeycloakAuthEnabled =}
// PUBLIC API
export * as keycloak from './providers/keycloak.js';
export { keycloak } from './providers/keycloak.js';
{=/ enabledProviders.isKeycloakAuthEnabled =}

// PRIVATE API
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { OAuth2Provider, OAuth2ProviderWithPKCE } from "arctic";

export function defineProvider<
OAuthClient extends OAuth2Provider | OAuth2ProviderWithPKCE,
Env extends Record<string, string>
>({
id,
displayName,
env,
oAuthClient,
}: {
id: string;
displayName: string;
env: Env;
oAuthClient: OAuthClient;
}) {
return {
id,
displayName,
env,
oAuthClient,
};
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,28 @@
{{={= =}=}}
import { Discord } from "arctic";

import { defineProvider } from "../provider.js";
import { ensureEnvVarsForProvider } from "../env.js";
import { getRedirectUriForCallback } from "../redirect.js";

export const id = "{= providerId =}";
export const displayName = "{= displayName =}";
const id = "{= providerId =}";
const displayName = "{= displayName =}";

const env = ensureEnvVarsForProvider(
["DISCORD_CLIENT_ID", "DISCORD_CLIENT_SECRET"],
displayName
);

export const oAuthClient = new Discord(
const oAuthClient = new Discord(
env.DISCORD_CLIENT_ID,
env.DISCORD_CLIENT_SECRET,
getRedirectUriForCallback(id).toString(),
);

// PUBLIC API
export const discord = defineProvider({
id,
displayName,
env,
oAuthClient,
})
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,25 @@
import { GitHub } from "arctic";

import { ensureEnvVarsForProvider } from "../env.js";
import { defineProvider } from "../provider.js";

export const id = "{= providerId =}";
export const displayName = "{= displayName =}";
const id = "{= providerId =}";
const displayName = "{= displayName =}";

const env = ensureEnvVarsForProvider(
["GITHUB_CLIENT_ID", "GITHUB_CLIENT_SECRET"],
displayName
);

export const oAuthClient = new GitHub(
const oAuthClient = new GitHub(
env.GITHUB_CLIENT_ID,
env.GITHUB_CLIENT_SECRET,
);

// PUBLIC API
export const github = defineProvider({
id,
displayName,
env,
oAuthClient,
})
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,26 @@ import { Google } from "arctic";

import { ensureEnvVarsForProvider } from "../env.js";
import { getRedirectUriForCallback } from "../redirect.js";
import { defineProvider } from "../provider.js";

export const id = "{= providerId =}";
export const displayName = "{= displayName =}";
const id = "{= providerId =}";
const displayName = "{= displayName =}";

const env = ensureEnvVarsForProvider(
["GOOGLE_CLIENT_ID", "GOOGLE_CLIENT_SECRET"],
displayName,
);

export const oAuthClient = new Google(
const oAuthClient = new Google(
env.GOOGLE_CLIENT_ID,
env.GOOGLE_CLIENT_SECRET,
getRedirectUriForCallback(id).toString(),
);

// PUBLIC API
export const google = defineProvider({
id,
displayName,
env,
oAuthClient,
})
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,27 @@ import { Keycloak } from "arctic";

import { ensureEnvVarsForProvider } from "../env.js";
import { getRedirectUriForCallback } from "../redirect.js";
import { defineProvider } from "../provider.js";

export const id = "{= providerId =}";
export const displayName = "{= displayName =}";
const id = "{= providerId =}";
const displayName = "{= displayName =}";

const env = ensureEnvVarsForProvider(
["KEYCLOAK_REALM_URL", "KEYCLOAK_CLIENT_ID", "KEYCLOAK_CLIENT_SECRET"],
displayName,
);

export const oAuthClient = new Keycloak(
const oAuthClient = new Keycloak(
env.KEYCLOAK_REALM_URL,
env.KEYCLOAK_CLIENT_ID,
env.KEYCLOAK_CLIENT_SECRET,
getRedirectUriForCallback(id).toString(),
);

// PUBLIC API
export const keycloak = defineProvider({
id,
displayName,
env,
oAuthClient,
});
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const _waspConfig: ProviderConfig = {
providerProfile: unknown;
providerUserId: string;
}> {
const userInfoEndpoint = `${env.KEYCLOAK_REALM_URL}/protocol/openid-connect/userinfo`;
const userInfoEndpoint = `${keycloak.env.KEYCLOAK_REALM_URL}/protocol/openid-connect/userinfo`;
const response = await fetch(
userInfoEndpoint,
{
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion waspc/src/Wasp/Generator/SdkGenerator/Server/OAuthG.hs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ genOAuth auth
[ genIndexTs auth,
genRedirectHelper,
genFileCopy $ oauthDirInSdkTemplatesDir </> [relfile|env.ts|],
genFileCopy $ oauthDirInSdkTemplatesDir </> [relfile|oneTimeCode.ts|]
genFileCopy $ oauthDirInSdkTemplatesDir </> [relfile|oneTimeCode.ts|],
genFileCopy $ oauthDirInSdkTemplatesDir </> [relfile|provider.ts|]
]
<++> genOAuthProvider discordAuthProvider (AS.Auth.discord . AS.Auth.methods $ auth)
<++> genOAuthProvider googleAuthProvider (AS.Auth.google . AS.Auth.methods $ auth)
Expand Down

0 comments on commit 2df9b52

Please sign in to comment.