@vunexa/lixa 0.0.1-alpha.3 → 0.0.1-alpha.33
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 +1081 -76
- package/dist/dao/session-cache.d.ts +10 -0
- package/dist/dao/session-cache.d.ts.map +1 -0
- package/dist/dao/state-cache.d.ts +12 -0
- package/dist/dao/state-cache.d.ts.map +1 -0
- package/dist/dao/types.d.ts +370 -0
- package/dist/dao/types.d.ts.map +1 -0
- package/dist/export-types/index.d.ts +1220 -0
- package/dist/export-types/tsdoc-metadata.json +11 -0
- package/dist/index.cjs +723 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +1189 -0
- package/dist/index.d.ts +19 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +681 -1
- package/dist/index.js.map +1 -1
- package/dist/lixa.d.ts +322 -11
- package/dist/lixa.d.ts.map +1 -1
- package/dist/models/session.d.ts +273 -0
- package/dist/models/session.d.ts.map +1 -0
- package/dist/providers/IProvider.d.ts +128 -0
- package/dist/providers/IProvider.d.ts.map +1 -1
- package/dist/providers/index.d.ts +0 -2
- package/dist/providers/index.d.ts.map +1 -1
- package/dist/types.d.ts +167 -16
- package/dist/types.d.ts.map +1 -1
- package/dist/utils/user-info.d.ts +82 -0
- package/dist/utils/user-info.d.ts.map +1 -0
- package/package.json +20 -10
- package/dist/lixa.js +0 -107
- package/dist/lixa.js.map +0 -1
- package/dist/providers/IProvider.js +0 -3
- package/dist/providers/IProvider.js.map +0 -1
- package/dist/providers/github.d.ts +0 -9
- package/dist/providers/github.d.ts.map +0 -1
- package/dist/providers/github.js +0 -8
- package/dist/providers/github.js.map +0 -1
- package/dist/providers/google.d.ts +0 -9
- package/dist/providers/google.d.ts.map +0 -1
- package/dist/providers/google.js +0 -8
- package/dist/providers/google.js.map +0 -1
- package/dist/providers/index.js +0 -3
- package/dist/providers/index.js.map +0 -1
- package/dist/types.js +0 -2
- package/dist/types.js.map +0 -1
- package/dist/utils/constants.js +0 -4
- package/dist/utils/constants.js.map +0 -1
- package/index.d.ts +0 -63
|
@@ -0,0 +1,273 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* OAuth 2.0 token response structure.
|
|
3
|
+
* Based on RFC 6749 Section 5.1 and OpenID Connect Core 1.0 Section 3.1.3.3
|
|
4
|
+
*
|
|
5
|
+
* @remarks
|
|
6
|
+
* This interface represents the standard OAuth 2.0 token response with
|
|
7
|
+
* optional OpenID Connect extensions. All OAuth providers should return
|
|
8
|
+
* at minimum the required fields (access_token, token_type).
|
|
9
|
+
*
|
|
10
|
+
* @public
|
|
11
|
+
*/
|
|
12
|
+
export interface OAuthTokenResponse {
|
|
13
|
+
/**
|
|
14
|
+
* OAuth 2.0 access token (required).
|
|
15
|
+
* Used to access protected resources on behalf of the user.
|
|
16
|
+
*/
|
|
17
|
+
access_token: string;
|
|
18
|
+
/**
|
|
19
|
+
* Token type (required).
|
|
20
|
+
* Typically "Bearer" for OAuth 2.0.
|
|
21
|
+
*/
|
|
22
|
+
token_type: string;
|
|
23
|
+
/**
|
|
24
|
+
* Token expiration time in seconds (optional).
|
|
25
|
+
* Time until the access token expires.
|
|
26
|
+
*/
|
|
27
|
+
expires_in?: number;
|
|
28
|
+
/**
|
|
29
|
+
* OAuth 2.0 refresh token (optional).
|
|
30
|
+
* Used to obtain new access tokens without re-authentication.
|
|
31
|
+
*/
|
|
32
|
+
refresh_token?: string;
|
|
33
|
+
/**
|
|
34
|
+
* Granted OAuth scopes (optional).
|
|
35
|
+
* Space-separated list of scopes that were granted.
|
|
36
|
+
*/
|
|
37
|
+
scope?: string;
|
|
38
|
+
/**
|
|
39
|
+
* OpenID Connect ID token (optional).
|
|
40
|
+
* JWT containing user identity claims (only present for OIDC providers).
|
|
41
|
+
*/
|
|
42
|
+
id_token?: string;
|
|
43
|
+
/**
|
|
44
|
+
* Additional provider-specific fields.
|
|
45
|
+
* Some providers may include extra fields like user_id, account_id, etc.
|
|
46
|
+
*/
|
|
47
|
+
[key: string]: string | number | boolean | undefined;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Represents a user session after successful OAuth authentication.
|
|
51
|
+
*
|
|
52
|
+
* @remarks
|
|
53
|
+
* The Session object is returned by SessionStrategy.createSession() and contains
|
|
54
|
+
* the session identifier and any additional data needed for your application.
|
|
55
|
+
*
|
|
56
|
+
* The structure is intentionally flexible to support various session management
|
|
57
|
+
* approaches (JWT tokens, session IDs, etc.).
|
|
58
|
+
*
|
|
59
|
+
* @public
|
|
60
|
+
*/
|
|
61
|
+
export interface Session<TRaw = OAuthTokenResponse> {
|
|
62
|
+
/**
|
|
63
|
+
* The session token or identifier.
|
|
64
|
+
* This could be an access token, a session ID, a JWT, or any other identifier
|
|
65
|
+
* that your application uses to track authenticated users.
|
|
66
|
+
*/
|
|
67
|
+
token: string;
|
|
68
|
+
/**
|
|
69
|
+
* Raw session data.
|
|
70
|
+
* Contains the complete OAuth token response and any additional data
|
|
71
|
+
* your SessionStrategy adds (user info, database IDs, etc.).
|
|
72
|
+
*
|
|
73
|
+
* Typical OAuth token data includes:
|
|
74
|
+
* - access_token: OAuth access token
|
|
75
|
+
* - refresh_token: OAuth refresh token (if requested)
|
|
76
|
+
* - expires_in: Token expiration time in seconds
|
|
77
|
+
* - token_type: Token type (usually "Bearer")
|
|
78
|
+
* - id_token: OpenID Connect ID token (if using OIDC)
|
|
79
|
+
* - scope: Granted scopes
|
|
80
|
+
*/
|
|
81
|
+
raw: TRaw;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Provider metadata passed to session strategy.
|
|
85
|
+
* Contains provider name and endpoints for user info extraction.
|
|
86
|
+
*
|
|
87
|
+
* @public
|
|
88
|
+
*/
|
|
89
|
+
export interface ProviderMetadata {
|
|
90
|
+
/** The provider name (e.g., 'google', 'github') */
|
|
91
|
+
name: string;
|
|
92
|
+
/** Provider endpoints */
|
|
93
|
+
endpoints: {
|
|
94
|
+
/** Authorization endpoint URL */
|
|
95
|
+
authorization: string;
|
|
96
|
+
/** Token endpoint URL */
|
|
97
|
+
token: string;
|
|
98
|
+
/** UserInfo endpoint URL */
|
|
99
|
+
userInfo: string;
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Strategy interface for custom session creation.
|
|
104
|
+
*
|
|
105
|
+
* @remarks
|
|
106
|
+
* Implement this interface to customize how OAuth tokens are converted into
|
|
107
|
+
* application sessions. This is where you typically:
|
|
108
|
+
* - Decode ID tokens (for OpenID Connect)
|
|
109
|
+
* - Look up or create users in your database
|
|
110
|
+
* - Generate session identifiers
|
|
111
|
+
* - Store session data
|
|
112
|
+
* - Add custom claims or metadata
|
|
113
|
+
*
|
|
114
|
+
* The default implementation (DefaultSessionStrategy) simply extracts the
|
|
115
|
+
* access token and returns it as the session token.
|
|
116
|
+
*
|
|
117
|
+
* @example
|
|
118
|
+
* Custom session strategy with database integration:
|
|
119
|
+
* ```typescript
|
|
120
|
+
* interface CustomSessionData {
|
|
121
|
+
* userId: string;
|
|
122
|
+
* email: string;
|
|
123
|
+
* provider: string;
|
|
124
|
+
* accessToken: string;
|
|
125
|
+
* refreshToken?: string;
|
|
126
|
+
* expiresAt: number;
|
|
127
|
+
* }
|
|
128
|
+
*
|
|
129
|
+
* class DatabaseSessionStrategy implements SessionStrategy {
|
|
130
|
+
* constructor(private db: Database) {}
|
|
131
|
+
*
|
|
132
|
+
* async createSession(oauthContext: OAuthContext): Promise<Session<CustomSessionData>> {
|
|
133
|
+
* // User info is already extracted by Lixa!
|
|
134
|
+
* const { userInfo, provider, tokenData } = oauthContext;
|
|
135
|
+
*
|
|
136
|
+
* // Create or update user in database
|
|
137
|
+
* const user = await this.db.users.upsert({
|
|
138
|
+
* email: userInfo.email,
|
|
139
|
+
* name: userInfo.name,
|
|
140
|
+
* picture: userInfo.picture
|
|
141
|
+
* });
|
|
142
|
+
*
|
|
143
|
+
* // Generate session ID
|
|
144
|
+
* const sessionId = generateSecureId();
|
|
145
|
+
*
|
|
146
|
+
* // Store session with tokens
|
|
147
|
+
* await this.db.sessions.create({
|
|
148
|
+
* id: sessionId,
|
|
149
|
+
* userId: user.id,
|
|
150
|
+
* accessToken: tokenData.access_token,
|
|
151
|
+
* refreshToken: tokenData.refresh_token,
|
|
152
|
+
* expiresAt: new Date(Date.now() + (tokenData.expires_in || 3600) * 1000)
|
|
153
|
+
* });
|
|
154
|
+
*
|
|
155
|
+
* return {
|
|
156
|
+
* token: sessionId,
|
|
157
|
+
* raw: {
|
|
158
|
+
* userId: user.id,
|
|
159
|
+
* email: user.email,
|
|
160
|
+
* provider,
|
|
161
|
+
* accessToken: tokenData.access_token,
|
|
162
|
+
* refreshToken: tokenData.refresh_token,
|
|
163
|
+
* expiresAt: Date.now() + (tokenData.expires_in || 3600) * 1000
|
|
164
|
+
* }
|
|
165
|
+
* };
|
|
166
|
+
* }
|
|
167
|
+
* }
|
|
168
|
+
* ```
|
|
169
|
+
*
|
|
170
|
+
* @public
|
|
171
|
+
*/
|
|
172
|
+
export interface SessionStrategy {
|
|
173
|
+
/**
|
|
174
|
+
* Creates a session from OAuth token data.
|
|
175
|
+
*
|
|
176
|
+
* @param tokenData - The token data received from the OAuth provider's token endpoint
|
|
177
|
+
* @param providerMetadata - Provider metadata including name and endpoints
|
|
178
|
+
* @returns A Promise that resolves to a Session object
|
|
179
|
+
*
|
|
180
|
+
* @remarks
|
|
181
|
+
* This method is called after successfully exchanging the authorization code
|
|
182
|
+
* for tokens. You receive:
|
|
183
|
+
*
|
|
184
|
+
* Token Data:
|
|
185
|
+
* - access_token: OAuth access token
|
|
186
|
+
* - refresh_token: OAuth refresh token (optional)
|
|
187
|
+
* - expires_in: Token expiration time in seconds
|
|
188
|
+
* - token_type: Token type (usually "Bearer")
|
|
189
|
+
* - id_token: OpenID Connect ID token (for OIDC providers)
|
|
190
|
+
* - scope: Granted scopes
|
|
191
|
+
*
|
|
192
|
+
* Provider Metadata:
|
|
193
|
+
* - name: The provider name (e.g., 'google', 'github')
|
|
194
|
+
* - endpoints: Provider endpoints (authorization, token, userInfo)
|
|
195
|
+
*
|
|
196
|
+
* The providerMetadata.endpoints.userInfo can be used with extractUserInfo():
|
|
197
|
+
* ```typescript
|
|
198
|
+
* import { extractUserInfo } from '@vunexa/lixa';
|
|
199
|
+
*
|
|
200
|
+
* const { userInfo } = await extractUserInfo(
|
|
201
|
+
* tokenData,
|
|
202
|
+
* providerMetadata.endpoints.userInfo,
|
|
203
|
+
* providerMetadata.name
|
|
204
|
+
* );
|
|
205
|
+
* ```
|
|
206
|
+
*
|
|
207
|
+
* Your session strategy should:
|
|
208
|
+
* 1. Extract user info (using extractUserInfo or decode ID token)
|
|
209
|
+
* 2. Create or lookup users in your database
|
|
210
|
+
* 3. Generate session identifiers
|
|
211
|
+
* 4. Store session data as needed
|
|
212
|
+
* 5. Return a Session object with token and raw data
|
|
213
|
+
*
|
|
214
|
+
* @throws \{Error\} If session creation fails (e.g., database error, invalid token)
|
|
215
|
+
*
|
|
216
|
+
* @example
|
|
217
|
+
* Simple implementation:
|
|
218
|
+
* ```typescript
|
|
219
|
+
* async createSession(tokenData: OAuthTokenResponse): Promise<Session> {
|
|
220
|
+
* return {
|
|
221
|
+
* token: tokenData.access_token,
|
|
222
|
+
* raw: tokenData
|
|
223
|
+
* };
|
|
224
|
+
* }
|
|
225
|
+
* ```
|
|
226
|
+
*
|
|
227
|
+
* @example
|
|
228
|
+
* Database integration with user info extraction:
|
|
229
|
+
* ```typescript
|
|
230
|
+
* async createSession(
|
|
231
|
+
* tokenData: OAuthTokenResponse,
|
|
232
|
+
* providerMetadata: ProviderMetadata
|
|
233
|
+
* ): Promise<Session> {
|
|
234
|
+
* // Extract user info from token or userinfo endpoint
|
|
235
|
+
* const { userInfo } = await extractUserInfo(
|
|
236
|
+
* tokenData,
|
|
237
|
+
* providerMetadata.endpoints.userInfo,
|
|
238
|
+
* providerMetadata.name
|
|
239
|
+
* );
|
|
240
|
+
*
|
|
241
|
+
* // Create or update user in database
|
|
242
|
+
* const user = await db.users.upsert({
|
|
243
|
+
* email: userInfo.email,
|
|
244
|
+
* name: userInfo.name
|
|
245
|
+
* });
|
|
246
|
+
*
|
|
247
|
+
* return {
|
|
248
|
+
* token: generateSessionId(),
|
|
249
|
+
* raw: { userId: user.id, provider: providerMetadata.name, ...tokenData }
|
|
250
|
+
* };
|
|
251
|
+
* }
|
|
252
|
+
* ```
|
|
253
|
+
*/
|
|
254
|
+
createSession(tokenData: OAuthTokenResponse, providerMetadata: ProviderMetadata): Promise<Session>;
|
|
255
|
+
}
|
|
256
|
+
/**
|
|
257
|
+
* Default session strategy that works with any OAuth provider.
|
|
258
|
+
* Extracts common token information and creates a standardized session.
|
|
259
|
+
*
|
|
260
|
+
* @public
|
|
261
|
+
*/
|
|
262
|
+
export declare class DefaultSessionStrategy implements SessionStrategy {
|
|
263
|
+
/**
|
|
264
|
+
* Creates a session from OAuth token data.
|
|
265
|
+
* Handles common OAuth token formats and extracts the access token.
|
|
266
|
+
*
|
|
267
|
+
* @param tokenData - The token data received from the OAuth provider
|
|
268
|
+
* @param providerMetadata - Provider metadata (not used in default implementation)
|
|
269
|
+
* @returns A Promise that resolves to a Session object
|
|
270
|
+
*/
|
|
271
|
+
createSession(tokenData: OAuthTokenResponse, providerMetadata: ProviderMetadata): Promise<Session>;
|
|
272
|
+
}
|
|
273
|
+
//# sourceMappingURL=session.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"session.d.ts","sourceRoot":"","sources":["../../src/models/session.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AACH,MAAM,WAAW,kBAAkB;IACjC;;;OAGG;IACH,YAAY,EAAE,MAAM,CAAC;IAErB;;;OAGG;IACH,UAAU,EAAE,MAAM,CAAC;IAEnB;;;OAGG;IACH,UAAU,CAAC,EAAE,MAAM,CAAC;IAEpB;;;OAGG;IACH,aAAa,CAAC,EAAE,MAAM,CAAC;IAEvB;;;OAGG;IACH,KA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|
+
*
|
|
4
|
+
* @remarks
|
|
5
|
+
* Implement this interface to add support for custom OAuth providers.
|
|
6
|
+
* Each provider defines the three core endpoints required for the OAuth 2.0
|
|
7
|
+
* authorization code flow with PKCE.
|
|
8
|
+
*
|
|
9
|
+
* Built-in providers (Google, GitHub) are available in the \@vunexa/lixa-providers package.
|
|
10
|
+
*
|
|
11
|
+
* All implementations must comply with:
|
|
12
|
+
* - RFC 6749 (OAuth 2.0)
|
|
13
|
+
* - RFC 7636 (PKCE)
|
|
14
|
+
* - OpenID Connect Core 1.0 (for OIDC providers)
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* Custom provider implementation:
|
|
18
|
+
* ```typescript
|
|
19
|
+
* import { IProvider } from '@vunexa/lixa';
|
|
20
|
+
*
|
|
21
|
+
* class CustomProvider implements IProvider {
|
|
22
|
+
* authorizationEndpoint = 'https://auth.example.com/oauth/authorize';
|
|
23
|
+
* tokenEndpoint = 'https://auth.example.com/oauth/token';
|
|
24
|
+
* userInfoEndpoint = 'https://api.example.com/user';
|
|
25
|
+
* }
|
|
26
|
+
*
|
|
27
|
+
* // Use in configuration
|
|
28
|
+
* const lixa = new Lixa({
|
|
29
|
+
* providers: {
|
|
30
|
+
* custom: {
|
|
31
|
+
* provider: new CustomProvider(),
|
|
32
|
+
* clientId: 'your-client-id',
|
|
33
|
+
* clientSecret: 'your-client-secret',
|
|
34
|
+
* redirectUri: 'https://app.com/callback',
|
|
35
|
+
* scopes: ['read:user']
|
|
36
|
+
* }
|
|
37
|
+
* }
|
|
38
|
+
* });
|
|
39
|
+
* ```
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* Object literal provider:
|
|
43
|
+
* ```typescript
|
|
44
|
+
* const customProvider: IProvider = {
|
|
45
|
+
* authorizationEndpoint: 'https://auth.example.com/oauth/authorize',
|
|
46
|
+
* tokenEndpoint: 'https://auth.example.com/oauth/token',
|
|
47
|
+
* userInfoEndpoint: 'https://api.example.com/user'
|
|
48
|
+
* };
|
|
49
|
+
* ```
|
|
50
|
+
*
|
|
51
|
+
* @public
|
|
52
|
+
*/
|
|
1
53
|
interface IProvider {
|
|
54
|
+
/**
|
|
55
|
+
* The OAuth 2.0 authorization endpoint URL.
|
|
56
|
+
*
|
|
57
|
+
* @remarks
|
|
58
|
+
* This is the URL where users are redirected to authenticate and authorize your application.
|
|
59
|
+
* The endpoint must support the OAuth 2.0 authorization code flow with PKCE.
|
|
60
|
+
*
|
|
61
|
+
* Standard query parameters sent to this endpoint:
|
|
62
|
+
* - client_id: Your application's client ID
|
|
63
|
+
* - redirect_uri: Where to redirect after authorization
|
|
64
|
+
* - response_type: Always "code" for authorization code flow
|
|
65
|
+
* - scope: Space-separated list of requested scopes
|
|
66
|
+
* - state: Random string for CSRF protection
|
|
67
|
+
* - code_challenge: PKCE code challenge (SHA-256 hash)
|
|
68
|
+
* - code_challenge_method: Always "S256" for SHA-256
|
|
69
|
+
*
|
|
70
|
+
* @example
|
|
71
|
+
* ```typescript
|
|
72
|
+
* authorizationEndpoint = 'https://accounts.google.com/o/oauth2/v2/auth'
|
|
73
|
+
* ```
|
|
74
|
+
*/
|
|
2
75
|
authorizationEndpoint: string;
|
|
76
|
+
/**
|
|
77
|
+
* The OAuth 2.0 token endpoint URL.
|
|
78
|
+
*
|
|
79
|
+
* @remarks
|
|
80
|
+
* This is the URL where authorization codes are exchanged for access tokens.
|
|
81
|
+
* The endpoint must support the OAuth 2.0 token exchange with PKCE.
|
|
82
|
+
*
|
|
83
|
+
* Standard parameters sent to this endpoint (POST request):
|
|
84
|
+
* - grant_type: Always "authorization_code"
|
|
85
|
+
* - code: The authorization code from the callback
|
|
86
|
+
* - redirect_uri: Must match the authorization request
|
|
87
|
+
* - client_id: Your application's client ID
|
|
88
|
+
* - client_secret: Your application's client secret
|
|
89
|
+
* - code_verifier: PKCE code verifier (original random string)
|
|
90
|
+
*
|
|
91
|
+
* Expected response:
|
|
92
|
+
* - access_token: OAuth access token
|
|
93
|
+
* - token_type: Token type (usually "Bearer")
|
|
94
|
+
* - expires_in: Token expiration time in seconds
|
|
95
|
+
* - refresh_token: Refresh token (optional)
|
|
96
|
+
* - id_token: OpenID Connect ID token (for OIDC providers)
|
|
97
|
+
* - scope: Granted scopes
|
|
98
|
+
*
|
|
99
|
+
* @example
|
|
100
|
+
* ```typescript
|
|
101
|
+
* tokenEndpoint = 'https://oauth2.googleapis.com/token'
|
|
102
|
+
* ```
|
|
103
|
+
*/
|
|
3
104
|
tokenEndpoint: string;
|
|
105
|
+
/**
|
|
106
|
+
* The user information endpoint URL.
|
|
107
|
+
*
|
|
108
|
+
* @remarks
|
|
109
|
+
* This is the URL where user profile information can be retrieved using the access token.
|
|
110
|
+
* For OpenID Connect providers, this is the UserInfo endpoint.
|
|
111
|
+
*
|
|
112
|
+
* The endpoint is called with the access token in the Authorization header:
|
|
113
|
+
* ```
|
|
114
|
+
* Authorization: Bearer <access_token>
|
|
115
|
+
* ```
|
|
116
|
+
*
|
|
117
|
+
* Common response fields:
|
|
118
|
+
* - sub: Subject identifier (user ID)
|
|
119
|
+
* - email: User's email address
|
|
120
|
+
* - name: User's full name
|
|
121
|
+
* - picture: User's profile picture URL
|
|
122
|
+
* - email_verified: Whether email is verified
|
|
123
|
+
*
|
|
124
|
+
* Note: This endpoint is not called automatically by Lixa. Your SessionStrategy
|
|
125
|
+
* can call it if needed to fetch user profile information.
|
|
126
|
+
*
|
|
127
|
+
* @example
|
|
128
|
+
* ```typescript
|
|
129
|
+
* userInfoEndpoint = 'https://www.googleapis.com/oauth2/v2/userinfo'
|
|
130
|
+
* ```
|
|
131
|
+
*/
|
|
4
132
|
userInfoEndpoint: string;
|
|
5
133
|
}
|
|
6
134
|
export { IProvider };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"IProvider.d.ts","sourceRoot":"","sources":["../../src/providers/IProvider.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"IProvider.d.ts","sourceRoot":"","sources":["../../src/providers/IProvider.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmDG;AACH,UAAU,SAAS;IACjB;;;;;;;;;;;;;;;;;;;;OAoBG;IACH,qBAAqB,EAAE,MAAM,CAAC;IAE9B;;;;;;;;;;;;;;;;;;;;;;;;;;;OA2BG;IACH,aAAa,EAAE,MAAM,CAAC;IAEtB;;;;;;;;;;;;;;;;;;;;;;;;;;OA0BG;IACH,gBAAgB,EAAE,MAAM,CAAC;CAC1B;AAED,OAAO,EAAE,SAAS,EAAE,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/providers/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/providers/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC"}
|
package/dist/types.d.ts
CHANGED
|
@@ -1,31 +1,182 @@
|
|
|
1
|
+
import { SessionHandler, SessionStorage, StateHandler, StateStorage, StateData } from "./dao/types";
|
|
2
|
+
import { Session, ProviderMetadata } from "./models/session";
|
|
3
|
+
import { IProvider } from "./providers/IProvider";
|
|
4
|
+
export { Session };
|
|
5
|
+
export { ProviderMetadata };
|
|
6
|
+
export { OAuthTokenResponse } from "./models/session";
|
|
7
|
+
export { IProvider };
|
|
8
|
+
export { StateHandler };
|
|
9
|
+
export { StateStorage };
|
|
10
|
+
export { SessionHandler };
|
|
11
|
+
export { SessionStorage };
|
|
12
|
+
export { StateData };
|
|
1
13
|
/**
|
|
14
|
+
* Configuration for an OAuth provider instance.
|
|
15
|
+
*
|
|
16
|
+
* @remarks
|
|
17
|
+
* For built-in providers (google, github), just provide credentials.
|
|
18
|
+
* For custom providers, include the provider implementation.
|
|
19
|
+
*
|
|
20
|
+
* The provider field uses a discriminated union to ensure type safety:
|
|
21
|
+
* - When omitted or undefined: assumes a built-in provider
|
|
22
|
+
* - When provided: must be a valid IProvider implementation
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* Built-in provider configuration:
|
|
26
|
+
* ```typescript
|
|
27
|
+
* {
|
|
28
|
+
* clientId: 'your-client-id',
|
|
29
|
+
* clientSecret: 'your-client-secret',
|
|
30
|
+
* redirectUri: 'https://app.com/callback',
|
|
31
|
+
* scopes: ['openid', 'email']
|
|
32
|
+
* }
|
|
33
|
+
* ```
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* Custom provider configuration:
|
|
37
|
+
* ```typescript
|
|
38
|
+
* {
|
|
39
|
+
* provider: new CustomProvider(),
|
|
40
|
+
* clientId: 'your-client-id',
|
|
41
|
+
* clientSecret: 'your-client-secret',
|
|
42
|
+
* redirectUri: 'https://app.com/callback',
|
|
43
|
+
* scopes: ['read:user']
|
|
44
|
+
* }
|
|
45
|
+
* ```
|
|
46
|
+
*
|
|
2
47
|
* @public
|
|
3
48
|
*/
|
|
4
|
-
export
|
|
49
|
+
export type ProviderConfig = {
|
|
50
|
+
/** The OAuth client ID provided by the provider */
|
|
5
51
|
clientId: string;
|
|
52
|
+
/** The OAuth client secret provided by the provider */
|
|
6
53
|
clientSecret: string;
|
|
54
|
+
/** The redirect URI registered with the provider */
|
|
7
55
|
redirectUri: string;
|
|
56
|
+
/** Array of OAuth scopes to request */
|
|
8
57
|
scopes: string[];
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
sessionStrategy?: SessionStrategy;
|
|
17
|
-
}
|
|
58
|
+
/** Additional provider-specific configuration parameters */
|
|
59
|
+
extraConfig?: Record<string, string>;
|
|
60
|
+
} & ({
|
|
61
|
+
provider?: never;
|
|
62
|
+
} | {
|
|
63
|
+
provider: IProvider;
|
|
64
|
+
});
|
|
18
65
|
/**
|
|
66
|
+
* Main configuration object for Lixa.
|
|
67
|
+
* Provides type-safe provider name inference.
|
|
68
|
+
*
|
|
69
|
+
* @remarks
|
|
70
|
+
* The generic type parameter TProviders enables TypeScript to infer provider names
|
|
71
|
+
* from the configuration object, providing autocomplete and type checking for
|
|
72
|
+
* provider names in methods like getAuthUrl() and handleCallback().
|
|
73
|
+
*
|
|
74
|
+
* @typeParam TProviders - The provider configuration map type, defaults to a generic record
|
|
75
|
+
*
|
|
76
|
+
* @example
|
|
77
|
+
* Basic configuration with built-in providers:
|
|
78
|
+
* ```typescript
|
|
79
|
+
* import { Lixa } from '@vunexa/lixa';
|
|
80
|
+
* import { GoogleProvider } from '@vunexa/lixa-providers';
|
|
81
|
+
*
|
|
82
|
+
* const lixa = new Lixa({
|
|
83
|
+
* providers: {
|
|
84
|
+
* google: {
|
|
85
|
+
* provider: new GoogleProvider(),
|
|
86
|
+
* clientId: process.env.GOOGLE_CLIENT_ID!,
|
|
87
|
+
* clientSecret: process.env.GOOGLE_CLIENT_SECRET!,
|
|
88
|
+
* redirectUri: 'https://app.com/auth/google/callback',
|
|
89
|
+
* scopes: ['openid', 'email', 'profile']
|
|
90
|
+
* }
|
|
91
|
+
* }
|
|
92
|
+
* });
|
|
93
|
+
* ```
|
|
94
|
+
*
|
|
95
|
+
* @example
|
|
96
|
+
* Configuration with custom session and state handlers:
|
|
97
|
+
* ```typescript
|
|
98
|
+
* const lixa = new Lixa({
|
|
99
|
+
* providers: {
|
|
100
|
+
* google: {
|
|
101
|
+
* provider: new GoogleProvider(),
|
|
102
|
+
* clientId: process.env.GOOGLE_CLIENT_ID!,
|
|
103
|
+
* clientSecret: process.env.GOOGLE_CLIENT_SECRET!,
|
|
104
|
+
* redirectUri: 'https://app.com/auth/google/callback',
|
|
105
|
+
* scopes: ['openid', 'email', 'profile']
|
|
106
|
+
* }
|
|
107
|
+
* },
|
|
108
|
+
* stateHandler: {
|
|
109
|
+
* storage: {
|
|
110
|
+
* saveState: async (state, data, ttl) => await redis.setex(state, ttl, JSON.stringify(data)),
|
|
111
|
+
* getState: async (state) => JSON.parse(await redis.get(state) || 'null'),
|
|
112
|
+
* deleteState: async (state) => await redis.del(state)
|
|
113
|
+
* }
|
|
114
|
+
* },
|
|
115
|
+
* sessionHandler: {
|
|
116
|
+
* GenerateSession: async (tokenData, providerMetadata) => {
|
|
117
|
+
* const { userInfo } = await extractUserInfo(tokenData, providerMetadata);
|
|
118
|
+
* const user = await db.users.upsert({ email: userInfo.email });
|
|
119
|
+
* return { token: tokenData.access_token, raw: { ...tokenData, userId: user.id } };
|
|
120
|
+
* },
|
|
121
|
+
* storage: {
|
|
122
|
+
* saveSession: async (id, session, ttl) => await db.sessions.create({ id, session, ttl }),
|
|
123
|
+
* getSession: async (id) => await db.sessions.findOne({ id }),
|
|
124
|
+
* deleteSession: async (id) => await db.sessions.delete({ id })
|
|
125
|
+
* }
|
|
126
|
+
* },
|
|
127
|
+
* debug: true
|
|
128
|
+
* });
|
|
129
|
+
* ```
|
|
130
|
+
*
|
|
19
131
|
* @public
|
|
20
132
|
*/
|
|
21
|
-
export interface
|
|
22
|
-
|
|
133
|
+
export interface LixaConfig<TProviders extends Record<string, ProviderConfig> = Record<string, ProviderConfig>> {
|
|
134
|
+
/**
|
|
135
|
+
* Map of provider names to their configurations.
|
|
136
|
+
* Provider names will be available for autocomplete in getAuthUrl() and handleCallback().
|
|
137
|
+
*/
|
|
138
|
+
providers: TProviders;
|
|
139
|
+
/**
|
|
140
|
+
* Optional custom state handler.
|
|
141
|
+
* Handles state generation and storage during OAuth authorization flow.
|
|
142
|
+
*
|
|
143
|
+
* - GenerateState: Customizes how state parameters and PKCE verifiers are generated
|
|
144
|
+
* - storage: Provides persistent state storage (save/get/delete operations)
|
|
145
|
+
*
|
|
146
|
+
* Defaults to in-memory cache if not provided (not suitable for production).
|
|
147
|
+
*
|
|
148
|
+
* @see {@link StateHandler}
|
|
149
|
+
*/
|
|
150
|
+
stateHandler?: StateHandler;
|
|
151
|
+
/**
|
|
152
|
+
* Optional custom session handler.
|
|
153
|
+
* Handles session generation and storage after authentication.
|
|
154
|
+
*
|
|
155
|
+
* - GenerateSession: Customizes how OAuth tokens are converted into session data
|
|
156
|
+
* - storage: Provides persistent session storage (save/get/delete operations)
|
|
157
|
+
*
|
|
158
|
+
* Defaults to in-memory cache if not provided (not suitable for production).
|
|
159
|
+
*
|
|
160
|
+
* @see {@link SessionHandler}
|
|
161
|
+
*/
|
|
162
|
+
sessionHandler?: SessionHandler;
|
|
163
|
+
/**
|
|
164
|
+
* Enable debug logging.
|
|
165
|
+
* When enabled, outputs structured logs for initialization, auth flow, and errors.
|
|
166
|
+
* Format: [Lixa] [timestamp] [level] [context] message
|
|
167
|
+
*/
|
|
168
|
+
debug?: boolean;
|
|
23
169
|
}
|
|
24
170
|
/**
|
|
171
|
+
* Helper type to create a configuration with only registered providers.
|
|
172
|
+
* Use this with Lixa.createConfig() for type safety.
|
|
173
|
+
*
|
|
174
|
+
* @deprecated This type is maintained for backward compatibility.
|
|
175
|
+
* The new inline provider configuration pattern makes this unnecessary.
|
|
176
|
+
*
|
|
25
177
|
* @public
|
|
26
178
|
*/
|
|
27
|
-
export
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
}
|
|
179
|
+
export type SafeLixaConfig<TProviders extends Record<string, ProviderConfig>> = LixaConfig<TProviders> & {
|
|
180
|
+
providers: TProviders;
|
|
181
|
+
};
|
|
31
182
|
//# sourceMappingURL=types.d.ts.map
|
package/dist/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,cAAc,EAAE,YAAY,EAAE,YAAY,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AACpG,OAAO,EAAE,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