@nuria-tech/auth-sdk 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/LICENSE +21 -0
- package/README.md +270 -0
- package/dist/index.cjs +591 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +144 -0
- package/dist/index.d.ts +144 -0
- package/dist/index.js +582 -0
- package/dist/index.js.map +1 -0
- package/package.json +61 -0
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
interface TokenSet {
|
|
2
|
+
accessToken: string;
|
|
3
|
+
tokenType?: string;
|
|
4
|
+
expiresIn?: number;
|
|
5
|
+
refreshToken?: string;
|
|
6
|
+
idToken?: string;
|
|
7
|
+
scope?: string;
|
|
8
|
+
expiresAt?: number;
|
|
9
|
+
}
|
|
10
|
+
interface Session {
|
|
11
|
+
tokens: TokenSet;
|
|
12
|
+
createdAt: number;
|
|
13
|
+
}
|
|
14
|
+
interface StartLoginOptions {
|
|
15
|
+
loginHint?: string;
|
|
16
|
+
scopes?: string[];
|
|
17
|
+
extraParams?: Record<string, string>;
|
|
18
|
+
}
|
|
19
|
+
interface StorageAdapter {
|
|
20
|
+
get(key: string): Promise<string | null> | string | null;
|
|
21
|
+
set(key: string, value: string): Promise<void> | void;
|
|
22
|
+
remove(key: string): Promise<void> | void;
|
|
23
|
+
}
|
|
24
|
+
interface AuthTransportRequest {
|
|
25
|
+
method?: 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
|
|
26
|
+
headers?: Record<string, string>;
|
|
27
|
+
query?: Record<string, string | undefined>;
|
|
28
|
+
body?: unknown;
|
|
29
|
+
timeoutMs?: number;
|
|
30
|
+
retries?: number;
|
|
31
|
+
}
|
|
32
|
+
interface AuthTransportResponse<T = unknown> {
|
|
33
|
+
status: number;
|
|
34
|
+
data: T;
|
|
35
|
+
headers: Headers;
|
|
36
|
+
}
|
|
37
|
+
interface AuthTransport {
|
|
38
|
+
request<T = unknown>(url: string, req?: AuthTransportRequest): Promise<AuthTransportResponse<T>>;
|
|
39
|
+
}
|
|
40
|
+
interface TransportInterceptor {
|
|
41
|
+
onRequest?: (url: string, req: AuthTransportRequest) => Promise<AuthTransportRequest> | AuthTransportRequest;
|
|
42
|
+
onResponse?: <T>(res: AuthTransportResponse<T>) => Promise<AuthTransportResponse<T>> | AuthTransportResponse<T>;
|
|
43
|
+
}
|
|
44
|
+
interface AuthConfig {
|
|
45
|
+
clientId: string;
|
|
46
|
+
authorizationEndpoint: string;
|
|
47
|
+
tokenEndpoint: string;
|
|
48
|
+
redirectUri: string;
|
|
49
|
+
scope?: string;
|
|
50
|
+
logoutEndpoint?: string;
|
|
51
|
+
userinfoEndpoint?: string;
|
|
52
|
+
storage?: StorageAdapter;
|
|
53
|
+
transport?: AuthTransport;
|
|
54
|
+
onRedirect?: (url: string) => void | Promise<void>;
|
|
55
|
+
enableRefreshToken?: boolean;
|
|
56
|
+
now?: () => number;
|
|
57
|
+
}
|
|
58
|
+
interface AuthClient {
|
|
59
|
+
startLogin(options?: StartLoginOptions): Promise<void>;
|
|
60
|
+
handleRedirectCallback(callbackUrl?: string): Promise<Session>;
|
|
61
|
+
getSession(): Session | null;
|
|
62
|
+
getAccessToken(): Promise<string | null>;
|
|
63
|
+
logout(options?: {
|
|
64
|
+
returnTo?: string;
|
|
65
|
+
}): Promise<void>;
|
|
66
|
+
isAuthenticated(): boolean;
|
|
67
|
+
onAuthStateChanged(handler: (session: Session | null) => void): () => void;
|
|
68
|
+
getUserinfo(): Promise<Record<string, unknown>>;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
declare function createAuthClient(config: AuthConfig): AuthClient;
|
|
72
|
+
|
|
73
|
+
declare class MemoryStorageAdapter implements StorageAdapter {
|
|
74
|
+
private store;
|
|
75
|
+
get(key: string): string | null;
|
|
76
|
+
set(key: string, value: string): void;
|
|
77
|
+
remove(key: string): void;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
declare class WebStorageAdapter implements StorageAdapter {
|
|
81
|
+
private readonly storage;
|
|
82
|
+
constructor(storage: Pick<Storage, 'getItem' | 'setItem' | 'removeItem'>);
|
|
83
|
+
get(key: string): string | null;
|
|
84
|
+
set(key: string, value: string): void;
|
|
85
|
+
remove(key: string): void;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
interface CookieStorageCallbacks {
|
|
89
|
+
getCookie(name: string): string | null | Promise<string | null>;
|
|
90
|
+
setCookie(name: string, value: string): void | Promise<void>;
|
|
91
|
+
removeCookie(name: string): void | Promise<void>;
|
|
92
|
+
}
|
|
93
|
+
declare class CookieStorageAdapter implements StorageAdapter {
|
|
94
|
+
private readonly callbacks;
|
|
95
|
+
constructor(callbacks: CookieStorageCallbacks);
|
|
96
|
+
get(key: string): Promise<string | null>;
|
|
97
|
+
set(key: string, value: string): Promise<void>;
|
|
98
|
+
remove(key: string): Promise<void>;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
interface BrowserCookieStorageOptions {
|
|
102
|
+
domain?: string;
|
|
103
|
+
path?: string;
|
|
104
|
+
sameSite?: 'strict' | 'lax' | 'none';
|
|
105
|
+
secure?: boolean;
|
|
106
|
+
}
|
|
107
|
+
declare function createBrowserCookieStorage(options?: BrowserCookieStorageOptions): StorageAdapter;
|
|
108
|
+
|
|
109
|
+
interface FetchTransportOptions {
|
|
110
|
+
fetchFn?: typeof fetch;
|
|
111
|
+
timeoutMs?: number;
|
|
112
|
+
retries?: number;
|
|
113
|
+
interceptors?: TransportInterceptor[];
|
|
114
|
+
}
|
|
115
|
+
declare class FetchAuthTransport implements AuthTransport {
|
|
116
|
+
private readonly fetchFn;
|
|
117
|
+
private readonly timeoutMs?;
|
|
118
|
+
private readonly retries;
|
|
119
|
+
private readonly interceptors;
|
|
120
|
+
constructor(options?: FetchTransportOptions);
|
|
121
|
+
request<T = unknown>(url: string, req?: AuthTransportRequest): Promise<AuthTransportResponse<T>>;
|
|
122
|
+
private withQuery;
|
|
123
|
+
private parseBody;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
declare enum AuthErrorCode {
|
|
127
|
+
INVALID_CONFIG = "INVALID_CONFIG",
|
|
128
|
+
STATE_MISMATCH = "STATE_MISMATCH",
|
|
129
|
+
CALLBACK_ERROR = "CALLBACK_ERROR",
|
|
130
|
+
TOKEN_EXCHANGE_FAILED = "TOKEN_EXCHANGE_FAILED",
|
|
131
|
+
REFRESH_FAILED = "REFRESH_FAILED",
|
|
132
|
+
STORAGE_ERROR = "STORAGE_ERROR",
|
|
133
|
+
MISSING_CODE = "MISSING_CODE",
|
|
134
|
+
MISSING_STATE = "MISSING_STATE",
|
|
135
|
+
NETWORK_ERROR = "NETWORK_ERROR",
|
|
136
|
+
HTTP_ERROR = "HTTP_ERROR"
|
|
137
|
+
}
|
|
138
|
+
declare class AuthError extends Error {
|
|
139
|
+
readonly code: AuthErrorCode;
|
|
140
|
+
readonly cause?: unknown | undefined;
|
|
141
|
+
constructor(code: AuthErrorCode, message: string, cause?: unknown | undefined);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
export { type AuthClient, type AuthConfig, AuthError, AuthErrorCode, type AuthTransport, type AuthTransportRequest, type AuthTransportResponse, type BrowserCookieStorageOptions, CookieStorageAdapter, FetchAuthTransport, MemoryStorageAdapter, type Session, type StartLoginOptions, type StorageAdapter, type TokenSet, type TransportInterceptor, WebStorageAdapter, createAuthClient, createBrowserCookieStorage };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
interface TokenSet {
|
|
2
|
+
accessToken: string;
|
|
3
|
+
tokenType?: string;
|
|
4
|
+
expiresIn?: number;
|
|
5
|
+
refreshToken?: string;
|
|
6
|
+
idToken?: string;
|
|
7
|
+
scope?: string;
|
|
8
|
+
expiresAt?: number;
|
|
9
|
+
}
|
|
10
|
+
interface Session {
|
|
11
|
+
tokens: TokenSet;
|
|
12
|
+
createdAt: number;
|
|
13
|
+
}
|
|
14
|
+
interface StartLoginOptions {
|
|
15
|
+
loginHint?: string;
|
|
16
|
+
scopes?: string[];
|
|
17
|
+
extraParams?: Record<string, string>;
|
|
18
|
+
}
|
|
19
|
+
interface StorageAdapter {
|
|
20
|
+
get(key: string): Promise<string | null> | string | null;
|
|
21
|
+
set(key: string, value: string): Promise<void> | void;
|
|
22
|
+
remove(key: string): Promise<void> | void;
|
|
23
|
+
}
|
|
24
|
+
interface AuthTransportRequest {
|
|
25
|
+
method?: 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
|
|
26
|
+
headers?: Record<string, string>;
|
|
27
|
+
query?: Record<string, string | undefined>;
|
|
28
|
+
body?: unknown;
|
|
29
|
+
timeoutMs?: number;
|
|
30
|
+
retries?: number;
|
|
31
|
+
}
|
|
32
|
+
interface AuthTransportResponse<T = unknown> {
|
|
33
|
+
status: number;
|
|
34
|
+
data: T;
|
|
35
|
+
headers: Headers;
|
|
36
|
+
}
|
|
37
|
+
interface AuthTransport {
|
|
38
|
+
request<T = unknown>(url: string, req?: AuthTransportRequest): Promise<AuthTransportResponse<T>>;
|
|
39
|
+
}
|
|
40
|
+
interface TransportInterceptor {
|
|
41
|
+
onRequest?: (url: string, req: AuthTransportRequest) => Promise<AuthTransportRequest> | AuthTransportRequest;
|
|
42
|
+
onResponse?: <T>(res: AuthTransportResponse<T>) => Promise<AuthTransportResponse<T>> | AuthTransportResponse<T>;
|
|
43
|
+
}
|
|
44
|
+
interface AuthConfig {
|
|
45
|
+
clientId: string;
|
|
46
|
+
authorizationEndpoint: string;
|
|
47
|
+
tokenEndpoint: string;
|
|
48
|
+
redirectUri: string;
|
|
49
|
+
scope?: string;
|
|
50
|
+
logoutEndpoint?: string;
|
|
51
|
+
userinfoEndpoint?: string;
|
|
52
|
+
storage?: StorageAdapter;
|
|
53
|
+
transport?: AuthTransport;
|
|
54
|
+
onRedirect?: (url: string) => void | Promise<void>;
|
|
55
|
+
enableRefreshToken?: boolean;
|
|
56
|
+
now?: () => number;
|
|
57
|
+
}
|
|
58
|
+
interface AuthClient {
|
|
59
|
+
startLogin(options?: StartLoginOptions): Promise<void>;
|
|
60
|
+
handleRedirectCallback(callbackUrl?: string): Promise<Session>;
|
|
61
|
+
getSession(): Session | null;
|
|
62
|
+
getAccessToken(): Promise<string | null>;
|
|
63
|
+
logout(options?: {
|
|
64
|
+
returnTo?: string;
|
|
65
|
+
}): Promise<void>;
|
|
66
|
+
isAuthenticated(): boolean;
|
|
67
|
+
onAuthStateChanged(handler: (session: Session | null) => void): () => void;
|
|
68
|
+
getUserinfo(): Promise<Record<string, unknown>>;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
declare function createAuthClient(config: AuthConfig): AuthClient;
|
|
72
|
+
|
|
73
|
+
declare class MemoryStorageAdapter implements StorageAdapter {
|
|
74
|
+
private store;
|
|
75
|
+
get(key: string): string | null;
|
|
76
|
+
set(key: string, value: string): void;
|
|
77
|
+
remove(key: string): void;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
declare class WebStorageAdapter implements StorageAdapter {
|
|
81
|
+
private readonly storage;
|
|
82
|
+
constructor(storage: Pick<Storage, 'getItem' | 'setItem' | 'removeItem'>);
|
|
83
|
+
get(key: string): string | null;
|
|
84
|
+
set(key: string, value: string): void;
|
|
85
|
+
remove(key: string): void;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
interface CookieStorageCallbacks {
|
|
89
|
+
getCookie(name: string): string | null | Promise<string | null>;
|
|
90
|
+
setCookie(name: string, value: string): void | Promise<void>;
|
|
91
|
+
removeCookie(name: string): void | Promise<void>;
|
|
92
|
+
}
|
|
93
|
+
declare class CookieStorageAdapter implements StorageAdapter {
|
|
94
|
+
private readonly callbacks;
|
|
95
|
+
constructor(callbacks: CookieStorageCallbacks);
|
|
96
|
+
get(key: string): Promise<string | null>;
|
|
97
|
+
set(key: string, value: string): Promise<void>;
|
|
98
|
+
remove(key: string): Promise<void>;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
interface BrowserCookieStorageOptions {
|
|
102
|
+
domain?: string;
|
|
103
|
+
path?: string;
|
|
104
|
+
sameSite?: 'strict' | 'lax' | 'none';
|
|
105
|
+
secure?: boolean;
|
|
106
|
+
}
|
|
107
|
+
declare function createBrowserCookieStorage(options?: BrowserCookieStorageOptions): StorageAdapter;
|
|
108
|
+
|
|
109
|
+
interface FetchTransportOptions {
|
|
110
|
+
fetchFn?: typeof fetch;
|
|
111
|
+
timeoutMs?: number;
|
|
112
|
+
retries?: number;
|
|
113
|
+
interceptors?: TransportInterceptor[];
|
|
114
|
+
}
|
|
115
|
+
declare class FetchAuthTransport implements AuthTransport {
|
|
116
|
+
private readonly fetchFn;
|
|
117
|
+
private readonly timeoutMs?;
|
|
118
|
+
private readonly retries;
|
|
119
|
+
private readonly interceptors;
|
|
120
|
+
constructor(options?: FetchTransportOptions);
|
|
121
|
+
request<T = unknown>(url: string, req?: AuthTransportRequest): Promise<AuthTransportResponse<T>>;
|
|
122
|
+
private withQuery;
|
|
123
|
+
private parseBody;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
declare enum AuthErrorCode {
|
|
127
|
+
INVALID_CONFIG = "INVALID_CONFIG",
|
|
128
|
+
STATE_MISMATCH = "STATE_MISMATCH",
|
|
129
|
+
CALLBACK_ERROR = "CALLBACK_ERROR",
|
|
130
|
+
TOKEN_EXCHANGE_FAILED = "TOKEN_EXCHANGE_FAILED",
|
|
131
|
+
REFRESH_FAILED = "REFRESH_FAILED",
|
|
132
|
+
STORAGE_ERROR = "STORAGE_ERROR",
|
|
133
|
+
MISSING_CODE = "MISSING_CODE",
|
|
134
|
+
MISSING_STATE = "MISSING_STATE",
|
|
135
|
+
NETWORK_ERROR = "NETWORK_ERROR",
|
|
136
|
+
HTTP_ERROR = "HTTP_ERROR"
|
|
137
|
+
}
|
|
138
|
+
declare class AuthError extends Error {
|
|
139
|
+
readonly code: AuthErrorCode;
|
|
140
|
+
readonly cause?: unknown | undefined;
|
|
141
|
+
constructor(code: AuthErrorCode, message: string, cause?: unknown | undefined);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
export { type AuthClient, type AuthConfig, AuthError, AuthErrorCode, type AuthTransport, type AuthTransportRequest, type AuthTransportResponse, type BrowserCookieStorageOptions, CookieStorageAdapter, FetchAuthTransport, MemoryStorageAdapter, type Session, type StartLoginOptions, type StorageAdapter, type TokenSet, type TransportInterceptor, WebStorageAdapter, createAuthClient, createBrowserCookieStorage };
|