@gatwech/core 0.0.11 → 1.0.0-beta.18

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.
Files changed (130) hide show
  1. package/dist/api/index.d.mts +2 -2
  2. package/dist/context/index.d.mts +2 -2
  3. package/dist/db/adapter/index.d.mts +2 -2
  4. package/dist/db/index.d.mts +2 -2
  5. package/dist/env/index.d.mts +1 -1
  6. package/dist/env/index.mjs +1 -1
  7. package/dist/{env-JMSMESs1.mjs → env-4_Lym7sj.mjs} +7 -7
  8. package/dist/index-65G5U5DB.d.mts +3002 -0
  9. package/dist/{index-DLP-K0uY.d.mts → index-CmdhhvA2.d.mts} +3 -3
  10. package/dist/index.d.mts +3 -3
  11. package/dist/oauth2/index.d.mts +2 -2
  12. package/dist/social-providers/index.d.mts +3 -3
  13. package/dist/social-providers/index.mjs +71 -2412
  14. package/dist/src/api/index.d.ts +119 -0
  15. package/dist/src/async_hooks/index.d.ts +6 -0
  16. package/dist/src/context/endpoint-context.d.ts +14 -0
  17. package/dist/src/context/index.d.ts +3 -0
  18. package/dist/src/context/request-state.d.ts +13 -0
  19. package/dist/src/context/transaction.d.ts +11 -0
  20. package/dist/src/db/adapter/index.d.ts +401 -0
  21. package/{src/db/index.ts → dist/src/db/index.d.ts} +2 -18
  22. package/dist/src/db/plugin.d.ts +10 -0
  23. package/dist/src/db/schema/account.d.ts +22 -0
  24. package/dist/src/db/schema/rate-limit.d.ts +10 -0
  25. package/dist/src/db/schema/session.d.ts +17 -0
  26. package/dist/src/db/schema/shared.d.ts +6 -0
  27. package/dist/src/db/schema/user.d.ts +16 -0
  28. package/{src/db/schema/verification.ts → dist/src/db/schema/verification.d.ts} +8 -8
  29. package/dist/src/db/type.d.ts +159 -0
  30. package/dist/src/env/color-depth.d.ts +1 -0
  31. package/dist/src/env/env-impl.d.ts +29 -0
  32. package/dist/src/env/index.d.ts +3 -0
  33. package/dist/src/env/logger.d.ts +50 -0
  34. package/dist/src/error/codes.d.ts +27 -0
  35. package/dist/src/error/index.d.ts +4 -0
  36. package/dist/src/oauth2/client-credentials-token.d.ts +21 -0
  37. package/dist/src/oauth2/create-authorization-url.d.ts +21 -0
  38. package/dist/src/oauth2/index.d.ts +6 -0
  39. package/dist/src/oauth2/oauth-provider.d.ts +178 -0
  40. package/dist/src/oauth2/refresh-access-token.d.ts +20 -0
  41. package/dist/src/oauth2/utils.d.ts +3 -0
  42. package/dist/src/oauth2/validate-authorization-code.d.ts +28 -0
  43. package/dist/src/social-providers/github.d.ts +89 -0
  44. package/dist/src/social-providers/google.d.ts +82 -0
  45. package/dist/src/social-providers/index.d.ts +93 -0
  46. package/dist/src/types/context.d.ts +177 -0
  47. package/dist/src/types/cookie.d.ts +15 -0
  48. package/dist/src/types/helper.d.ts +9 -0
  49. package/dist/src/types/index.d.ts +7 -0
  50. package/dist/src/types/init-options.d.ts +1140 -0
  51. package/dist/src/types/plugin-client.d.ts +88 -0
  52. package/dist/src/types/plugin.d.ts +120 -0
  53. package/dist/src/utils/error-codes.d.ts +9 -0
  54. package/package.json +11 -5
  55. package/.turbo/turbo-build.log +0 -42
  56. package/dist/index-DjrJweYn.d.mts +0 -7312
  57. package/src/api/index.ts +0 -57
  58. package/src/async_hooks/index.ts +0 -35
  59. package/src/context/endpoint-context.ts +0 -48
  60. package/src/context/index.ts +0 -21
  61. package/src/context/request-state.test.ts +0 -346
  62. package/src/context/request-state.ts +0 -75
  63. package/src/context/transaction.ts +0 -72
  64. package/src/db/adapter/index.ts +0 -466
  65. package/src/db/plugin.ts +0 -11
  66. package/src/db/schema/account.ts +0 -34
  67. package/src/db/schema/rate-limit.ts +0 -21
  68. package/src/db/schema/session.ts +0 -17
  69. package/src/db/schema/shared.ts +0 -7
  70. package/src/db/schema/user.ts +0 -16
  71. package/src/db/type.ts +0 -180
  72. package/src/env/color-depth.ts +0 -172
  73. package/src/env/env-impl.ts +0 -124
  74. package/src/env/index.ts +0 -23
  75. package/src/env/logger.test.ts +0 -33
  76. package/src/env/logger.ts +0 -145
  77. package/src/error/codes.ts +0 -31
  78. package/src/error/index.ts +0 -11
  79. package/src/oauth2/client-credentials-token.ts +0 -102
  80. package/src/oauth2/create-authorization-url.ts +0 -85
  81. package/src/oauth2/index.ts +0 -21
  82. package/src/oauth2/oauth-provider.ts +0 -211
  83. package/src/oauth2/refresh-access-token.ts +0 -124
  84. package/src/oauth2/utils.ts +0 -36
  85. package/src/oauth2/validate-authorization-code.ts +0 -154
  86. package/src/social-providers/apple.ts +0 -213
  87. package/src/social-providers/atlassian.ts +0 -132
  88. package/src/social-providers/cognito.ts +0 -269
  89. package/src/social-providers/discord.ts +0 -169
  90. package/src/social-providers/dropbox.ts +0 -112
  91. package/src/social-providers/facebook.ts +0 -204
  92. package/src/social-providers/figma.ts +0 -115
  93. package/src/social-providers/github.ts +0 -154
  94. package/src/social-providers/gitlab.ts +0 -152
  95. package/src/social-providers/google.ts +0 -171
  96. package/src/social-providers/huggingface.ts +0 -118
  97. package/src/social-providers/index.ts +0 -124
  98. package/src/social-providers/kakao.ts +0 -178
  99. package/src/social-providers/kick.ts +0 -92
  100. package/src/social-providers/line.ts +0 -169
  101. package/src/social-providers/linear.ts +0 -120
  102. package/src/social-providers/linkedin.ts +0 -110
  103. package/src/social-providers/microsoft-entra-id.ts +0 -245
  104. package/src/social-providers/naver.ts +0 -112
  105. package/src/social-providers/notion.ts +0 -108
  106. package/src/social-providers/paybin.ts +0 -122
  107. package/src/social-providers/paypal.ts +0 -263
  108. package/src/social-providers/polar.ts +0 -107
  109. package/src/social-providers/reddit.ts +0 -122
  110. package/src/social-providers/roblox.ts +0 -110
  111. package/src/social-providers/salesforce.ts +0 -159
  112. package/src/social-providers/slack.ts +0 -111
  113. package/src/social-providers/spotify.ts +0 -93
  114. package/src/social-providers/tiktok.ts +0 -210
  115. package/src/social-providers/twitch.ts +0 -111
  116. package/src/social-providers/twitter.ts +0 -198
  117. package/src/social-providers/vk.ts +0 -126
  118. package/src/social-providers/zoom.ts +0 -217
  119. package/src/types/context.ts +0 -269
  120. package/src/types/cookie.ts +0 -7
  121. package/src/types/helper.ts +0 -6
  122. package/src/types/index.ts +0 -21
  123. package/src/types/init-options.ts +0 -1346
  124. package/src/types/plugin-client.ts +0 -100
  125. package/src/types/plugin.ts +0 -158
  126. package/src/utils/error-codes.ts +0 -51
  127. package/tsconfig.json +0 -7
  128. package/tsdown.config.ts +0 -20
  129. /package/{src/index.ts → dist/src/index.d.ts} +0 -0
  130. /package/{src/utils/index.ts → dist/src/utils/index.d.ts} +0 -0
@@ -1,11 +0,0 @@
1
- export class PayvoAuthError extends Error {
2
- constructor(message: string, cause?: string | undefined) {
3
- super(message);
4
- this.name = "PayvoAuthError";
5
- this.message = message;
6
- this.cause = cause;
7
- this.stack = "";
8
- }
9
- }
10
-
11
- export { BASE_ERROR_CODES } from "./codes";
@@ -1,102 +0,0 @@
1
- import { base64Url } from "@gatwech/utils/base64";
2
- import { betterFetch } from "@better-fetch/fetch";
3
- import type { OAuth2Tokens, ProviderOptions } from "./oauth-provider";
4
-
5
- export function createClientCredentialsTokenRequest({
6
- options,
7
- scope,
8
- authentication,
9
- resource,
10
- }: {
11
- options: ProviderOptions & { clientSecret: string };
12
- scope?: string | undefined;
13
- authentication?: ("basic" | "post") | undefined;
14
- resource?: (string | string[]) | undefined;
15
- }) {
16
- const body = new URLSearchParams();
17
- const headers: Record<string, any> = {
18
- "content-type": "application/x-www-form-urlencoded",
19
- accept: "application/json",
20
- };
21
-
22
- body.set("grant_type", "client_credentials");
23
- scope && body.set("scope", scope);
24
- if (resource) {
25
- if (typeof resource === "string") {
26
- body.append("resource", resource);
27
- } else {
28
- for (const _resource of resource) {
29
- body.append("resource", _resource);
30
- }
31
- }
32
- }
33
- if (authentication === "basic") {
34
- const primaryClientId = Array.isArray(options.clientId)
35
- ? options.clientId[0]
36
- : options.clientId;
37
- const encodedCredentials = base64Url.encode(
38
- `${primaryClientId}:${options.clientSecret}`
39
- );
40
- headers["authorization"] = `Basic ${encodedCredentials}`;
41
- } else {
42
- const primaryClientId = Array.isArray(options.clientId)
43
- ? options.clientId[0]
44
- : options.clientId;
45
- body.set("client_id", primaryClientId);
46
- body.set("client_secret", options.clientSecret);
47
- }
48
-
49
- return {
50
- body,
51
- headers,
52
- };
53
- }
54
-
55
- export async function clientCredentialsToken({
56
- options,
57
- tokenEndpoint,
58
- scope,
59
- authentication,
60
- resource,
61
- }: {
62
- options: ProviderOptions & { clientSecret: string };
63
- tokenEndpoint: string;
64
- scope: string;
65
- authentication?: ("basic" | "post") | undefined;
66
- resource?: (string | string[]) | undefined;
67
- }): Promise<OAuth2Tokens> {
68
- const { body, headers } = createClientCredentialsTokenRequest({
69
- options,
70
- scope,
71
- authentication,
72
- resource,
73
- });
74
-
75
- const { data, error } = await betterFetch<{
76
- access_token: string;
77
- expires_in?: number | undefined;
78
- token_type?: string | undefined;
79
- scope?: string | undefined;
80
- }>(tokenEndpoint, {
81
- method: "POST",
82
- body,
83
- headers,
84
- });
85
- if (error) {
86
- throw error;
87
- }
88
- const tokens: OAuth2Tokens = {
89
- accessToken: data.access_token,
90
- tokenType: data.token_type,
91
- scopes: data.scope?.split(" "),
92
- };
93
-
94
- if (data.expires_in) {
95
- const now = new Date();
96
- tokens.accessTokenExpiresAt = new Date(
97
- now.getTime() + data.expires_in * 1000
98
- );
99
- }
100
-
101
- return tokens;
102
- }
@@ -1,85 +0,0 @@
1
- import type { ProviderOptions } from "./index";
2
- import { generateCodeChallenge } from "./utils";
3
-
4
- export async function createAuthorizationURL({
5
- id,
6
- options,
7
- authorizationEndpoint,
8
- state,
9
- codeVerifier,
10
- scopes,
11
- claims,
12
- redirectURI,
13
- duration,
14
- prompt,
15
- accessType,
16
- responseType,
17
- display,
18
- loginHint,
19
- hd,
20
- responseMode,
21
- additionalParams,
22
- scopeJoiner,
23
- }: {
24
- id: string;
25
- options: ProviderOptions;
26
- redirectURI: string;
27
- authorizationEndpoint: string;
28
- state: string;
29
- codeVerifier?: string | undefined;
30
- scopes: string[];
31
- claims?: string[] | undefined;
32
- duration?: string | undefined;
33
- prompt?: string | undefined;
34
- accessType?: string | undefined;
35
- responseType?: string | undefined;
36
- display?: string | undefined;
37
- loginHint?: string | undefined;
38
- hd?: string | undefined;
39
- responseMode?: string | undefined;
40
- additionalParams?: Record<string, string> | undefined;
41
- scopeJoiner?: string | undefined;
42
- }) {
43
- const url = new URL(authorizationEndpoint);
44
- url.searchParams.set("response_type", responseType || "code");
45
- const primaryClientId = Array.isArray(options.clientId)
46
- ? options.clientId[0]
47
- : options.clientId;
48
- url.searchParams.set("client_id", primaryClientId);
49
- url.searchParams.set("state", state);
50
- url.searchParams.set("scope", scopes.join(scopeJoiner || " "));
51
- url.searchParams.set("redirect_uri", options.redirectURI || redirectURI);
52
- duration && url.searchParams.set("duration", duration);
53
- display && url.searchParams.set("display", display);
54
- loginHint && url.searchParams.set("login_hint", loginHint);
55
- prompt && url.searchParams.set("prompt", prompt);
56
- hd && url.searchParams.set("hd", hd);
57
- accessType && url.searchParams.set("access_type", accessType);
58
- responseMode && url.searchParams.set("response_mode", responseMode);
59
- if (codeVerifier) {
60
- const codeChallenge = await generateCodeChallenge(codeVerifier);
61
- url.searchParams.set("code_challenge_method", "S256");
62
- url.searchParams.set("code_challenge", codeChallenge);
63
- }
64
- if (claims) {
65
- const claimsObj = claims.reduce(
66
- (acc, claim) => {
67
- acc[claim] = null;
68
- return acc;
69
- },
70
- {} as Record<string, null>,
71
- );
72
- url.searchParams.set(
73
- "claims",
74
- JSON.stringify({
75
- id_token: { email: null, email_verified: null, ...claimsObj },
76
- }),
77
- );
78
- }
79
- if (additionalParams) {
80
- Object.entries(additionalParams).forEach(([key, value]) => {
81
- url.searchParams.set(key, value);
82
- });
83
- }
84
- return url;
85
- }
@@ -1,21 +0,0 @@
1
- export {
2
- clientCredentialsToken,
3
- createClientCredentialsTokenRequest,
4
- } from "./client-credentials-token";
5
- export { createAuthorizationURL } from "./create-authorization-url";
6
- export type {
7
- OAuth2Tokens,
8
- OAuth2UserInfo,
9
- OAuthProvider,
10
- ProviderOptions,
11
- } from "./oauth-provider";
12
- export {
13
- createRefreshAccessTokenRequest,
14
- refreshAccessToken,
15
- } from "./refresh-access-token";
16
- export { generateCodeChallenge, getOAuth2Tokens } from "./utils";
17
- export {
18
- createAuthorizationCodeRequest,
19
- validateAuthorizationCode,
20
- validateToken,
21
- } from "./validate-authorization-code";
@@ -1,211 +0,0 @@
1
- import type { LiteralString } from "../types";
2
-
3
- export interface OAuth2Tokens {
4
- tokenType?: string | undefined;
5
- accessToken?: string | undefined;
6
- refreshToken?: string | undefined;
7
- accessTokenExpiresAt?: Date | undefined;
8
- refreshTokenExpiresAt?: Date | undefined;
9
- scopes?: string[] | undefined;
10
- idToken?: string | undefined;
11
- }
12
-
13
- export type OAuth2UserInfo = {
14
- id: string | number;
15
- name?: string | undefined;
16
- email?: (string | null) | undefined;
17
- image?: string | undefined;
18
- emailVerified: boolean;
19
- };
20
-
21
- export interface OAuthProvider<
22
- T extends Record<string, any> = Record<string, any>,
23
- O extends Record<string, any> = Partial<ProviderOptions>,
24
- > {
25
- id: LiteralString;
26
- createAuthorizationURL: (data: {
27
- state: string;
28
- codeVerifier: string;
29
- scopes?: string[] | undefined;
30
- redirectURI: string;
31
- display?: string | undefined;
32
- loginHint?: string | undefined;
33
- }) => Promise<URL> | URL;
34
- name: string;
35
- validateAuthorizationCode: (data: {
36
- code: string;
37
- redirectURI: string;
38
- codeVerifier?: string | undefined;
39
- deviceId?: string | undefined;
40
- }) => Promise<OAuth2Tokens>;
41
- getUserInfo: (
42
- token: OAuth2Tokens & {
43
- /**
44
- * The user object from the provider
45
- * This is only available for some providers like Apple
46
- */
47
- user?:
48
- | {
49
- name?: {
50
- firstName?: string;
51
- lastName?: string;
52
- };
53
- email?: string;
54
- }
55
- | undefined;
56
- },
57
- ) => Promise<{
58
- user: OAuth2UserInfo;
59
- data: T;
60
- } | null>;
61
- /**
62
- * Custom function to refresh a token
63
- */
64
- refreshAccessToken?:
65
- | ((refreshToken: string) => Promise<OAuth2Tokens>)
66
- | undefined;
67
- revokeToken?: ((token: string) => Promise<void>) | undefined;
68
- /**
69
- * Verify the id token
70
- * @param token - The id token
71
- * @param nonce - The nonce
72
- * @returns True if the id token is valid, false otherwise
73
- */
74
- verifyIdToken?:
75
- | ((token: string, nonce?: string) => Promise<boolean>)
76
- | undefined;
77
- /**
78
- * Disable implicit sign up for new users. When set to true for the provider,
79
- * sign-in need to be called with with requestSignUp as true to create new users.
80
- */
81
- disableImplicitSignUp?: boolean | undefined;
82
- /**
83
- * Disable sign up for new users.
84
- */
85
- disableSignUp?: boolean | undefined;
86
- /**
87
- * Options for the provider
88
- */
89
- options?: O | undefined;
90
- }
91
-
92
- export type ProviderOptions<Profile extends Record<string, any> = any> = {
93
- /**
94
- * The client ID of your application.
95
- *
96
- * This is usually a string but can be any type depending on the provider.
97
- */
98
- clientId?: unknown | undefined;
99
- /**
100
- * The client secret of your application
101
- */
102
- clientSecret?: string | undefined;
103
- /**
104
- * The scopes you want to request from the provider
105
- */
106
- scope?: string[] | undefined;
107
- /**
108
- * Remove default scopes of the provider
109
- */
110
- disableDefaultScope?: boolean | undefined;
111
- /**
112
- * The redirect URL for your application. This is where the provider will
113
- * redirect the user after the sign in process. Make sure this URL is
114
- * whitelisted in the provider's dashboard.
115
- */
116
- redirectURI?: string | undefined;
117
- /**
118
- * The client key of your application
119
- * Tiktok Social Provider uses this field instead of clientId
120
- */
121
- clientKey?: string | undefined;
122
- /**
123
- * Disable provider from allowing users to sign in
124
- * with this provider with an id token sent from the
125
- * client.
126
- */
127
- disableIdTokenSignIn?: boolean | undefined;
128
- /**
129
- * verifyIdToken function to verify the id token
130
- */
131
- verifyIdToken?:
132
- | ((token: string, nonce?: string) => Promise<boolean>)
133
- | undefined;
134
- /**
135
- * Custom function to get user info from the provider
136
- */
137
- getUserInfo?:
138
- | ((token: OAuth2Tokens) => Promise<{
139
- user: {
140
- id: string;
141
- name?: string;
142
- email?: string | null;
143
- image?: string;
144
- emailVerified: boolean;
145
- [key: string]: any;
146
- };
147
- data: any;
148
- }>)
149
- | undefined;
150
- /**
151
- * Custom function to refresh a token
152
- */
153
- refreshAccessToken?:
154
- | ((refreshToken: string) => Promise<OAuth2Tokens>)
155
- | undefined;
156
- /**
157
- * Custom function to map the provider profile to a
158
- * user.
159
- */
160
- mapProfileToUser?:
161
- | ((profile: Profile) =>
162
- | {
163
- id?: string;
164
- name?: string;
165
- email?: string | null;
166
- image?: string;
167
- emailVerified?: boolean;
168
- [key: string]: any;
169
- }
170
- | Promise<{
171
- id?: string;
172
- name?: string;
173
- email?: string | null;
174
- image?: string;
175
- emailVerified?: boolean;
176
- [key: string]: any;
177
- }>)
178
- | undefined;
179
- /**
180
- * Disable implicit sign up for new users. When set to true for the provider,
181
- * sign-in need to be called with with requestSignUp as true to create new users.
182
- */
183
- disableImplicitSignUp?: boolean | undefined;
184
- /**
185
- * Disable sign up for new users.
186
- */
187
- disableSignUp?: boolean | undefined;
188
- /**
189
- * The prompt to use for the authorization code request
190
- */
191
- prompt?:
192
- | (
193
- | "select_account"
194
- | "consent"
195
- | "login"
196
- | "none"
197
- | "select_account consent"
198
- )
199
- | undefined;
200
- /**
201
- * The response mode to use for the authorization code request
202
- */
203
- responseMode?: ("query" | "form_post") | undefined;
204
- /**
205
- * If enabled, the user info will be overridden with the provider user info
206
- * This is useful if you want to use the provider user info to update the user info
207
- *
208
- * @default false
209
- */
210
- overrideUserInfoOnSignIn?: boolean | undefined;
211
- };
@@ -1,124 +0,0 @@
1
- import { base64 } from "@gatwech/utils/base64";
2
- import { betterFetch } from "@better-fetch/fetch";
3
- import type { OAuth2Tokens, ProviderOptions } from "./oauth-provider";
4
-
5
- export function createRefreshAccessTokenRequest({
6
- refreshToken,
7
- options,
8
- authentication,
9
- extraParams,
10
- resource,
11
- }: {
12
- refreshToken: string;
13
- options: Partial<ProviderOptions>;
14
- authentication?: ("basic" | "post") | undefined;
15
- extraParams?: Record<string, string> | undefined;
16
- resource?: (string | string[]) | undefined;
17
- }) {
18
- const body = new URLSearchParams();
19
- const headers: Record<string, any> = {
20
- "content-type": "application/x-www-form-urlencoded",
21
- accept: "application/json",
22
- };
23
-
24
- body.set("grant_type", "refresh_token");
25
- body.set("refresh_token", refreshToken);
26
- // Use standard Base64 encoding for HTTP Basic Auth (OAuth2 spec, RFC 7617)
27
- // Fixes compatibility with providers like Notion, Twitter, etc.
28
- if (authentication === "basic") {
29
- const primaryClientId = Array.isArray(options.clientId)
30
- ? options.clientId[0]
31
- : options.clientId;
32
- if (primaryClientId) {
33
- headers["authorization"] =
34
- "Basic " +
35
- base64.encode(`${primaryClientId}:${options.clientSecret ?? ""}`);
36
- } else {
37
- headers["authorization"] =
38
- "Basic " + base64.encode(`:${options.clientSecret ?? ""}`);
39
- }
40
- } else {
41
- const primaryClientId = Array.isArray(options.clientId)
42
- ? options.clientId[0]
43
- : options.clientId;
44
- body.set("client_id", primaryClientId);
45
- if (options.clientSecret) {
46
- body.set("client_secret", options.clientSecret);
47
- }
48
- }
49
-
50
- if (resource) {
51
- if (typeof resource === "string") {
52
- body.append("resource", resource);
53
- } else {
54
- for (const _resource of resource) {
55
- body.append("resource", _resource);
56
- }
57
- }
58
- }
59
- if (extraParams) {
60
- for (const [key, value] of Object.entries(extraParams)) {
61
- body.set(key, value);
62
- }
63
- }
64
-
65
- return {
66
- body,
67
- headers,
68
- };
69
- }
70
-
71
- export async function refreshAccessToken({
72
- refreshToken,
73
- options,
74
- tokenEndpoint,
75
- authentication,
76
- extraParams,
77
- }: {
78
- refreshToken: string;
79
- options: Partial<ProviderOptions>;
80
- tokenEndpoint: string;
81
- authentication?: ("basic" | "post") | undefined;
82
- extraParams?: Record<string, string> | undefined;
83
- /** @deprecated always "refresh_token" */
84
- grantType?: string | undefined;
85
- }): Promise<OAuth2Tokens> {
86
- const { body, headers } = createRefreshAccessTokenRequest({
87
- refreshToken,
88
- options,
89
- authentication,
90
- extraParams,
91
- });
92
-
93
- const { data, error } = await betterFetch<{
94
- access_token: string;
95
- refresh_token?: string | undefined;
96
- expires_in?: number | undefined;
97
- token_type?: string | undefined;
98
- scope?: string | undefined;
99
- id_token?: string | undefined;
100
- }>(tokenEndpoint, {
101
- method: "POST",
102
- body,
103
- headers,
104
- });
105
- if (error) {
106
- throw error;
107
- }
108
- const tokens: OAuth2Tokens = {
109
- accessToken: data.access_token,
110
- refreshToken: data.refresh_token,
111
- tokenType: data.token_type,
112
- scopes: data.scope?.split(" "),
113
- idToken: data.id_token,
114
- };
115
-
116
- if (data.expires_in) {
117
- const now = new Date();
118
- tokens.accessTokenExpiresAt = new Date(
119
- now.getTime() + data.expires_in * 1000
120
- );
121
- }
122
-
123
- return tokens;
124
- }
@@ -1,36 +0,0 @@
1
- import { base64Url } from "@gatwech/utils/base64";
2
- import type { OAuth2Tokens } from "./oauth-provider";
3
-
4
- export function getOAuth2Tokens(data: Record<string, any>): OAuth2Tokens {
5
- const getDate = (seconds: number) => {
6
- const now = new Date();
7
- return new Date(now.getTime() + seconds * 1000);
8
- };
9
-
10
- return {
11
- tokenType: data.token_type,
12
- accessToken: data.access_token,
13
- refreshToken: data.refresh_token,
14
- accessTokenExpiresAt: data.expires_in
15
- ? getDate(data.expires_in)
16
- : undefined,
17
- refreshTokenExpiresAt: data.refresh_token_expires_in
18
- ? getDate(data.refresh_token_expires_in)
19
- : undefined,
20
- scopes: data?.scope
21
- ? typeof data.scope === "string"
22
- ? data.scope.split(" ")
23
- : data.scope
24
- : [],
25
- idToken: data.id_token,
26
- };
27
- }
28
-
29
- export async function generateCodeChallenge(codeVerifier: string) {
30
- const encoder = new TextEncoder();
31
- const data = encoder.encode(codeVerifier);
32
- const hash = await crypto.subtle.digest("SHA-256", data);
33
- return base64Url.encode(new Uint8Array(hash), {
34
- padding: false,
35
- });
36
- }