@aura-stack/auth 0.1.0-rc.9 → 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/dist/@types/index.cjs +15 -15
- package/dist/@types/index.d.ts +6 -31
- package/dist/@types/index.js +1 -1
- package/dist/@types/router.d.cjs +1 -1
- package/dist/@types/router.d.d.ts +6 -7
- package/dist/@types/utility.cjs +15 -15
- package/dist/@types/utility.d.ts +4 -8
- package/dist/@types/utility.js +1 -1
- package/dist/actions/callback/access-token.cjs +159 -149
- package/dist/actions/callback/access-token.d.ts +14 -20
- package/dist/actions/callback/access-token.js +9 -4
- package/dist/actions/callback/callback.cjs +371 -484
- package/dist/actions/callback/callback.d.ts +9 -12
- package/dist/actions/callback/callback.js +15 -12
- package/dist/actions/callback/userinfo.cjs +149 -139
- package/dist/actions/callback/userinfo.d.ts +8 -9
- package/dist/actions/callback/userinfo.js +11 -6
- package/dist/actions/csrfToken/csrfToken.cjs +112 -190
- package/dist/actions/csrfToken/csrfToken.d.ts +3 -3
- package/dist/actions/csrfToken/csrfToken.js +12 -8
- package/dist/actions/index.cjs +623 -807
- package/dist/actions/index.d.ts +12 -13
- package/dist/actions/index.js +33 -18
- package/dist/actions/session/session.cjs +86 -176
- package/dist/actions/session/session.d.ts +3 -3
- package/dist/actions/session/session.js +10 -7
- package/dist/actions/signIn/authorization.cjs +248 -257
- package/dist/actions/signIn/authorization.d.ts +11 -18
- package/dist/actions/signIn/authorization.js +16 -6
- package/dist/actions/signIn/signIn.cjs +302 -451
- package/dist/actions/signIn/signIn.d.ts +9 -10
- package/dist/actions/signIn/signIn.js +12 -10
- package/dist/actions/signOut/signOut.cjs +299 -468
- package/dist/actions/signOut/signOut.d.ts +3 -3
- package/dist/actions/signOut/signOut.js +14 -11
- package/dist/assert.cjs +40 -36
- package/dist/assert.d.ts +12 -4
- package/dist/assert.js +12 -2
- package/dist/chunk-2RXNXMCZ.js +55 -0
- package/dist/chunk-42XB3YCW.js +19 -17
- package/dist/chunk-4V4JNXVF.js +55 -0
- package/dist/chunk-6R2YZ4AC.js +22 -0
- package/dist/chunk-7H3OR6UU.js +81 -0
- package/dist/chunk-CXLATHS5.js +143 -0
- package/dist/chunk-E3OXBRYF.js +19 -17
- package/dist/chunk-EIL2FPSS.js +22 -0
- package/dist/chunk-FIPU4MLT.js +18 -16
- package/dist/chunk-FKRDCWBF.js +19 -17
- package/dist/chunk-IKHPGFCW.js +11 -9
- package/dist/chunk-IMICRJ5U.js +197 -0
- package/dist/chunk-IUYZQTJV.js +30 -0
- package/dist/chunk-KRNOMBXQ.js +19 -17
- package/dist/chunk-N2APGLXA.js +71 -0
- package/dist/chunk-NEVKX6K2.js +70 -0
- package/dist/chunk-PTJUYB33.js +33 -0
- package/dist/chunk-QDO2KSRJ.js +35 -0
- package/dist/chunk-QEZL7EYN.js +96 -0
- package/dist/chunk-RRLIF4PQ.js +55 -0
- package/dist/chunk-STHEPPUZ.js +8 -6
- package/dist/chunk-TLE4PXY3.js +39 -0
- package/dist/chunk-UEH3LVON.js +97 -0
- package/dist/chunk-WD7AUHQ5.js +79 -0
- package/dist/chunk-ZLR3LI6X.js +55 -0
- package/dist/cookie.cjs +222 -187
- package/dist/cookie.d.ts +38 -76
- package/dist/cookie.js +27 -34
- package/dist/errors.cjs +85 -0
- package/dist/errors.d.ts +48 -0
- package/dist/errors.js +18 -0
- package/dist/headers.cjs +28 -28
- package/dist/headers.d.ts +2 -2
- package/dist/headers.js +6 -2
- package/dist/{index-DpfbvTZ_.d.ts → index-EqsoyjrF.d.ts} +351 -318
- package/dist/index.cjs +1006 -1001
- package/dist/index.d.ts +9 -31
- package/dist/index.js +82 -51
- package/dist/jose.cjs +74 -66
- package/dist/jose.d.ts +11 -8
- package/dist/jose.js +10 -5
- package/dist/oauth/bitbucket.cjs +38 -38
- package/dist/oauth/bitbucket.d.ts +6 -7
- package/dist/oauth/bitbucket.js +6 -2
- package/dist/oauth/discord.cjs +47 -48
- package/dist/oauth/discord.d.ts +6 -7
- package/dist/oauth/discord.js +6 -2
- package/dist/oauth/figma.cjs +39 -39
- package/dist/oauth/figma.d.ts +6 -7
- package/dist/oauth/figma.js +6 -2
- package/dist/oauth/github.cjs +31 -31
- package/dist/oauth/github.d.ts +6 -7
- package/dist/oauth/github.js +6 -2
- package/dist/oauth/gitlab.cjs +39 -39
- package/dist/oauth/gitlab.d.ts +6 -7
- package/dist/oauth/gitlab.js +6 -2
- package/dist/oauth/index.cjs +306 -180
- package/dist/oauth/index.d.ts +6 -26
- package/dist/oauth/index.js +43 -9
- package/dist/oauth/spotify.cjs +39 -39
- package/dist/oauth/spotify.d.ts +6 -7
- package/dist/oauth/spotify.js +6 -2
- package/dist/oauth/strava.cjs +46 -0
- package/dist/oauth/strava.d.ts +6 -0
- package/dist/oauth/strava.js +6 -0
- package/dist/oauth/x.cjs +39 -39
- package/dist/oauth/x.d.ts +6 -7
- package/dist/oauth/x.js +6 -2
- package/dist/schemas.cjs +97 -91
- package/dist/schemas.d.ts +96 -126
- package/dist/schemas.js +20 -18
- package/dist/secure.cjs +106 -101
- package/dist/secure.d.ts +16 -17
- package/dist/secure.js +19 -4
- package/dist/utils.cjs +197 -135
- package/dist/utils.d.ts +20 -26
- package/dist/utils.js +25 -21
- package/package.json +5 -6
- package/dist/chunk-256KIVJL.js +0 -110
- package/dist/chunk-6SM22VVJ.js +0 -15
- package/dist/chunk-CAKJT3KS.js +0 -77
- package/dist/chunk-EBPE35JT.js +0 -29
- package/dist/chunk-FJUDBLCP.js +0 -52
- package/dist/chunk-GZU3RBTB.js +0 -51
- package/dist/chunk-HGJ4TXY4.js +0 -105
- package/dist/chunk-HMRKN75I.js +0 -74
- package/dist/chunk-JAPMIE6S.js +0 -8
- package/dist/chunk-LLR722CL.js +0 -75
- package/dist/chunk-RLT4RFKV.js +0 -36
- package/dist/chunk-SJPDVKUS.js +0 -93
- package/dist/chunk-SMQO5WD7.js +0 -20
- package/dist/chunk-UJJ7R56J.js +0 -42
- package/dist/chunk-UTDLUEEG.js +0 -25
- package/dist/chunk-VFTYH33W.js +0 -44
- package/dist/chunk-XXJKNKGQ.js +0 -27
- package/dist/chunk-ZV4BH47P.js +0 -156
- package/dist/error.cjs +0 -88
- package/dist/error.d.ts +0 -62
- package/dist/error.js +0 -9
- package/dist/response.cjs +0 -34
- package/dist/response.d.ts +0 -10
- package/dist/response.js +0 -2
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
// src/errors.ts
|
|
2
|
+
var OAuthProtocolError = class extends Error {
|
|
3
|
+
type = "OAUTH_PROTOCOL_ERROR";
|
|
4
|
+
error;
|
|
5
|
+
errorURI;
|
|
6
|
+
constructor(error, description, errorURI, options) {
|
|
7
|
+
super(description, options);
|
|
8
|
+
this.error = error;
|
|
9
|
+
this.errorURI = errorURI;
|
|
10
|
+
this.name = new.target.name;
|
|
11
|
+
Error.captureStackTrace(this, new.target);
|
|
12
|
+
}
|
|
13
|
+
};
|
|
14
|
+
var AuthInternalError = class extends Error {
|
|
15
|
+
type = "AUTH_INTERNAL_ERROR";
|
|
16
|
+
code;
|
|
17
|
+
constructor(code, message, options) {
|
|
18
|
+
super(message, options);
|
|
19
|
+
this.code = code;
|
|
20
|
+
this.name = new.target.name;
|
|
21
|
+
Error.captureStackTrace(this, new.target);
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
var AuthSecurityError = class extends Error {
|
|
25
|
+
type = "AUTH_SECURITY_ERROR";
|
|
26
|
+
code;
|
|
27
|
+
constructor(code, message, options) {
|
|
28
|
+
super(message, options);
|
|
29
|
+
this.code = code;
|
|
30
|
+
this.name = new.target.name;
|
|
31
|
+
Error.captureStackTrace(this, new.target);
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
var isNativeError = (error) => {
|
|
35
|
+
return error instanceof Error;
|
|
36
|
+
};
|
|
37
|
+
var isOAuthProtocolError = (error) => {
|
|
38
|
+
return error instanceof OAuthProtocolError;
|
|
39
|
+
};
|
|
40
|
+
var isAuthInternalError = (error) => {
|
|
41
|
+
return error instanceof AuthInternalError;
|
|
42
|
+
};
|
|
43
|
+
var isAuthSecurityError = (error) => {
|
|
44
|
+
return error instanceof AuthSecurityError;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
export {
|
|
48
|
+
OAuthProtocolError,
|
|
49
|
+
AuthInternalError,
|
|
50
|
+
AuthSecurityError,
|
|
51
|
+
isNativeError,
|
|
52
|
+
isOAuthProtocolError,
|
|
53
|
+
isAuthInternalError,
|
|
54
|
+
isAuthSecurityError
|
|
55
|
+
};
|
package/dist/chunk-STHEPPUZ.js
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
// src/headers.ts
|
|
2
2
|
var cacheControl = {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
}
|
|
3
|
+
"Cache-Control": "no-store",
|
|
4
|
+
Pragma: "no-cache",
|
|
5
|
+
Expires: "0",
|
|
6
|
+
Vary: "Cookie"
|
|
7
|
+
};
|
|
8
8
|
|
|
9
|
-
export {
|
|
9
|
+
export {
|
|
10
|
+
cacheControl
|
|
11
|
+
};
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createDerivedSalt
|
|
3
|
+
} from "./chunk-N2APGLXA.js";
|
|
4
|
+
import {
|
|
5
|
+
AuthInternalError
|
|
6
|
+
} from "./chunk-RRLIF4PQ.js";
|
|
7
|
+
|
|
8
|
+
// src/jose.ts
|
|
9
|
+
import "dotenv/config";
|
|
10
|
+
import { createJWT, createJWS, createJWE, createDeriveKey } from "@aura-stack/jose";
|
|
11
|
+
var createJoseInstance = (secret) => {
|
|
12
|
+
const env = process.env;
|
|
13
|
+
secret ??= env.AURA_AUTH_SECRET ?? env.AUTH_SECRET;
|
|
14
|
+
if (!secret) {
|
|
15
|
+
throw new AuthInternalError(
|
|
16
|
+
"JOSE_INITIALIZATION_FAILED",
|
|
17
|
+
"AURA_AUTH_SECRET environment variable is not set and no secret was provided."
|
|
18
|
+
);
|
|
19
|
+
}
|
|
20
|
+
const salt = env.AURA_AUTH_SALT ?? env.AUTH_SALT ?? createDerivedSalt(secret);
|
|
21
|
+
const { derivedKey: derivedSigningKey } = createDeriveKey(secret, salt, "signing");
|
|
22
|
+
const { derivedKey: derivedEncryptionKey } = createDeriveKey(secret, salt, "encryption");
|
|
23
|
+
const { derivedKey: derivedCsrfTokenKey } = createDeriveKey(secret, salt, "csrfToken");
|
|
24
|
+
const { decodeJWT, encodeJWT } = createJWT({ jws: derivedSigningKey, jwe: derivedEncryptionKey });
|
|
25
|
+
const { signJWS, verifyJWS } = createJWS(derivedCsrfTokenKey);
|
|
26
|
+
const { encryptJWE, decryptJWE } = createJWE(derivedEncryptionKey);
|
|
27
|
+
return {
|
|
28
|
+
decodeJWT,
|
|
29
|
+
encodeJWT,
|
|
30
|
+
signJWS,
|
|
31
|
+
verifyJWS,
|
|
32
|
+
encryptJWE,
|
|
33
|
+
decryptJWE
|
|
34
|
+
};
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
export {
|
|
38
|
+
createJoseInstance
|
|
39
|
+
};
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import {
|
|
2
|
+
getUserInfo
|
|
3
|
+
} from "./chunk-ZLR3LI6X.js";
|
|
4
|
+
import {
|
|
5
|
+
createAccessToken
|
|
6
|
+
} from "./chunk-4V4JNXVF.js";
|
|
7
|
+
import {
|
|
8
|
+
createSessionCookie,
|
|
9
|
+
expiredCookieAttributes,
|
|
10
|
+
getCookie
|
|
11
|
+
} from "./chunk-IMICRJ5U.js";
|
|
12
|
+
import {
|
|
13
|
+
cacheControl
|
|
14
|
+
} from "./chunk-STHEPPUZ.js";
|
|
15
|
+
import {
|
|
16
|
+
OAuthAuthorizationErrorResponse,
|
|
17
|
+
OAuthAuthorizationResponse
|
|
18
|
+
} from "./chunk-WD7AUHQ5.js";
|
|
19
|
+
import {
|
|
20
|
+
createCSRF
|
|
21
|
+
} from "./chunk-N2APGLXA.js";
|
|
22
|
+
import {
|
|
23
|
+
equals,
|
|
24
|
+
isValidRelativePath,
|
|
25
|
+
sanitizeURL
|
|
26
|
+
} from "./chunk-CXLATHS5.js";
|
|
27
|
+
import {
|
|
28
|
+
AuthSecurityError,
|
|
29
|
+
OAuthProtocolError
|
|
30
|
+
} from "./chunk-RRLIF4PQ.js";
|
|
31
|
+
|
|
32
|
+
// src/actions/callback/callback.ts
|
|
33
|
+
import z from "zod";
|
|
34
|
+
import { createEndpoint, createEndpointConfig, HeadersBuilder } from "@aura-stack/router";
|
|
35
|
+
var callbackConfig = (oauth) => {
|
|
36
|
+
return createEndpointConfig("/callback/:oauth", {
|
|
37
|
+
schemas: {
|
|
38
|
+
searchParams: OAuthAuthorizationResponse,
|
|
39
|
+
params: z.object({
|
|
40
|
+
oauth: z.enum(Object.keys(oauth), "The OAuth provider is not supported or invalid.")
|
|
41
|
+
})
|
|
42
|
+
},
|
|
43
|
+
middlewares: [
|
|
44
|
+
(ctx) => {
|
|
45
|
+
const response = OAuthAuthorizationErrorResponse.safeParse(ctx.searchParams);
|
|
46
|
+
if (response.success) {
|
|
47
|
+
const { error, error_description } = response.data;
|
|
48
|
+
throw new OAuthProtocolError(error, error_description ?? "OAuth Authorization Error");
|
|
49
|
+
}
|
|
50
|
+
return ctx;
|
|
51
|
+
}
|
|
52
|
+
]
|
|
53
|
+
});
|
|
54
|
+
};
|
|
55
|
+
var callbackAction = (oauth) => {
|
|
56
|
+
return createEndpoint(
|
|
57
|
+
"GET",
|
|
58
|
+
"/callback/:oauth",
|
|
59
|
+
async (ctx) => {
|
|
60
|
+
const {
|
|
61
|
+
request,
|
|
62
|
+
params: { oauth: oauth2 },
|
|
63
|
+
searchParams: { code, state },
|
|
64
|
+
context: { oauth: providers, cookies, jose }
|
|
65
|
+
} = ctx;
|
|
66
|
+
const oauthConfig = providers[oauth2];
|
|
67
|
+
const cookieState = getCookie(request, cookies.state.name);
|
|
68
|
+
const cookieRedirectTo = getCookie(request, cookies.redirect_to.name);
|
|
69
|
+
const cookieRedirectURI = getCookie(request, cookies.redirect_uri.name);
|
|
70
|
+
const codeVerifier = getCookie(request, cookies.code_verifier.name);
|
|
71
|
+
if (!equals(cookieState, state)) {
|
|
72
|
+
throw new AuthSecurityError(
|
|
73
|
+
"MISMATCHING_STATE",
|
|
74
|
+
"The provided state passed in the OAuth response does not match the stored state."
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
const accessToken = await createAccessToken(oauthConfig, cookieRedirectURI, code, codeVerifier);
|
|
78
|
+
const sanitized = sanitizeURL(cookieRedirectTo);
|
|
79
|
+
if (!isValidRelativePath(sanitized)) {
|
|
80
|
+
throw new AuthSecurityError(
|
|
81
|
+
"POTENTIAL_OPEN_REDIRECT_ATTACK_DETECTED",
|
|
82
|
+
"Invalid redirect path. Potential open redirect attack detected."
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
const userInfo = await getUserInfo(oauthConfig, accessToken.access_token);
|
|
86
|
+
const sessionCookie = await createSessionCookie(jose, userInfo);
|
|
87
|
+
const csrfToken = await createCSRF(jose);
|
|
88
|
+
const headers = new HeadersBuilder(cacheControl).setHeader("Location", sanitized).setCookie(cookies.sessionToken.name, sessionCookie, cookies.sessionToken.attributes).setCookie(cookies.csrfToken.name, csrfToken, cookies.csrfToken.attributes).setCookie(cookies.state.name, "", expiredCookieAttributes).setCookie(cookies.redirect_uri.name, "", expiredCookieAttributes).setCookie(cookies.redirect_to.name, "", expiredCookieAttributes).setCookie(cookies.code_verifier.name, "", expiredCookieAttributes).toHeaders();
|
|
89
|
+
return Response.json({ oauth: oauth2 }, { status: 302, headers });
|
|
90
|
+
},
|
|
91
|
+
callbackConfig(oauth)
|
|
92
|
+
);
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
export {
|
|
96
|
+
callbackAction
|
|
97
|
+
};
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
// src/schemas.ts
|
|
2
|
+
import { object, string, enum as options, number, httpUrl, z } from "zod/v4";
|
|
3
|
+
var OAuthProviderConfigSchema = object({
|
|
4
|
+
authorizeURL: httpUrl(),
|
|
5
|
+
accessToken: httpUrl(),
|
|
6
|
+
scope: string().optional(),
|
|
7
|
+
userInfo: httpUrl(),
|
|
8
|
+
responseType: options(["code", "token", "id_token"]),
|
|
9
|
+
clientId: string(),
|
|
10
|
+
clientSecret: string()
|
|
11
|
+
});
|
|
12
|
+
var OAuthAuthorization = OAuthProviderConfigSchema.extend({
|
|
13
|
+
redirectURI: string(),
|
|
14
|
+
state: string(),
|
|
15
|
+
codeChallenge: string(),
|
|
16
|
+
codeChallengeMethod: options(["plain", "S256"])
|
|
17
|
+
});
|
|
18
|
+
var OAuthAuthorizationResponse = object({
|
|
19
|
+
state: string("Missing state parameter in the OAuth authorization response."),
|
|
20
|
+
code: string("Missing code parameter in the OAuth authorization response.")
|
|
21
|
+
});
|
|
22
|
+
var OAuthAuthorizationErrorResponse = object({
|
|
23
|
+
error: options([
|
|
24
|
+
"invalid_request",
|
|
25
|
+
"unauthorized_client",
|
|
26
|
+
"access_denied",
|
|
27
|
+
"unsupported_response_type",
|
|
28
|
+
"invalid_scope",
|
|
29
|
+
"server_error",
|
|
30
|
+
"temporarily_unavailable"
|
|
31
|
+
]),
|
|
32
|
+
error_description: string().optional(),
|
|
33
|
+
error_uri: string().optional(),
|
|
34
|
+
state: string()
|
|
35
|
+
});
|
|
36
|
+
var OAuthAccessToken = OAuthProviderConfigSchema.extend({
|
|
37
|
+
redirectURI: string(),
|
|
38
|
+
code: string(),
|
|
39
|
+
codeVerifier: string().min(43).max(128)
|
|
40
|
+
});
|
|
41
|
+
var OAuthAccessTokenResponse = object({
|
|
42
|
+
access_token: string(),
|
|
43
|
+
token_type: string(),
|
|
44
|
+
expires_in: number().optional(),
|
|
45
|
+
refresh_token: string().optional(),
|
|
46
|
+
scope: string().optional()
|
|
47
|
+
});
|
|
48
|
+
var OAuthAccessTokenErrorResponse = object({
|
|
49
|
+
error: options([
|
|
50
|
+
"invalid_request",
|
|
51
|
+
"invalid_client",
|
|
52
|
+
"invalid_grant",
|
|
53
|
+
"unauthorized_client",
|
|
54
|
+
"unsupported_grant_type",
|
|
55
|
+
"invalid_scope"
|
|
56
|
+
]),
|
|
57
|
+
error_description: string().optional(),
|
|
58
|
+
error_uri: string().optional()
|
|
59
|
+
});
|
|
60
|
+
var OAuthErrorResponse = object({
|
|
61
|
+
error: string(),
|
|
62
|
+
error_description: string().optional()
|
|
63
|
+
});
|
|
64
|
+
var OAuthEnvSchema = object({
|
|
65
|
+
clientId: z.string().min(1, "OAuth Client ID is required in the environment variables."),
|
|
66
|
+
clientSecret: z.string().min(1, "OAuth Client Secret is required in the environment variables.")
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
export {
|
|
70
|
+
OAuthProviderConfigSchema,
|
|
71
|
+
OAuthAuthorization,
|
|
72
|
+
OAuthAuthorizationResponse,
|
|
73
|
+
OAuthAuthorizationErrorResponse,
|
|
74
|
+
OAuthAccessToken,
|
|
75
|
+
OAuthAccessTokenResponse,
|
|
76
|
+
OAuthAccessTokenErrorResponse,
|
|
77
|
+
OAuthErrorResponse,
|
|
78
|
+
OAuthEnvSchema
|
|
79
|
+
};
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import {
|
|
2
|
+
OAuthErrorResponse
|
|
3
|
+
} from "./chunk-WD7AUHQ5.js";
|
|
4
|
+
import {
|
|
5
|
+
generateSecure
|
|
6
|
+
} from "./chunk-N2APGLXA.js";
|
|
7
|
+
import {
|
|
8
|
+
OAuthProtocolError,
|
|
9
|
+
isNativeError,
|
|
10
|
+
isOAuthProtocolError
|
|
11
|
+
} from "./chunk-RRLIF4PQ.js";
|
|
12
|
+
|
|
13
|
+
// src/actions/callback/userinfo.ts
|
|
14
|
+
var getDefaultUserInfo = (profile) => {
|
|
15
|
+
const sub = generateSecure(16);
|
|
16
|
+
return {
|
|
17
|
+
sub: profile?.id ?? profile?.sub ?? sub,
|
|
18
|
+
email: profile?.email,
|
|
19
|
+
name: profile?.name ?? profile?.username ?? profile?.nickname,
|
|
20
|
+
image: profile?.image ?? profile?.picture
|
|
21
|
+
};
|
|
22
|
+
};
|
|
23
|
+
var getUserInfo = async (oauthConfig, accessToken) => {
|
|
24
|
+
const userinfoEndpoint = oauthConfig.userInfo;
|
|
25
|
+
try {
|
|
26
|
+
const response = await fetch(userinfoEndpoint, {
|
|
27
|
+
method: "GET",
|
|
28
|
+
headers: {
|
|
29
|
+
Accept: "application/json",
|
|
30
|
+
Authorization: `Bearer ${accessToken}`
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
const json = await response.json();
|
|
34
|
+
const { success, data } = OAuthErrorResponse.safeParse(json);
|
|
35
|
+
if (success) {
|
|
36
|
+
throw new OAuthProtocolError(
|
|
37
|
+
data.error,
|
|
38
|
+
data?.error_description ?? "An error occurred while fetching user information."
|
|
39
|
+
);
|
|
40
|
+
}
|
|
41
|
+
return oauthConfig?.profile ? oauthConfig.profile(json) : getDefaultUserInfo(json);
|
|
42
|
+
} catch (error) {
|
|
43
|
+
if (isOAuthProtocolError(error)) {
|
|
44
|
+
throw error;
|
|
45
|
+
}
|
|
46
|
+
if (isNativeError(error)) {
|
|
47
|
+
throw new OAuthProtocolError("invalid_request", error.message, "", { cause: error });
|
|
48
|
+
}
|
|
49
|
+
throw new OAuthProtocolError("invalid_request", "Failed to fetch user information.", "", { cause: error });
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
export {
|
|
54
|
+
getUserInfo
|
|
55
|
+
};
|