@tekcify/auth-core-client 1.0.0 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/oauth.ts DELETED
@@ -1,250 +0,0 @@
1
- import type {
2
- TokenResponse,
3
- UserInfo,
4
- IntrospectResult,
5
- OAuthConfig,
6
- } from './types';
7
- import { generateCodeVerifier, generateCodeChallenge } from './pkce';
8
-
9
- export function buildAuthorizeUrl(
10
- authServerUrl: string,
11
- config: {
12
- clientId: string;
13
- redirectUri: string;
14
- scopes: string[];
15
- state?: string;
16
- codeChallenge?: string;
17
- codeChallengeMethod?: 'plain' | 'S256';
18
- },
19
- ): string {
20
- const url = new URL(`${authServerUrl}/oauth/authorize`);
21
- url.searchParams.set('clientId', config.clientId);
22
- url.searchParams.set('redirectUri', config.redirectUri);
23
- url.searchParams.set('scopes', config.scopes.join(' '));
24
- if (config.state) {
25
- url.searchParams.set('state', config.state);
26
- }
27
- if (config.codeChallenge) {
28
- url.searchParams.set('codeChallenge', config.codeChallenge);
29
- url.searchParams.set(
30
- 'codeChallengeMethod',
31
- config.codeChallengeMethod ?? 'S256',
32
- );
33
- }
34
- return url.toString();
35
- }
36
-
37
- export async function exchangeCode(
38
- authServerUrl: string,
39
- config: {
40
- code: string;
41
- clientId: string;
42
- clientSecret: string;
43
- redirectUri: string;
44
- codeVerifier?: string;
45
- },
46
- ): Promise<TokenResponse> {
47
- const response = await fetch(`${authServerUrl}/oauth/token`, {
48
- method: 'POST',
49
- headers: {
50
- 'Content-Type': 'application/json',
51
- },
52
- body: JSON.stringify({
53
- grant_type: 'authorization_code',
54
- code: config.code,
55
- clientId: config.clientId,
56
- clientSecret: config.clientSecret,
57
- redirectUri: config.redirectUri,
58
- codeVerifier: config.codeVerifier,
59
- }),
60
- });
61
-
62
- if (!response.ok) {
63
- const error = (await response.json().catch(() => ({}))) as {
64
- message?: string;
65
- };
66
- throw new Error(error.message ?? 'Failed to exchange authorization code');
67
- }
68
-
69
- return (await response.json()) as TokenResponse;
70
- }
71
-
72
- export async function refreshAccessToken(
73
- authServerUrl: string,
74
- config: {
75
- refreshToken: string;
76
- clientId: string;
77
- clientSecret: string;
78
- },
79
- ): Promise<TokenResponse> {
80
- const response = await fetch(`${authServerUrl}/oauth/token`, {
81
- method: 'POST',
82
- headers: {
83
- 'Content-Type': 'application/json',
84
- },
85
- body: JSON.stringify({
86
- grant_type: 'refresh_token',
87
- refreshToken: config.refreshToken,
88
- clientId: config.clientId,
89
- clientSecret: config.clientSecret,
90
- }),
91
- });
92
-
93
- if (!response.ok) {
94
- const error = (await response.json().catch(() => ({}))) as {
95
- message?: string;
96
- };
97
- throw new Error(error.message ?? 'Failed to refresh access token');
98
- }
99
-
100
- return (await response.json()) as TokenResponse;
101
- }
102
-
103
- export async function revokeToken(
104
- authServerUrl: string,
105
- config: {
106
- token: string;
107
- clientId: string;
108
- clientSecret: string;
109
- },
110
- ): Promise<void> {
111
- const response = await fetch(`${authServerUrl}/oauth/revoke`, {
112
- method: 'POST',
113
- headers: {
114
- 'Content-Type': 'application/json',
115
- },
116
- body: JSON.stringify({
117
- token: config.token,
118
- clientId: config.clientId,
119
- clientSecret: config.clientSecret,
120
- }),
121
- });
122
-
123
- if (!response.ok) {
124
- const error = (await response.json().catch(() => ({}))) as {
125
- message?: string;
126
- };
127
- throw new Error(error.message ?? 'Failed to revoke token');
128
- }
129
- }
130
-
131
- export async function introspectToken(
132
- authServerUrl: string,
133
- config: {
134
- token: string;
135
- clientId?: string;
136
- clientSecret?: string;
137
- },
138
- ): Promise<IntrospectResult> {
139
- const body: Record<string, string> = {
140
- token: config.token,
141
- };
142
-
143
- if (config.clientId) {
144
- body.clientId = config.clientId;
145
- }
146
- if (config.clientSecret) {
147
- body.clientSecret = config.clientSecret;
148
- }
149
-
150
- const response = await fetch(`${authServerUrl}/oauth/token/introspect`, {
151
- method: 'POST',
152
- headers: {
153
- 'Content-Type': 'application/json',
154
- },
155
- body: JSON.stringify(body),
156
- });
157
-
158
- if (!response.ok) {
159
- const error = (await response.json().catch(() => ({}))) as {
160
- message?: string;
161
- };
162
- throw new Error(error.message ?? 'Failed to introspect token');
163
- }
164
-
165
- return (await response.json()) as IntrospectResult;
166
- }
167
-
168
- export async function getUserInfo(
169
- authServerUrl: string,
170
- accessToken: string,
171
- ): Promise<UserInfo> {
172
- const response = await fetch(`${authServerUrl}/oauth/userinfo`, {
173
- method: 'GET',
174
- headers: {
175
- Authorization: `Bearer ${accessToken}`,
176
- },
177
- });
178
-
179
- if (!response.ok) {
180
- const error = (await response.json().catch(() => ({}))) as {
181
- message?: string;
182
- };
183
- throw new Error(error.message ?? 'Failed to get user info');
184
- }
185
-
186
- return (await response.json()) as UserInfo;
187
- }
188
-
189
- export class OAuthClient {
190
- constructor(private config: OAuthConfig) {}
191
-
192
- async buildAuthorizeUrl(options?: {
193
- state?: string;
194
- codeChallenge?: string;
195
- codeChallengeMethod?: 'plain' | 'S256';
196
- }): Promise<string> {
197
- return buildAuthorizeUrl(this.config.authServerUrl, {
198
- clientId: this.config.clientId,
199
- redirectUri: this.config.redirectUri,
200
- scopes: this.config.scopes,
201
- ...options,
202
- });
203
- }
204
-
205
- async exchangeCode(
206
- code: string,
207
- codeVerifier?: string,
208
- ): Promise<TokenResponse> {
209
- return exchangeCode(this.config.authServerUrl, {
210
- code,
211
- clientId: this.config.clientId,
212
- clientSecret: this.config.clientSecret,
213
- redirectUri: this.config.redirectUri,
214
- codeVerifier,
215
- });
216
- }
217
-
218
- async refreshAccessToken(refreshToken: string): Promise<TokenResponse> {
219
- return refreshAccessToken(this.config.authServerUrl, {
220
- refreshToken,
221
- clientId: this.config.clientId,
222
- clientSecret: this.config.clientSecret,
223
- });
224
- }
225
-
226
- async revokeToken(token: string): Promise<void> {
227
- return revokeToken(this.config.authServerUrl, {
228
- token,
229
- clientId: this.config.clientId,
230
- clientSecret: this.config.clientSecret,
231
- });
232
- }
233
-
234
- async introspectToken(
235
- token: string,
236
- options?: { clientId?: string; clientSecret?: string },
237
- ): Promise<IntrospectResult> {
238
- return introspectToken(this.config.authServerUrl, {
239
- token,
240
- clientId: options?.clientId ?? this.config.clientId,
241
- clientSecret: options?.clientSecret ?? this.config.clientSecret,
242
- });
243
- }
244
-
245
- async getUserInfo(accessToken: string): Promise<UserInfo> {
246
- return getUserInfo(this.config.authServerUrl, accessToken);
247
- }
248
- }
249
-
250
- export { generateCodeVerifier, generateCodeChallenge };
package/src/pkce.ts DELETED
@@ -1,38 +0,0 @@
1
- function generateRandomBytes(length: number): Uint8Array {
2
- if (typeof crypto !== 'undefined') {
3
- return crypto.getRandomValues(new Uint8Array(length));
4
- }
5
- throw new Error('crypto.getRandomValues is not available');
6
- }
7
-
8
- function base64UrlEncode(buffer: Uint8Array): string {
9
- const base64 = btoa(String.fromCharCode(...buffer));
10
- return base64.replace(/\+/g, '-').replace(/\//g, '_').replace(/=/g, '');
11
- }
12
-
13
- function sha256(data: string): Promise<Uint8Array> {
14
- if (typeof crypto !== 'undefined') {
15
- const encoder = new TextEncoder();
16
- const dataBuffer = encoder.encode(data);
17
- return crypto.subtle.digest('SHA-256', dataBuffer).then((hashBuffer) => {
18
- return new Uint8Array(hashBuffer);
19
- });
20
- }
21
- throw new Error('crypto.subtle is not available');
22
- }
23
-
24
- export function generateCodeVerifier(): string {
25
- const randomBytes = generateRandomBytes(32);
26
- return base64UrlEncode(randomBytes);
27
- }
28
-
29
- export async function generateCodeChallenge(
30
- verifier: string,
31
- method: 'plain' | 'S256' = 'S256',
32
- ): Promise<string> {
33
- if (method === 'plain') {
34
- return verifier;
35
- }
36
- const hash = await sha256(verifier);
37
- return base64UrlEncode(hash);
38
- }
package/src/types.ts DELETED
@@ -1,37 +0,0 @@
1
- export interface TokenResponse {
2
- accessToken: string;
3
- refreshToken: string;
4
- tokenType: string;
5
- expiresIn: number;
6
- scope: string;
7
- }
8
-
9
- export interface UserInfo {
10
- sub: string;
11
- email: string;
12
- email_verified: boolean;
13
- given_name: string | null;
14
- family_name: string | null;
15
- name: string | null;
16
- picture: string | null;
17
- updated_at: number;
18
- }
19
-
20
- export interface IntrospectResult {
21
- active: boolean;
22
- clientId?: string;
23
- username?: string;
24
- scope?: string;
25
- sub?: string;
26
- exp?: number;
27
- iat?: number;
28
- tokenType?: string;
29
- }
30
-
31
- export interface OAuthConfig {
32
- authServerUrl: string;
33
- clientId: string;
34
- clientSecret: string;
35
- redirectUri: string;
36
- scopes: string[];
37
- }
package/tsconfig.json DELETED
@@ -1,11 +0,0 @@
1
- {
2
- "extends": "../../tsconfig.base.json",
3
- "compilerOptions": {
4
- "outDir": "./dist",
5
- "rootDir": "./src",
6
- "declaration": true,
7
- "declarationMap": true
8
- },
9
- "include": ["src/**/*"]
10
- }
11
-