nuxt-auth-utils 0.4.0 → 0.4.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/README.md +6 -4
- package/dist/module.json +1 -1
- package/dist/module.mjs +5 -0
- package/dist/runtime/app/plugins/session.client.d.ts +1 -1
- package/dist/runtime/app/plugins/session.server.d.ts +1 -1
- package/dist/runtime/server/lib/oauth/polar.d.ts +49 -0
- package/dist/runtime/server/lib/oauth/polar.js +67 -0
- package/dist/runtime/server/lib/webauthn/authenticate.js +10 -10
- package/dist/runtime/server/lib/webauthn/register.js +9 -9
- package/dist/runtime/types/oauth-config.d.ts +1 -1
- package/package.json +1 -1
- package/dist/runtime/server/lib/webauthn/utils.d.ts +0 -3
- package/dist/runtime/server/lib/webauthn/utils.js +0 -35
package/README.md
CHANGED
|
@@ -8,7 +8,8 @@
|
|
|
8
8
|
Add Authentication to Nuxt applications with secured & sealed cookies sessions.
|
|
9
9
|
|
|
10
10
|
- [Release Notes](/CHANGELOG.md)
|
|
11
|
-
- [Demo](https://github.com/atinux/
|
|
11
|
+
- [Demo with OAuth](https://github.com/atinux/atidone)
|
|
12
|
+
- [Demo with Passkeys](https://github.com/atinux/todo-passkeys)
|
|
12
13
|
<!-- - [🏀 Online playground](https://stackblitz.com/github/your-org/nuxt-auth-utils?file=playground%2Fapp.vue) -->
|
|
13
14
|
<!-- - [📖 Documentation](https://example.com) -->
|
|
14
15
|
|
|
@@ -16,7 +17,7 @@ Add Authentication to Nuxt applications with secured & sealed cookies sessions.
|
|
|
16
17
|
|
|
17
18
|
- [Hybrid Rendering](#hybrid-rendering) support (SSR / CSR / SWR / Prerendering)
|
|
18
19
|
- [20+ OAuth Providers](#supported-oauth-providers)
|
|
19
|
-
- [Password
|
|
20
|
+
- [Password Hashing](#password-hashing)
|
|
20
21
|
- [WebAuthn (passkey)](#webauthn-passkey)
|
|
21
22
|
- [`useUserSession()` Vue composable](#vue-composable)
|
|
22
23
|
- [Tree-shakable server utils](#server-utils)
|
|
@@ -125,7 +126,7 @@ await setUserSession(event, {
|
|
|
125
126
|
user: {
|
|
126
127
|
login: 'atinux'
|
|
127
128
|
},
|
|
128
|
-
// Private data accessible on server/ routes
|
|
129
|
+
// Private data accessible only on server/ routes
|
|
129
130
|
secure: {
|
|
130
131
|
apiToken: '1234567890'
|
|
131
132
|
},
|
|
@@ -217,6 +218,7 @@ It can also be set using environment variables:
|
|
|
217
218
|
- LinkedIn
|
|
218
219
|
- Microsoft
|
|
219
220
|
- PayPal
|
|
221
|
+
- Polar
|
|
220
222
|
- Spotify
|
|
221
223
|
- Steam
|
|
222
224
|
- TikTok
|
|
@@ -422,7 +424,7 @@ export default defineWebAuthnAuthenticateEventHandler({
|
|
|
422
424
|
```
|
|
423
425
|
|
|
424
426
|
> [!IMPORTANT]
|
|
425
|
-
>
|
|
427
|
+
> Webauthn uses challenges to prevent replay attacks. By default, this module does not make use if this feature. If you want to use challenges (**which is highly recommended**), the `storeChallenge` and `getChallenge` functions are provided. An attempt ID is created and sent with each autentication request. You can use this ID to store the challenge in a database or KV store as shown in the example below.
|
|
426
428
|
|
|
427
429
|
> ```ts
|
|
428
430
|
> export default defineWebAuthnAuthenticateEventHandler({
|
package/dist/module.json
CHANGED
package/dist/module.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
declare const _default:
|
|
1
|
+
declare const _default: import("nuxt/app").Plugin<Record<string, unknown>> & import("nuxt/app").ObjectPlugin<Record<string, unknown>>;
|
|
2
2
|
export default _default;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
declare const _default:
|
|
1
|
+
declare const _default: import("nuxt/app").Plugin<Record<string, unknown>> & import("nuxt/app").ObjectPlugin<Record<string, unknown>>;
|
|
2
2
|
export default _default;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import type { OAuthConfig } from '#auth-utils';
|
|
2
|
+
export interface OAuthPolarConfig {
|
|
3
|
+
/**
|
|
4
|
+
* Polar Client ID
|
|
5
|
+
* @default process.env.NUXT_OAUTH_POLAR_CLIENT_ID
|
|
6
|
+
*/
|
|
7
|
+
clientId?: string;
|
|
8
|
+
/**
|
|
9
|
+
* Polar OAuth Client Secret
|
|
10
|
+
* @default process.env.NUXT_OAUTH_POLAR_CLIENT_SECRET
|
|
11
|
+
*/
|
|
12
|
+
clientSecret?: string;
|
|
13
|
+
/**
|
|
14
|
+
* Polar OAuth Scope
|
|
15
|
+
* @default []
|
|
16
|
+
* @see https://api.polar.sh/.well-known/openid-configuration
|
|
17
|
+
* @example ['openid']
|
|
18
|
+
*/
|
|
19
|
+
scope?: string[];
|
|
20
|
+
/**
|
|
21
|
+
* Require email from user, adds the ['email'] scope if not present
|
|
22
|
+
* @default false
|
|
23
|
+
*/
|
|
24
|
+
emailRequired?: boolean;
|
|
25
|
+
/**
|
|
26
|
+
* Polar OAuth Authorization URL
|
|
27
|
+
* @see https://docs.polar.sh/api/authentication#start-the-authorization-flow
|
|
28
|
+
* @default 'https://polar.sh/oauth2/authorize'
|
|
29
|
+
*/
|
|
30
|
+
authorizationURL?: string;
|
|
31
|
+
/**
|
|
32
|
+
* Polar OAuth Token URL
|
|
33
|
+
* @see https://docs.polar.sh/api/authentication#exchange-the-authorization-code
|
|
34
|
+
* @default 'https://api.polar.sh/v1/oauth2/token'
|
|
35
|
+
*/
|
|
36
|
+
tokenURL?: string;
|
|
37
|
+
/**
|
|
38
|
+
* Extra authorization parameters to provide to the authorization URL
|
|
39
|
+
* @see https://docs.polar.sh/api/authentication#user-vs-organization-access-tokens
|
|
40
|
+
* @example { sub_type: 'organization' }
|
|
41
|
+
*/
|
|
42
|
+
authorizationParams?: Record<string, string>;
|
|
43
|
+
/**
|
|
44
|
+
* Redirect URL to to allow overriding for situations like prod failing to determine public hostname
|
|
45
|
+
* @default process.env.NUXT_OAUTH_POLAR_REDIRECT_URL or current URL
|
|
46
|
+
*/
|
|
47
|
+
redirectURL?: string;
|
|
48
|
+
}
|
|
49
|
+
export declare function defineOAuthPolarEventHandler({ config, onSuccess, onError }: OAuthConfig<OAuthPolarConfig>): import("h3").EventHandler<import("h3").EventHandlerRequest, Promise<void>>;
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { eventHandler, getQuery, sendRedirect } from "h3";
|
|
2
|
+
import { withQuery } from "ufo";
|
|
3
|
+
import { defu } from "defu";
|
|
4
|
+
import { handleAccessTokenErrorResponse, handleMissingConfiguration, getOAuthRedirectURL, requestAccessToken } from "../utils.js";
|
|
5
|
+
import { useRuntimeConfig, createError } from "#imports";
|
|
6
|
+
export function defineOAuthPolarEventHandler({ config, onSuccess, onError }) {
|
|
7
|
+
return eventHandler(async (event) => {
|
|
8
|
+
config = defu(config, useRuntimeConfig(event).oauth?.polar, {
|
|
9
|
+
authorizationURL: "https://polar.sh/oauth2/authorize",
|
|
10
|
+
tokenURL: "https://api.polar.sh/v1/oauth2/token"
|
|
11
|
+
});
|
|
12
|
+
const query = getQuery(event);
|
|
13
|
+
if (!config.clientId || !config.clientSecret) {
|
|
14
|
+
return handleMissingConfiguration(event, "polar", ["clientId", "clientSecret"], onError);
|
|
15
|
+
}
|
|
16
|
+
const redirectURL = config.redirectURL || getOAuthRedirectURL(event);
|
|
17
|
+
if (!query.code) {
|
|
18
|
+
config.scope = config.scope || [];
|
|
19
|
+
if (!config.scope.includes("openid"))
|
|
20
|
+
config.scope.push("openid");
|
|
21
|
+
if (config.emailRequired && !config.scope.includes("email"))
|
|
22
|
+
config.scope.push("email");
|
|
23
|
+
return sendRedirect(
|
|
24
|
+
event,
|
|
25
|
+
withQuery(config.authorizationURL, {
|
|
26
|
+
response_type: "code",
|
|
27
|
+
client_id: config.clientId,
|
|
28
|
+
redirect_uri: redirectURL,
|
|
29
|
+
scope: config.scope.join(" "),
|
|
30
|
+
...config.authorizationParams
|
|
31
|
+
})
|
|
32
|
+
);
|
|
33
|
+
}
|
|
34
|
+
const tokens = await requestAccessToken(config.tokenURL, {
|
|
35
|
+
body: {
|
|
36
|
+
grant_type: "authorization_code",
|
|
37
|
+
redirect_uri: redirectURL,
|
|
38
|
+
client_id: config.clientId,
|
|
39
|
+
client_secret: config.clientSecret,
|
|
40
|
+
code: query.code
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
if (tokens.error) {
|
|
44
|
+
return handleAccessTokenErrorResponse(event, "polar", tokens, onError);
|
|
45
|
+
}
|
|
46
|
+
const accessToken = tokens.access_token;
|
|
47
|
+
const user = await $fetch("https://api.polar.sh/v1/oauth2/userinfo", {
|
|
48
|
+
headers: {
|
|
49
|
+
Authorization: `Bearer ${accessToken}`,
|
|
50
|
+
Accept: "application/json"
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
if (!user) {
|
|
54
|
+
const error = createError({
|
|
55
|
+
statusCode: 500,
|
|
56
|
+
message: "Could not get Polar user",
|
|
57
|
+
data: tokens
|
|
58
|
+
});
|
|
59
|
+
if (!onError) throw error;
|
|
60
|
+
return onError(event, error);
|
|
61
|
+
}
|
|
62
|
+
return onSuccess(event, {
|
|
63
|
+
tokens,
|
|
64
|
+
user
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
}
|
|
@@ -3,7 +3,6 @@ import { generateAuthenticationOptions, verifyAuthenticationResponse } from "@si
|
|
|
3
3
|
import defu from "defu";
|
|
4
4
|
import { getRandomValues } from "uncrypto";
|
|
5
5
|
import { base64URLStringToBuffer, bufferToBase64URLString } from "@simplewebauthn/browser";
|
|
6
|
-
import { storeChallengeAsSession, getChallengeFromSession } from "./utils.js";
|
|
7
6
|
import { useRuntimeConfig } from "#imports";
|
|
8
7
|
export function defineWebAuthnAuthenticateEventHandler({
|
|
9
8
|
storeChallenge,
|
|
@@ -23,14 +22,16 @@ export function defineWebAuthnAuthenticateEventHandler({
|
|
|
23
22
|
if (allowCredentials && body.userName) {
|
|
24
23
|
_config.allowCredentials = await allowCredentials(event, body.userName);
|
|
25
24
|
}
|
|
25
|
+
if (!storeChallenge) {
|
|
26
|
+
_config.challenge = "";
|
|
27
|
+
}
|
|
26
28
|
try {
|
|
27
29
|
if (!body.verify) {
|
|
28
30
|
const options = await generateAuthenticationOptions(_config);
|
|
29
31
|
const attemptId = bufferToBase64URLString(getRandomValues(new Uint8Array(32)));
|
|
30
|
-
if (storeChallenge)
|
|
32
|
+
if (storeChallenge) {
|
|
31
33
|
await storeChallenge(event, options.challenge, attemptId);
|
|
32
|
-
|
|
33
|
-
await storeChallengeAsSession(event, options.challenge, attemptId);
|
|
34
|
+
}
|
|
34
35
|
return {
|
|
35
36
|
requestOptions: options,
|
|
36
37
|
attemptId
|
|
@@ -38,15 +39,14 @@ export function defineWebAuthnAuthenticateEventHandler({
|
|
|
38
39
|
}
|
|
39
40
|
if (!body.attemptId)
|
|
40
41
|
throw createError({ statusCode: 400 });
|
|
41
|
-
let
|
|
42
|
-
if (getChallenge)
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
challenge = await getChallengeFromSession(event, body.attemptId);
|
|
42
|
+
let expectedChallenge = "";
|
|
43
|
+
if (getChallenge) {
|
|
44
|
+
expectedChallenge = await getChallenge(event, body.attemptId);
|
|
45
|
+
}
|
|
46
46
|
const credential = await getCredential(event, body.response.id);
|
|
47
47
|
const verification = await verifyAuthenticationResponse({
|
|
48
48
|
response: body.response,
|
|
49
|
-
expectedChallenge
|
|
49
|
+
expectedChallenge,
|
|
50
50
|
expectedOrigin: url.origin,
|
|
51
51
|
expectedRPID: url.hostname,
|
|
52
52
|
authenticator: {
|
|
@@ -3,7 +3,6 @@ import { generateRegistrationOptions, verifyRegistrationResponse } from "@simple
|
|
|
3
3
|
import defu from "defu";
|
|
4
4
|
import { bufferToBase64URLString } from "@simplewebauthn/browser";
|
|
5
5
|
import { getRandomValues } from "uncrypto";
|
|
6
|
-
import { storeChallengeAsSession, getChallengeFromSession } from "./utils.js";
|
|
7
6
|
import { useRuntimeConfig } from "#imports";
|
|
8
7
|
export function defineWebAuthnRegisterEventHandler({
|
|
9
8
|
storeChallenge,
|
|
@@ -34,14 +33,16 @@ export function defineWebAuthnRegisterEventHandler({
|
|
|
34
33
|
userVerification: "preferred"
|
|
35
34
|
}
|
|
36
35
|
});
|
|
36
|
+
if (!storeChallenge) {
|
|
37
|
+
_config.challenge = "";
|
|
38
|
+
}
|
|
37
39
|
try {
|
|
38
40
|
if (!body.verify) {
|
|
39
41
|
const options = await generateRegistrationOptions(_config);
|
|
40
42
|
const attemptId = bufferToBase64URLString(getRandomValues(new Uint8Array(32)));
|
|
41
|
-
if (storeChallenge)
|
|
42
|
-
await storeChallenge
|
|
43
|
-
|
|
44
|
-
await storeChallengeAsSession(event, options.challenge, attemptId);
|
|
43
|
+
if (storeChallenge) {
|
|
44
|
+
await storeChallenge(event, options.challenge, attemptId);
|
|
45
|
+
}
|
|
45
46
|
return {
|
|
46
47
|
creationOptions: options,
|
|
47
48
|
attemptId
|
|
@@ -53,11 +54,10 @@ export function defineWebAuthnRegisterEventHandler({
|
|
|
53
54
|
statusCode: 400
|
|
54
55
|
});
|
|
55
56
|
}
|
|
56
|
-
let expectedChallenge;
|
|
57
|
-
if (getChallenge)
|
|
57
|
+
let expectedChallenge = "";
|
|
58
|
+
if (getChallenge) {
|
|
58
59
|
expectedChallenge = await getChallenge(event, body.attemptId);
|
|
59
|
-
|
|
60
|
-
expectedChallenge = await getChallengeFromSession(event, body.attemptId);
|
|
60
|
+
}
|
|
61
61
|
const verification = await verifyRegistrationResponse({
|
|
62
62
|
response: body.response,
|
|
63
63
|
expectedChallenge,
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { H3Event, H3Error } from 'h3';
|
|
2
|
-
export type OAuthProvider = 'auth0' | 'battledotnet' | 'cognito' | 'discord' | 'dropbox' | 'facebook' | 'github' | 'gitlab' | 'google' | 'instagram' | 'keycloak' | 'linkedin' | 'microsoft' | 'paypal' | 'spotify' | 'steam' | 'tiktok' | 'twitch' | 'vk' | 'x' | 'xsuaa' | 'yandex' | (string & {});
|
|
2
|
+
export type OAuthProvider = 'auth0' | 'battledotnet' | 'cognito' | 'discord' | 'dropbox' | 'facebook' | 'github' | 'gitlab' | 'google' | 'instagram' | 'keycloak' | 'linkedin' | 'microsoft' | 'paypal' | 'polar' | 'spotify' | 'steam' | 'tiktok' | 'twitch' | 'vk' | 'x' | 'xsuaa' | 'yandex' | (string & {});
|
|
3
3
|
export type OnError = (event: H3Event, error: H3Error) => Promise<void> | void;
|
|
4
4
|
export interface OAuthConfig<TConfig, TUser = any, TTokens = any> {
|
|
5
5
|
config?: TConfig;
|
package/package.json
CHANGED
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
import { useSession, createError } from "h3";
|
|
2
|
-
import { useRuntimeConfig } from "#imports";
|
|
3
|
-
function useChallengeSession(event) {
|
|
4
|
-
return useSession(event, {
|
|
5
|
-
name: "webauthn",
|
|
6
|
-
password: useRuntimeConfig(event).session.password,
|
|
7
|
-
cookie: {
|
|
8
|
-
httpOnly: true,
|
|
9
|
-
secure: true,
|
|
10
|
-
maxAge: 180
|
|
11
|
-
// 3 minutes
|
|
12
|
-
}
|
|
13
|
-
});
|
|
14
|
-
}
|
|
15
|
-
export async function storeChallengeAsSession(event, challenge, challengeId) {
|
|
16
|
-
const challengeSession = await useChallengeSession(event);
|
|
17
|
-
await challengeSession.update({
|
|
18
|
-
data: {
|
|
19
|
-
challenge,
|
|
20
|
-
challengeId
|
|
21
|
-
},
|
|
22
|
-
expires: Date.now() + 180 * 1e3
|
|
23
|
-
// 3 minutes
|
|
24
|
-
});
|
|
25
|
-
}
|
|
26
|
-
export async function getChallengeFromSession(event, challengeId) {
|
|
27
|
-
const challengeSession = await useChallengeSession(event);
|
|
28
|
-
const challenge = challengeSession.data;
|
|
29
|
-
await challengeSession.clear();
|
|
30
|
-
if (challenge.expires < Date.now())
|
|
31
|
-
throw createError({ statusCode: 400, message: "Challenge expired" });
|
|
32
|
-
if (challenge.data.challengeId !== challengeId)
|
|
33
|
-
throw createError({ statusCode: 400, message: "Challenge id mismatch" });
|
|
34
|
-
return challenge.data.challenge;
|
|
35
|
-
}
|