@tekcify/auth-core-client 1.0.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 +374 -0
- package/package.json +42 -0
- package/src/__tests__/pkce.test.ts +31 -0
- package/src/index.ts +3 -0
- package/src/oauth.ts +250 -0
- package/src/pkce.ts +38 -0
- package/src/types.ts +37 -0
- package/tsconfig.json +11 -0
- package/tsconfig.tsbuildinfo +1 -0
- package/vitest.config.ts +9 -0
package/src/pkce.ts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
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
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
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
|
+
}
|