@hamur/auth 0.0.0-stage → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +22 -0
- package/README.md +14 -2
- package/dist/chunks/bun-index-0grce57w.js +47 -0
- package/dist/chunks/bun-index-nn9z2k40.js +92 -0
- package/dist/chunks/bun-index-pmpez2a8.js +10 -0
- package/dist/chunks/bun-index-r6757s88.js +206 -0
- package/dist/src/adapters/angular.js +63 -0
- package/dist/src/adapters/html.js +29 -0
- package/dist/src/adapters/preact.js +37 -0
- package/dist/src/adapters/react.js +32 -0
- package/dist/src/adapters/solid.js +45 -0
- package/dist/src/adapters/svelte.js +44 -0
- package/dist/src/adapters/vue.js +55 -0
- package/dist/src/index.js +688 -0
- package/dist/src/middleware.js +45 -0
- package/dist/src/route-middleware.js +42 -0
- package/dist/src/server/index.js +2971 -0
- package/dist/types/src/adapters/angular.d.ts +35 -0
- package/dist/types/src/adapters/html.d.ts +15 -0
- package/dist/types/src/adapters/preact.d.ts +19 -0
- package/dist/types/src/adapters/react.d.ts +19 -0
- package/dist/types/src/adapters/solid.d.ts +25 -0
- package/dist/types/src/adapters/svelte-session.d.ts +8 -0
- package/dist/types/src/adapters/svelte-webauthn.d.ts +8 -0
- package/dist/types/src/adapters/svelte.d.ts +5 -0
- package/dist/types/src/adapters/vue.d.ts +25 -0
- package/dist/types/src/build/access-tokens.d.ts +4 -0
- package/dist/types/src/build/client.d.ts +4 -0
- package/dist/types/src/build/dev-key.d.ts +6 -0
- package/dist/types/src/build/email.d.ts +4 -0
- package/dist/types/src/build/guards.d.ts +4 -0
- package/dist/types/src/build/module-entry.d.ts +3 -0
- package/dist/types/src/build/oauth.d.ts +8 -0
- package/dist/types/src/build/session.d.ts +6 -0
- package/dist/types/src/build/single-use.d.ts +15 -0
- package/dist/types/src/build/webauthn.d.ts +7 -0
- package/dist/types/src/client/webauthn.d.ts +37 -0
- package/dist/types/src/index.d.ts +43 -0
- package/dist/types/src/middleware.d.ts +4 -0
- package/dist/types/src/module.d.ts +3 -0
- package/dist/types/src/options.d.ts +4 -0
- package/dist/types/src/route-middleware.d.ts +2 -0
- package/dist/types/src/runtime/middleware/auth.d.ts +3 -0
- package/dist/types/src/runtime/middleware/guest.d.ts +3 -0
- package/dist/types/src/runtime/payload.d.ts +2 -0
- package/dist/types/src/runtime/plugin.d.ts +2 -0
- package/dist/types/src/runtime/store.d.ts +28 -0
- package/dist/types/src/runtime/targets.d.ts +4 -0
- package/dist/types/src/server/access-token.d.ts +27 -0
- package/dist/types/src/server/auth-link.d.ts +15 -0
- package/dist/types/src/server/door.d.ts +4 -0
- package/dist/types/src/server/endpoint.d.ts +8 -0
- package/dist/types/src/server/guard.d.ts +12 -0
- package/dist/types/src/server/hooks.d.ts +5 -0
- package/dist/types/src/server/index.d.ts +88 -0
- package/dist/types/src/server/oauth/atproto-dns.d.ts +1 -0
- package/dist/types/src/server/oauth/atproto-fetch.d.ts +13 -0
- package/dist/types/src/server/oauth/atproto-metadata.d.ts +1 -0
- package/dist/types/src/server/oauth/atproto.d.ts +36 -0
- package/dist/types/src/server/oauth/cookie.d.ts +17 -0
- package/dist/types/src/server/oauth/dpop.d.ts +4 -0
- package/dist/types/src/server/oauth/handler.d.ts +101 -0
- package/dist/types/src/server/oauth/oidc.d.ts +20 -0
- package/dist/types/src/server/oauth/providers/apple.d.ts +26 -0
- package/dist/types/src/server/oauth/providers/atlassian.d.ts +5 -0
- package/dist/types/src/server/oauth/providers/auth0.d.ts +9 -0
- package/dist/types/src/server/oauth/providers/authentik.d.ts +9 -0
- package/dist/types/src/server/oauth/providers/azureb2c.d.ts +11 -0
- package/dist/types/src/server/oauth/providers/battledotnet.d.ts +5 -0
- package/dist/types/src/server/oauth/providers/box.d.ts +5 -0
- package/dist/types/src/server/oauth/providers/catalog.d.ts +12 -0
- package/dist/types/src/server/oauth/providers/cloudflare.d.ts +11 -0
- package/dist/types/src/server/oauth/providers/cognito.d.ts +9 -0
- package/dist/types/src/server/oauth/providers/discord.d.ts +5 -0
- package/dist/types/src/server/oauth/providers/dropbox.d.ts +5 -0
- package/dist/types/src/server/oauth/providers/facebook.d.ts +5 -0
- package/dist/types/src/server/oauth/providers/gitea.d.ts +9 -0
- package/dist/types/src/server/oauth/providers/github.d.ts +5 -0
- package/dist/types/src/server/oauth/providers/gitlab.d.ts +9 -0
- package/dist/types/src/server/oauth/providers/google.d.ts +5 -0
- package/dist/types/src/server/oauth/providers/heroku.d.ts +5 -0
- package/dist/types/src/server/oauth/providers/hubspot.d.ts +5 -0
- package/dist/types/src/server/oauth/providers/instagram.d.ts +5 -0
- package/dist/types/src/server/oauth/providers/keycloak.d.ts +11 -0
- package/dist/types/src/server/oauth/providers/kick.d.ts +5 -0
- package/dist/types/src/server/oauth/providers/line.d.ts +5 -0
- package/dist/types/src/server/oauth/providers/linear.d.ts +5 -0
- package/dist/types/src/server/oauth/providers/linkedin.d.ts +5 -0
- package/dist/types/src/server/oauth/providers/livechat.d.ts +5 -0
- package/dist/types/src/server/oauth/providers/mastodon.d.ts +9 -0
- package/dist/types/src/server/oauth/providers/microsoft.d.ts +9 -0
- package/dist/types/src/server/oauth/providers/okta.d.ts +9 -0
- package/dist/types/src/server/oauth/providers/ory.d.ts +9 -0
- package/dist/types/src/server/oauth/providers/osu.d.ts +5 -0
- package/dist/types/src/server/oauth/providers/paypal.d.ts +5 -0
- package/dist/types/src/server/oauth/providers/polar.d.ts +5 -0
- package/dist/types/src/server/oauth/providers/preset.d.ts +74 -0
- package/dist/types/src/server/oauth/providers/riotgames.d.ts +5 -0
- package/dist/types/src/server/oauth/providers/roblox.d.ts +5 -0
- package/dist/types/src/server/oauth/providers/salesforce.d.ts +9 -0
- package/dist/types/src/server/oauth/providers/seznam.d.ts +5 -0
- package/dist/types/src/server/oauth/providers/shopifyCustomer.d.ts +9 -0
- package/dist/types/src/server/oauth/providers/slack.d.ts +5 -0
- package/dist/types/src/server/oauth/providers/spotify.d.ts +5 -0
- package/dist/types/src/server/oauth/providers/steam.d.ts +23 -0
- package/dist/types/src/server/oauth/providers/strava.d.ts +5 -0
- package/dist/types/src/server/oauth/providers/tiktok.d.ts +5 -0
- package/dist/types/src/server/oauth/providers/twitch.d.ts +5 -0
- package/dist/types/src/server/oauth/providers/vk.d.ts +5 -0
- package/dist/types/src/server/oauth/providers/wechat.d.ts +5 -0
- package/dist/types/src/server/oauth/providers/workos.d.ts +5 -0
- package/dist/types/src/server/oauth/providers/x.d.ts +5 -0
- package/dist/types/src/server/oauth/providers/xsuaa.d.ts +9 -0
- package/dist/types/src/server/oauth/providers/yandex.d.ts +5 -0
- package/dist/types/src/server/oauth/providers/zendesk.d.ts +9 -0
- package/dist/types/src/server/oauth/providers/zitadel.d.ts +9 -0
- package/dist/types/src/server/personal.d.ts +5 -0
- package/dist/types/src/server/session.d.ts +20 -0
- package/dist/types/src/server/token-requests.d.ts +5 -0
- package/dist/types/src/server/token.d.ts +17 -0
- package/dist/types/src/server/totp.d.ts +28 -0
- package/dist/types/src/server/webauthn/cbor.d.ts +16 -0
- package/dist/types/src/server/webauthn/cose.d.ts +17 -0
- package/dist/types/src/server/webauthn/data.d.ts +20 -0
- package/dist/types/src/server/webauthn/handler.d.ts +63 -0
- package/dist/types/src/single-use.d.ts +17 -0
- package/dist/types/src/types.d.ts +88 -0
- package/package.json +155 -4
- package/src/adapters/AuthState.svelte +14 -0
- package/src/adapters/angular.ts +57 -0
- package/src/adapters/html.ts +39 -0
- package/src/adapters/preact.ts +53 -0
- package/src/adapters/react.ts +49 -0
- package/src/adapters/solid.ts +64 -0
- package/src/adapters/svelte-session.ts +21 -0
- package/src/adapters/svelte-webauthn.ts +20 -0
- package/src/adapters/svelte.ts +5 -0
- package/src/adapters/vue.ts +70 -0
- package/src/build/access-tokens.ts +44 -0
- package/src/build/client.ts +16 -0
- package/src/build/dev-key.ts +29 -0
- package/src/build/email.ts +32 -0
- package/src/build/guards.ts +12 -0
- package/src/build/module-entry.ts +8 -0
- package/src/build/oauth.ts +54 -0
- package/src/build/session.ts +54 -0
- package/src/build/single-use.ts +104 -0
- package/src/build/webauthn.ts +28 -0
- package/src/client/webauthn.ts +129 -0
- package/src/env.d.ts +84 -0
- package/src/index.ts +44 -0
- package/src/middleware.ts +9 -0
- package/src/module.ts +26 -0
- package/src/options.ts +241 -0
- package/src/route-middleware.ts +2 -0
- package/src/runtime/middleware/auth.ts +16 -0
- package/src/runtime/middleware/guest.ts +17 -0
- package/src/runtime/payload.ts +40 -0
- package/src/runtime/plugin.ts +12 -0
- package/src/runtime/store.ts +72 -0
- package/src/runtime/targets.ts +9 -0
- package/src/server/access-token.ts +130 -0
- package/src/server/auth-link.ts +95 -0
- package/src/server/door.ts +33 -0
- package/src/server/endpoint.ts +33 -0
- package/src/server/guard.ts +41 -0
- package/src/server/hooks.ts +13 -0
- package/src/server/index.ts +96 -0
- package/src/server/oauth/atproto-dns.ts +3 -0
- package/src/server/oauth/atproto-fetch.ts +67 -0
- package/src/server/oauth/atproto-metadata.ts +20 -0
- package/src/server/oauth/atproto.ts +292 -0
- package/src/server/oauth/cookie.ts +38 -0
- package/src/server/oauth/dpop.ts +32 -0
- package/src/server/oauth/handler.ts +377 -0
- package/src/server/oauth/oidc.ts +283 -0
- package/src/server/oauth/providers/apple.ts +108 -0
- package/src/server/oauth/providers/atlassian.ts +20 -0
- package/src/server/oauth/providers/auth0.ts +29 -0
- package/src/server/oauth/providers/authentik.ts +29 -0
- package/src/server/oauth/providers/azureb2c.ts +41 -0
- package/src/server/oauth/providers/battledotnet.ts +21 -0
- package/src/server/oauth/providers/box.ts +19 -0
- package/src/server/oauth/providers/catalog.ts +73 -0
- package/src/server/oauth/providers/cloudflare.ts +34 -0
- package/src/server/oauth/providers/cognito.ts +29 -0
- package/src/server/oauth/providers/discord.ts +19 -0
- package/src/server/oauth/providers/dropbox.ts +26 -0
- package/src/server/oauth/providers/facebook.ts +19 -0
- package/src/server/oauth/providers/gitea.ts +29 -0
- package/src/server/oauth/providers/github.ts +33 -0
- package/src/server/oauth/providers/gitlab.ts +29 -0
- package/src/server/oauth/providers/google.ts +19 -0
- package/src/server/oauth/providers/heroku.ts +28 -0
- package/src/server/oauth/providers/hubspot.ts +27 -0
- package/src/server/oauth/providers/instagram.ts +32 -0
- package/src/server/oauth/providers/keycloak.ts +31 -0
- package/src/server/oauth/providers/kick.ts +26 -0
- package/src/server/oauth/providers/line.ts +19 -0
- package/src/server/oauth/providers/linear.ts +34 -0
- package/src/server/oauth/providers/linkedin.ts +20 -0
- package/src/server/oauth/providers/livechat.ts +19 -0
- package/src/server/oauth/providers/mastodon.ts +29 -0
- package/src/server/oauth/providers/microsoft.ts +29 -0
- package/src/server/oauth/providers/okta.ts +29 -0
- package/src/server/oauth/providers/ory.ts +29 -0
- package/src/server/oauth/providers/osu.ts +19 -0
- package/src/server/oauth/providers/paypal.ts +19 -0
- package/src/server/oauth/providers/polar.ts +19 -0
- package/src/server/oauth/providers/preset.ts +206 -0
- package/src/server/oauth/providers/riotgames.ts +19 -0
- package/src/server/oauth/providers/roblox.ts +19 -0
- package/src/server/oauth/providers/salesforce.ts +29 -0
- package/src/server/oauth/providers/seznam.ts +19 -0
- package/src/server/oauth/providers/shopifyCustomer.ts +47 -0
- package/src/server/oauth/providers/slack.ts +27 -0
- package/src/server/oauth/providers/spotify.ts +19 -0
- package/src/server/oauth/providers/steam.ts +162 -0
- package/src/server/oauth/providers/strava.ts +20 -0
- package/src/server/oauth/providers/tiktok.ts +22 -0
- package/src/server/oauth/providers/twitch.ts +32 -0
- package/src/server/oauth/providers/vk.ts +36 -0
- package/src/server/oauth/providers/wechat.ts +39 -0
- package/src/server/oauth/providers/workos.ts +19 -0
- package/src/server/oauth/providers/x.ts +19 -0
- package/src/server/oauth/providers/xsuaa.ts +29 -0
- package/src/server/oauth/providers/yandex.ts +26 -0
- package/src/server/oauth/providers/zendesk.ts +29 -0
- package/src/server/oauth/providers/zitadel.ts +29 -0
- package/src/server/personal.ts +17 -0
- package/src/server/session.ts +150 -0
- package/src/server/token-requests.ts +13 -0
- package/src/server/token.ts +81 -0
- package/src/server/totp.ts +165 -0
- package/src/server/webauthn/cbor.ts +154 -0
- package/src/server/webauthn/cose.ts +95 -0
- package/src/server/webauthn/data.ts +50 -0
- package/src/server/webauthn/handler.ts +510 -0
- package/src/single-use.ts +41 -0
- package/src/types.ts +91 -0
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { HandlerContext } from "hamur/server"
|
|
2
|
+
import { presetHandler, type OAuthPreset, type OAuthPresetOptions } from "./preset.ts"
|
|
3
|
+
|
|
4
|
+
export const seznam: OAuthPreset = {
|
|
5
|
+
name: "seznam",
|
|
6
|
+
record: () => ({
|
|
7
|
+
authorizationURL: "https://login.szn.cz/api/v1/oauth/auth",
|
|
8
|
+
tokenURL: "https://login.szn.cz/api/v1/oauth/token",
|
|
9
|
+
userURL: "https://login.szn.cz/api/v1/user",
|
|
10
|
+
scope: ["identity"],
|
|
11
|
+
tokenAuth: "client_secret_post",
|
|
12
|
+
mapUser: (profile) => profile,
|
|
13
|
+
}),
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** Sign in with Seznam; `user` is the `GET /api/v1/user` profile. */
|
|
17
|
+
export function oauthSeznam(options: OAuthPresetOptions): (input: unknown, ctx: HandlerContext) => Promise<unknown> {
|
|
18
|
+
return presetHandler(seznam, options)
|
|
19
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import type { HandlerContext } from "hamur/server"
|
|
2
|
+
import { providerJSON } from "../handler.ts"
|
|
3
|
+
import { presetHandler, requireKey, userOf, type OAuthPreset, type OAuthPresetOptions } from "./preset.ts"
|
|
4
|
+
|
|
5
|
+
export interface ShopifyCustomerConfig {
|
|
6
|
+
/** The shop's numeric id, from the Customer Account API settings; required. */
|
|
7
|
+
shopId?: string
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
const QUERY = "{ customer { id firstName lastName displayName emailAddress { emailAddress } } }"
|
|
11
|
+
|
|
12
|
+
export const shopifyCustomer: OAuthPreset<ShopifyCustomerConfig> = {
|
|
13
|
+
name: "shopifyCustomer",
|
|
14
|
+
record: (config) => {
|
|
15
|
+
const shop = encodeURIComponent(requireKey("shopifyCustomer", config, "shopId"))
|
|
16
|
+
return {
|
|
17
|
+
authorizationURL: `https://shopify.com/authentication/${shop}/oauth/authorize`,
|
|
18
|
+
tokenURL: `https://shopify.com/authentication/${shop}/oauth/token`,
|
|
19
|
+
scope: ["openid", "email", "customer-account-api:full"],
|
|
20
|
+
tokenAuth: "client_secret_basic",
|
|
21
|
+
// The Customer Account API takes the bare access token, without "Bearer".
|
|
22
|
+
profile: ({ event, tokens }) =>
|
|
23
|
+
providerJSON(
|
|
24
|
+
event,
|
|
25
|
+
`https://shopify.com/${shop}/account/customer/api/2025-01/graphql`,
|
|
26
|
+
{
|
|
27
|
+
method: "POST",
|
|
28
|
+
headers: {
|
|
29
|
+
authorization: tokens.access_token,
|
|
30
|
+
accept: "application/json",
|
|
31
|
+
"content-type": "application/json",
|
|
32
|
+
},
|
|
33
|
+
body: JSON.stringify({ query: QUERY }),
|
|
34
|
+
},
|
|
35
|
+
"user request",
|
|
36
|
+
),
|
|
37
|
+
mapUser: (profile) => userOf(profile.data?.customer),
|
|
38
|
+
}
|
|
39
|
+
},
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** Sign in a shop's customer with the Customer Account API; `user` is the GraphQL `customer`. */
|
|
43
|
+
export function oauthShopifyCustomer(
|
|
44
|
+
options: OAuthPresetOptions & ShopifyCustomerConfig,
|
|
45
|
+
): (input: unknown, ctx: HandlerContext) => Promise<unknown> {
|
|
46
|
+
return presetHandler(shopifyCustomer, options)
|
|
47
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { HandlerContext } from "hamur/server"
|
|
2
|
+
import { providerJSON } from "../handler.ts"
|
|
3
|
+
import { presetHandler, userOf, type OAuthPreset, type OAuthPresetOptions } from "./preset.ts"
|
|
4
|
+
|
|
5
|
+
export const slack: OAuthPreset = {
|
|
6
|
+
name: "slack",
|
|
7
|
+
record: () => ({
|
|
8
|
+
authorizationURL: "https://slack.com/openid/connect/authorize",
|
|
9
|
+
tokenURL: "https://slack.com/api/openid.connect.token",
|
|
10
|
+
scope: ["openid", "email", "profile"],
|
|
11
|
+
tokenAuth: "client_secret_post",
|
|
12
|
+
profile: ({ event, tokens }) =>
|
|
13
|
+
providerJSON(
|
|
14
|
+
event,
|
|
15
|
+
"https://slack.com/api/openid.connect.userInfo",
|
|
16
|
+
{ method: "POST", headers: { authorization: `Bearer ${tokens.access_token}`, accept: "application/json" } },
|
|
17
|
+
"user request",
|
|
18
|
+
),
|
|
19
|
+
// The Web API answers a failure with 200 and { ok: false, error }.
|
|
20
|
+
mapUser: (profile) => userOf(profile.ok === true ? profile : undefined),
|
|
21
|
+
}),
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** Sign in with Slack; `user` is the OpenID userinfo. */
|
|
25
|
+
export function oauthSlack(options: OAuthPresetOptions): (input: unknown, ctx: HandlerContext) => Promise<unknown> {
|
|
26
|
+
return presetHandler(slack, options)
|
|
27
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { HandlerContext } from "hamur/server"
|
|
2
|
+
import { presetHandler, type OAuthPreset, type OAuthPresetOptions } from "./preset.ts"
|
|
3
|
+
|
|
4
|
+
export const spotify: OAuthPreset = {
|
|
5
|
+
name: "spotify",
|
|
6
|
+
record: () => ({
|
|
7
|
+
authorizationURL: "https://accounts.spotify.com/authorize",
|
|
8
|
+
tokenURL: "https://accounts.spotify.com/api/token",
|
|
9
|
+
userURL: "https://api.spotify.com/v1/me",
|
|
10
|
+
scope: ["user-read-email"],
|
|
11
|
+
tokenAuth: "client_secret_basic",
|
|
12
|
+
mapUser: (profile) => profile,
|
|
13
|
+
}),
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** Sign in with Spotify; `user` is the `GET /v1/me` profile. */
|
|
17
|
+
export function oauthSpotify(options: OAuthPresetOptions): (input: unknown, ctx: HandlerContext) => Promise<unknown> {
|
|
18
|
+
return presetHandler(spotify, options)
|
|
19
|
+
}
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
/// <reference path="../../../env.d.ts" />
|
|
2
|
+
import { baseURL, redirect as targets } from "#hamur/auth/guards"
|
|
3
|
+
import {
|
|
4
|
+
createError,
|
|
5
|
+
deleteCookie,
|
|
6
|
+
getCookie,
|
|
7
|
+
getRequestURL,
|
|
8
|
+
setCookie,
|
|
9
|
+
type HandlerContext,
|
|
10
|
+
type HamurEvent,
|
|
11
|
+
} from "hamur/server"
|
|
12
|
+
import { sealFor, timingSafeEqual, unsealFor } from "hamur/server/internal"
|
|
13
|
+
import { flowCookie } from "../cookie.ts"
|
|
14
|
+
import { appPath, noAnswer, PURPOSE, randomToken, TIMEOUT, TTL, type OAuthError } from "../handler.ts"
|
|
15
|
+
import { presetConfig } from "./preset.ts"
|
|
16
|
+
|
|
17
|
+
export const STEAM_ENDPOINT = "https://steamcommunity.com/openid/login"
|
|
18
|
+
const NS = "http://specs.openid.net/auth/2.0"
|
|
19
|
+
const SELECT = `${NS}/identifier_select`
|
|
20
|
+
const CLAIMED = /^https:\/\/steamcommunity\.com\/openid\/id\/(\d+)$/
|
|
21
|
+
/** OpenID 2.0 §10.1: the fields a positive assertion must sign; one left out could be edited without breaking `sig`. */
|
|
22
|
+
const MUST_SIGN = ["op_endpoint", "claimed_id", "identity", "return_to", "response_nonce", "assoc_handle"]
|
|
23
|
+
const PROVIDER = "steam"
|
|
24
|
+
|
|
25
|
+
export interface SteamUser {
|
|
26
|
+
/** The SteamID64. */
|
|
27
|
+
steamId: string
|
|
28
|
+
claimedId: string
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export interface SteamOptions {
|
|
32
|
+
onSuccess(event: HamurEvent, result: { user: SteamUser }): unknown
|
|
33
|
+
/** Called with `{ error: "cancel" }` when the user cancels at Steam. */
|
|
34
|
+
onError?(event: HamurEvent, error: OAuthError): unknown
|
|
35
|
+
/** The `openid.return_to` without `hamur_state`; default the route's absolute URL. */
|
|
36
|
+
redirectURL?: string
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
interface SteamFlow {
|
|
40
|
+
state: string
|
|
41
|
+
provider: string
|
|
42
|
+
route: string
|
|
43
|
+
redirectURI: string
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const isFlow = (value: unknown): value is SteamFlow =>
|
|
47
|
+
typeof value === "object" &&
|
|
48
|
+
value !== null &&
|
|
49
|
+
["state", "provider", "route", "redirectURI"].every(
|
|
50
|
+
(key) => typeof (value as Record<string, unknown>)[key] === "string",
|
|
51
|
+
)
|
|
52
|
+
|
|
53
|
+
const failed = () => createError({ status: 401, message: "Steam OpenID check failed" })
|
|
54
|
+
|
|
55
|
+
const returnTo = (redirectURI: string, state: string): string => {
|
|
56
|
+
const url = new URL(redirectURI)
|
|
57
|
+
url.searchParams.set("hamur_state", state)
|
|
58
|
+
return url.href
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** What the callback's assertion must hold before it is sent back to Steam; a SteamUser when it does. */
|
|
62
|
+
export function checkAssertion(params: URLSearchParams, expectedReturnTo: string): SteamUser {
|
|
63
|
+
if (params.get("openid.mode") !== "id_res") throw failed()
|
|
64
|
+
if (params.get("openid.ns") !== NS) throw failed()
|
|
65
|
+
if (params.get("openid.op_endpoint") !== STEAM_ENDPOINT) throw failed()
|
|
66
|
+
if (params.get("openid.return_to") !== expectedReturnTo) throw failed()
|
|
67
|
+
const claimedId = params.get("openid.claimed_id")
|
|
68
|
+
const match = claimedId === null ? null : CLAIMED.exec(claimedId)
|
|
69
|
+
if (!match || params.get("openid.identity") !== claimedId) throw failed()
|
|
70
|
+
const signed = (params.get("openid.signed") ?? "").split(",")
|
|
71
|
+
if (!MUST_SIGN.every((field) => signed.includes(field))) throw failed()
|
|
72
|
+
if (!params.get("openid.sig") || !params.get("openid.assoc_handle") || !params.get("openid.response_nonce"))
|
|
73
|
+
throw failed()
|
|
74
|
+
return { steamId: match[1]!, claimedId: claimedId! }
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** OpenID 2.0 §11.4.2.1: every `openid.*` field of the callback, in order, with only `openid.mode` replaced. */
|
|
78
|
+
export function verificationBody(params: URLSearchParams): URLSearchParams {
|
|
79
|
+
const body = new URLSearchParams()
|
|
80
|
+
for (const [key, value] of params)
|
|
81
|
+
if (key.startsWith("openid.")) body.append(key, key === "openid.mode" ? "check_authentication" : value)
|
|
82
|
+
return body
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
async function verified(event: HamurEvent, params: URLSearchParams): Promise<boolean> {
|
|
86
|
+
let response: Response
|
|
87
|
+
let text: string
|
|
88
|
+
try {
|
|
89
|
+
response = await fetch(STEAM_ENDPOINT, {
|
|
90
|
+
method: "POST",
|
|
91
|
+
headers: { "content-type": "application/x-www-form-urlencoded", accept: "text/plain" },
|
|
92
|
+
body: verificationBody(params),
|
|
93
|
+
redirect: "manual",
|
|
94
|
+
signal: AbortSignal.timeout(TIMEOUT),
|
|
95
|
+
})
|
|
96
|
+
text = await response.text()
|
|
97
|
+
} catch {
|
|
98
|
+
throw noAnswer()
|
|
99
|
+
}
|
|
100
|
+
if (!response.ok) {
|
|
101
|
+
event.log.child({ name: "hamur" }).warn("Steam check_authentication failed", { status: response.status })
|
|
102
|
+
return false
|
|
103
|
+
}
|
|
104
|
+
return text.split(/\r?\n/).includes("is_valid:true")
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** Sign in with Steam, an OpenID 2.0 assertion bound to a sealed `state` in the flow cookie. */
|
|
108
|
+
export function oauthSteam(options: SteamOptions): (input: unknown, ctx: HandlerContext) => Promise<unknown> {
|
|
109
|
+
const config = presetConfig(PROVIDER, options as unknown as Record<string, unknown>)
|
|
110
|
+
const redirectURL = typeof config.redirectURL === "string" ? config.redirectURL : undefined
|
|
111
|
+
return async (_input, { event }) => {
|
|
112
|
+
const url = getRequestURL(event)
|
|
113
|
+
if (event.request.method !== "GET") {
|
|
114
|
+
event.response.headers.set("allow", "GET")
|
|
115
|
+
throw createError({ status: 405, message: "Method Not Allowed" })
|
|
116
|
+
}
|
|
117
|
+
event.response.headers.set("cache-control", "no-store")
|
|
118
|
+
const route = appPath(url.pathname)
|
|
119
|
+
const cookie = await flowCookie({ dev: Boolean(import.meta.dev), protocol: url.protocol, route, formPost: false })
|
|
120
|
+
const redirectURI = redirectURL ?? `${url.origin}${url.pathname}`
|
|
121
|
+
const params = url.searchParams
|
|
122
|
+
|
|
123
|
+
if (!params.has("openid.mode") && !params.has("hamur_state")) {
|
|
124
|
+
const state = randomToken()
|
|
125
|
+
const flow: SteamFlow = { state, provider: PROVIDER, route, redirectURI }
|
|
126
|
+
setCookie(event, cookie.name, await sealFor(flow, { purpose: PURPOSE, ttl: TTL }), {
|
|
127
|
+
...cookie.options,
|
|
128
|
+
maxAge: TTL,
|
|
129
|
+
})
|
|
130
|
+
const target = new URL(STEAM_ENDPOINT)
|
|
131
|
+
target.searchParams.set("openid.ns", NS)
|
|
132
|
+
target.searchParams.set("openid.mode", "checkid_setup")
|
|
133
|
+
target.searchParams.set("openid.return_to", returnTo(redirectURI, state))
|
|
134
|
+
target.searchParams.set("openid.realm", new URL(redirectURI).origin)
|
|
135
|
+
target.searchParams.set("openid.identity", SELECT)
|
|
136
|
+
target.searchParams.set("openid.claimed_id", SELECT)
|
|
137
|
+
return new Response(null, { status: 302, headers: { location: target.href } })
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
deleteCookie(event, cookie.name, cookie.options)
|
|
141
|
+
const given = params.get("hamur_state") ?? ""
|
|
142
|
+
const sealed = getCookie(event, cookie.name)
|
|
143
|
+
const flow = sealed === undefined ? undefined : await unsealFor(sealed, { purpose: PURPOSE })
|
|
144
|
+
if (
|
|
145
|
+
!isFlow(flow) ||
|
|
146
|
+
flow.route !== route ||
|
|
147
|
+
flow.provider !== PROVIDER ||
|
|
148
|
+
flow.redirectURI !== redirectURI ||
|
|
149
|
+
!given ||
|
|
150
|
+
!timingSafeEqual(flow.state, given)
|
|
151
|
+
) {
|
|
152
|
+
throw createError({ status: 400, message: "Invalid OAuth state" })
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
if (params.get("openid.mode") === "cancel" && options.onError) return options.onError(event, { error: "cancel" })
|
|
156
|
+
const user = checkAssertion(params, returnTo(flow.redirectURI, flow.state))
|
|
157
|
+
if (!(await verified(event, params))) throw failed()
|
|
158
|
+
const value = await options.onSuccess(event, { user })
|
|
159
|
+
if (value !== undefined) return value
|
|
160
|
+
return new Response(null, { status: 302, headers: { location: `${baseURL.replace(/\/+$/, "")}${targets.home}` } })
|
|
161
|
+
}
|
|
162
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { HandlerContext } from "hamur/server"
|
|
2
|
+
import { presetHandler, type OAuthPreset, type OAuthPresetOptions } from "./preset.ts"
|
|
3
|
+
|
|
4
|
+
export const strava: OAuthPreset = {
|
|
5
|
+
name: "strava",
|
|
6
|
+
record: () => ({
|
|
7
|
+
authorizationURL: "https://www.strava.com/oauth/authorize",
|
|
8
|
+
tokenURL: "https://www.strava.com/oauth/token",
|
|
9
|
+
userURL: "https://www.strava.com/api/v3/athlete",
|
|
10
|
+
scope: ["read"],
|
|
11
|
+
scopeSeparator: ",",
|
|
12
|
+
tokenAuth: "client_secret_post",
|
|
13
|
+
mapUser: (profile) => profile,
|
|
14
|
+
}),
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** Sign in with Strava; `user` is the `GET /athlete` profile. */
|
|
18
|
+
export function oauthStrava(options: OAuthPresetOptions): (input: unknown, ctx: HandlerContext) => Promise<unknown> {
|
|
19
|
+
return presetHandler(strava, options)
|
|
20
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { HandlerContext } from "hamur/server"
|
|
2
|
+
import { presetHandler, userOf, type OAuthPreset, type OAuthPresetOptions } from "./preset.ts"
|
|
3
|
+
|
|
4
|
+
export const tiktok: OAuthPreset = {
|
|
5
|
+
name: "tiktok",
|
|
6
|
+
record: () => ({
|
|
7
|
+
authorizationURL: "https://www.tiktok.com/v2/auth/authorize/",
|
|
8
|
+
tokenURL: "https://open.tiktokapis.com/v2/oauth/token/",
|
|
9
|
+
userURL: "https://open.tiktokapis.com/v2/user/info/?fields=open_id,union_id,avatar_url,display_name",
|
|
10
|
+
scope: ["user.info.basic"],
|
|
11
|
+
scopeSeparator: ",",
|
|
12
|
+
clientIdParam: "client_key",
|
|
13
|
+
tokenAuth: "client_secret_post",
|
|
14
|
+
pkce: false,
|
|
15
|
+
mapUser: (profile) => userOf(profile.data?.user),
|
|
16
|
+
}),
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** Sign in with TikTok (Login Kit for web; the client id is the app's client key); `user` is `data.user` of `GET /v2/user/info/`. */
|
|
20
|
+
export function oauthTikTok(options: OAuthPresetOptions): (input: unknown, ctx: HandlerContext) => Promise<unknown> {
|
|
21
|
+
return presetHandler(tiktok, options)
|
|
22
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { HandlerContext } from "hamur/server"
|
|
2
|
+
import { providerJSON } from "../handler.ts"
|
|
3
|
+
import { presetHandler, userOf, type OAuthPreset, type OAuthPresetOptions } from "./preset.ts"
|
|
4
|
+
|
|
5
|
+
export const twitch: OAuthPreset = {
|
|
6
|
+
name: "twitch",
|
|
7
|
+
record: () => ({
|
|
8
|
+
authorizationURL: "https://id.twitch.tv/oauth2/authorize",
|
|
9
|
+
tokenURL: "https://id.twitch.tv/oauth2/token",
|
|
10
|
+
scope: ["user:read:email"],
|
|
11
|
+
tokenAuth: "client_secret_post",
|
|
12
|
+
profile: ({ event, tokens, clientId }) =>
|
|
13
|
+
providerJSON(
|
|
14
|
+
event,
|
|
15
|
+
"https://api.twitch.tv/helix/users",
|
|
16
|
+
{
|
|
17
|
+
headers: {
|
|
18
|
+
authorization: `Bearer ${tokens.access_token}`,
|
|
19
|
+
"client-id": clientId,
|
|
20
|
+
accept: "application/json",
|
|
21
|
+
},
|
|
22
|
+
},
|
|
23
|
+
"user request",
|
|
24
|
+
),
|
|
25
|
+
mapUser: (profile) => userOf(profile.data?.[0]),
|
|
26
|
+
}),
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** Sign in with Twitch; `user` is the first of Helix `GET /users`'s `data`. */
|
|
30
|
+
export function oauthTwitch(options: OAuthPresetOptions): (input: unknown, ctx: HandlerContext) => Promise<unknown> {
|
|
31
|
+
return presetHandler(twitch, options)
|
|
32
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { HandlerContext } from "hamur/server"
|
|
2
|
+
import { providerJSON } from "../handler.ts"
|
|
3
|
+
import { presetHandler, userOf, type OAuthPreset, type OAuthPresetBaseOptions } from "./preset.ts"
|
|
4
|
+
|
|
5
|
+
export const vk: OAuthPreset = {
|
|
6
|
+
name: "vk",
|
|
7
|
+
record: () => ({
|
|
8
|
+
authorizationURL: "https://id.vk.com/authorize",
|
|
9
|
+
tokenURL: "https://id.vk.com/oauth2/auth",
|
|
10
|
+
scope: ["vkid.personal_info", "email"],
|
|
11
|
+
tokenAuth: "none",
|
|
12
|
+
// VK ID binds the code to the device_id and state of its callback, and wants both back.
|
|
13
|
+
tokenRequest: ({ event, url, headers, body, params }) => {
|
|
14
|
+
body.set("device_id", params.get("device_id") ?? "")
|
|
15
|
+
body.set("state", params.get("state") ?? "")
|
|
16
|
+
return providerJSON(event, url, { method: "POST", headers, body }, "token request")
|
|
17
|
+
},
|
|
18
|
+
profile: ({ event, tokens, clientId }) =>
|
|
19
|
+
providerJSON(
|
|
20
|
+
event,
|
|
21
|
+
"https://id.vk.com/oauth2/user_info",
|
|
22
|
+
{
|
|
23
|
+
method: "POST",
|
|
24
|
+
headers: { "content-type": "application/x-www-form-urlencoded", accept: "application/json" },
|
|
25
|
+
body: new URLSearchParams({ client_id: clientId, access_token: tokens.access_token }),
|
|
26
|
+
},
|
|
27
|
+
"user request",
|
|
28
|
+
),
|
|
29
|
+
mapUser: (profile) => userOf(profile.user),
|
|
30
|
+
}),
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** Sign in with VK ID, a public client with PKCE (no client secret); `user` is the `user` of `oauth2/user_info`. */
|
|
34
|
+
export function oauthVK(options: OAuthPresetBaseOptions): (input: unknown, ctx: HandlerContext) => Promise<unknown> {
|
|
35
|
+
return presetHandler(vk, options)
|
|
36
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { HandlerContext } from "hamur/server"
|
|
2
|
+
import { providerJSON } from "../handler.ts"
|
|
3
|
+
import { presetHandler, userOf, type OAuthPreset, type OAuthPresetOptions } from "./preset.ts"
|
|
4
|
+
|
|
5
|
+
export const wechat: OAuthPreset = {
|
|
6
|
+
name: "wechat",
|
|
7
|
+
record: () => ({
|
|
8
|
+
authorizationURL: "https://open.weixin.qq.com/connect/qrconnect#wechat_redirect",
|
|
9
|
+
tokenURL: "https://api.weixin.qq.com/sns/oauth2/access_token",
|
|
10
|
+
scope: ["snsapi_login"],
|
|
11
|
+
clientIdParam: "appid",
|
|
12
|
+
tokenAuth: "client_secret_post",
|
|
13
|
+
pkce: false,
|
|
14
|
+
// WeChat takes the code exchange as a GET with appid and secret in the query, and answers errors with 200 and errcode.
|
|
15
|
+
tokenRequest: ({ event, url, body }) => {
|
|
16
|
+
const target = new URL(url)
|
|
17
|
+
for (const [key, value] of [
|
|
18
|
+
["appid", body.get("appid")],
|
|
19
|
+
["secret", body.get("client_secret")],
|
|
20
|
+
["code", body.get("code")],
|
|
21
|
+
["grant_type", "authorization_code"],
|
|
22
|
+
] as const)
|
|
23
|
+
target.searchParams.set(key, value ?? "")
|
|
24
|
+
return providerJSON(event, target.href, { headers: { accept: "application/json" } }, "token request")
|
|
25
|
+
},
|
|
26
|
+
profile: ({ event, tokens }) => {
|
|
27
|
+
const target = new URL("https://api.weixin.qq.com/sns/userinfo")
|
|
28
|
+
target.searchParams.set("access_token", tokens.access_token)
|
|
29
|
+
target.searchParams.set("openid", typeof tokens.openid === "string" ? tokens.openid : "")
|
|
30
|
+
return providerJSON(event, target.href, { headers: { accept: "application/json" } }, "user request")
|
|
31
|
+
},
|
|
32
|
+
mapUser: (profile) => userOf(typeof profile.openid === "string" ? profile : undefined),
|
|
33
|
+
}),
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** Sign in with WeChat (website QR login; the client id is the AppID); `user` is `sns/userinfo` (`openid`, `unionid`, `nickname`, …). */
|
|
37
|
+
export function oauthWeChat(options: OAuthPresetOptions): (input: unknown, ctx: HandlerContext) => Promise<unknown> {
|
|
38
|
+
return presetHandler(wechat, options)
|
|
39
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { HandlerContext } from "hamur/server"
|
|
2
|
+
import { presetHandler, userOf, type OAuthPreset, type OAuthPresetOptions } from "./preset.ts"
|
|
3
|
+
|
|
4
|
+
export const workos: OAuthPreset = {
|
|
5
|
+
name: "workos",
|
|
6
|
+
record: () => ({
|
|
7
|
+
authorizationURL: "https://api.workos.com/user_management/authorize",
|
|
8
|
+
tokenURL: "https://api.workos.com/user_management/authenticate",
|
|
9
|
+
scope: [],
|
|
10
|
+
tokenAuth: "client_secret_post",
|
|
11
|
+
authorizationParams: { provider: "authkit" },
|
|
12
|
+
mapUser: (_profile, tokens) => userOf(tokens.user),
|
|
13
|
+
}),
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** Sign in with WorkOS AuthKit (`authorizationParams: { provider }` picks another); `user` is the `user` of the authenticate answer. */
|
|
17
|
+
export function oauthWorkOS(options: OAuthPresetOptions): (input: unknown, ctx: HandlerContext) => Promise<unknown> {
|
|
18
|
+
return presetHandler(workos, options)
|
|
19
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { HandlerContext } from "hamur/server"
|
|
2
|
+
import { presetHandler, userOf, type OAuthPreset, type OAuthPresetOptions } from "./preset.ts"
|
|
3
|
+
|
|
4
|
+
export const x: OAuthPreset = {
|
|
5
|
+
name: "x",
|
|
6
|
+
record: () => ({
|
|
7
|
+
authorizationURL: "https://x.com/i/oauth2/authorize",
|
|
8
|
+
tokenURL: "https://api.x.com/2/oauth2/token",
|
|
9
|
+
userURL: "https://api.x.com/2/users/me?user.fields=profile_image_url,verified",
|
|
10
|
+
scope: ["users.read", "tweet.read"],
|
|
11
|
+
tokenAuth: "client_secret_basic",
|
|
12
|
+
mapUser: (profile) => userOf(profile.data),
|
|
13
|
+
}),
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** Sign in with X; `user` is the `data` of `GET /2/users/me`. */
|
|
17
|
+
export function oauthX(options: OAuthPresetOptions): (input: unknown, ctx: HandlerContext) => Promise<unknown> {
|
|
18
|
+
return presetHandler(x, options)
|
|
19
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { HandlerContext } from "hamur/server"
|
|
2
|
+
import { originOf, presetHandler, requireKey, type OAuthPreset, type OAuthPresetOptions } from "./preset.ts"
|
|
3
|
+
|
|
4
|
+
export interface XsuaaConfig {
|
|
5
|
+
/** The service's `url` without the scheme, `example.authentication.eu10.hana.ondemand.com`; required. */
|
|
6
|
+
domain?: string
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export const xsuaa: OAuthPreset<XsuaaConfig> = {
|
|
10
|
+
name: "xsuaa",
|
|
11
|
+
record: (config) => {
|
|
12
|
+
const base = originOf(requireKey("xsuaa", config, "domain"))
|
|
13
|
+
return {
|
|
14
|
+
authorizationURL: `${base}/oauth/authorize`,
|
|
15
|
+
tokenURL: `${base}/oauth/token`,
|
|
16
|
+
userURL: `${base}/userinfo`,
|
|
17
|
+
scope: ["openid"],
|
|
18
|
+
tokenAuth: "client_secret_basic",
|
|
19
|
+
mapUser: (profile) => profile,
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** Sign in with SAP BTP's XSUAA; `user` is the OpenID userinfo. */
|
|
25
|
+
export function oauthXsuaa(
|
|
26
|
+
options: OAuthPresetOptions & XsuaaConfig,
|
|
27
|
+
): (input: unknown, ctx: HandlerContext) => Promise<unknown> {
|
|
28
|
+
return presetHandler(xsuaa, options)
|
|
29
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { HandlerContext } from "hamur/server"
|
|
2
|
+
import { providerJSON } from "../handler.ts"
|
|
3
|
+
import { presetHandler, type OAuthPreset, type OAuthPresetOptions } from "./preset.ts"
|
|
4
|
+
|
|
5
|
+
export const yandex: OAuthPreset = {
|
|
6
|
+
name: "yandex",
|
|
7
|
+
record: () => ({
|
|
8
|
+
authorizationURL: "https://oauth.yandex.com/authorize",
|
|
9
|
+
tokenURL: "https://oauth.yandex.com/token",
|
|
10
|
+
scope: ["login:info", "login:email"],
|
|
11
|
+
tokenAuth: "client_secret_post",
|
|
12
|
+
profile: ({ event, tokens }) =>
|
|
13
|
+
providerJSON(
|
|
14
|
+
event,
|
|
15
|
+
"https://login.yandex.ru/info?format=json",
|
|
16
|
+
{ headers: { authorization: `OAuth ${tokens.access_token}`, accept: "application/json" } },
|
|
17
|
+
"user request",
|
|
18
|
+
),
|
|
19
|
+
mapUser: (profile) => profile,
|
|
20
|
+
}),
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** Sign in with Yandex; `user` is the `login.yandex.ru/info` profile. */
|
|
24
|
+
export function oauthYandex(options: OAuthPresetOptions): (input: unknown, ctx: HandlerContext) => Promise<unknown> {
|
|
25
|
+
return presetHandler(yandex, options)
|
|
26
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { HandlerContext } from "hamur/server"
|
|
2
|
+
import { presetHandler, requireKey, userOf, type OAuthPreset, type OAuthPresetOptions } from "./preset.ts"
|
|
3
|
+
|
|
4
|
+
export interface ZendeskConfig {
|
|
5
|
+
/** The account's subdomain, `example` for `example.zendesk.com`; required. */
|
|
6
|
+
subdomain?: string
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export const zendesk: OAuthPreset<ZendeskConfig> = {
|
|
10
|
+
name: "zendesk",
|
|
11
|
+
record: (config) => {
|
|
12
|
+
const base = `https://${encodeURIComponent(requireKey("zendesk", config, "subdomain"))}.zendesk.com`
|
|
13
|
+
return {
|
|
14
|
+
authorizationURL: `${base}/oauth/authorizations/new`,
|
|
15
|
+
tokenURL: `${base}/oauth/tokens`,
|
|
16
|
+
userURL: `${base}/api/v2/users/me`,
|
|
17
|
+
scope: ["read"],
|
|
18
|
+
tokenAuth: "client_secret_post",
|
|
19
|
+
mapUser: (profile) => userOf(profile.user),
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** Sign in with Zendesk; `user` is the `user` of `GET /api/v2/users/me`. */
|
|
25
|
+
export function oauthZendesk(
|
|
26
|
+
options: OAuthPresetOptions & ZendeskConfig,
|
|
27
|
+
): (input: unknown, ctx: HandlerContext) => Promise<unknown> {
|
|
28
|
+
return presetHandler(zendesk, options)
|
|
29
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { HandlerContext } from "hamur/server"
|
|
2
|
+
import { originOf, presetHandler, requireKey, type OAuthPreset, type OAuthPresetOptions } from "./preset.ts"
|
|
3
|
+
|
|
4
|
+
export interface ZitadelConfig {
|
|
5
|
+
/** The instance, `example.zitadel.cloud`; required. */
|
|
6
|
+
domain?: string
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export const zitadel: OAuthPreset<ZitadelConfig> = {
|
|
10
|
+
name: "zitadel",
|
|
11
|
+
record: (config) => {
|
|
12
|
+
const base = originOf(requireKey("zitadel", config, "domain"))
|
|
13
|
+
return {
|
|
14
|
+
authorizationURL: `${base}/oauth/v2/authorize`,
|
|
15
|
+
tokenURL: `${base}/oauth/v2/token`,
|
|
16
|
+
userURL: `${base}/oidc/v1/userinfo`,
|
|
17
|
+
scope: ["openid", "profile", "email"],
|
|
18
|
+
tokenAuth: "client_secret_basic",
|
|
19
|
+
mapUser: (profile) => profile,
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** Sign in with Zitadel; `user` is the OpenID userinfo. */
|
|
25
|
+
export function oauthZitadel(
|
|
26
|
+
options: OAuthPresetOptions & ZitadelConfig,
|
|
27
|
+
): (input: unknown, ctx: HandlerContext) => Promise<unknown> {
|
|
28
|
+
return presetHandler(zitadel, options)
|
|
29
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { HamurEvent } from "hamur/server"
|
|
2
|
+
|
|
3
|
+
const personal = new WeakSet<HamurEvent>()
|
|
4
|
+
|
|
5
|
+
/** The render put a user's session in the page, so no shared cache may keep the answer. */
|
|
6
|
+
export function markPersonal(event: HamurEvent): void {
|
|
7
|
+
personal.add(event)
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/** `private, no-store` and `Vary: Cookie` on a personal answer, over any route-rule `headers` value. */
|
|
11
|
+
export function withPersonalHeaders(event: HamurEvent, response: Response): void {
|
|
12
|
+
if (!personal.has(event)) return
|
|
13
|
+
response.headers.set("cache-control", "private, no-store")
|
|
14
|
+
const vary = response.headers.get("vary")
|
|
15
|
+
if (!vary?.split(",").some((name) => name.trim().toLowerCase() === "cookie"))
|
|
16
|
+
response.headers.set("vary", vary ? `${vary}, Cookie` : "Cookie")
|
|
17
|
+
}
|