nuxt-auth-utils 0.3.5 → 0.3.6
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 +2 -1
- package/dist/module.json +1 -1
- package/dist/module.mjs +5 -0
- package/dist/runtime/server/lib/oauth/auth0.js +16 -22
- package/dist/runtime/server/lib/oauth/battledotnet.js +16 -25
- package/dist/runtime/server/lib/oauth/cognito.js +15 -15
- package/dist/runtime/server/lib/oauth/discord.js +13 -24
- package/dist/runtime/server/lib/oauth/facebook.js +6 -12
- package/dist/runtime/server/lib/oauth/github.js +12 -14
- package/dist/runtime/server/lib/oauth/google.js +14 -23
- package/dist/runtime/server/lib/oauth/keycloak.js +12 -25
- package/dist/runtime/server/lib/oauth/linkedin.d.ts +2 -11
- package/dist/runtime/server/lib/oauth/linkedin.js +13 -24
- package/dist/runtime/server/lib/oauth/microsoft.d.ts +2 -11
- package/dist/runtime/server/lib/oauth/microsoft.js +14 -23
- package/dist/runtime/server/lib/oauth/paypal.js +15 -23
- package/dist/runtime/server/lib/oauth/spotify.js +15 -22
- package/dist/runtime/server/lib/oauth/tiktok.d.ts +45 -0
- package/dist/runtime/server/lib/oauth/tiktok.js +84 -0
- package/dist/runtime/server/lib/oauth/twitch.js +14 -23
- package/dist/runtime/server/lib/oauth/x.js +14 -21
- package/dist/runtime/server/lib/oauth/xsuaa.js +13 -22
- package/dist/runtime/server/lib/oauth/yandex.js +12 -23
- package/dist/runtime/server/lib/utils.d.ts +28 -1
- package/dist/runtime/server/lib/utils.js +25 -0
- package/dist/runtime/types/oauth-config.d.ts +1 -1
- package/package.json +7 -7
package/README.md
CHANGED
|
@@ -199,6 +199,7 @@ It can also be set using environment variables:
|
|
|
199
199
|
- PayPal
|
|
200
200
|
- Spotify
|
|
201
201
|
- Steam
|
|
202
|
+
- TikTok
|
|
202
203
|
- Twitch
|
|
203
204
|
- X (Twitter)
|
|
204
205
|
- XSUAA
|
|
@@ -250,7 +251,7 @@ export default defineNitroPlugin(() => {
|
|
|
250
251
|
// throw createError({ ... }) if session is invalid for example
|
|
251
252
|
})
|
|
252
253
|
|
|
253
|
-
// Called when we call
|
|
254
|
+
// Called when we call useUserSession().clear() or clearUserSession(event)
|
|
254
255
|
sessionHooks.hook('clear', async (session, event) => {
|
|
255
256
|
// Log that user logged out
|
|
256
257
|
})
|
package/dist/module.json
CHANGED
package/dist/module.mjs
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
import { eventHandler, getQuery,
|
|
2
|
-
import { withQuery
|
|
1
|
+
import { eventHandler, getQuery, sendRedirect } from "h3";
|
|
2
|
+
import { withQuery } from "ufo";
|
|
3
3
|
import { defu } from "defu";
|
|
4
|
-
import { handleMissingConfiguration, handleAccessTokenErrorResponse } from "../utils.js";
|
|
4
|
+
import { handleMissingConfiguration, handleAccessTokenErrorResponse, getOAuthRedirectURL, requestAccessToken } from "../utils.js";
|
|
5
5
|
import { useRuntimeConfig } from "#imports";
|
|
6
6
|
export function oauthAuth0EventHandler({ config, onSuccess, onError }) {
|
|
7
7
|
return eventHandler(async (event) => {
|
|
8
8
|
config = defu(config, useRuntimeConfig(event).oauth?.auth0, {
|
|
9
9
|
authorizationParams: {}
|
|
10
10
|
});
|
|
11
|
-
const { code } = getQuery(event);
|
|
12
11
|
if (!config.clientId || !config.clientSecret || !config.domain) {
|
|
13
12
|
return handleMissingConfiguration(event, "auth0", ["clientId", "clientSecret", "domain"], onError);
|
|
14
13
|
}
|
|
15
14
|
const authorizationURL = `https://${config.domain}/authorize`;
|
|
16
15
|
const tokenURL = `https://${config.domain}/oauth/token`;
|
|
17
|
-
const
|
|
18
|
-
|
|
16
|
+
const query = getQuery(event);
|
|
17
|
+
const redirectURL = config.redirectURL || getOAuthRedirectURL(event);
|
|
18
|
+
if (!query.code) {
|
|
19
19
|
config.scope = config.scope || ["openid", "offline_access"];
|
|
20
20
|
if (config.emailRequired && !config.scope.includes("email")) {
|
|
21
21
|
config.scope.push("email");
|
|
@@ -34,23 +34,17 @@ export function oauthAuth0EventHandler({ config, onSuccess, onError }) {
|
|
|
34
34
|
})
|
|
35
35
|
);
|
|
36
36
|
}
|
|
37
|
-
const tokens = await
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
client_secret: config.clientSecret,
|
|
48
|
-
redirect_uri: parsePath(redirectURL).pathname,
|
|
49
|
-
code
|
|
50
|
-
}
|
|
37
|
+
const tokens = await requestAccessToken(tokenURL, {
|
|
38
|
+
headers: {
|
|
39
|
+
"Content-Type": "application/json"
|
|
40
|
+
},
|
|
41
|
+
body: {
|
|
42
|
+
grant_type: "authorization_code",
|
|
43
|
+
client_id: config.clientId,
|
|
44
|
+
client_secret: config.clientSecret,
|
|
45
|
+
redirect_uri: redirectURL,
|
|
46
|
+
code: query.code
|
|
51
47
|
}
|
|
52
|
-
).catch((error) => {
|
|
53
|
-
return { error };
|
|
54
48
|
});
|
|
55
49
|
if (tokens.error) {
|
|
56
50
|
return handleAccessTokenErrorResponse(event, "auth0", tokens, onError);
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import { withQuery, parsePath } from "ufo";
|
|
1
|
+
import { eventHandler, getQuery, sendRedirect } from "h3";
|
|
2
|
+
import { withQuery } from "ufo";
|
|
4
3
|
import { defu } from "defu";
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
4
|
+
import { randomUUID } from "uncrypto";
|
|
5
|
+
import { handleMissingConfiguration, handleAccessTokenErrorResponse, getOAuthRedirectURL, requestAccessToken } from "../utils.js";
|
|
6
|
+
import { useRuntimeConfig, createError } from "#imports";
|
|
7
7
|
export function oauthBattledotnetEventHandler({ config, onSuccess, onError }) {
|
|
8
8
|
return eventHandler(async (event) => {
|
|
9
9
|
config = defu(config, useRuntimeConfig(event).oauth?.battledotnet, {
|
|
@@ -12,7 +12,6 @@ export function oauthBattledotnetEventHandler({ config, onSuccess, onError }) {
|
|
|
12
12
|
authorizationParams: {}
|
|
13
13
|
});
|
|
14
14
|
const query = getQuery(event);
|
|
15
|
-
const { code } = query;
|
|
16
15
|
if (query.error) {
|
|
17
16
|
const error = createError({
|
|
18
17
|
statusCode: 401,
|
|
@@ -30,8 +29,8 @@ export function oauthBattledotnetEventHandler({ config, onSuccess, onError }) {
|
|
|
30
29
|
onError
|
|
31
30
|
);
|
|
32
31
|
}
|
|
33
|
-
const redirectURL = config.redirectURL ||
|
|
34
|
-
if (!code) {
|
|
32
|
+
const redirectURL = config.redirectURL || getOAuthRedirectURL(event);
|
|
33
|
+
if (!query.code) {
|
|
35
34
|
config.scope = config.scope || ["openid"];
|
|
36
35
|
config.region = config.region || "EU";
|
|
37
36
|
if (config.region === "CN") {
|
|
@@ -55,24 +54,16 @@ export function oauthBattledotnetEventHandler({ config, onSuccess, onError }) {
|
|
|
55
54
|
if (!config.scope.includes("openid")) {
|
|
56
55
|
config.scope.push("openid");
|
|
57
56
|
}
|
|
58
|
-
const
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
params: {
|
|
68
|
-
code,
|
|
69
|
-
grant_type: "authorization_code",
|
|
70
|
-
scope: config.scope.join(" "),
|
|
71
|
-
redirect_uri: parsePath(redirectURL).pathname
|
|
72
|
-
}
|
|
57
|
+
const tokens = await requestAccessToken(config.tokenURL, {
|
|
58
|
+
headers: {
|
|
59
|
+
Authorization: `Basic ${Buffer.from(`${config.clientId}:${config.clientSecret}`).toString("base64")}`
|
|
60
|
+
},
|
|
61
|
+
params: {
|
|
62
|
+
grant_type: "authorization_code",
|
|
63
|
+
scope: config.scope.join(" "),
|
|
64
|
+
redirect_uri: redirectURL,
|
|
65
|
+
code: query.code
|
|
73
66
|
}
|
|
74
|
-
).catch((error) => {
|
|
75
|
-
return { error };
|
|
76
67
|
});
|
|
77
68
|
if (tokens.error) {
|
|
78
69
|
return handleAccessTokenErrorResponse(event, "battle.net", tokens, onError);
|
|
@@ -1,22 +1,22 @@
|
|
|
1
|
-
import { eventHandler, getQuery,
|
|
2
|
-
import { withQuery
|
|
1
|
+
import { eventHandler, getQuery, sendRedirect } from "h3";
|
|
2
|
+
import { withQuery } from "ufo";
|
|
3
3
|
import { defu } from "defu";
|
|
4
|
-
import { handleAccessTokenErrorResponse,
|
|
4
|
+
import { handleMissingConfiguration, handleAccessTokenErrorResponse, getOAuthRedirectURL, requestAccessToken } from "../utils.js";
|
|
5
5
|
import { useRuntimeConfig } from "#imports";
|
|
6
6
|
export function oauthCognitoEventHandler({ config, onSuccess, onError }) {
|
|
7
7
|
return eventHandler(async (event) => {
|
|
8
8
|
config = defu(config, useRuntimeConfig(event).oauth?.cognito, {
|
|
9
9
|
authorizationParams: {}
|
|
10
10
|
});
|
|
11
|
-
const { code } = getQuery(event);
|
|
12
11
|
if (!config.clientId || !config.clientSecret || !config.userPoolId || !config.region) {
|
|
13
12
|
return handleMissingConfiguration(event, "cognito", ["clientId", "clientSecret", "userPoolId", "region"], onError);
|
|
14
13
|
}
|
|
15
14
|
const urlBase = config?.domain || `${config.userPoolId}.auth.${config.region}.amazoncognito.com`;
|
|
16
15
|
const authorizationURL = `https://${urlBase}/oauth2/authorize`;
|
|
17
16
|
const tokenURL = `https://${urlBase}/oauth2/token`;
|
|
18
|
-
const
|
|
19
|
-
|
|
17
|
+
const query = getQuery(event);
|
|
18
|
+
const redirectURL = config.redirectURL || getOAuthRedirectURL(event);
|
|
19
|
+
if (!query.code) {
|
|
20
20
|
config.scope = config.scope || ["openid", "profile"];
|
|
21
21
|
return sendRedirect(
|
|
22
22
|
event,
|
|
@@ -29,18 +29,18 @@ export function oauthCognitoEventHandler({ config, onSuccess, onError }) {
|
|
|
29
29
|
})
|
|
30
30
|
);
|
|
31
31
|
}
|
|
32
|
-
const tokens = await
|
|
32
|
+
const tokens = await requestAccessToken(
|
|
33
33
|
tokenURL,
|
|
34
34
|
{
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
35
|
+
body: {
|
|
36
|
+
grant_type: "authorization_code",
|
|
37
|
+
client_id: config.clientId,
|
|
38
|
+
client_secret: config.clientSecret,
|
|
39
|
+
redirect_uri: redirectURL,
|
|
40
|
+
code: query.code
|
|
41
|
+
}
|
|
40
42
|
}
|
|
41
|
-
)
|
|
42
|
-
return { error };
|
|
43
|
-
});
|
|
43
|
+
);
|
|
44
44
|
if (tokens.error) {
|
|
45
45
|
return handleAccessTokenErrorResponse(event, "cognito", tokens, onError);
|
|
46
46
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { eventHandler, getQuery,
|
|
2
|
-
import { withQuery
|
|
1
|
+
import { eventHandler, getQuery, sendRedirect } from "h3";
|
|
2
|
+
import { withQuery } from "ufo";
|
|
3
3
|
import { defu } from "defu";
|
|
4
|
-
import { handleAccessTokenErrorResponse,
|
|
4
|
+
import { handleMissingConfiguration, handleAccessTokenErrorResponse, getOAuthRedirectURL, requestAccessToken } from "../utils.js";
|
|
5
5
|
import { useRuntimeConfig } from "#imports";
|
|
6
6
|
export function oauthDiscordEventHandler({ config, onSuccess, onError }) {
|
|
7
7
|
return eventHandler(async (event) => {
|
|
@@ -11,12 +11,12 @@ export function oauthDiscordEventHandler({ config, onSuccess, onError }) {
|
|
|
11
11
|
profileRequired: true,
|
|
12
12
|
authorizationParams: {}
|
|
13
13
|
});
|
|
14
|
-
const
|
|
14
|
+
const query = getQuery(event);
|
|
15
15
|
if (!config.clientId || !config.clientSecret) {
|
|
16
16
|
return handleMissingConfiguration(event, "discord", ["clientId", "clientSecret"], onError);
|
|
17
17
|
}
|
|
18
|
-
const redirectURL = config.redirectURL ||
|
|
19
|
-
if (!code) {
|
|
18
|
+
const redirectURL = config.redirectURL || getOAuthRedirectURL(event);
|
|
19
|
+
if (!query.code) {
|
|
20
20
|
config.scope = config.scope || [];
|
|
21
21
|
if (config.emailRequired && !config.scope.includes("email")) {
|
|
22
22
|
config.scope.push("email");
|
|
@@ -35,25 +35,14 @@ export function oauthDiscordEventHandler({ config, onSuccess, onError }) {
|
|
|
35
35
|
})
|
|
36
36
|
);
|
|
37
37
|
}
|
|
38
|
-
const
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
"Content-Type": "application/x-www-form-urlencoded"
|
|
46
|
-
},
|
|
47
|
-
body: new URLSearchParams({
|
|
48
|
-
client_id: config.clientId,
|
|
49
|
-
client_secret: config.clientSecret,
|
|
50
|
-
grant_type: "authorization_code",
|
|
51
|
-
redirect_uri: stringifyParsedURL(parsedRedirectUrl),
|
|
52
|
-
code
|
|
53
|
-
}).toString()
|
|
38
|
+
const tokens = await requestAccessToken(config.tokenURL, {
|
|
39
|
+
body: {
|
|
40
|
+
client_id: config.clientId,
|
|
41
|
+
client_secret: config.clientSecret,
|
|
42
|
+
grant_type: "authorization_code",
|
|
43
|
+
redirect_uri: redirectURL,
|
|
44
|
+
code: query.code
|
|
54
45
|
}
|
|
55
|
-
).catch((error) => {
|
|
56
|
-
return { error };
|
|
57
46
|
});
|
|
58
47
|
if (tokens.error) {
|
|
59
48
|
return handleAccessTokenErrorResponse(event, "discord", tokens, onError);
|
|
@@ -1,14 +1,8 @@
|
|
|
1
|
-
import {
|
|
2
|
-
eventHandler,
|
|
3
|
-
createError,
|
|
4
|
-
getQuery,
|
|
5
|
-
getRequestURL,
|
|
6
|
-
sendRedirect
|
|
7
|
-
} from "h3";
|
|
1
|
+
import { eventHandler, getQuery, sendRedirect } from "h3";
|
|
8
2
|
import { withQuery } from "ufo";
|
|
9
3
|
import { defu } from "defu";
|
|
10
|
-
import { handleAccessTokenErrorResponse,
|
|
11
|
-
import { useRuntimeConfig } from "#imports";
|
|
4
|
+
import { handleMissingConfiguration, handleAccessTokenErrorResponse, getOAuthRedirectURL, requestAccessToken } from "../utils.js";
|
|
5
|
+
import { useRuntimeConfig, createError } from "#imports";
|
|
12
6
|
export function oauthFacebookEventHandler({
|
|
13
7
|
config,
|
|
14
8
|
onSuccess,
|
|
@@ -33,7 +27,7 @@ export function oauthFacebookEventHandler({
|
|
|
33
27
|
if (!config.clientId) {
|
|
34
28
|
return handleMissingConfiguration(event, "facebook", ["clientId"], onError);
|
|
35
29
|
}
|
|
36
|
-
const redirectURL = config.redirectURL ||
|
|
30
|
+
const redirectURL = config.redirectURL || getOAuthRedirectURL(event);
|
|
37
31
|
if (!query.code) {
|
|
38
32
|
config.scope = config.scope || [];
|
|
39
33
|
return sendRedirect(
|
|
@@ -45,11 +39,11 @@ export function oauthFacebookEventHandler({
|
|
|
45
39
|
})
|
|
46
40
|
);
|
|
47
41
|
}
|
|
48
|
-
const tokens = await
|
|
49
|
-
method: "POST",
|
|
42
|
+
const tokens = await requestAccessToken(config.tokenURL, {
|
|
50
43
|
body: {
|
|
51
44
|
client_id: config.clientId,
|
|
52
45
|
client_secret: config.clientSecret,
|
|
46
|
+
grant_type: "authorization_code",
|
|
53
47
|
redirect_uri: redirectURL,
|
|
54
48
|
code: query.code
|
|
55
49
|
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { eventHandler,
|
|
1
|
+
import { eventHandler, getQuery, sendRedirect } from "h3";
|
|
2
2
|
import { withQuery } from "ufo";
|
|
3
3
|
import { defu } from "defu";
|
|
4
|
-
import { handleAccessTokenErrorResponse,
|
|
5
|
-
import { useRuntimeConfig } from "#imports";
|
|
4
|
+
import { handleMissingConfiguration, handleAccessTokenErrorResponse, getOAuthRedirectURL, requestAccessToken } from "../utils.js";
|
|
5
|
+
import { useRuntimeConfig, createError } from "#imports";
|
|
6
6
|
export function oauthGitHubEventHandler({ config, onSuccess, onError }) {
|
|
7
7
|
return eventHandler(async (event) => {
|
|
8
8
|
config = defu(config, useRuntimeConfig(event).oauth?.github, {
|
|
@@ -23,12 +23,12 @@ export function oauthGitHubEventHandler({ config, onSuccess, onError }) {
|
|
|
23
23
|
if (!config.clientId || !config.clientSecret) {
|
|
24
24
|
return handleMissingConfiguration(event, "github", ["clientId", "clientSecret"], onError);
|
|
25
25
|
}
|
|
26
|
+
const redirectURL = config.redirectURL || getOAuthRedirectURL(event);
|
|
26
27
|
if (!query.code) {
|
|
27
28
|
config.scope = config.scope || [];
|
|
28
29
|
if (config.emailRequired && !config.scope.includes("user:email")) {
|
|
29
30
|
config.scope.push("user:email");
|
|
30
31
|
}
|
|
31
|
-
const redirectURL = config.redirectURL || getRequestURL(event).href;
|
|
32
32
|
return sendRedirect(
|
|
33
33
|
event,
|
|
34
34
|
withQuery(config.authorizationURL, {
|
|
@@ -39,17 +39,15 @@ export function oauthGitHubEventHandler({ config, onSuccess, onError }) {
|
|
|
39
39
|
})
|
|
40
40
|
);
|
|
41
41
|
}
|
|
42
|
-
const tokens = await
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
code: query.code
|
|
50
|
-
}
|
|
42
|
+
const tokens = await requestAccessToken(config.tokenURL, {
|
|
43
|
+
body: {
|
|
44
|
+
grant_type: "authorization_code",
|
|
45
|
+
client_id: config.clientId,
|
|
46
|
+
client_secret: config.clientSecret,
|
|
47
|
+
redirect_uri: redirectURL,
|
|
48
|
+
code: query.code
|
|
51
49
|
}
|
|
52
|
-
);
|
|
50
|
+
});
|
|
53
51
|
if (tokens.error) {
|
|
54
52
|
return handleAccessTokenErrorResponse(event, "github", tokens, onError);
|
|
55
53
|
}
|
|
@@ -1,12 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
getQuery,
|
|
4
|
-
getRequestURL,
|
|
5
|
-
sendRedirect
|
|
6
|
-
} from "h3";
|
|
7
|
-
import { withQuery, parsePath } from "ufo";
|
|
1
|
+
import { eventHandler, getQuery, sendRedirect } from "h3";
|
|
2
|
+
import { withQuery } from "ufo";
|
|
8
3
|
import { defu } from "defu";
|
|
9
|
-
import { handleAccessTokenErrorResponse,
|
|
4
|
+
import { handleMissingConfiguration, handleAccessTokenErrorResponse, getOAuthRedirectURL, requestAccessToken } from "../utils.js";
|
|
10
5
|
import { useRuntimeConfig } from "#imports";
|
|
11
6
|
export function oauthGoogleEventHandler({
|
|
12
7
|
config,
|
|
@@ -20,12 +15,12 @@ export function oauthGoogleEventHandler({
|
|
|
20
15
|
userURL: "https://www.googleapis.com/oauth2/v3/userinfo",
|
|
21
16
|
authorizationParams: {}
|
|
22
17
|
});
|
|
23
|
-
const
|
|
18
|
+
const query = getQuery(event);
|
|
24
19
|
if (!config.clientId) {
|
|
25
20
|
return handleMissingConfiguration(event, "google", ["clientId"], onError);
|
|
26
21
|
}
|
|
27
|
-
const redirectURL = config.redirectURL ||
|
|
28
|
-
if (!code) {
|
|
22
|
+
const redirectURL = config.redirectURL || getOAuthRedirectURL(event);
|
|
23
|
+
if (!query.code) {
|
|
29
24
|
config.scope = config.scope || ["email", "profile"];
|
|
30
25
|
return sendRedirect(
|
|
31
26
|
event,
|
|
@@ -38,18 +33,14 @@ export function oauthGoogleEventHandler({
|
|
|
38
33
|
})
|
|
39
34
|
);
|
|
40
35
|
}
|
|
41
|
-
const
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
method: "POST",
|
|
50
|
-
body
|
|
51
|
-
}).catch((error) => {
|
|
52
|
-
return { error };
|
|
36
|
+
const tokens = await requestAccessToken(config.tokenURL, {
|
|
37
|
+
body: {
|
|
38
|
+
grant_type: "authorization_code",
|
|
39
|
+
code: query.code,
|
|
40
|
+
client_id: config.clientId,
|
|
41
|
+
client_secret: config.clientSecret,
|
|
42
|
+
redirect_uri: redirectURL
|
|
43
|
+
}
|
|
53
44
|
});
|
|
54
45
|
if (tokens.error) {
|
|
55
46
|
return handleAccessTokenErrorResponse(event, "google", tokens, onError);
|
|
@@ -1,14 +1,8 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
createError,
|
|
4
|
-
getQuery,
|
|
5
|
-
getRequestURL,
|
|
6
|
-
sendRedirect
|
|
7
|
-
} from "h3";
|
|
8
|
-
import { withQuery, parsePath } from "ufo";
|
|
1
|
+
import { eventHandler, getQuery, sendRedirect } from "h3";
|
|
2
|
+
import { withQuery } from "ufo";
|
|
9
3
|
import { defu } from "defu";
|
|
10
|
-
import { handleAccessTokenErrorResponse,
|
|
11
|
-
import { useRuntimeConfig } from "#imports";
|
|
4
|
+
import { handleMissingConfiguration, handleAccessTokenErrorResponse, getOAuthRedirectURL, requestAccessToken } from "../utils.js";
|
|
5
|
+
import { useRuntimeConfig, createError } from "#imports";
|
|
12
6
|
export function oauthKeycloakEventHandler({
|
|
13
7
|
config,
|
|
14
8
|
onSuccess,
|
|
@@ -19,7 +13,6 @@ export function oauthKeycloakEventHandler({
|
|
|
19
13
|
authorizationParams: {}
|
|
20
14
|
});
|
|
21
15
|
const query = getQuery(event);
|
|
22
|
-
const { code } = query;
|
|
23
16
|
if (query.error) {
|
|
24
17
|
const error = createError({
|
|
25
18
|
statusCode: 401,
|
|
@@ -35,8 +28,8 @@ export function oauthKeycloakEventHandler({
|
|
|
35
28
|
const realmURL = `${config.serverUrl}/realms/${config.realm}`;
|
|
36
29
|
const authorizationURL = `${realmURL}/protocol/openid-connect/auth`;
|
|
37
30
|
const tokenURL = `${realmURL}/protocol/openid-connect/token`;
|
|
38
|
-
const redirectURL = config.redirectURL ||
|
|
39
|
-
if (!code) {
|
|
31
|
+
const redirectURL = config.redirectURL || getOAuthRedirectURL(event);
|
|
32
|
+
if (!query.code) {
|
|
40
33
|
config.scope = config.scope || ["openid"];
|
|
41
34
|
return sendRedirect(
|
|
42
35
|
event,
|
|
@@ -53,20 +46,14 @@ export function oauthKeycloakEventHandler({
|
|
|
53
46
|
if (!config.scope.includes("openid")) {
|
|
54
47
|
config.scope.push("openid");
|
|
55
48
|
}
|
|
56
|
-
const tokens = await
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
"Content-Type": "application/x-www-form-urlencoded"
|
|
60
|
-
},
|
|
61
|
-
body: new URLSearchParams({
|
|
49
|
+
const tokens = await requestAccessToken(tokenURL, {
|
|
50
|
+
body: {
|
|
51
|
+
grant_type: "authorization_code",
|
|
62
52
|
client_id: config.clientId,
|
|
63
53
|
client_secret: config.clientSecret,
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
}).toString()
|
|
68
|
-
}).catch((error) => {
|
|
69
|
-
return { error };
|
|
54
|
+
redirect_uri: redirectURL,
|
|
55
|
+
code: query.code
|
|
56
|
+
}
|
|
70
57
|
});
|
|
71
58
|
if (tokens.error) {
|
|
72
59
|
return handleAccessTokenErrorResponse(event, "keycloak", tokens, onError);
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { OAuthConfig } from '#auth-utils';
|
|
2
2
|
export interface OAuthLinkedInConfig {
|
|
3
3
|
/**
|
|
4
4
|
* LinkedIn OAuth Client ID
|
|
@@ -42,13 +42,4 @@ export interface OAuthLinkedInConfig {
|
|
|
42
42
|
*/
|
|
43
43
|
redirectURL?: string;
|
|
44
44
|
}
|
|
45
|
-
|
|
46
|
-
config?: OAuthLinkedInConfig;
|
|
47
|
-
onSuccess: (event: H3Event, result: {
|
|
48
|
-
user: any;
|
|
49
|
-
tokens: any;
|
|
50
|
-
}) => Promise<void> | void;
|
|
51
|
-
onError?: (event: H3Event, error: H3Error) => Promise<void> | void;
|
|
52
|
-
}
|
|
53
|
-
export declare function oauthLinkedInEventHandler({ config, onSuccess, onError }: OAuthConfig): import("h3").EventHandler<import("h3").EventHandlerRequest, Promise<void>>;
|
|
54
|
-
export {};
|
|
45
|
+
export declare function oauthLinkedInEventHandler({ config, onSuccess, onError }: OAuthConfig<OAuthLinkedInConfig>): import("h3").EventHandler<import("h3").EventHandlerRequest, Promise<void>>;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { eventHandler, getQuery,
|
|
2
|
-
import { withQuery
|
|
1
|
+
import { eventHandler, getQuery, sendRedirect } from "h3";
|
|
2
|
+
import { withQuery } from "ufo";
|
|
3
3
|
import { defu } from "defu";
|
|
4
|
-
import { handleAccessTokenErrorResponse,
|
|
4
|
+
import { handleMissingConfiguration, handleAccessTokenErrorResponse, getOAuthRedirectURL, requestAccessToken } from "../utils.js";
|
|
5
5
|
import { useRuntimeConfig } from "#imports";
|
|
6
6
|
export function oauthLinkedInEventHandler({ config, onSuccess, onError }) {
|
|
7
7
|
return eventHandler(async (event) => {
|
|
@@ -10,12 +10,12 @@ export function oauthLinkedInEventHandler({ config, onSuccess, onError }) {
|
|
|
10
10
|
tokenURL: "https://www.linkedin.com/oauth/v2/accessToken",
|
|
11
11
|
authorizationParams: {}
|
|
12
12
|
});
|
|
13
|
-
const
|
|
13
|
+
const query = getQuery(event);
|
|
14
14
|
if (!config.clientId || !config.clientSecret) {
|
|
15
15
|
return handleMissingConfiguration(event, "linkedin", ["clientId", "clientSecret"], onError);
|
|
16
16
|
}
|
|
17
|
-
const redirectURL = config.redirectURL ||
|
|
18
|
-
if (!code) {
|
|
17
|
+
const redirectURL = config.redirectURL || getOAuthRedirectURL(event);
|
|
18
|
+
if (!query.code) {
|
|
19
19
|
config.scope = config.scope || [];
|
|
20
20
|
if (!config.scope.length) {
|
|
21
21
|
config.scope.push("profile", "openid", "email");
|
|
@@ -34,25 +34,14 @@ export function oauthLinkedInEventHandler({ config, onSuccess, onError }) {
|
|
|
34
34
|
})
|
|
35
35
|
);
|
|
36
36
|
}
|
|
37
|
-
const
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
"Content-Type": "application/x-www-form-urlencoded"
|
|
45
|
-
},
|
|
46
|
-
body: new URLSearchParams({
|
|
47
|
-
grant_type: "authorization_code",
|
|
48
|
-
code,
|
|
49
|
-
client_id: config.clientId,
|
|
50
|
-
client_secret: config.clientSecret,
|
|
51
|
-
redirect_uri: stringifyParsedURL(parsedRedirectUrl)
|
|
52
|
-
}).toString()
|
|
37
|
+
const tokens = await requestAccessToken(config.tokenURL, {
|
|
38
|
+
body: {
|
|
39
|
+
grant_type: "authorization_code",
|
|
40
|
+
code: query.code,
|
|
41
|
+
client_id: config.clientId,
|
|
42
|
+
client_secret: config.clientSecret,
|
|
43
|
+
redirect_uri: redirectURL
|
|
53
44
|
}
|
|
54
|
-
).catch((error) => {
|
|
55
|
-
return { error };
|
|
56
45
|
});
|
|
57
46
|
if (tokens.error) {
|
|
58
47
|
return handleAccessTokenErrorResponse(event, "linkedin", tokens, onError);
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { OAuthConfig } from '#auth-utils';
|
|
2
2
|
export interface OAuthMicrosoftConfig {
|
|
3
3
|
/**
|
|
4
4
|
* Microsoft OAuth Client ID
|
|
@@ -51,13 +51,4 @@ export interface OAuthMicrosoftConfig {
|
|
|
51
51
|
*/
|
|
52
52
|
redirectURL?: string;
|
|
53
53
|
}
|
|
54
|
-
|
|
55
|
-
config?: OAuthMicrosoftConfig;
|
|
56
|
-
onSuccess: (event: H3Event, result: {
|
|
57
|
-
user: any;
|
|
58
|
-
tokens: any;
|
|
59
|
-
}) => Promise<void> | void;
|
|
60
|
-
onError?: (event: H3Event, error: H3Error) => Promise<void> | void;
|
|
61
|
-
}
|
|
62
|
-
export declare function oauthMicrosoftEventHandler({ config, onSuccess, onError }: OAuthConfig): import("h3").EventHandler<import("h3").EventHandlerRequest, Promise<void>>;
|
|
63
|
-
export {};
|
|
54
|
+
export declare function oauthMicrosoftEventHandler({ config, onSuccess, onError }: OAuthConfig<OAuthMicrosoftConfig>): import("h3").EventHandler<import("h3").EventHandlerRequest, Promise<void>>;
|