@kayjs/auth 0.0.0-stage → 1.104.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 +21 -0
- package/README.md +78 -2
- package/llms/admin-and-organizations.md +70 -0
- package/llms/admin-pages.md +30 -0
- package/llms/grouped-methods.md +106 -0
- package/llms/hardening-and-testing.md +108 -0
- package/llms/oauth-passkeys-two-factor.md +111 -0
- package/llms/oauth-providers.md +28 -0
- package/llms/oauth-server-and-tokens.md +76 -0
- package/llms/organization-pages.md +20 -0
- package/llms/passwords-and-mails.md +129 -0
- package/llms/sessions-and-guards.md +51 -0
- package/llms/setup.md +122 -0
- package/llms/signed-in-components.md +59 -0
- package/llms.txt +28 -0
- package/package.json +85 -4
- package/src/access.ts +31 -0
- package/src/account.ts +198 -0
- package/src/add.ts +70 -0
- package/src/address.ts +51 -0
- package/src/admin.ts +361 -0
- package/src/auditWording.ts +153 -0
- package/src/auth.ts +219 -0
- package/src/authorize/clients.ts +160 -0
- package/src/authorize/grants.ts +44 -0
- package/src/authorize/metadata.ts +72 -0
- package/src/authorize/register.ts +38 -0
- package/src/authorize/reply.ts +9 -0
- package/src/authorize/request.ts +253 -0
- package/src/authorize/resource.ts +34 -0
- package/src/authorize/revoke.ts +36 -0
- package/src/authorize/routes.ts +87 -0
- package/src/authorize/token.ts +213 -0
- package/src/authorize/uris.ts +76 -0
- package/src/bearer.ts +208 -0
- package/src/bearerHeader.ts +11 -0
- package/src/breached.ts +47 -0
- package/src/browser.ts +59 -0
- package/src/captcha.ts +196 -0
- package/src/cbor.ts +129 -0
- package/src/components/deletion-banner.kay +20 -0
- package/src/components/impersonation-banner.kay +21 -0
- package/src/components/signed-in.kay +8 -0
- package/src/components/signed-out.kay +6 -0
- package/src/components/user-menu.kay +21 -0
- package/src/context.ts +5 -0
- package/src/core.ts +308 -0
- package/src/createUserCommand.ts +217 -0
- package/src/decide.ts +45 -0
- package/src/deletion.ts +80 -0
- package/src/devTool.ts +104 -0
- package/src/device.ts +37 -0
- package/src/email.ts +594 -0
- package/src/erase.ts +237 -0
- package/src/events.ts +297 -0
- package/src/export.ts +101 -0
- package/src/failure.ts +63 -0
- package/src/forwardAuth.ts +55 -0
- package/src/forwardRoutes.ts +68 -0
- package/src/gate.ts +104 -0
- package/src/guard.ts +22 -0
- package/src/guards.ts +72 -0
- package/src/guesses.ts +99 -0
- package/src/index.ts +46 -0
- package/src/keys.ts +51 -0
- package/src/limits.ts +182 -0
- package/src/load.ts +18 -0
- package/src/locale.ts +15 -0
- package/src/log.ts +4 -0
- package/src/mail.ts +225 -0
- package/src/memoryStore.ts +599 -0
- package/src/messages/fr.ts +357 -0
- package/src/methods/account.ts +80 -0
- package/src/methods/admin.ts +63 -0
- package/src/methods/audit.ts +15 -0
- package/src/methods/email.ts +39 -0
- package/src/methods/magicLink.ts +32 -0
- package/src/methods/oauth.ts +35 -0
- package/src/methods/oauthServer.ts +31 -0
- package/src/methods/organizations.ts +96 -0
- package/src/methods/passkeys.ts +61 -0
- package/src/methods/password.ts +53 -0
- package/src/methods/sessions.ts +18 -0
- package/src/methods/tokens.ts +24 -0
- package/src/methods/twoFactor.ts +62 -0
- package/src/oauth/client.ts +324 -0
- package/src/oauth/flow.ts +350 -0
- package/src/oauth/index.ts +5 -0
- package/src/oauth/pkce.ts +20 -0
- package/src/oauth/providers.ts +490 -0
- package/src/oauth/refresher.ts +177 -0
- package/src/oauth/timeout.ts +2 -0
- package/src/oauth/types.ts +78 -0
- package/src/oauthTest.ts +112 -0
- package/src/organizations.ts +675 -0
- package/src/organizationsCore.ts +60 -0
- package/src/own.ts +313 -0
- package/src/pages/components/captcha.kay +16 -0
- package/src/pages/components/frame.kay +11 -0
- package/src/pages/components/message.kay +8 -0
- package/src/pages/components/panel.kay +12 -0
- package/src/pages/components/passkey-add.kay +48 -0
- package/src/pages/components/passkey-sign-in.kay +34 -0
- package/src/pages/components/passkey-two-factor.kay +33 -0
- package/src/pages/components/proof.kay +15 -0
- package/src/pages/components/provider-buttons.kay +25 -0
- package/src/pages/components/recovery-codes.kay +13 -0
- package/src/pages/components/separator.kay +4 -0
- package/src/pages/english.ts +82 -0
- package/src/pages/forms.ts +185 -0
- package/src/pages/index.ts +361 -0
- package/src/pages/organizationPage.ts +12 -0
- package/src/pages/routes/account/_guard.ts +15 -0
- package/src/pages/routes/account/_layout.kay +37 -0
- package/src/pages/routes/account/clients.kay +33 -0
- package/src/pages/routes/account/connections.kay +57 -0
- package/src/pages/routes/account/data.kay +106 -0
- package/src/pages/routes/account/index.kay +109 -0
- package/src/pages/routes/account/organizations/[org].kay +188 -0
- package/src/pages/routes/account/organizations/_guard.ts +8 -0
- package/src/pages/routes/account/organizations/index.kay +86 -0
- package/src/pages/routes/account/security.kay +164 -0
- package/src/pages/routes/account/sessions.kay +51 -0
- package/src/pages/routes/account/tokens.kay +84 -0
- package/src/pages/routes/admin/_layout.kay +43 -0
- package/src/pages/routes/admin/audit.kay +43 -0
- package/src/pages/routes/admin/index.kay +60 -0
- package/src/pages/routes/admin/users/[id].kay +181 -0
- package/src/pages/routes/confirm-email.kay +39 -0
- package/src/pages/routes/invitation.kay +104 -0
- package/src/pages/routes/magic-link.kay +42 -0
- package/src/pages/routes/oauth/authorize.kay +80 -0
- package/src/pages/routes/register.kay +83 -0
- package/src/pages/routes/reset-password.kay +73 -0
- package/src/pages/routes/sign-in.kay +128 -0
- package/src/pages/routes/two-factor.kay +61 -0
- package/src/pages/routes/verify-email.kay +83 -0
- package/src/passkeys.ts +373 -0
- package/src/password.ts +316 -0
- package/src/payloads.ts +22 -0
- package/src/pg.ts +705 -0
- package/src/profile.ts +43 -0
- package/src/proof.ts +110 -0
- package/src/prune.ts +123 -0
- package/src/rates.ts +82 -0
- package/src/request.ts +712 -0
- package/src/requiredTwoFactor.ts +71 -0
- package/src/requirement.ts +23 -0
- package/src/roles.ts +18 -0
- package/src/routes.ts +202 -0
- package/src/rules-check.ts +37 -0
- package/src/rules.ts +108 -0
- package/src/securityNotice.ts +45 -0
- package/src/sessions.ts +184 -0
- package/src/setup.ts +417 -0
- package/src/signInOptions.ts +9 -0
- package/src/state.ts +34 -0
- package/src/store.ts +410 -0
- package/src/storeHelpers.ts +23 -0
- package/src/test.ts +46 -0
- package/src/testCookies.ts +9 -0
- package/src/testHelpers.ts +58 -0
- package/src/testing.ts +113 -0
- package/src/text.ts +6 -0
- package/src/totp.ts +120 -0
- package/src/twoFactor.ts +200 -0
- package/src/types.ts +23 -0
- package/src/userData.ts +47 -0
- package/src/viewer-layout/_layout.kay +11 -0
- package/src/viewer.ts +116 -0
- package/src/wayIn.ts +40 -0
- package/src/webauthn.ts +375 -0
- package/src/wsAuth.ts +53 -0
- package/types/access.d.ts +12 -0
- package/types/access.d.ts.map +1 -0
- package/types/account.d.ts +32 -0
- package/types/account.d.ts.map +1 -0
- package/types/add.d.ts +29 -0
- package/types/add.d.ts.map +1 -0
- package/types/address.d.ts +2 -0
- package/types/address.d.ts.map +1 -0
- package/types/admin.d.ts +84 -0
- package/types/admin.d.ts.map +1 -0
- package/types/auditWording.d.ts +9 -0
- package/types/auditWording.d.ts.map +1 -0
- package/types/auth.d.ts +3475 -0
- package/types/auth.d.ts.map +1 -0
- package/types/authorize/clients.d.ts +15 -0
- package/types/authorize/clients.d.ts.map +1 -0
- package/types/authorize/grants.d.ts +15 -0
- package/types/authorize/grants.d.ts.map +1 -0
- package/types/authorize/metadata.d.ts +20 -0
- package/types/authorize/metadata.d.ts.map +1 -0
- package/types/authorize/register.d.ts +4 -0
- package/types/authorize/register.d.ts.map +1 -0
- package/types/authorize/reply.d.ts +6 -0
- package/types/authorize/reply.d.ts.map +1 -0
- package/types/authorize/request.d.ts +48 -0
- package/types/authorize/request.d.ts.map +1 -0
- package/types/authorize/resource.d.ts +4 -0
- package/types/authorize/resource.d.ts.map +1 -0
- package/types/authorize/revoke.d.ts +4 -0
- package/types/authorize/revoke.d.ts.map +1 -0
- package/types/authorize/routes.d.ts +5 -0
- package/types/authorize/routes.d.ts.map +1 -0
- package/types/authorize/token.d.ts +7 -0
- package/types/authorize/token.d.ts.map +1 -0
- package/types/authorize/uris.d.ts +5 -0
- package/types/authorize/uris.d.ts.map +1 -0
- package/types/bearer.d.ts +45 -0
- package/types/bearer.d.ts.map +1 -0
- package/types/bearerHeader.d.ts +3 -0
- package/types/bearerHeader.d.ts.map +1 -0
- package/types/breached.d.ts +5 -0
- package/types/breached.d.ts.map +1 -0
- package/types/browser.d.ts +11 -0
- package/types/browser.d.ts.map +1 -0
- package/types/captcha.d.ts +42 -0
- package/types/captcha.d.ts.map +1 -0
- package/types/cbor.d.ts +4 -0
- package/types/cbor.d.ts.map +1 -0
- package/types/context.d.ts +3 -0
- package/types/context.d.ts.map +1 -0
- package/types/core.d.ts +180 -0
- package/types/core.d.ts.map +1 -0
- package/types/createUserCommand.d.ts +11 -0
- package/types/createUserCommand.d.ts.map +1 -0
- package/types/decide.d.ts +12 -0
- package/types/decide.d.ts.map +1 -0
- package/types/deletion.d.ts +17 -0
- package/types/deletion.d.ts.map +1 -0
- package/types/devTool.d.ts +3 -0
- package/types/devTool.d.ts.map +1 -0
- package/types/device.d.ts +7 -0
- package/types/device.d.ts.map +1 -0
- package/types/email.d.ts +73 -0
- package/types/email.d.ts.map +1 -0
- package/types/erase.d.ts +31 -0
- package/types/erase.d.ts.map +1 -0
- package/types/events.d.ts +139 -0
- package/types/events.d.ts.map +1 -0
- package/types/export.d.ts +54 -0
- package/types/export.d.ts.map +1 -0
- package/types/failure.d.ts +19 -0
- package/types/failure.d.ts.map +1 -0
- package/types/forwardAuth.d.ts +12 -0
- package/types/forwardAuth.d.ts.map +1 -0
- package/types/forwardRoutes.d.ts +11 -0
- package/types/forwardRoutes.d.ts.map +1 -0
- package/types/gate.d.ts +31 -0
- package/types/gate.d.ts.map +1 -0
- package/types/guard.d.ts +10 -0
- package/types/guard.d.ts.map +1 -0
- package/types/guards.d.ts +37 -0
- package/types/guards.d.ts.map +1 -0
- package/types/guesses.d.ts +18 -0
- package/types/guesses.d.ts.map +1 -0
- package/types/index.d.ts +47 -0
- package/types/index.d.ts.map +1 -0
- package/types/keys.d.ts +11 -0
- package/types/keys.d.ts.map +1 -0
- package/types/limits.d.ts +133 -0
- package/types/limits.d.ts.map +1 -0
- package/types/load.d.ts +18 -0
- package/types/load.d.ts.map +1 -0
- package/types/locale.d.ts +4 -0
- package/types/locale.d.ts.map +1 -0
- package/types/log.d.ts +2 -0
- package/types/log.d.ts.map +1 -0
- package/types/mail.d.ts +51 -0
- package/types/mail.d.ts.map +1 -0
- package/types/memoryStore.d.ts +3 -0
- package/types/memoryStore.d.ts.map +1 -0
- package/types/messages/fr.d.ts +409 -0
- package/types/messages/fr.d.ts.map +1 -0
- package/types/methods/account.d.ts +24 -0
- package/types/methods/account.d.ts.map +1 -0
- package/types/methods/admin.d.ts +30 -0
- package/types/methods/admin.d.ts.map +1 -0
- package/types/methods/audit.d.ts +11 -0
- package/types/methods/audit.d.ts.map +1 -0
- package/types/methods/email.d.ts +14 -0
- package/types/methods/email.d.ts.map +1 -0
- package/types/methods/magicLink.d.ts +12 -0
- package/types/methods/magicLink.d.ts.map +1 -0
- package/types/methods/oauth.d.ts +16 -0
- package/types/methods/oauth.d.ts.map +1 -0
- package/types/methods/oauthServer.d.ts +13 -0
- package/types/methods/oauthServer.d.ts.map +1 -0
- package/types/methods/organizations.d.ts +30 -0
- package/types/methods/organizations.d.ts.map +1 -0
- package/types/methods/passkeys.d.ts +19 -0
- package/types/methods/passkeys.d.ts.map +1 -0
- package/types/methods/password.d.ts +17 -0
- package/types/methods/password.d.ts.map +1 -0
- package/types/methods/sessions.d.ts +9 -0
- package/types/methods/sessions.d.ts.map +1 -0
- package/types/methods/tokens.d.ts +11 -0
- package/types/methods/tokens.d.ts.map +1 -0
- package/types/methods/twoFactor.d.ts +20 -0
- package/types/methods/twoFactor.d.ts.map +1 -0
- package/types/oauth/client.d.ts +39 -0
- package/types/oauth/client.d.ts.map +1 -0
- package/types/oauth/flow.d.ts +38 -0
- package/types/oauth/flow.d.ts.map +1 -0
- package/types/oauth/index.d.ts +6 -0
- package/types/oauth/index.d.ts.map +1 -0
- package/types/oauth/pkce.d.ts +10 -0
- package/types/oauth/pkce.d.ts.map +1 -0
- package/types/oauth/providers.d.ts +100 -0
- package/types/oauth/providers.d.ts.map +1 -0
- package/types/oauth/refresher.d.ts +55 -0
- package/types/oauth/refresher.d.ts.map +1 -0
- package/types/oauth/timeout.d.ts +2 -0
- package/types/oauth/timeout.d.ts.map +1 -0
- package/types/oauth/types.d.ts +75 -0
- package/types/oauth/types.d.ts.map +1 -0
- package/types/oauthTest.d.ts +23 -0
- package/types/oauthTest.d.ts.map +1 -0
- package/types/organizations.d.ts +111 -0
- package/types/organizations.d.ts.map +1 -0
- package/types/organizationsCore.d.ts +29 -0
- package/types/organizationsCore.d.ts.map +1 -0
- package/types/own.d.ts +55 -0
- package/types/own.d.ts.map +1 -0
- package/types/pages/english.d.ts +80 -0
- package/types/pages/english.d.ts.map +1 -0
- package/types/pages/forms.d.ts +204 -0
- package/types/pages/forms.d.ts.map +1 -0
- package/types/pages/index.d.ts +166 -0
- package/types/pages/index.d.ts.map +1 -0
- package/types/pages/organizationPage.d.ts +7 -0
- package/types/pages/organizationPage.d.ts.map +1 -0
- package/types/pages/routes/account/_guard.d.ts +3 -0
- package/types/pages/routes/account/_guard.d.ts.map +1 -0
- package/types/pages/routes/account/organizations/_guard.d.ts +3 -0
- package/types/pages/routes/account/organizations/_guard.d.ts.map +1 -0
- package/types/passkeys.d.ts +61 -0
- package/types/passkeys.d.ts.map +1 -0
- package/types/password.d.ts +49 -0
- package/types/password.d.ts.map +1 -0
- package/types/payloads.d.ts +4 -0
- package/types/payloads.d.ts.map +1 -0
- package/types/pg.d.ts +303 -0
- package/types/pg.d.ts.map +1 -0
- package/types/profile.d.ts +11 -0
- package/types/profile.d.ts.map +1 -0
- package/types/proof.d.ts +33 -0
- package/types/proof.d.ts.map +1 -0
- package/types/prune.d.ts +5 -0
- package/types/prune.d.ts.map +1 -0
- package/types/rates.d.ts +15 -0
- package/types/rates.d.ts.map +1 -0
- package/types/request.d.ts +119 -0
- package/types/request.d.ts.map +1 -0
- package/types/requiredTwoFactor.d.ts +16 -0
- package/types/requiredTwoFactor.d.ts.map +1 -0
- package/types/requirement.d.ts +4 -0
- package/types/requirement.d.ts.map +1 -0
- package/types/roles.d.ts +9 -0
- package/types/roles.d.ts.map +1 -0
- package/types/routes.d.ts +31 -0
- package/types/routes.d.ts.map +1 -0
- package/types/rules-check.d.ts +3 -0
- package/types/rules-check.d.ts.map +1 -0
- package/types/rules.d.ts +38 -0
- package/types/rules.d.ts.map +1 -0
- package/types/securityNotice.d.ts +7 -0
- package/types/securityNotice.d.ts.map +1 -0
- package/types/sessions.d.ts +32 -0
- package/types/sessions.d.ts.map +1 -0
- package/types/setup.d.ts +18 -0
- package/types/setup.d.ts.map +1 -0
- package/types/signInOptions.d.ts +6 -0
- package/types/signInOptions.d.ts.map +1 -0
- package/types/state.d.ts +25 -0
- package/types/state.d.ts.map +1 -0
- package/types/store.d.ts +295 -0
- package/types/store.d.ts.map +1 -0
- package/types/storeHelpers.d.ts +4 -0
- package/types/storeHelpers.d.ts.map +1 -0
- package/types/test.d.ts +21 -0
- package/types/test.d.ts.map +1 -0
- package/types/testCookies.d.ts +3 -0
- package/types/testCookies.d.ts.map +1 -0
- package/types/testHelpers.d.ts +34 -0
- package/types/testHelpers.d.ts.map +1 -0
- package/types/testing.d.ts +12 -0
- package/types/testing.d.ts.map +1 -0
- package/types/text.d.ts +2 -0
- package/types/text.d.ts.map +1 -0
- package/types/totp.d.ts +12 -0
- package/types/totp.d.ts.map +1 -0
- package/types/twoFactor.d.ts +41 -0
- package/types/twoFactor.d.ts.map +1 -0
- package/types/types.d.ts +17 -0
- package/types/types.d.ts.map +1 -0
- package/types/userData.d.ts +9 -0
- package/types/userData.d.ts.map +1 -0
- package/types/viewer.d.ts +23 -0
- package/types/viewer.d.ts.map +1 -0
- package/types/wayIn.d.ts +12 -0
- package/types/wayIn.d.ts.map +1 -0
- package/types/webauthn.d.ts +111 -0
- package/types/webauthn.d.ts.map +1 -0
- package/types/wsAuth.d.ts +14 -0
- package/types/wsAuth.d.ts.map +1 -0
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/** Normalized OAuth credentials. `expiresAt` is a `Date`; `undefined` = non-expiring (e.g. GitHub classic tokens). */
|
|
2
|
+
export interface OAuthTokens {
|
|
3
|
+
accessToken: string
|
|
4
|
+
refreshToken?: string | undefined
|
|
5
|
+
expiresAt?: Date | undefined
|
|
6
|
+
scope?: string | undefined
|
|
7
|
+
tokenType?: string | undefined
|
|
8
|
+
/** The OpenID Connect ID token, checked before anything is read from it. */
|
|
9
|
+
idToken?: string | undefined
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/** The normalized provider identity both presets map onto. */
|
|
13
|
+
export interface OAuthUser {
|
|
14
|
+
providerId: string
|
|
15
|
+
username: string
|
|
16
|
+
displayName?: string | undefined
|
|
17
|
+
avatar?: string | null | undefined
|
|
18
|
+
/** The wide image atop the provider's profile, as a URL, for a provider that has one. */
|
|
19
|
+
banner?: string | null | undefined
|
|
20
|
+
email?: string | null | undefined
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** How an OpenID Connect provider's ID token is checked: signed by a key of `jwksUrl`, by `issuer`, for this client, unexpired, carrying the flow's nonce. */
|
|
24
|
+
export interface OpenIdCheck {
|
|
25
|
+
/** The expected `iss`, or a test of the claims for a provider whose issuer names the tenant, as Microsoft's does. */
|
|
26
|
+
issuer: string | string[] | ((claims: Record<string, unknown>) => boolean)
|
|
27
|
+
jwksUrl: string
|
|
28
|
+
/** The `alg` header values an ID token may carry, every asymmetric one (`RS*`, `PS*`, `ES*`, `EdDSA`) when left out; a token signed otherwise, with `none` or with a shared-secret `HS*` included, is refused. */
|
|
29
|
+
algorithms?: string[] | undefined
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** Where a provider is reached; an OpenID Connect one with `openId` reads its user off the ID token, and off `userUrl` too when it has one. */
|
|
33
|
+
export interface OAuthEndpoints {
|
|
34
|
+
authorizeUrl?: string | undefined
|
|
35
|
+
tokenUrl?: string | undefined
|
|
36
|
+
/** Where a token is revoked (RFC 7009), so unlinking or deleting a user ends the provider's grant; a provider without one leaves it to the user. */
|
|
37
|
+
revokeUrl?: string | undefined
|
|
38
|
+
userUrl?: string | undefined
|
|
39
|
+
openId?: OpenIdCheck | undefined
|
|
40
|
+
/** The URL the endpoints are discovered from, such as an OpenID Connect issuer, checked like an endpoint. */
|
|
41
|
+
discoveryBase?: string | undefined
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** A provider's OAuth endpoints plus how to parse its token/user responses. */
|
|
45
|
+
// eslint-disable-next-line ts/no-explicit-any -- an app's own provider reads its user answer untyped unless it names its type
|
|
46
|
+
export interface OAuthProvider<Raw = any, User = OAuthUser> extends OAuthEndpoints {
|
|
47
|
+
name: string
|
|
48
|
+
/** The provider's name as a sign-in button shows it, `GitHub`; `name` without it. */
|
|
49
|
+
label?: string
|
|
50
|
+
/** Endpoints read from the provider on first use, such as an OpenID Connect discovery document, over the ones set here. */
|
|
51
|
+
discover?: () => Promise<OAuthEndpoints>
|
|
52
|
+
scopes: string[]
|
|
53
|
+
/** Extra authorize URL parameters, such as Apple's `response_mode=form_post` or Google's `access_type=offline`. */
|
|
54
|
+
authorizeParams?: Record<string, string>
|
|
55
|
+
/** A client secret made per request, such as Apple's signed JWT, over the app's `clientSecret`. */
|
|
56
|
+
clientSecret?: (clientId: string) => Promise<string>
|
|
57
|
+
/** How the app's credentials reach the token and revocation endpoints: in the form body (`'body'`, the default), or as an HTTP Basic header (`'basic'`), which X asks of a confidential client. */
|
|
58
|
+
clientAuth?: 'body' | 'basic'
|
|
59
|
+
/** How scopes are joined in the authorize URL (default `' '`). */
|
|
60
|
+
scopeSeparator?: string
|
|
61
|
+
/** Forces token-response parsing; when unset the response `content-type` decides. */
|
|
62
|
+
tokenAccept?: 'json' | 'form'
|
|
63
|
+
/** Extra headers for the user-info request (e.g. GitHub's `User-Agent`/`Accept`). */
|
|
64
|
+
authHeaders?: (accessToken: string) => Record<string, string>
|
|
65
|
+
/** Completes the user endpoint's answer with what another request holds, such as GitHub's `/user/emails`; `scope` is what the token was granted when the provider said. A failure there is the provider's to absorb: the sign-in goes on with the answer as it is. */
|
|
66
|
+
completeUser?: (raw: Raw, token: { accessToken: string, scope: string | undefined }) => Promise<Raw>
|
|
67
|
+
// eslint-disable-next-line ts/no-explicit-any -- an app's own provider reads the token answer untyped
|
|
68
|
+
parseTokens: (raw: any) => OAuthTokens
|
|
69
|
+
/** `raw` is the user endpoint's answer or, for OpenID Connect, the ID token's claims with the user endpoint's over them; `callback` the parameters the provider came back with. */
|
|
70
|
+
normalizeUser: (raw: Raw, callback?: Record<string, string>) => User
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** The consumer's OAuth app credentials; no `clientSecret` for a public client, or a provider making its own. */
|
|
74
|
+
export interface OAuthClientConfig {
|
|
75
|
+
clientId: string
|
|
76
|
+
clientSecret?: string | undefined
|
|
77
|
+
redirectUri: string
|
|
78
|
+
}
|
package/src/oauthTest.ts
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import type { Elysia } from 'elysia'
|
|
2
|
+
import type { Core } from './core'
|
|
3
|
+
import type { OAuthTokens, OAuthUser } from './oauth/types'
|
|
4
|
+
import { Elysia as ElysiaApp } from 'elysia'
|
|
5
|
+
import { OAuthExchangeError } from './oauth/client'
|
|
6
|
+
|
|
7
|
+
// What a stubbed provider answers: the account a sign-in comes back with, or why it fails. `access_denied` is a refused consent, `invalid_grant` a code the provider rejects and `server_error` a provider that is down.
|
|
8
|
+
export type OAuthStub = { id: string, email?: string | undefined, username?: string | undefined, displayName?: string | undefined, avatar?: string | undefined } | { error: 'access_denied' | 'invalid_grant' | 'server_error' }
|
|
9
|
+
|
|
10
|
+
export interface OAuthTest {
|
|
11
|
+
// Makes the provider answer with this account, or this failure, for every sign-in until the returned function is called, with no request to the provider; a second stub of the provider replaces the first.
|
|
12
|
+
stub: (provider: string, outcome: OAuthStub) => Promise<() => void>
|
|
13
|
+
// Leaves for the stubbed provider and comes back, as the browser does, and answers the callback's response: a redirect to where the sign-in goes, or to the sign-in page with `?error=`. `redirect` is the page asked for.
|
|
14
|
+
signIn: (provider: string, options?: { redirect?: string | undefined }) => Promise<Response>
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
// A site with a `fetch`, the one `testSite()` returns, is driven through it; one with only its cookies, through the plugin.
|
|
18
|
+
export interface OAuthSite {
|
|
19
|
+
cookies: Bun.CookieMap
|
|
20
|
+
fetch?: ((path: string) => Promise<Response>) | undefined
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
type Entry = Core['oauth'] extends Map<string, infer Value> ? Value : never
|
|
24
|
+
|
|
25
|
+
const STUB_TOKENS: OAuthTokens = { accessToken: 'stub-access-token', refreshToken: 'stub-refresh-token', tokenType: 'Bearer' }
|
|
26
|
+
const FAILURE_STATUS = { access_denied: 400, invalid_grant: 400, server_error: 502 }
|
|
27
|
+
// The client each stubbed provider had, and what it was stubbed with.
|
|
28
|
+
const originals = new WeakMap<Entry, Entry['client']>()
|
|
29
|
+
const stubs = new WeakMap<Entry, OAuthStub>()
|
|
30
|
+
|
|
31
|
+
function userOf(account: Extract<OAuthStub, { id: string }>): OAuthUser {
|
|
32
|
+
const { id, email, username, displayName, avatar } = account
|
|
33
|
+
const at = email?.indexOf('@') ?? -1
|
|
34
|
+
|
|
35
|
+
return { providerId: id, username: username ?? (email !== undefined && at > 0 ? email.slice(0, at) : id), displayName, avatar: avatar ?? null, email: email ?? null }
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// A request to the plugin as the site's browser sends it: the cookies so far go along, and the ones the response sets are kept, redirects not followed. A cookie's `Path`, `Domain` and `Secure` are ignored, as the plugin's routes are the only ones asked and every one of its cookies is for them; a test reaching the whole app passes the site's own `fetch`.
|
|
39
|
+
async function throughPlugin(plugin: Elysia, cookies: Bun.CookieMap, path: string): Promise<Response> {
|
|
40
|
+
const cookie = [...cookies].map(([name, value]) => `${name}=${value}`).join('; ')
|
|
41
|
+
const response = await new ElysiaApp().use(plugin).handle(new Request(`http://localhost${path}`, { headers: cookie === '' ? {} : { cookie } }))
|
|
42
|
+
|
|
43
|
+
for (const line of response.headers.getSetCookie()) {
|
|
44
|
+
const parsed = Bun.Cookie.parse(line)
|
|
45
|
+
|
|
46
|
+
if (parsed.maxAge === 0 || (parsed.expires !== undefined && parsed.expires.getTime() <= Date.now()))
|
|
47
|
+
cookies.delete(parsed.name)
|
|
48
|
+
else
|
|
49
|
+
cookies.set(parsed.name, parsed.value)
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return response
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export function oauthTest(core: Core, plugin: Elysia, site: OAuthSite): OAuthTest {
|
|
56
|
+
const visit = site.fetch ?? (async (path: string) => throughPlugin(plugin, site.cookies, path))
|
|
57
|
+
|
|
58
|
+
const stubbed = (provider: string, outcome: OAuthStub): () => void => {
|
|
59
|
+
const entry = core.oauth.get(provider)
|
|
60
|
+
|
|
61
|
+
if (!entry)
|
|
62
|
+
throw new Error(`kayjs-auth: oauth.stub found no provider ${provider} configured, its names are ${[...core.oauth.keys()].join(', ') || 'none'}`)
|
|
63
|
+
|
|
64
|
+
if ('id' in outcome && outcome.id === '')
|
|
65
|
+
throw new Error('kayjs-auth: oauth.stub needs the account\'s id')
|
|
66
|
+
|
|
67
|
+
const real = originals.get(entry) ?? entry.client
|
|
68
|
+
const failure = 'error' in outcome ? new OAuthExchangeError(FAILURE_STATUS[outcome.error], `OAuth token request failed (${provider}): ${outcome.error}`) : undefined
|
|
69
|
+
const user = 'id' in outcome ? userOf(outcome) : undefined
|
|
70
|
+
|
|
71
|
+
originals.set(entry, real)
|
|
72
|
+
stubs.set(entry, outcome)
|
|
73
|
+
entry.client = {
|
|
74
|
+
...real,
|
|
75
|
+
// A provider whose endpoints are discovered would be asked for them first.
|
|
76
|
+
authorizeUrl: async options => entry.app.provider.discover ? `https://${provider}.stub.invalid/authorize?state=${options?.state ?? ''}` : real.authorizeUrl(options),
|
|
77
|
+
exchangeCode: async () => failure ? Promise.reject(failure) : STUB_TOKENS,
|
|
78
|
+
readUser: async () => user ?? Promise.reject(failure),
|
|
79
|
+
fetchUser: async () => user ?? Promise.reject(failure),
|
|
80
|
+
revokeToken: async () => Promise.resolve(false),
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
return () => {
|
|
84
|
+
entry.client = real
|
|
85
|
+
originals.delete(entry)
|
|
86
|
+
stubs.delete(entry)
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
return {
|
|
91
|
+
stub: async (provider, outcome) => Promise.resolve(stubbed(provider, outcome)),
|
|
92
|
+
|
|
93
|
+
async signIn(provider, options = {}) {
|
|
94
|
+
const entry = core.oauth.get(provider)
|
|
95
|
+
const outcome = entry && stubs.get(entry)
|
|
96
|
+
|
|
97
|
+
if (!outcome)
|
|
98
|
+
throw new Error(`kayjs-auth: oauth.signIn found no stub for ${provider}: call oauth.stub first`)
|
|
99
|
+
|
|
100
|
+
const start = await visit(`${core.prefix}/${provider}${options.redirect === undefined ? '' : `?redirect=${encodeURIComponent(options.redirect)}`}`)
|
|
101
|
+
const location = start.headers.get('location')
|
|
102
|
+
|
|
103
|
+
if (location === null)
|
|
104
|
+
throw new Error(`kayjs-auth: oauth.signIn found no redirect to ${provider}, the sign-in route answered ${start.status}`)
|
|
105
|
+
|
|
106
|
+
const state = encodeURIComponent(new URL(location, 'http://localhost').searchParams.get('state') ?? '')
|
|
107
|
+
const answer = 'error' in outcome && outcome.error === 'access_denied' ? 'error=access_denied' : 'code=stub-code'
|
|
108
|
+
|
|
109
|
+
return visit(`${core.prefix}/${provider}/callback?${answer}&state=${state}`)
|
|
110
|
+
},
|
|
111
|
+
}
|
|
112
|
+
}
|