nuxt-auth-utils 0.0.25 → 0.2.0
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 +87 -8
- package/dist/module.json +2 -2
- package/dist/module.mjs +22 -24
- package/dist/runtime/app/components/AuthState.vue +17 -0
- package/dist/runtime/{composables → app/composables}/session.d.ts +4 -0
- package/dist/runtime/{composables/session.mjs → app/composables/session.js} +7 -0
- package/dist/runtime/app/plugins/session.client.js +8 -0
- package/dist/runtime/app/plugins/session.server.js +11 -0
- package/dist/runtime/server/api/{session.delete.mjs → session.delete.js} +1 -1
- package/dist/runtime/server/api/{session.get.mjs → session.get.js} +1 -1
- package/dist/runtime/server/lib/oauth/{auth0.mjs → auth0.js} +2 -3
- package/dist/runtime/server/lib/oauth/{battledotnet.mjs → battledotnet.js} +1 -2
- package/dist/runtime/server/lib/oauth/{cognito.mjs → cognito.js} +2 -3
- package/dist/runtime/server/lib/oauth/{discord.mjs → discord.js} +2 -3
- package/dist/runtime/server/lib/oauth/{facebook.mjs → facebook.js} +1 -2
- package/dist/runtime/server/lib/oauth/{github.mjs → github.js} +2 -3
- package/dist/runtime/server/lib/oauth/{google.mjs → google.js} +2 -3
- package/dist/runtime/server/lib/oauth/{keycloak.mjs → keycloak.js} +2 -3
- package/dist/runtime/server/lib/oauth/{linkedin.mjs → linkedin.js} +2 -3
- package/dist/runtime/server/lib/oauth/{microsoft.mjs → microsoft.js} +2 -3
- package/dist/runtime/server/lib/oauth/paypal.d.ts +47 -0
- package/dist/runtime/server/lib/oauth/paypal.js +104 -0
- package/dist/runtime/server/lib/oauth/{spotify.mjs → spotify.js} +2 -3
- package/dist/runtime/server/lib/oauth/steam.d.ts +15 -0
- package/dist/runtime/server/lib/oauth/steam.js +54 -0
- package/dist/runtime/server/lib/oauth/{twitch.mjs → twitch.js} +2 -3
- package/dist/runtime/server/lib/oauth/x.d.ts +41 -0
- package/dist/runtime/server/lib/oauth/x.js +98 -0
- package/dist/runtime/server/lib/oauth/xsuaa.d.ts +26 -0
- package/dist/runtime/server/lib/oauth/xsuaa.js +73 -0
- package/dist/runtime/server/utils/oauth.d.ts +20 -12
- package/dist/runtime/server/utils/oauth.js +34 -0
- package/dist/runtime/server/utils/session.d.ts +15 -3
- package/dist/runtime/types/index.d.ts +2 -2
- package/dist/runtime/types/oauth-config.js +0 -0
- package/dist/runtime/types/session.d.ts +19 -0
- package/dist/runtime/types/session.js +0 -0
- package/dist/types.d.mts +1 -0
- package/dist/types.d.ts +1 -0
- package/package.json +15 -14
- package/dist/runtime/plugins/session.server.d.ts +0 -2
- package/dist/runtime/plugins/session.server.mjs +0 -9
- package/dist/runtime/server/utils/oauth.mjs +0 -26
- /package/dist/runtime/{types/index.mjs → app/plugins/session.client.d.ts} +0 -0
- /package/dist/runtime/{types/oauth-config.mjs → app/plugins/session.server.d.ts} +0 -0
- /package/dist/runtime/server/plugins/{oauth.mjs → oauth.js} +0 -0
- /package/dist/runtime/server/utils/{session.mjs → session.js} +0 -0
- /package/dist/runtime/types/{session.mjs → index.js} +0 -0
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { eventHandler, createError, getQuery, getRequestURL, sendRedirect } from "h3";
|
|
2
|
+
import { withQuery, parsePath } from "ufo";
|
|
3
|
+
import { defu } from "defu";
|
|
4
|
+
import { useRuntimeConfig } from "#imports";
|
|
5
|
+
export function paypalEventHandler({ config, onSuccess, onError }) {
|
|
6
|
+
return eventHandler(async (event) => {
|
|
7
|
+
config = defu(config, useRuntimeConfig(event).oauth?.paypal, {
|
|
8
|
+
sandbox: import.meta.dev,
|
|
9
|
+
authorizationURL: "https://www.paypal.com/signin/authorize",
|
|
10
|
+
tokenURL: "https://api-m.paypal.com/v1/oauth2/token",
|
|
11
|
+
authorizationParams: {}
|
|
12
|
+
});
|
|
13
|
+
const { code } = getQuery(event);
|
|
14
|
+
if (!config.clientId) {
|
|
15
|
+
const error = createError({
|
|
16
|
+
statusCode: 500,
|
|
17
|
+
message: "Missing NUXT_OAUTH_PAYPAL_CLIENT_ID env variables."
|
|
18
|
+
});
|
|
19
|
+
if (!onError)
|
|
20
|
+
throw error;
|
|
21
|
+
return onError(event, error);
|
|
22
|
+
}
|
|
23
|
+
let paypalAPI = "api-m.paypal.com";
|
|
24
|
+
if (config.sandbox) {
|
|
25
|
+
paypalAPI = "api-m.sandbox.paypal.com";
|
|
26
|
+
config.authorizationURL = "https://www.sandbox.paypal.com/signin/authorize";
|
|
27
|
+
config.tokenURL = `https://${paypalAPI}/v1/oauth2/token`;
|
|
28
|
+
}
|
|
29
|
+
const redirectUrl = getRequestURL(event).href;
|
|
30
|
+
if (!code) {
|
|
31
|
+
config.scope = config.scope || [];
|
|
32
|
+
if (!config.scope.includes("openid")) {
|
|
33
|
+
config.scope.push("openid");
|
|
34
|
+
}
|
|
35
|
+
if (config.emailRequired && !config.scope.includes("email")) {
|
|
36
|
+
config.scope.push("email");
|
|
37
|
+
}
|
|
38
|
+
return sendRedirect(
|
|
39
|
+
event,
|
|
40
|
+
withQuery(config.authorizationURL, {
|
|
41
|
+
response_type: "code",
|
|
42
|
+
client_id: config.clientId,
|
|
43
|
+
redirect_uri: redirectUrl,
|
|
44
|
+
scope: config.scope.join(" "),
|
|
45
|
+
flowEntry: "static",
|
|
46
|
+
...config.authorizationParams
|
|
47
|
+
})
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
const authCode = Buffer.from(`${config.clientId}:${config.clientSecret}`).toString("base64");
|
|
51
|
+
const tokens = await $fetch(
|
|
52
|
+
config.tokenURL,
|
|
53
|
+
{
|
|
54
|
+
method: "POST",
|
|
55
|
+
headers: {
|
|
56
|
+
"Authorization": `Basic ${authCode}`,
|
|
57
|
+
"Content-Type": "application/x-www-form-urlencoded"
|
|
58
|
+
},
|
|
59
|
+
params: {
|
|
60
|
+
grant_type: "authorization_code",
|
|
61
|
+
redirect_uri: encodeURIComponent(parsePath(redirectUrl).pathname),
|
|
62
|
+
code
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
).catch((error) => {
|
|
66
|
+
return { error };
|
|
67
|
+
});
|
|
68
|
+
if (tokens.error) {
|
|
69
|
+
const error = createError({
|
|
70
|
+
statusCode: 401,
|
|
71
|
+
message: `PayPal login failed: ${tokens.error?.data?.error_description || "Unknown error"}`,
|
|
72
|
+
data: tokens
|
|
73
|
+
});
|
|
74
|
+
if (!onError)
|
|
75
|
+
throw error;
|
|
76
|
+
return onError(event, error);
|
|
77
|
+
}
|
|
78
|
+
const accessToken = tokens.access_token;
|
|
79
|
+
const users = await $fetch(`https://${paypalAPI}/v1/identity/openidconnect/userinfo`, {
|
|
80
|
+
headers: {
|
|
81
|
+
"Authorization": `Bearer ${accessToken}`,
|
|
82
|
+
"Content-Type": "application/x-www-form-urlencoded"
|
|
83
|
+
},
|
|
84
|
+
params: {
|
|
85
|
+
schema: "openid"
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
const user = users;
|
|
89
|
+
if (!user) {
|
|
90
|
+
const error = createError({
|
|
91
|
+
statusCode: 500,
|
|
92
|
+
message: "Could not get PayPal user",
|
|
93
|
+
data: tokens
|
|
94
|
+
});
|
|
95
|
+
if (!onError)
|
|
96
|
+
throw error;
|
|
97
|
+
return onError(event, error);
|
|
98
|
+
}
|
|
99
|
+
return onSuccess(event, {
|
|
100
|
+
tokens,
|
|
101
|
+
user
|
|
102
|
+
});
|
|
103
|
+
});
|
|
104
|
+
}
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { eventHandler, createError, getQuery, getRequestURL, sendRedirect } from "h3";
|
|
2
2
|
import { withQuery, parsePath } from "ufo";
|
|
3
|
-
import { ofetch } from "ofetch";
|
|
4
3
|
import { defu } from "defu";
|
|
5
4
|
import { useRuntimeConfig } from "#imports";
|
|
6
5
|
export function spotifyEventHandler({ config, onSuccess, onError }) {
|
|
@@ -38,7 +37,7 @@ export function spotifyEventHandler({ config, onSuccess, onError }) {
|
|
|
38
37
|
);
|
|
39
38
|
}
|
|
40
39
|
const authCode = Buffer.from(`${config.clientId}:${config.clientSecret}`).toString("base64");
|
|
41
|
-
const tokens = await
|
|
40
|
+
const tokens = await $fetch(
|
|
42
41
|
config.tokenURL,
|
|
43
42
|
{
|
|
44
43
|
method: "POST",
|
|
@@ -66,7 +65,7 @@ export function spotifyEventHandler({ config, onSuccess, onError }) {
|
|
|
66
65
|
return onError(event, error);
|
|
67
66
|
}
|
|
68
67
|
const accessToken = tokens.access_token;
|
|
69
|
-
const user = await
|
|
68
|
+
const user = await $fetch("https://api.spotify.com/v1/me", {
|
|
70
69
|
headers: {
|
|
71
70
|
Authorization: `Bearer ${accessToken}`
|
|
72
71
|
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { OAuthConfig } from '#auth-utils';
|
|
2
|
+
export interface OAuthSteamConfig {
|
|
3
|
+
/**
|
|
4
|
+
* Steam API Key
|
|
5
|
+
* @default process.env.NUXT_OAUTH_STEAM_API_KEY
|
|
6
|
+
* @see https://steamcommunity.com/dev
|
|
7
|
+
*/
|
|
8
|
+
apiKey?: string;
|
|
9
|
+
/**
|
|
10
|
+
* Steam Open ID OAuth Authorization URL
|
|
11
|
+
* @default 'https://steamcommunity.com/openid/login'
|
|
12
|
+
*/
|
|
13
|
+
authorizationURL?: string;
|
|
14
|
+
}
|
|
15
|
+
export declare function steamEventHandler({ config, onSuccess, onError }: OAuthConfig<OAuthSteamConfig>): import("h3").EventHandler<import("h3").EventHandlerRequest, Promise<void>>;
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { eventHandler, createError, getQuery, getRequestURL, sendRedirect } from "h3";
|
|
2
|
+
import { withQuery } from "ufo";
|
|
3
|
+
import { defu } from "defu";
|
|
4
|
+
import { useRuntimeConfig } from "#imports";
|
|
5
|
+
export function steamEventHandler({ config, onSuccess, onError }) {
|
|
6
|
+
return eventHandler(async (event) => {
|
|
7
|
+
config = defu(config, useRuntimeConfig(event).oauth?.steam, {
|
|
8
|
+
authorizationURL: "https://steamcommunity.com/openid/login"
|
|
9
|
+
});
|
|
10
|
+
const query = getQuery(event);
|
|
11
|
+
if (!config.apiKey) {
|
|
12
|
+
const error = createError({
|
|
13
|
+
statusCode: 500,
|
|
14
|
+
message: "Missing NUXT_OAUTH_STEAM_API_KEY env variable."
|
|
15
|
+
});
|
|
16
|
+
if (!onError)
|
|
17
|
+
throw error;
|
|
18
|
+
return onError(event, error);
|
|
19
|
+
}
|
|
20
|
+
if (!query["openid.claimed_id"]) {
|
|
21
|
+
const redirectUrl = getRequestURL(event).href;
|
|
22
|
+
const steamOpenIdParams = {
|
|
23
|
+
"openid.ns": "http://specs.openid.net/auth/2.0",
|
|
24
|
+
"openid.mode": "checkid_setup",
|
|
25
|
+
"openid.return_to": redirectUrl,
|
|
26
|
+
"openid.identity": "http://specs.openid.net/auth/2.0/identifier_select",
|
|
27
|
+
"openid.claimed_id": "http://specs.openid.net/auth/2.0/identifier_select"
|
|
28
|
+
};
|
|
29
|
+
return sendRedirect(event, withQuery(config.authorizationURL, steamOpenIdParams));
|
|
30
|
+
}
|
|
31
|
+
const validateAuth = await $fetch(withQuery(config.authorizationURL, {
|
|
32
|
+
...query,
|
|
33
|
+
"openid.mode": "check_authentication"
|
|
34
|
+
}));
|
|
35
|
+
if (!validateAuth.includes("is_valid:true")) {
|
|
36
|
+
const error = createError({
|
|
37
|
+
statusCode: 401,
|
|
38
|
+
message: "Steam login failed: Unknown error"
|
|
39
|
+
});
|
|
40
|
+
if (!onError)
|
|
41
|
+
throw error;
|
|
42
|
+
return onError(event, error);
|
|
43
|
+
}
|
|
44
|
+
const steamId = query["openid.claimed_id"].split("/").pop();
|
|
45
|
+
const user = await $fetch(withQuery("https://api.steampowered.com/ISteamUser/GetPlayerSummaries/v2/", {
|
|
46
|
+
key: config.apiKey,
|
|
47
|
+
steamids: steamId
|
|
48
|
+
}));
|
|
49
|
+
return onSuccess(event, {
|
|
50
|
+
user: user.response.players[0],
|
|
51
|
+
tokens: null
|
|
52
|
+
});
|
|
53
|
+
});
|
|
54
|
+
}
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { eventHandler, createError, getQuery, getRequestURL, sendRedirect } from "h3";
|
|
2
2
|
import { withQuery, parsePath } from "ufo";
|
|
3
|
-
import { ofetch } from "ofetch";
|
|
4
3
|
import { defu } from "defu";
|
|
5
4
|
import { useRuntimeConfig } from "#imports";
|
|
6
5
|
export function twitchEventHandler({ config, onSuccess, onError }) {
|
|
@@ -37,7 +36,7 @@ export function twitchEventHandler({ config, onSuccess, onError }) {
|
|
|
37
36
|
})
|
|
38
37
|
);
|
|
39
38
|
}
|
|
40
|
-
const tokens = await
|
|
39
|
+
const tokens = await $fetch(
|
|
41
40
|
config.tokenURL,
|
|
42
41
|
{
|
|
43
42
|
method: "POST",
|
|
@@ -66,7 +65,7 @@ export function twitchEventHandler({ config, onSuccess, onError }) {
|
|
|
66
65
|
return onError(event, error);
|
|
67
66
|
}
|
|
68
67
|
const accessToken = tokens.access_token;
|
|
69
|
-
const users = await
|
|
68
|
+
const users = await $fetch("https://api.twitch.tv/helix/users", {
|
|
70
69
|
headers: {
|
|
71
70
|
"Client-ID": config.clientId,
|
|
72
71
|
"Authorization": `Bearer ${accessToken}`
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { OAuthConfig } from '#auth-utils';
|
|
2
|
+
export interface OAuthXConfig {
|
|
3
|
+
/**
|
|
4
|
+
* X OAuth Client ID
|
|
5
|
+
* @default process.env.NUXT_OAUTH_X_CLIENT_ID
|
|
6
|
+
*/
|
|
7
|
+
clientId?: string;
|
|
8
|
+
/**
|
|
9
|
+
* X OAuth Client Secret
|
|
10
|
+
* @default process.env.NUXT_OAUTH_X_CLIENT_SECRET
|
|
11
|
+
*/
|
|
12
|
+
clientSecret?: string;
|
|
13
|
+
/**
|
|
14
|
+
* X OAuth Scope
|
|
15
|
+
* @default []
|
|
16
|
+
* @see https://developer.x.com/en/docs/authentication/oauth-2-0/user-access-token
|
|
17
|
+
* @example [ 'tweet.read','users.read','offline.access ],
|
|
18
|
+
*/
|
|
19
|
+
scope?: string[];
|
|
20
|
+
/**
|
|
21
|
+
* X OAuth Authorization URL
|
|
22
|
+
* @default 'https://twitter.com/i/oauth2/authorize'
|
|
23
|
+
*/
|
|
24
|
+
authorizationURL?: string;
|
|
25
|
+
/**
|
|
26
|
+
* X OAuth Token URL
|
|
27
|
+
* @default 'https://api.twitter.com/2/oauth2/token'
|
|
28
|
+
*/
|
|
29
|
+
tokenURL?: string;
|
|
30
|
+
/**
|
|
31
|
+
* X OAuth User URL
|
|
32
|
+
* @default 'https://api.twitter.com/2/users/me'
|
|
33
|
+
*/
|
|
34
|
+
userURL?: string;
|
|
35
|
+
/**
|
|
36
|
+
* Extra authorization parameters to provide to the authorization URL
|
|
37
|
+
* @see https://developer.x.com/en/docs/authentication/oauth-2-0/user-access-token
|
|
38
|
+
*/
|
|
39
|
+
authorizationParams: Record<string, string>;
|
|
40
|
+
}
|
|
41
|
+
export declare function xEventHandler({ config, onSuccess, onError, }: OAuthConfig<OAuthXConfig>): import("h3").EventHandler<import("h3").EventHandlerRequest, Promise<void>>;
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import {
|
|
3
|
+
eventHandler,
|
|
4
|
+
createError,
|
|
5
|
+
getQuery,
|
|
6
|
+
getRequestURL,
|
|
7
|
+
sendRedirect
|
|
8
|
+
} from "h3";
|
|
9
|
+
import { withQuery, parsePath } from "ufo";
|
|
10
|
+
import { defu } from "defu";
|
|
11
|
+
import { useRuntimeConfig } from "#imports";
|
|
12
|
+
export function xEventHandler({
|
|
13
|
+
config,
|
|
14
|
+
onSuccess,
|
|
15
|
+
onError
|
|
16
|
+
}) {
|
|
17
|
+
return eventHandler(async (event) => {
|
|
18
|
+
config = defu(config, useRuntimeConfig(event).oauth?.x, {
|
|
19
|
+
authorizationURL: "https://twitter.com/i/oauth2/authorize",
|
|
20
|
+
tokenURL: "https://api.twitter.com/2/oauth2/token",
|
|
21
|
+
userURL: "https://api.twitter.com/2/users/me",
|
|
22
|
+
authorizationParams: {
|
|
23
|
+
state: randomUUID(),
|
|
24
|
+
code_challenge: randomUUID()
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
const { code } = getQuery(event);
|
|
28
|
+
if (!config.clientId) {
|
|
29
|
+
const error = createError({
|
|
30
|
+
statusCode: 500,
|
|
31
|
+
message: "Missing NUXT_OAUTH_X_CLIENT_ID env variables."
|
|
32
|
+
});
|
|
33
|
+
if (!onError)
|
|
34
|
+
throw error;
|
|
35
|
+
return onError(event, error);
|
|
36
|
+
}
|
|
37
|
+
const redirectUrl = getRequestURL(event).href;
|
|
38
|
+
if (!code) {
|
|
39
|
+
config.scope = config.scope || ["tweet.read", "users.read", "offline.access"];
|
|
40
|
+
return sendRedirect(
|
|
41
|
+
event,
|
|
42
|
+
withQuery(config.authorizationURL, {
|
|
43
|
+
response_type: "code",
|
|
44
|
+
client_id: config.clientId,
|
|
45
|
+
code_challenge_method: "plain",
|
|
46
|
+
redirect_uri: redirectUrl,
|
|
47
|
+
scope: config.scope.join(" "),
|
|
48
|
+
...config.authorizationParams
|
|
49
|
+
})
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
const params = {
|
|
53
|
+
grant_type: "authorization_code",
|
|
54
|
+
code_verifier: config.authorizationParams.code_challenge,
|
|
55
|
+
redirect_uri: parsePath(redirectUrl).pathname,
|
|
56
|
+
code
|
|
57
|
+
};
|
|
58
|
+
const authCode = Buffer.from(`${config.clientId}:${config.clientSecret}`).toString("base64");
|
|
59
|
+
const tokens = await $fetch(config.tokenURL, {
|
|
60
|
+
method: "POST",
|
|
61
|
+
headers: {
|
|
62
|
+
"Content-Type": "application/x-www-form-urlencoded",
|
|
63
|
+
"Authorization": `Basic ${authCode}`
|
|
64
|
+
},
|
|
65
|
+
params
|
|
66
|
+
}).catch((error) => {
|
|
67
|
+
return { error };
|
|
68
|
+
});
|
|
69
|
+
if (tokens.error) {
|
|
70
|
+
const error = createError({
|
|
71
|
+
statusCode: 401,
|
|
72
|
+
message: `X login failed: ${tokens.error?.data?.error_description || "Unknown error"}`,
|
|
73
|
+
data: tokens
|
|
74
|
+
});
|
|
75
|
+
if (!onError)
|
|
76
|
+
throw error;
|
|
77
|
+
return onError(event, error);
|
|
78
|
+
}
|
|
79
|
+
const accessToken = tokens.access_token;
|
|
80
|
+
const user = await $fetch(
|
|
81
|
+
config.userURL,
|
|
82
|
+
{
|
|
83
|
+
headers: {
|
|
84
|
+
Authorization: `Bearer ${accessToken}`
|
|
85
|
+
},
|
|
86
|
+
query: {
|
|
87
|
+
"user.fields": "description,id,name,profile_image_url,username,verified,verified_type"
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
).catch((error) => {
|
|
91
|
+
return error;
|
|
92
|
+
});
|
|
93
|
+
return onSuccess(event, {
|
|
94
|
+
tokens,
|
|
95
|
+
user: user?.data
|
|
96
|
+
});
|
|
97
|
+
});
|
|
98
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { OAuthConfig } from '#auth-utils';
|
|
2
|
+
export interface OAuthXSUAAConfig {
|
|
3
|
+
/**
|
|
4
|
+
* XSUAA OAuth Client ID
|
|
5
|
+
* @default process.env.NUXT_OAUTH_XSUAA_CLIENT_ID
|
|
6
|
+
*/
|
|
7
|
+
clientId?: string;
|
|
8
|
+
/**
|
|
9
|
+
* XSUAA OAuth Client Secret
|
|
10
|
+
* @default process.env.NUXT_OAUTH_XSUAA_CLIENT_SECRET
|
|
11
|
+
*/
|
|
12
|
+
clientSecret?: string;
|
|
13
|
+
/**
|
|
14
|
+
* XSUAA OAuth Issuer
|
|
15
|
+
* @default process.env.NUXT_OAUTH_XSUAA_DOMAIN
|
|
16
|
+
*/
|
|
17
|
+
domain?: string;
|
|
18
|
+
/**
|
|
19
|
+
* XSUAA OAuth Scope
|
|
20
|
+
* @default []
|
|
21
|
+
* @see https://sap.github.io/cloud-sdk/docs/java/guides/cloud-foundry-xsuaa-service
|
|
22
|
+
* @example ['openid']
|
|
23
|
+
*/
|
|
24
|
+
scope?: string[];
|
|
25
|
+
}
|
|
26
|
+
export declare function xsuaaEventHandler({ config, onSuccess, onError }: OAuthConfig<OAuthXSUAAConfig>): import("h3").EventHandler<import("h3").EventHandlerRequest, Promise<void>>;
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { eventHandler, createError, getQuery, getRequestURL, sendRedirect } from "h3";
|
|
2
|
+
import { withQuery, parsePath } from "ufo";
|
|
3
|
+
import { defu } from "defu";
|
|
4
|
+
import { useRuntimeConfig } from "#imports";
|
|
5
|
+
export function xsuaaEventHandler({ config, onSuccess, onError }) {
|
|
6
|
+
return eventHandler(async (event) => {
|
|
7
|
+
config = defu(config, useRuntimeConfig(event).oauth?.xsuaa);
|
|
8
|
+
const { code } = getQuery(event);
|
|
9
|
+
if (!config.clientId || !config.clientSecret || !config.domain) {
|
|
10
|
+
const error = createError({
|
|
11
|
+
statusCode: 500,
|
|
12
|
+
message: "Missing NUXT_OAUTH_XSUAA_CLIENT_ID or NUXT_OAUTH_XSUAA_CLIENT_SECRET or NUXT_OAUTH_XSUAA_DOMAIN env variables."
|
|
13
|
+
});
|
|
14
|
+
if (!onError)
|
|
15
|
+
throw error;
|
|
16
|
+
return onError(event, error);
|
|
17
|
+
}
|
|
18
|
+
const authorizationURL = `https://${config.domain}/oauth/authorize`;
|
|
19
|
+
const tokenURL = `https://${config.domain}/oauth/token`;
|
|
20
|
+
const redirectUrl = getRequestURL(event).href;
|
|
21
|
+
if (!code) {
|
|
22
|
+
config.scope = config.scope || [];
|
|
23
|
+
return sendRedirect(
|
|
24
|
+
event,
|
|
25
|
+
withQuery(authorizationURL, {
|
|
26
|
+
response_type: "code",
|
|
27
|
+
client_id: config.clientId,
|
|
28
|
+
redirect_uri: redirectUrl,
|
|
29
|
+
scope: config.scope.join(" ")
|
|
30
|
+
})
|
|
31
|
+
);
|
|
32
|
+
}
|
|
33
|
+
const tokens = await $fetch(
|
|
34
|
+
tokenURL,
|
|
35
|
+
{
|
|
36
|
+
method: "POST",
|
|
37
|
+
headers: {
|
|
38
|
+
"Content-Type": "application/x-www-form-urlencoded"
|
|
39
|
+
},
|
|
40
|
+
body: new URLSearchParams({
|
|
41
|
+
grant_type: "authorization_code",
|
|
42
|
+
client_id: config.clientId,
|
|
43
|
+
client_secret: config.clientSecret,
|
|
44
|
+
redirect_uri: parsePath(redirectUrl).pathname,
|
|
45
|
+
code: `${code}`
|
|
46
|
+
})
|
|
47
|
+
}
|
|
48
|
+
).catch((error) => {
|
|
49
|
+
return { error };
|
|
50
|
+
});
|
|
51
|
+
if (tokens.error) {
|
|
52
|
+
const error = createError({
|
|
53
|
+
statusCode: 401,
|
|
54
|
+
message: `XSUAA login failed: ${tokens.error?.data?.error_description || "Unknown error"}`,
|
|
55
|
+
data: tokens
|
|
56
|
+
});
|
|
57
|
+
if (!onError)
|
|
58
|
+
throw error;
|
|
59
|
+
return onError(event, error);
|
|
60
|
+
}
|
|
61
|
+
const tokenType = tokens.token_type;
|
|
62
|
+
const accessToken = tokens.access_token;
|
|
63
|
+
const user = await $fetch(`https://${config.domain}/userinfo`, {
|
|
64
|
+
headers: {
|
|
65
|
+
Authorization: `${tokenType} ${accessToken}`
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
return onSuccess(event, {
|
|
69
|
+
tokens,
|
|
70
|
+
user
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
|
+
}
|
|
@@ -1,15 +1,19 @@
|
|
|
1
|
-
import { githubEventHandler } from '../lib/oauth/github';
|
|
2
|
-
import { googleEventHandler } from '../lib/oauth/google';
|
|
3
|
-
import { spotifyEventHandler } from '../lib/oauth/spotify';
|
|
4
|
-
import { twitchEventHandler } from '../lib/oauth/twitch';
|
|
5
|
-
import { auth0EventHandler } from '../lib/oauth/auth0';
|
|
6
|
-
import { microsoftEventHandler } from '../lib/oauth/microsoft';
|
|
7
|
-
import { discordEventHandler } from '../lib/oauth/discord';
|
|
8
|
-
import { battledotnetEventHandler } from '../lib/oauth/battledotnet';
|
|
9
|
-
import { keycloakEventHandler } from '../lib/oauth/keycloak';
|
|
10
|
-
import { linkedinEventHandler } from '../lib/oauth/linkedin';
|
|
11
|
-
import { cognitoEventHandler } from '../lib/oauth/cognito';
|
|
12
|
-
import { facebookEventHandler } from '../lib/oauth/facebook';
|
|
1
|
+
import { githubEventHandler } from '../lib/oauth/github.js';
|
|
2
|
+
import { googleEventHandler } from '../lib/oauth/google.js';
|
|
3
|
+
import { spotifyEventHandler } from '../lib/oauth/spotify.js';
|
|
4
|
+
import { twitchEventHandler } from '../lib/oauth/twitch.js';
|
|
5
|
+
import { auth0EventHandler } from '../lib/oauth/auth0.js';
|
|
6
|
+
import { microsoftEventHandler } from '../lib/oauth/microsoft.js';
|
|
7
|
+
import { discordEventHandler } from '../lib/oauth/discord.js';
|
|
8
|
+
import { battledotnetEventHandler } from '../lib/oauth/battledotnet.js';
|
|
9
|
+
import { keycloakEventHandler } from '../lib/oauth/keycloak.js';
|
|
10
|
+
import { linkedinEventHandler } from '../lib/oauth/linkedin.js';
|
|
11
|
+
import { cognitoEventHandler } from '../lib/oauth/cognito.js';
|
|
12
|
+
import { facebookEventHandler } from '../lib/oauth/facebook.js';
|
|
13
|
+
import { paypalEventHandler } from '../lib/oauth/paypal.js';
|
|
14
|
+
import { steamEventHandler } from '../lib/oauth/steam.js';
|
|
15
|
+
import { xEventHandler } from '../lib/oauth/x.js';
|
|
16
|
+
import { xsuaaEventHandler } from '../lib/oauth/xsuaa.js';
|
|
13
17
|
export declare const oauth: {
|
|
14
18
|
githubEventHandler: typeof githubEventHandler;
|
|
15
19
|
spotifyEventHandler: typeof spotifyEventHandler;
|
|
@@ -23,4 +27,8 @@ export declare const oauth: {
|
|
|
23
27
|
linkedinEventHandler: typeof linkedinEventHandler;
|
|
24
28
|
cognitoEventHandler: typeof cognitoEventHandler;
|
|
25
29
|
facebookEventHandler: typeof facebookEventHandler;
|
|
30
|
+
paypalEventHandler: typeof paypalEventHandler;
|
|
31
|
+
steamEventHandler: typeof steamEventHandler;
|
|
32
|
+
xEventHandler: typeof xEventHandler;
|
|
33
|
+
xsuaaEventHandler: typeof xsuaaEventHandler;
|
|
26
34
|
};
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { githubEventHandler } from "../lib/oauth/github.js";
|
|
2
|
+
import { googleEventHandler } from "../lib/oauth/google.js";
|
|
3
|
+
import { spotifyEventHandler } from "../lib/oauth/spotify.js";
|
|
4
|
+
import { twitchEventHandler } from "../lib/oauth/twitch.js";
|
|
5
|
+
import { auth0EventHandler } from "../lib/oauth/auth0.js";
|
|
6
|
+
import { microsoftEventHandler } from "../lib/oauth/microsoft.js";
|
|
7
|
+
import { discordEventHandler } from "../lib/oauth/discord.js";
|
|
8
|
+
import { battledotnetEventHandler } from "../lib/oauth/battledotnet.js";
|
|
9
|
+
import { keycloakEventHandler } from "../lib/oauth/keycloak.js";
|
|
10
|
+
import { linkedinEventHandler } from "../lib/oauth/linkedin.js";
|
|
11
|
+
import { cognitoEventHandler } from "../lib/oauth/cognito.js";
|
|
12
|
+
import { facebookEventHandler } from "../lib/oauth/facebook.js";
|
|
13
|
+
import { paypalEventHandler } from "../lib/oauth/paypal.js";
|
|
14
|
+
import { steamEventHandler } from "../lib/oauth/steam.js";
|
|
15
|
+
import { xEventHandler } from "../lib/oauth/x.js";
|
|
16
|
+
import { xsuaaEventHandler } from "../lib/oauth/xsuaa.js";
|
|
17
|
+
export const oauth = {
|
|
18
|
+
githubEventHandler,
|
|
19
|
+
spotifyEventHandler,
|
|
20
|
+
googleEventHandler,
|
|
21
|
+
twitchEventHandler,
|
|
22
|
+
auth0EventHandler,
|
|
23
|
+
microsoftEventHandler,
|
|
24
|
+
discordEventHandler,
|
|
25
|
+
battledotnetEventHandler,
|
|
26
|
+
keycloakEventHandler,
|
|
27
|
+
linkedinEventHandler,
|
|
28
|
+
cognitoEventHandler,
|
|
29
|
+
facebookEventHandler,
|
|
30
|
+
paypalEventHandler,
|
|
31
|
+
steamEventHandler,
|
|
32
|
+
xEventHandler,
|
|
33
|
+
xsuaaEventHandler
|
|
34
|
+
};
|
|
@@ -13,26 +13,38 @@ export interface SessionHooks {
|
|
|
13
13
|
clear: (session: UserSession, event: H3Event) => void | Promise<void>;
|
|
14
14
|
}
|
|
15
15
|
export declare const sessionHooks: import("hookable").Hookable<SessionHooks, import("hookable").HookKeys<SessionHooks>>;
|
|
16
|
+
/**
|
|
17
|
+
* Get the user session from the current request
|
|
18
|
+
* @param event The Request (h3) event
|
|
19
|
+
* @returns The user session
|
|
20
|
+
*/
|
|
16
21
|
export declare function getUserSession(event: H3Event): Promise<UserSession>;
|
|
17
22
|
/**
|
|
18
23
|
* Set a user session
|
|
19
|
-
* @param event
|
|
24
|
+
* @param event The Request (h3) event
|
|
20
25
|
* @param data User session data, please only store public information since it can be decoded with API calls
|
|
26
|
+
* @see https://github.com/atinux/nuxt-auth-utils
|
|
21
27
|
*/
|
|
22
28
|
export declare function setUserSession(event: H3Event, data: UserSession): Promise<UserSession>;
|
|
23
29
|
/**
|
|
24
30
|
* Replace a user session
|
|
25
|
-
* @param event
|
|
31
|
+
* @param event The Request (h3) event
|
|
26
32
|
* @param data User session data, please only store public information since it can be decoded with API calls
|
|
27
33
|
*/
|
|
28
34
|
export declare function replaceUserSession(event: H3Event, data: UserSession): Promise<UserSession>;
|
|
35
|
+
/**
|
|
36
|
+
* Clear the user session and removing the session cookie
|
|
37
|
+
* @param event The Request (h3) event
|
|
38
|
+
* @returns true if the session was cleared
|
|
39
|
+
*/
|
|
29
40
|
export declare function clearUserSession(event: H3Event): Promise<boolean>;
|
|
30
41
|
/**
|
|
31
|
-
* Require a user session
|
|
42
|
+
* Require a user session, throw a 401 error if the user is not logged in
|
|
32
43
|
* @param event
|
|
33
44
|
* @param opts Options to customize the error message and status code
|
|
34
45
|
* @param opts.statusCode The status code to use for the error (defaults to 401)
|
|
35
46
|
* @param opts.message The message to use for the error (defaults to Unauthorized)
|
|
47
|
+
* @see https://github.com/atinux/nuxt-auth-utils
|
|
36
48
|
*/
|
|
37
49
|
export declare function requireUserSession(event: H3Event, opts?: {
|
|
38
50
|
statusCode?: number;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export type { User, UserSession, UserSessionRequired, UserSessionComposable } from './session';
|
|
2
|
-
export type { OAuthConfig } from './oauth-config';
|
|
1
|
+
export type { User, UserSession, UserSessionRequired, UserSessionComposable } from './session.js';
|
|
2
|
+
export type { OAuthConfig } from './oauth-config.js';
|
|
File without changes
|
|
@@ -8,9 +8,28 @@ export interface UserSessionRequired extends UserSession {
|
|
|
8
8
|
user: User;
|
|
9
9
|
}
|
|
10
10
|
export interface UserSessionComposable {
|
|
11
|
+
/**
|
|
12
|
+
* Computed indicating if the auth session is ready
|
|
13
|
+
*/
|
|
14
|
+
ready: ComputedRef<boolean>;
|
|
15
|
+
/**
|
|
16
|
+
* Computed indicating if the user is logged in.
|
|
17
|
+
*/
|
|
11
18
|
loggedIn: ComputedRef<boolean>;
|
|
19
|
+
/**
|
|
20
|
+
* The user object if logged in, null otherwise.
|
|
21
|
+
*/
|
|
12
22
|
user: ComputedRef<User | null>;
|
|
23
|
+
/**
|
|
24
|
+
* The session object.
|
|
25
|
+
*/
|
|
13
26
|
session: Ref<UserSession>;
|
|
27
|
+
/**
|
|
28
|
+
* Fetch the user session from the server.
|
|
29
|
+
*/
|
|
14
30
|
fetch: () => Promise<void>;
|
|
31
|
+
/**
|
|
32
|
+
* Clear the user session and remove the session cookie.
|
|
33
|
+
*/
|
|
15
34
|
clear: () => Promise<void>;
|
|
16
35
|
}
|
|
File without changes
|
package/dist/types.d.mts
CHANGED