@hamur/auth 0.0.0-stage → 0.0.1
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 +32 -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 +122 -4
- package/src/adapters/AuthState.svelte +14 -0
- package/src/adapters/angular.ts +80 -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,80 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Component,
|
|
3
|
+
DestroyRef,
|
|
4
|
+
afterNextRender,
|
|
5
|
+
assertInInjectionContext,
|
|
6
|
+
computed,
|
|
7
|
+
inject,
|
|
8
|
+
signal,
|
|
9
|
+
type Signal,
|
|
10
|
+
} from "@angular/core"
|
|
11
|
+
import { injectApp } from "@hamur/adapter-angular"
|
|
12
|
+
import type { PublicUserSession, User } from "../index.ts"
|
|
13
|
+
import { authStore, type AuthStateSlot } from "../runtime/store.ts"
|
|
14
|
+
import {
|
|
15
|
+
webAuthn,
|
|
16
|
+
webAuthnSupported,
|
|
17
|
+
type WebAuthnCalls,
|
|
18
|
+
type WebAuthnOptions,
|
|
19
|
+
type WebAuthnUser,
|
|
20
|
+
} from "../client/webauthn.ts"
|
|
21
|
+
|
|
22
|
+
export type { AuthStateSlot }
|
|
23
|
+
export type { WebAuthnOptions, WebAuthnUser }
|
|
24
|
+
|
|
25
|
+
export interface UserSession {
|
|
26
|
+
ready: Signal<boolean>
|
|
27
|
+
loggedIn: Signal<boolean>
|
|
28
|
+
user: Signal<User | null>
|
|
29
|
+
session: Signal<PublicUserSession | null>
|
|
30
|
+
fetch(): Promise<void>
|
|
31
|
+
clear(): Promise<void>
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** The session of the app the injector belongs to, one per request on the server; the listener ends with the injector. */
|
|
35
|
+
export function injectUserSession(): UserSession {
|
|
36
|
+
assertInInjectionContext(injectUserSession)
|
|
37
|
+
const store = authStore(injectApp())
|
|
38
|
+
const state = signal(store.get())
|
|
39
|
+
inject(DestroyRef).onDestroy(store.subscribe(() => state.set(store.get())))
|
|
40
|
+
return {
|
|
41
|
+
ready: computed(() => state().ready),
|
|
42
|
+
loggedIn: computed(() => state().loggedIn),
|
|
43
|
+
user: computed(() => state().user),
|
|
44
|
+
session: computed(() => state().session),
|
|
45
|
+
fetch: store.fetch,
|
|
46
|
+
clear: store.clear,
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* `<hamur-auth-state #auth>@if (auth.loggedIn()) { … } @else { … }</hamur-auth-state>`: the content once the session is read,
|
|
52
|
+
* the `[placeholder]` content until then.
|
|
53
|
+
*/
|
|
54
|
+
@Component({
|
|
55
|
+
selector: "hamur-auth-state",
|
|
56
|
+
template: `@if (ready()) {
|
|
57
|
+
<ng-content />
|
|
58
|
+
} @else {
|
|
59
|
+
<ng-content select="[placeholder]" />
|
|
60
|
+
}`,
|
|
61
|
+
})
|
|
62
|
+
export class AuthState {
|
|
63
|
+
private readonly session = injectUserSession()
|
|
64
|
+
readonly ready = this.session.ready
|
|
65
|
+
readonly loggedIn = this.session.loggedIn
|
|
66
|
+
readonly user = this.session.user
|
|
67
|
+
readonly clear = this.session.clear
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export interface WebAuthn extends WebAuthnCalls {
|
|
71
|
+
isSupported: Signal<boolean>
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** `isSupported` is false in the server render and until the first render in the browser, so hydration agrees. */
|
|
75
|
+
export function injectWebAuthn(options: WebAuthnOptions): WebAuthn {
|
|
76
|
+
assertInInjectionContext(injectWebAuthn)
|
|
77
|
+
const isSupported = signal(false)
|
|
78
|
+
afterNextRender(() => isSupported.set(webAuthnSupported()))
|
|
79
|
+
return { isSupported: isSupported.asReadonly(), ...webAuthn(options) }
|
|
80
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { useApp } from "@hamur/adapter-html"
|
|
2
|
+
import { authStore, type UserSessionState } from "../runtime/store.ts"
|
|
3
|
+
import {
|
|
4
|
+
webAuthn,
|
|
5
|
+
webAuthnSupported,
|
|
6
|
+
type WebAuthnCalls,
|
|
7
|
+
type WebAuthnOptions,
|
|
8
|
+
type WebAuthnUser,
|
|
9
|
+
} from "../client/webauthn.ts"
|
|
10
|
+
|
|
11
|
+
export type { UserSessionState }
|
|
12
|
+
export type { WebAuthnOptions, WebAuthnUser }
|
|
13
|
+
|
|
14
|
+
/** The server's values: an html page does not re-render on the client, so `fetch()` and `clear()` reload; `ready` is false when the server did not read the session. */
|
|
15
|
+
export interface UserSession extends UserSessionState {
|
|
16
|
+
fetch(): Promise<void>
|
|
17
|
+
clear(): Promise<void>
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function useUserSession(): UserSession {
|
|
21
|
+
const store = authStore(useApp())
|
|
22
|
+
return {
|
|
23
|
+
...store.get(),
|
|
24
|
+
fetch: async () => location.reload(),
|
|
25
|
+
clear: async () => {
|
|
26
|
+
await store.clear()
|
|
27
|
+
location.reload()
|
|
28
|
+
},
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface WebAuthn extends WebAuthnCalls {
|
|
33
|
+
isSupported: boolean
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** `isSupported` is read when called: an html page has no client render, so call it from a handler or after `app:mounted`. */
|
|
37
|
+
export function useWebAuthn(options: WebAuthnOptions): WebAuthn {
|
|
38
|
+
return { isSupported: webAuthnSupported(), ...webAuthn(options) }
|
|
39
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { createElement, Fragment, type ComponentChildren, type VNode } from "preact"
|
|
2
|
+
import { useEffect, useMemo, useState } from "preact/hooks"
|
|
3
|
+
import { useApp } from "@hamur/adapter-preact"
|
|
4
|
+
import { authStore, type AuthStateSlot, type UserSessionState } from "../runtime/store.ts"
|
|
5
|
+
import {
|
|
6
|
+
webAuthn,
|
|
7
|
+
webAuthnSupported,
|
|
8
|
+
type WebAuthnCalls,
|
|
9
|
+
type WebAuthnOptions,
|
|
10
|
+
type WebAuthnUser,
|
|
11
|
+
} from "../client/webauthn.ts"
|
|
12
|
+
|
|
13
|
+
export type { AuthStateSlot, UserSessionState }
|
|
14
|
+
export type { WebAuthnOptions, WebAuthnUser }
|
|
15
|
+
|
|
16
|
+
export interface UserSession extends UserSessionState {
|
|
17
|
+
fetch(): Promise<void>
|
|
18
|
+
clear(): Promise<void>
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface AuthStateProps {
|
|
22
|
+
placeholder?: ComponentChildren
|
|
23
|
+
children?: (state: AuthStateSlot) => ComponentChildren
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function useUserSession(): UserSession {
|
|
27
|
+
const store = authStore(useApp())
|
|
28
|
+
const [state, setState] = useState(store.get())
|
|
29
|
+
useEffect(() => {
|
|
30
|
+
setState(store.get())
|
|
31
|
+
return store.subscribe(() => setState(store.get()))
|
|
32
|
+
}, [store])
|
|
33
|
+
return useMemo(() => ({ ...state, fetch: store.fetch, clear: store.clear }), [state, store])
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function AuthState({ placeholder = null, children }: AuthStateProps): VNode {
|
|
37
|
+
const { ready, loggedIn, user, clear } = useUserSession()
|
|
38
|
+
return createElement(Fragment, null, ready ? children?.({ loggedIn, user, clear }) : placeholder)
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export interface WebAuthn extends WebAuthnCalls {
|
|
42
|
+
isSupported: boolean
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function useWebAuthn({ registerEndpoint, authenticateEndpoint }: WebAuthnOptions): WebAuthn {
|
|
46
|
+
const [isSupported, setSupported] = useState(false)
|
|
47
|
+
useEffect(() => setSupported(webAuthnSupported()), [])
|
|
48
|
+
const calls = useMemo(
|
|
49
|
+
() => webAuthn({ registerEndpoint, authenticateEndpoint }),
|
|
50
|
+
[registerEndpoint, authenticateEndpoint],
|
|
51
|
+
)
|
|
52
|
+
return useMemo(() => ({ isSupported, ...calls }), [isSupported, calls])
|
|
53
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { createElement, Fragment, useMemo, useSyncExternalStore, type ReactNode } from "react"
|
|
2
|
+
import { useApp } from "@hamur/adapter-react"
|
|
3
|
+
import { authStore, type AuthStateSlot, type UserSessionState } from "../runtime/store.ts"
|
|
4
|
+
import {
|
|
5
|
+
webAuthn,
|
|
6
|
+
webAuthnSupported,
|
|
7
|
+
type WebAuthnCalls,
|
|
8
|
+
type WebAuthnOptions,
|
|
9
|
+
type WebAuthnUser,
|
|
10
|
+
} from "../client/webauthn.ts"
|
|
11
|
+
|
|
12
|
+
export type { AuthStateSlot, UserSessionState }
|
|
13
|
+
export type { WebAuthnOptions, WebAuthnUser }
|
|
14
|
+
|
|
15
|
+
export interface UserSession extends UserSessionState {
|
|
16
|
+
fetch(): Promise<void>
|
|
17
|
+
clear(): Promise<void>
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface AuthStateProps {
|
|
21
|
+
placeholder?: ReactNode
|
|
22
|
+
children?: (state: AuthStateSlot) => ReactNode
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function useUserSession(): UserSession {
|
|
26
|
+
const store = authStore(useApp())
|
|
27
|
+
const state = useSyncExternalStore(store.subscribe, store.get, store.get)
|
|
28
|
+
return useMemo(() => ({ ...state, fetch: store.fetch, clear: store.clear }), [state, store])
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function AuthState({ placeholder = null, children }: AuthStateProps): ReactNode {
|
|
32
|
+
const { ready, loggedIn, user, clear } = useUserSession()
|
|
33
|
+
return createElement(Fragment, null, ready ? children?.({ loggedIn, user, clear }) : placeholder)
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const never = () => () => {}
|
|
37
|
+
|
|
38
|
+
export interface WebAuthn extends WebAuthnCalls {
|
|
39
|
+
isSupported: boolean
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function useWebAuthn({ registerEndpoint, authenticateEndpoint }: WebAuthnOptions): WebAuthn {
|
|
43
|
+
const isSupported = useSyncExternalStore(never, webAuthnSupported, () => false)
|
|
44
|
+
const calls = useMemo(
|
|
45
|
+
() => webAuthn({ registerEndpoint, authenticateEndpoint }),
|
|
46
|
+
[registerEndpoint, authenticateEndpoint],
|
|
47
|
+
)
|
|
48
|
+
return useMemo(() => ({ isSupported, ...calls }), [isSupported, calls])
|
|
49
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { createMemo, createSignal, getOwner, onCleanup, onSettled, type Accessor } from "solid-js"
|
|
2
|
+
import type { JSX } from "@solidjs/web"
|
|
3
|
+
import { useApp } from "@hamur/adapter-solid"
|
|
4
|
+
import type { PublicUserSession, User } from "../index.ts"
|
|
5
|
+
import { authStore, type AuthStateSlot } from "../runtime/store.ts"
|
|
6
|
+
import {
|
|
7
|
+
webAuthn,
|
|
8
|
+
webAuthnSupported,
|
|
9
|
+
type WebAuthnCalls,
|
|
10
|
+
type WebAuthnOptions,
|
|
11
|
+
type WebAuthnUser,
|
|
12
|
+
} from "../client/webauthn.ts"
|
|
13
|
+
|
|
14
|
+
export type { AuthStateSlot }
|
|
15
|
+
export type { WebAuthnOptions, WebAuthnUser }
|
|
16
|
+
|
|
17
|
+
export interface UserSession {
|
|
18
|
+
ready: Accessor<boolean>
|
|
19
|
+
loggedIn: Accessor<boolean>
|
|
20
|
+
user: Accessor<User | null>
|
|
21
|
+
session: Accessor<PublicUserSession | null>
|
|
22
|
+
fetch(): Promise<void>
|
|
23
|
+
clear(): Promise<void>
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface AuthStateProps {
|
|
27
|
+
placeholder?: JSX.Element
|
|
28
|
+
children?: (state: AuthStateSlot) => JSX.Element
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function useUserSession(): UserSession {
|
|
32
|
+
const store = authStore(useApp())
|
|
33
|
+
const [state, setState] = createSignal(store.get())
|
|
34
|
+
if (getOwner()) onCleanup(store.subscribe(() => setState(store.get())))
|
|
35
|
+
return {
|
|
36
|
+
ready: () => state().ready,
|
|
37
|
+
loggedIn: () => state().loggedIn,
|
|
38
|
+
user: () => state().user,
|
|
39
|
+
session: () => state().session,
|
|
40
|
+
fetch: store.fetch,
|
|
41
|
+
clear: store.clear,
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function AuthState(props: AuthStateProps): JSX.Element {
|
|
46
|
+
const { ready, loggedIn, user, clear } = useUserSession()
|
|
47
|
+
return createMemo(() =>
|
|
48
|
+
ready() ? props.children?.({ loggedIn: loggedIn(), user: user(), clear }) : (props.placeholder ?? null),
|
|
49
|
+
) as unknown as JSX.Element
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export interface WebAuthn extends WebAuthnCalls {
|
|
53
|
+
isSupported: Accessor<boolean>
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export function useWebAuthn(options: WebAuthnOptions): WebAuthn {
|
|
57
|
+
const [isSupported, setSupported] = createSignal(false)
|
|
58
|
+
if (getOwner())
|
|
59
|
+
onSettled(() => {
|
|
60
|
+
setSupported(webAuthnSupported())
|
|
61
|
+
})
|
|
62
|
+
else setSupported(webAuthnSupported())
|
|
63
|
+
return { isSupported, ...webAuthn(options) }
|
|
64
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { useApp } from "@hamur/adapter-svelte"
|
|
2
|
+
import { authStore, type UserSessionState } from "../runtime/store.ts"
|
|
3
|
+
|
|
4
|
+
/** A store in Svelte's contract (`$session.user`), with the two actions beside `subscribe`. */
|
|
5
|
+
export interface UserSession {
|
|
6
|
+
subscribe(run: (state: UserSessionState) => void): () => void
|
|
7
|
+
fetch(): Promise<void>
|
|
8
|
+
clear(): Promise<void>
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function useUserSession(): UserSession {
|
|
12
|
+
const store = authStore(useApp())
|
|
13
|
+
return {
|
|
14
|
+
subscribe(run) {
|
|
15
|
+
run(store.get())
|
|
16
|
+
return store.subscribe(() => run(store.get()))
|
|
17
|
+
},
|
|
18
|
+
fetch: store.fetch,
|
|
19
|
+
clear: store.clear,
|
|
20
|
+
}
|
|
21
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { webAuthn, webAuthnSupported, type WebAuthnCalls, type WebAuthnOptions } from "../client/webauthn.ts"
|
|
2
|
+
|
|
3
|
+
/** `isSupported` is a store in Svelte's contract (`$isSupported`): `false` first, so the server render and the hydration agree, then the browser's answer. */
|
|
4
|
+
export interface WebAuthn extends WebAuthnCalls {
|
|
5
|
+
isSupported: { subscribe(run: (supported: boolean) => void): () => void }
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export function useWebAuthn(options: WebAuthnOptions): WebAuthn {
|
|
9
|
+
return {
|
|
10
|
+
isSupported: {
|
|
11
|
+
subscribe(run) {
|
|
12
|
+
let live = true
|
|
13
|
+
run(false)
|
|
14
|
+
queueMicrotask(() => live && run(webAuthnSupported()))
|
|
15
|
+
return () => void (live = false)
|
|
16
|
+
},
|
|
17
|
+
},
|
|
18
|
+
...webAuthn(options),
|
|
19
|
+
}
|
|
20
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { default as AuthState } from "./AuthState.svelte"
|
|
2
|
+
export { useUserSession, type UserSession } from "./svelte-session.ts"
|
|
3
|
+
export { useWebAuthn, type WebAuthn } from "./svelte-webauthn.ts"
|
|
4
|
+
export type { AuthStateSlot, UserSessionState } from "../runtime/store.ts"
|
|
5
|
+
export type { WebAuthnOptions, WebAuthnUser } from "../client/webauthn.ts"
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import {
|
|
2
|
+
computed,
|
|
3
|
+
defineComponent,
|
|
4
|
+
getCurrentInstance,
|
|
5
|
+
getCurrentScope,
|
|
6
|
+
onMounted,
|
|
7
|
+
onScopeDispose,
|
|
8
|
+
shallowRef,
|
|
9
|
+
type Ref,
|
|
10
|
+
type ComputedRef,
|
|
11
|
+
type SlotsType,
|
|
12
|
+
type VNode,
|
|
13
|
+
} from "vue"
|
|
14
|
+
import { useApp } from "@hamur/adapter-vue"
|
|
15
|
+
import type { PublicUserSession, User } from "../index.ts"
|
|
16
|
+
import { authStore, type AuthStateSlot } from "../runtime/store.ts"
|
|
17
|
+
import {
|
|
18
|
+
webAuthn,
|
|
19
|
+
webAuthnSupported,
|
|
20
|
+
type WebAuthnCalls,
|
|
21
|
+
type WebAuthnOptions,
|
|
22
|
+
type WebAuthnUser,
|
|
23
|
+
} from "../client/webauthn.ts"
|
|
24
|
+
|
|
25
|
+
export type { AuthStateSlot }
|
|
26
|
+
export type { WebAuthnOptions, WebAuthnUser }
|
|
27
|
+
|
|
28
|
+
export interface UserSession {
|
|
29
|
+
ready: ComputedRef<boolean>
|
|
30
|
+
loggedIn: ComputedRef<boolean>
|
|
31
|
+
user: ComputedRef<User | null>
|
|
32
|
+
session: ComputedRef<PublicUserSession | null>
|
|
33
|
+
fetch(): Promise<void>
|
|
34
|
+
clear(): Promise<void>
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function useUserSession(): UserSession {
|
|
38
|
+
const store = authStore(useApp())
|
|
39
|
+
const state = shallowRef(store.get())
|
|
40
|
+
if (getCurrentScope()) onScopeDispose(store.subscribe(() => (state.value = store.get())))
|
|
41
|
+
return {
|
|
42
|
+
ready: computed(() => state.value.ready),
|
|
43
|
+
loggedIn: computed(() => state.value.loggedIn),
|
|
44
|
+
user: computed(() => state.value.user),
|
|
45
|
+
session: computed(() => state.value.session),
|
|
46
|
+
fetch: store.fetch,
|
|
47
|
+
clear: store.clear,
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export const AuthState = defineComponent({
|
|
52
|
+
name: "AuthState",
|
|
53
|
+
slots: Object as SlotsType<{ default?: (state: AuthStateSlot) => VNode[]; placeholder?: () => VNode[] }>,
|
|
54
|
+
setup(_, { slots }) {
|
|
55
|
+
const { ready, loggedIn, user, clear } = useUserSession()
|
|
56
|
+
return () =>
|
|
57
|
+
ready.value ? slots.default?.({ loggedIn: loggedIn.value, user: user.value, clear }) : slots.placeholder?.()
|
|
58
|
+
},
|
|
59
|
+
})
|
|
60
|
+
|
|
61
|
+
export interface WebAuthn extends WebAuthnCalls {
|
|
62
|
+
isSupported: Readonly<Ref<boolean>>
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export function useWebAuthn(options: WebAuthnOptions): WebAuthn {
|
|
66
|
+
const isSupported = shallowRef(false)
|
|
67
|
+
if (getCurrentInstance()) onMounted(() => (isSupported.value = webAuthnSupported()))
|
|
68
|
+
else isSupported.value = webAuthnSupported()
|
|
69
|
+
return { isSupported, ...webAuthn(options) }
|
|
70
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { resolve } from "node:path"
|
|
2
|
+
import { DiagnosticError, addTemplate, hasModule, type HamurContext } from "hamur/kit"
|
|
3
|
+
import type { ResolvedAuthOptions } from "../types.ts"
|
|
4
|
+
import { moduleEntry } from "./module-entry.ts"
|
|
5
|
+
|
|
6
|
+
const TABLE = {
|
|
7
|
+
table: "access_tokens",
|
|
8
|
+
columns: ["id", "user_id", "name", "token_hash", "scopes", "expires_at", "last_used_at", "created_at"],
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
type TableProblem = (hamur: HamurContext, options: typeof TABLE) => Promise<string | undefined>
|
|
12
|
+
|
|
13
|
+
/** The database and the user file the access tokens read, and the build check of their table. */
|
|
14
|
+
export function setupAccessTokens({ accessTokens }: ResolvedAuthOptions, hamur: HamurContext): void {
|
|
15
|
+
let contents = "export const accessTokens = undefined;\n"
|
|
16
|
+
addTemplate({ filename: "auth/access-tokens", getContents: () => contents })
|
|
17
|
+
if (!accessTokens) return
|
|
18
|
+
let cli: string
|
|
19
|
+
hamur.hooks.hook("modules:done", () => {
|
|
20
|
+
if (!hasModule("@hamur/db"))
|
|
21
|
+
throw new DiagnosticError("HAMUR_E5108", "@hamur/auth: auth.accessTokens needs @hamur/db in modules")
|
|
22
|
+
cli = moduleEntry(hamur, "db", "@hamur/db/cli")
|
|
23
|
+
const { rootDir } = hamur.options
|
|
24
|
+
let user: string
|
|
25
|
+
try {
|
|
26
|
+
user = Bun.resolveSync(resolve(rootDir, accessTokens.user), rootDir)
|
|
27
|
+
} catch {
|
|
28
|
+
throw new DiagnosticError(
|
|
29
|
+
"HAMUR_E5101",
|
|
30
|
+
`Cannot find auth.accessTokens.user ${JSON.stringify(accessTokens.user)}`,
|
|
31
|
+
)
|
|
32
|
+
}
|
|
33
|
+
contents = `export const accessTokens = {
|
|
34
|
+
loadDb: () => import(${JSON.stringify(moduleEntry(hamur, "db", "@hamur/db/server"))}),
|
|
35
|
+
loadUser: () => import(${JSON.stringify(user)}),
|
|
36
|
+
};
|
|
37
|
+
`
|
|
38
|
+
})
|
|
39
|
+
hamur.hooks.hook("build:before", async () => {
|
|
40
|
+
const { tableProblem } = (await import(cli)) as { tableProblem: TableProblem }
|
|
41
|
+
const problem = await tableProblem(hamur, TABLE)
|
|
42
|
+
if (problem) throw new DiagnosticError("HAMUR_E5109", `@hamur/auth: ${problem}`)
|
|
43
|
+
})
|
|
44
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { addAppPlugin, addTemplate, createResolver, type HamurContext } from "hamur/kit"
|
|
2
|
+
import type { ResolvedAuthOptions } from "../types.ts"
|
|
3
|
+
|
|
4
|
+
/** The render payload of the session and the client plugin that fetches it after hydration. */
|
|
5
|
+
export function setupClient(options: ResolvedAuthOptions, hamur: HamurContext): void {
|
|
6
|
+
const { loadStrategy } = options
|
|
7
|
+
const fetchAfterMount = loadStrategy !== "none" && hamur.options.framework !== "html"
|
|
8
|
+
addTemplate({
|
|
9
|
+
filename: "auth/client",
|
|
10
|
+
getContents: () =>
|
|
11
|
+
`export const loadStrategy = ${JSON.stringify(loadStrategy)};\nexport const fetchAfterMount = ${fetchAfterMount};\n`,
|
|
12
|
+
})
|
|
13
|
+
const r = createResolver(import.meta.url)
|
|
14
|
+
addAppPlugin({ src: r.resolve("../runtime/payload.ts"), mode: "server" })
|
|
15
|
+
addAppPlugin({ src: r.resolve("../runtime/plugin.ts"), mode: "client" })
|
|
16
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { diagnostic, useLogger, writeAppKey } from "hamur/kit"
|
|
2
|
+
|
|
3
|
+
/** True when git ignores `.env` in `rootDir`, or when `rootDir` is not in a git work tree (exit 128) or git cannot run. */
|
|
4
|
+
function ignoredByGit(rootDir: string): boolean {
|
|
5
|
+
try {
|
|
6
|
+
return (
|
|
7
|
+
Bun.spawnSync(["git", "check-ignore", "-q", ".env"], { cwd: rootDir, stdout: "ignore", stderr: "ignore" })
|
|
8
|
+
.exitCode !== 1
|
|
9
|
+
)
|
|
10
|
+
} catch {
|
|
11
|
+
return true
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Under `hamur dev`, with no `HAMUR_KEY` in the environment or set to a value in `<rootDir>/.env`, writes one to
|
|
17
|
+
* `.env` (in place of an empty `HAMUR_KEY=` line, else appended) and sets it in this process, so the dev handler and its SSR
|
|
18
|
+
* workers (which inherit the environment) use it from this start on.
|
|
19
|
+
*/
|
|
20
|
+
export function ensureDevKey(rootDir: string): void {
|
|
21
|
+
if (process.env.HAMUR_KEY) return
|
|
22
|
+
const written = writeAppKey(rootDir)
|
|
23
|
+
if (written.status === "present") return
|
|
24
|
+
process.env.HAMUR_KEY = written.key
|
|
25
|
+
const log = useLogger("auth")
|
|
26
|
+
log.info("Wrote HAMUR_KEY to .env")
|
|
27
|
+
if (!ignoredByGit(rootDir))
|
|
28
|
+
log.warn(diagnostic("HAMUR_W5105", ".env holds HAMUR_KEY and is not ignored by git; add .env to .gitignore"))
|
|
29
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { DiagnosticError, addTemplate, hasModule, type HamurContext } from "hamur/kit"
|
|
2
|
+
import type { ResolvedAuthOptions } from "../types.ts"
|
|
3
|
+
import { moduleEntry } from "./module-entry.ts"
|
|
4
|
+
import { serverImports } from "./single-use.ts"
|
|
5
|
+
|
|
6
|
+
/** What `sendAuthLink` reads, with `@hamur/mail` behind it when the app has the module, and the build check of its import. */
|
|
7
|
+
export function setupEmail(options: ResolvedAuthOptions, hamur: HamurContext): void {
|
|
8
|
+
let mail: string | undefined
|
|
9
|
+
hamur.hooks.hook("modules:done", () => {
|
|
10
|
+
if (!hasModule("@hamur/mail")) return
|
|
11
|
+
mail = moduleEntry(hamur, "mail", "@hamur/mail/server")
|
|
12
|
+
})
|
|
13
|
+
addTemplate({
|
|
14
|
+
filename: "auth/email",
|
|
15
|
+
getContents: (ctx) =>
|
|
16
|
+
[
|
|
17
|
+
`export const email = ${JSON.stringify(options.email)};`,
|
|
18
|
+
`export const siteUrl = ${JSON.stringify(ctx.options.site.url ?? null)};`,
|
|
19
|
+
...(mail === undefined
|
|
20
|
+
? ["export const templates = [];", "export const loadMail = undefined;"]
|
|
21
|
+
: [
|
|
22
|
+
`export { templates } from "#hamur/mail/config";`,
|
|
23
|
+
`export const loadMail = () => import(${JSON.stringify(mail)});`,
|
|
24
|
+
]),
|
|
25
|
+
"",
|
|
26
|
+
].join("\n"),
|
|
27
|
+
})
|
|
28
|
+
hamur.hooks.hook("build:before", () => {
|
|
29
|
+
if (mail === undefined && serverImports(hamur)("sendAuthLink"))
|
|
30
|
+
throw new DiagnosticError("HAMUR_E5107", "@hamur/auth: sendAuthLink needs @hamur/mail in modules")
|
|
31
|
+
})
|
|
32
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { addTemplate, type HamurContext } from "hamur/kit"
|
|
2
|
+
import type { ResolvedAuthOptions } from "../types.ts"
|
|
3
|
+
|
|
4
|
+
/** The redirect targets `requireUser` and the `auth` and `guest` route middleware (`@hamur/auth/route-middleware`) read. */
|
|
5
|
+
export function setupGuards(options: ResolvedAuthOptions, hamur: HamurContext): void {
|
|
6
|
+
const { baseURL } = hamur.options.app
|
|
7
|
+
addTemplate({
|
|
8
|
+
filename: "auth/guards",
|
|
9
|
+
getContents: () =>
|
|
10
|
+
`export const redirect = ${JSON.stringify(options.redirect)};\nexport const baseURL = ${JSON.stringify(baseURL)};\n`,
|
|
11
|
+
})
|
|
12
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { dirname } from "node:path"
|
|
2
|
+
import { useInstalledModules, type HamurContext } from "hamur/kit"
|
|
3
|
+
|
|
4
|
+
/** The file of `specifier`, an entry of the installed module `name`. A layer may install it where the app's root does not reach, so it is resolved from the module's own file. */
|
|
5
|
+
export function moduleEntry(hamur: HamurContext, name: string, specifier: string): string {
|
|
6
|
+
const file = useInstalledModules().find((module) => module.name === name)?.file
|
|
7
|
+
return Bun.resolveSync(specifier, file === undefined ? hamur.options.rootDir : dirname(file))
|
|
8
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { addRouteRules, addTemplate, createResolver, type HamurContext } from "hamur/kit"
|
|
2
|
+
import { APPLE_ORIGIN, ATPROTO_METADATA_ROUTE } from "../server/oauth/providers/catalog.ts"
|
|
3
|
+
import type { ResolvedAuthOptions } from "../types.ts"
|
|
4
|
+
|
|
5
|
+
const SERVER = "@hamur/auth/server"
|
|
6
|
+
const DNS_PRESETS = ["bun", "node", "vercel", "netlify"]
|
|
7
|
+
const DNS_MODULE = createResolver(import.meta.url).resolve("../server/oauth/atproto-dns.ts")
|
|
8
|
+
|
|
9
|
+
type ServerRoutes = HamurContext["serverRoutes"]
|
|
10
|
+
|
|
11
|
+
/** The `oauthAtproto` routes without a parameter: a parameter's URL cannot be a listed `redirect_uri`. */
|
|
12
|
+
const atprotoRoutes = (routes: ServerRoutes): ServerRoutes =>
|
|
13
|
+
routes.filter(
|
|
14
|
+
({ path, fn }) =>
|
|
15
|
+
fn.handlerImport?.from === SERVER && fn.handlerImport.name === "oauthAtproto" && !/[:*]/.test(path),
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
export function keepAtprotoMetadata(routes: ServerRoutes): void {
|
|
19
|
+
if (routes.some(({ fn }) => fn.handlerImport?.from === SERVER && fn.handlerImport.name === "oauthAtproto")) return
|
|
20
|
+
for (let at = routes.length - 1; at >= 0; at--) {
|
|
21
|
+
const { path, fn } = routes[at]!
|
|
22
|
+
if (path === ATPROTO_METADATA_ROUTE && fn.export === "atprotoClientMetadata") routes.splice(at, 1)
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// Under hamur dev the server runs on Bun whatever the preset.
|
|
27
|
+
export function atprotoModule(hamur: HamurContext): string {
|
|
28
|
+
const dns = hamur.options.dev || DNS_PRESETS.includes(hamur.options.preset)
|
|
29
|
+
const routes = atprotoRoutes(hamur.serverRoutes).map(
|
|
30
|
+
({ path, fn }) =>
|
|
31
|
+
` { path: ${JSON.stringify(path)}, load: () => import(${JSON.stringify(fn.file)}).then((module) => module[${JSON.stringify(fn.export)}]) },`,
|
|
32
|
+
)
|
|
33
|
+
return [
|
|
34
|
+
dns ? `export { hostFetch } from ${JSON.stringify(DNS_MODULE)};` : "export const hostFetch = undefined;",
|
|
35
|
+
routes.length ? `export const routes = [\n${routes.join("\n")}\n];` : "export const routes = [];",
|
|
36
|
+
"",
|
|
37
|
+
].join("\n")
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** `auth.oauth` for the presets, Apple's origin on each `oauthApple` route without a parameter, and the AT Protocol metadata route and checks. */
|
|
41
|
+
export function setupOAuth(options: ResolvedAuthOptions, hamur: HamurContext): void {
|
|
42
|
+
addTemplate({ filename: "auth/oauth", getContents: () => `export const oauth = ${JSON.stringify(options.oauth)};\n` })
|
|
43
|
+
addTemplate({ filename: "auth/atproto", getContents: atprotoModule })
|
|
44
|
+
hamur.hooks.hook("server:extend", (routes) => {
|
|
45
|
+
keepAtprotoMetadata(routes)
|
|
46
|
+
for (const { path, fn } of routes) {
|
|
47
|
+
const helper = fn.handlerImport
|
|
48
|
+
if (helper?.from !== SERVER || helper.name !== "oauthApple") continue
|
|
49
|
+
// A parameter's rule key would be `*`, which would let Apple post to every sibling route.
|
|
50
|
+
if (/[:*]/.test(path)) continue
|
|
51
|
+
addRouteRules({ [path]: { originCheck: { allow: [APPLE_ORIGIN] } } })
|
|
52
|
+
}
|
|
53
|
+
})
|
|
54
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import {
|
|
2
|
+
DiagnosticError,
|
|
3
|
+
addServer,
|
|
4
|
+
addTemplate,
|
|
5
|
+
addTypeTemplate,
|
|
6
|
+
createResolver,
|
|
7
|
+
diagnostic,
|
|
8
|
+
hasStorageMount,
|
|
9
|
+
useLogger,
|
|
10
|
+
type HamurContext,
|
|
11
|
+
} from "hamur/kit"
|
|
12
|
+
import type { ResolvedAuthOptions } from "../types.ts"
|
|
13
|
+
import { ensureDevKey } from "./dev-key.ts"
|
|
14
|
+
|
|
15
|
+
const SERVER = "@hamur/auth/server"
|
|
16
|
+
|
|
17
|
+
/** The message for a storage mount the build does not resolve on its preset. */
|
|
18
|
+
export function unmountedStorage(name: string, preset: string): string {
|
|
19
|
+
const hint =
|
|
20
|
+
name === "session" && preset === "cloudflare"
|
|
21
|
+
? 'add a kv_namespaces entry with binding "SESSION" to the wrangler config, or set storage.session'
|
|
22
|
+
: `set storage.${name}`
|
|
23
|
+
return `auth.session.storage ${JSON.stringify(name)} has no mount on the ${preset} preset; ${hint}`
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** The session endpoint door, its hooks, the app-key check, typegen and the build checks. */
|
|
27
|
+
export function setupSession(options: ResolvedAuthOptions, hamur: HamurContext): void {
|
|
28
|
+
const { session } = options
|
|
29
|
+
const protection = hamur.options.build.importProtection
|
|
30
|
+
const client = (protection.client ??= {})
|
|
31
|
+
;(client.deny ??= []).push(SERVER)
|
|
32
|
+
|
|
33
|
+
if (typeof session.password === "string") {
|
|
34
|
+
useLogger("auth").warn(
|
|
35
|
+
diagnostic(
|
|
36
|
+
"HAMUR_W5104",
|
|
37
|
+
'auth.session.password is a literal in the config file, which ends up in the repository; use env("NAME")',
|
|
38
|
+
),
|
|
39
|
+
)
|
|
40
|
+
}
|
|
41
|
+
if (hamur.options.dev) ensureDevKey(hamur.options.rootDir)
|
|
42
|
+
|
|
43
|
+
addTemplate({ filename: "auth/options", getContents: () => `export const session = ${JSON.stringify(session)};\n` })
|
|
44
|
+
addTypeTemplate({ filename: "auth.d.ts", getContents: () => `import type {} from "@hamur/auth";\nexport {};\n` })
|
|
45
|
+
addServer(createResolver(import.meta.url).resolve("../server/door.ts"))
|
|
46
|
+
|
|
47
|
+
const { storage } = session
|
|
48
|
+
if (storage !== undefined) {
|
|
49
|
+
hamur.hooks.hook("build:before", () => {
|
|
50
|
+
if (!hasStorageMount(storage))
|
|
51
|
+
throw new DiagnosticError("HAMUR_E5102", unmountedStorage(storage, hamur.options.preset))
|
|
52
|
+
})
|
|
53
|
+
}
|
|
54
|
+
}
|