-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for refresh tokens (#2212)
- Loading branch information
Showing
69 changed files
with
1,594 additions
and
514 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 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
7 changes: 3 additions & 4 deletions
7
...es/server/src/auth/providers/oauth/env.ts → ...mplates/sdk/wasp/server/auth/oauth/env.ts
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
31 changes: 31 additions & 0 deletions
31
waspc/data/Generator/templates/sdk/wasp/server/auth/oauth/index.ts
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,31 @@ | ||
{{={= =}=}} | ||
{=# enabledProviders.isGoogleAuthEnabled =} | ||
// PUBLIC API | ||
export { google } from './providers/google.js'; | ||
{=/ enabledProviders.isGoogleAuthEnabled =} | ||
{=# enabledProviders.isDiscordAuthEnabled =} | ||
// PUBLIC API | ||
export { discord } from './providers/discord.js'; | ||
{=/ enabledProviders.isDiscordAuthEnabled =} | ||
{=# enabledProviders.isGitHubAuthEnabled =} | ||
// PUBLIC API | ||
export { github } from './providers/github.js'; | ||
{=/ enabledProviders.isGitHubAuthEnabled =} | ||
{=# enabledProviders.isKeycloakAuthEnabled =} | ||
// PUBLIC API | ||
export { keycloak } from './providers/keycloak.js'; | ||
{=/ enabledProviders.isKeycloakAuthEnabled =} | ||
|
||
// PRIVATE API | ||
export { | ||
loginPath, | ||
callbackPath, | ||
exchangeCodeForTokenPath, | ||
handleOAuthErrorAndGetRedirectUri, | ||
getRedirectUriForOneTimeCode, | ||
} from './redirect.js' | ||
|
||
// PRIVATE API | ||
export { | ||
tokenStore, | ||
} from './oneTimeCode.js' |
50 changes: 50 additions & 0 deletions
50
waspc/data/Generator/templates/sdk/wasp/server/auth/oauth/oneTimeCode.ts
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,50 @@ | ||
import { createJWT, validateJWT, TimeSpan } from '../../../auth/jwt.js' | ||
|
||
export const tokenStore = createTokenStore(); | ||
|
||
function createTokenStore() { | ||
const usedTokens = new Map<string, number>(); | ||
|
||
const validFor = new TimeSpan(1, 'm') // 1 minute | ||
const cleanupAfter = 1000 * 60 * 60; // 1 hour | ||
|
||
function createToken(userId: string): Promise<string> { | ||
return createJWT( | ||
{ | ||
id: userId, | ||
}, | ||
{ | ||
expiresIn: validFor, | ||
} | ||
); | ||
} | ||
|
||
function verifyToken(token: string): Promise<{ id: string }> { | ||
return validateJWT(token); | ||
} | ||
|
||
function isUsed(token: string): boolean { | ||
return usedTokens.has(token); | ||
} | ||
|
||
function markUsed(token: string): void { | ||
usedTokens.set(token, Date.now()); | ||
cleanUp(); | ||
} | ||
|
||
function cleanUp(): void { | ||
const now = Date.now(); | ||
for (const [token, timestamp] of usedTokens.entries()) { | ||
if (now - timestamp > cleanupAfter) { | ||
usedTokens.delete(token); | ||
} | ||
} | ||
} | ||
|
||
return { | ||
createToken, | ||
verifyToken, | ||
isUsed, | ||
markUsed, | ||
}; | ||
} |
23 changes: 23 additions & 0 deletions
23
waspc/data/Generator/templates/sdk/wasp/server/auth/oauth/provider.ts
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,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, | ||
}; | ||
} |
28 changes: 28 additions & 0 deletions
28
waspc/data/Generator/templates/sdk/wasp/server/auth/oauth/providers/discord.ts
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,28 @@ | ||
{{={= =}=}} | ||
import { Discord } from "arctic"; | ||
|
||
import { defineProvider } from "../provider.js"; | ||
import { ensureEnvVarsForProvider } from "../env.js"; | ||
import { getRedirectUriForCallback } from "../redirect.js"; | ||
|
||
const id = "{= providerId =}"; | ||
const displayName = "{= displayName =}"; | ||
|
||
const env = ensureEnvVarsForProvider( | ||
["DISCORD_CLIENT_ID", "DISCORD_CLIENT_SECRET"], | ||
displayName | ||
); | ||
|
||
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, | ||
}); |
26 changes: 26 additions & 0 deletions
26
waspc/data/Generator/templates/sdk/wasp/server/auth/oauth/providers/github.ts
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,26 @@ | ||
{{={= =}=}} | ||
import { GitHub } from "arctic"; | ||
|
||
import { ensureEnvVarsForProvider } from "../env.js"; | ||
import { defineProvider } from "../provider.js"; | ||
|
||
const id = "{= providerId =}"; | ||
const displayName = "{= displayName =}"; | ||
|
||
const env = ensureEnvVarsForProvider( | ||
["GITHUB_CLIENT_ID", "GITHUB_CLIENT_SECRET"], | ||
displayName | ||
); | ||
|
||
const oAuthClient = new GitHub( | ||
env.GITHUB_CLIENT_ID, | ||
env.GITHUB_CLIENT_SECRET, | ||
); | ||
|
||
// PUBLIC API | ||
export const github = defineProvider({ | ||
id, | ||
displayName, | ||
env, | ||
oAuthClient, | ||
}); |
28 changes: 28 additions & 0 deletions
28
waspc/data/Generator/templates/sdk/wasp/server/auth/oauth/providers/google.ts
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,28 @@ | ||
{{={= =}=}} | ||
import { Google } from "arctic"; | ||
|
||
import { ensureEnvVarsForProvider } from "../env.js"; | ||
import { getRedirectUriForCallback } from "../redirect.js"; | ||
import { defineProvider } from "../provider.js"; | ||
|
||
const id = "{= providerId =}"; | ||
const displayName = "{= displayName =}"; | ||
|
||
const env = ensureEnvVarsForProvider( | ||
["GOOGLE_CLIENT_ID", "GOOGLE_CLIENT_SECRET"], | ||
displayName, | ||
); | ||
|
||
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, | ||
}); |
Oops, something went wrong.