@vunexa/lixa 0.0.1-alpha.10

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 (44) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +240 -0
  3. package/dist/dao/state-cache.d.ts +10 -0
  4. package/dist/dao/state-cache.d.ts.map +1 -0
  5. package/dist/dao/state-cache.js +18 -0
  6. package/dist/dao/state-cache.js.map +1 -0
  7. package/dist/dao/types.d.ts +6 -0
  8. package/dist/dao/types.d.ts.map +1 -0
  9. package/dist/dao/types.js +2 -0
  10. package/dist/dao/types.js.map +1 -0
  11. package/dist/index.d.ts +12 -0
  12. package/dist/index.d.ts.map +1 -0
  13. package/dist/index.js +10 -0
  14. package/dist/index.js.map +1 -0
  15. package/dist/lixa.d.ts +160 -0
  16. package/dist/lixa.d.ts.map +1 -0
  17. package/dist/lixa.js +283 -0
  18. package/dist/lixa.js.map +1 -0
  19. package/dist/providers/IProvider.d.ts +18 -0
  20. package/dist/providers/IProvider.d.ts.map +1 -0
  21. package/dist/providers/IProvider.js +2 -0
  22. package/dist/providers/IProvider.js.map +1 -0
  23. package/dist/providers/github.d.ts +9 -0
  24. package/dist/providers/github.d.ts.map +1 -0
  25. package/dist/providers/github.js +8 -0
  26. package/dist/providers/github.js.map +1 -0
  27. package/dist/providers/google.d.ts +9 -0
  28. package/dist/providers/google.d.ts.map +1 -0
  29. package/dist/providers/google.js +8 -0
  30. package/dist/providers/google.js.map +1 -0
  31. package/dist/providers/index.d.ts +4 -0
  32. package/dist/providers/index.d.ts.map +1 -0
  33. package/dist/providers/index.js +3 -0
  34. package/dist/providers/index.js.map +1 -0
  35. package/dist/types.d.ts +67 -0
  36. package/dist/types.d.ts.map +1 -0
  37. package/dist/types.js +2 -0
  38. package/dist/types.js.map +1 -0
  39. package/dist/utils/constants.d.ts +4 -0
  40. package/dist/utils/constants.d.ts.map +1 -0
  41. package/dist/utils/constants.js +4 -0
  42. package/dist/utils/constants.js.map +1 -0
  43. package/index.d.ts +261 -0
  44. package/package.json +63 -0
package/index.d.ts ADDED
@@ -0,0 +1,261 @@
1
+ /**
2
+ * A flexible, provider-agnostic OAuth 2.0 and OpenID Connect (OIDC) client library for backend applications.
3
+ *
4
+ * @remarks
5
+ * This package simplifies multi-provider authentication flows (e.g., Google, GitHub), supports extensible session management, and enables custom provider registration.
6
+ *
7
+ * @packageDocumentation
8
+ */
9
+
10
+ /**
11
+ * Type representing the keys of configured providers
12
+ */
13
+ declare type ConfiguredProviderKey<T extends LixaConfig<any>> = keyof T['providers'];
14
+
15
+ /**
16
+ * Interface for OAuth provider implementations.
17
+ *
18
+ * @remarks
19
+ * Implement this interface to add support for custom OAuth providers.
20
+ *
21
+ * @public
22
+ */
23
+ export declare interface IProvider {
24
+ /** The OAuth authorization endpoint URL */
25
+ authorizationEndpoint: string;
26
+ /** The OAuth token exchange endpoint URL */
27
+ tokenEndpoint: string;
28
+ /** The user information endpoint URL */
29
+ userInfoEndpoint: string;
30
+ }
31
+
32
+ /**
33
+ * A flexible, provider-agnostic OAuth 2.0 and OpenID Connect (OIDC) client library.
34
+ *
35
+ * @remarks
36
+ * Lixa simplifies multi-provider authentication flows and supports extensible session management.
37
+ *
38
+ * @example
39
+ * ```typescript
40
+ * import { Lixa } from '@vunexa/lixa';
41
+ * import { GoogleProvider } from '@vunexa/lixa/providers';
42
+ *
43
+ * // Register providers before using them
44
+ * Lixa.registerProvider({ google: new GoogleProvider() });
45
+ *
46
+ * const config = Lixa.createConfig({
47
+ * providers: {
48
+ * google: {
49
+ * clientId: 'your-client-id',
50
+ * clientSecret: 'your-client-secret',
51
+ * redirectUri: 'https://yourapp.com/auth/google/callback',
52
+ * scopes: ['openid', 'email', 'profile']
53
+ * }
54
+ * }
55
+ * });
56
+ *
57
+ * const lixa = new Lixa(config);
58
+ * ```
59
+ *
60
+ * @public
61
+ */
62
+ export declare class Lixa<TConfig extends LixaConfig<any> = LixaConfig> {
63
+ private static CONFIGURED_PROVIDERS;
64
+ private static LOCAL_STATE_CACHE;
65
+ private config;
66
+ private stateDao;
67
+ /**
68
+ * Creates a new Lixa instance with the provided configuration.
69
+ *
70
+ * @param config - The configuration object containing provider settings and optional session strategy
71
+ */
72
+ constructor(config: TConfig);
73
+ /**
74
+ * Checks if a provider is both registered and configured for this instance.
75
+ * This is a type guard that narrows the provider type for use with getAuthUrl.
76
+ *
77
+ * @param provider - The provider name to check (case-insensitive)
78
+ * @returns True if the provider is registered and configured, false otherwise
79
+ *
80
+ * @example
81
+ * ```typescript
82
+ * if (lixa.isProviderConfigured(provider)) {
83
+ * // TypeScript now knows provider is a valid ConfiguredProviderKey
84
+ * const authUrl = lixa.getAuthUrl(provider, state);
85
+ * }
86
+ * ```
87
+ */
88
+ isProviderConfigured<T extends string>(provider: T): provider is T & ConfiguredProviderKey<TConfig>;
89
+ /**
90
+ * Registers custom OAuth providers for use with Lixa.
91
+ *
92
+ * @param providerMap - A map of provider names to IProvider implementations
93
+ *
94
+ * @example
95
+ * ```typescript
96
+ * class CustomProvider implements IProvider {
97
+ * authorizationEndpoint = 'https://custom.com/oauth/authorize';
98
+ * tokenEndpoint = 'https://custom.com/oauth/token';
99
+ * userInfoEndpoint = 'https://custom.com/api/user';
100
+ * }
101
+ *
102
+ * Lixa.registerProvider({ custom: new CustomProvider() });
103
+ * ```
104
+ */
105
+ static registerProvider<T extends Record<string, IProvider>>(providerMap: T): void;
106
+ /**
107
+ * Gets the list of registered provider names.
108
+ *
109
+ * @returns Array of registered provider names
110
+ */
111
+ static getRegisteredProviders(): string[];
112
+ /**
113
+ * Creates a type-safe configuration that only allows registered providers.
114
+ *
115
+ * @param config - Configuration object with providers that must be registered
116
+ * @returns The same configuration object, but with type safety for registered providers
117
+ */
118
+ static createConfig<T extends Record<string, ProviderConfig>>(config: LixaConfig<keyof T & string> & {
119
+ providers: T;
120
+ }): LixaConfig<keyof T & string>;
121
+ /**
122
+ * Generates a cryptographically secure random state parameter for OAuth flows.
123
+ *
124
+ * @returns A 32-character hexadecimal string
125
+ *
126
+ * @remarks
127
+ * The state parameter is used to prevent CSRF attacks in OAuth flows.
128
+ */
129
+ static generateRandomState(): string;
130
+ /**
131
+ * Generates a cryptographically secure code verifier for PKCE flows.
132
+ *
133
+ * @returns A 64-character hexadecimal string
134
+ *
135
+ * @remarks
136
+ * The code verifier is used in PKCE (Proof Key for Code Exchange) to enhance security.
137
+ */
138
+ private static generateCodeVerifier;
139
+ private static buildCodeChallenge;
140
+ /**
141
+ * Generates the authorization URL for the specified provider.
142
+ *
143
+ * @param provider - The provider name (must be a configured provider key)
144
+ * @param state - The state parameter for CSRF protection
145
+ * @returns The complete authorization URL to redirect users to
146
+ *
147
+ * @throws Error when the provider is not configured
148
+ *
149
+ * @example
150
+ * ```typescript
151
+ * const state = Lixa.generateRandomState();
152
+ * const authUrl = lixa.getAuthUrl('google', state);
153
+ * res.redirect(authUrl);
154
+ * ```
155
+ */
156
+ getAuthUrl(provider: ConfiguredProviderKey<TConfig> | string, state: string): string;
157
+ /**
158
+ * Handles the OAuth callback and creates a user session.
159
+ *
160
+ * @param provider - The provider name (must be a configured provider key)
161
+ * @param code - The authorization code from the provider
162
+ * @param state - The state parameter for validation
163
+ * @returns A Promise that resolves to a Session object
164
+ *
165
+ * @throws Error when code or state is missing/invalid, or provider is not configured
166
+ *
167
+ * @example
168
+ * ```typescript
169
+ * const session = await lixa.handleCallback({
170
+ * provider: 'google',
171
+ * code: req.query.code,
172
+ * state: req.query.state
173
+ * });
174
+ * ```
175
+ */
176
+ handleCallback({ provider, code, state, }: {
177
+ provider: ConfiguredProviderKey<TConfig> | string;
178
+ code: string;
179
+ state?: string;
180
+ }): Promise<Session>;
181
+ private exchangeCodeForToken;
182
+ private findProviderByType;
183
+ }
184
+
185
+ /**
186
+ * Main configuration object for Lixa.
187
+ * Only allows providers that have been registered through registerProvider.
188
+ *
189
+ * @public
190
+ */
191
+ export declare interface LixaConfig<TRegisteredProviders extends string = string> {
192
+ /** Map of registered provider names to their configurations */
193
+ providers: Record<TRegisteredProviders, ProviderConfig>;
194
+ /** Optional custom session creation strategy */
195
+ sessionStrategy?: SessionStrategy;
196
+ /** Optional custom state storage implementation */
197
+ stateDao?: StateDao;
198
+ }
199
+
200
+ /**
201
+ * Configuration for an OAuth provider.
202
+ *
203
+ * @public
204
+ */
205
+ export declare interface ProviderConfig {
206
+ /** The OAuth client ID provided by the provider */
207
+ clientId: string;
208
+ /** The OAuth client secret provided by the provider */
209
+ clientSecret: string;
210
+ /** The redirect URI registered with the provider */
211
+ redirectUri: string;
212
+ /** Array of OAuth scopes to request */
213
+ scopes: string[];
214
+ /** Additional provider-specific configuration parameters */
215
+ extraConfig?: Record<string, any>;
216
+ }
217
+
218
+ /**
219
+ * Helper type to create a configuration with only registered providers.
220
+ * Use this with Lixa.createConfig() for type safety.
221
+ *
222
+ * @public
223
+ */
224
+ export declare type SafeLixaConfig<TProviders extends Record<string, ProviderConfig>> = LixaConfig<keyof TProviders & string> & {
225
+ providers: TProviders;
226
+ };
227
+
228
+ /**
229
+ * Represents a user session after successful OAuth authentication.
230
+ *
231
+ * @public
232
+ */
233
+ export declare interface Session {
234
+ /** The session token (typically the access token) */
235
+ token: string;
236
+ /** Raw token data from the OAuth provider */
237
+ raw: any;
238
+ }
239
+
240
+ /**
241
+ * Strategy interface for custom session creation.
242
+ *
243
+ * @public
244
+ */
245
+ export declare interface SessionStrategy {
246
+ /**
247
+ * Creates a session from OAuth token data.
248
+ *
249
+ * @param userInfo - The token data received from the OAuth provider
250
+ * @returns A Promise that resolves to a Session object
251
+ */
252
+ createSession(userInfo: any): Promise<Session>;
253
+ }
254
+
255
+ declare interface StateDao {
256
+ saveState(state: string, data: any, expiresInSeconds: number): Promise<void>;
257
+ getState(state: string): Promise<any | null>;
258
+ deleteState(state: string): Promise<void>;
259
+ }
260
+
261
+ export { }
package/package.json ADDED
@@ -0,0 +1,63 @@
1
+ {
2
+ "name": "@vunexa/lixa",
3
+ "version": "0.0.1-alpha.10",
4
+ "description": "Lixa is a flexible, provider-agnostic OAuth and OpenID Connect (OIDC) client library that simplifies multi-provider authentication flows. It supports seamless integration with providers like Google and GitHub, offers extensible session management, and enables dynamic provider resolution based on callback URLs.",
5
+ "keywords": [
6
+ "oauth",
7
+ "oidc"
8
+ ],
9
+ "license": "MIT",
10
+ "author": "vamsi",
11
+ "type": "module",
12
+ "main": "dist/index.js",
13
+ "types": "index.d.ts",
14
+ "exports": {
15
+ ".": {
16
+ "types": "./index.d.ts",
17
+ "import": "./dist/index.js"
18
+ },
19
+ "./providers": {
20
+ "types": "./providers.d.ts",
21
+ "import": "./dist/providers.js"
22
+ }
23
+ },
24
+ "scripts": {
25
+ "build": "tsc && npm run test && api-extractor run --local",
26
+ "clean": "rm -rf dist",
27
+ "prepublishOnly": "npm run clean && npm run build",
28
+ "lint": "eslint src/**/*.ts",
29
+ "lint:fix": "eslint src/**/*.ts --fix",
30
+ "test": "jest",
31
+ "test:watch": "jest --watch",
32
+ "test:coverage": "jest --coverage",
33
+ "api-extractor": "api-extractor run --local --verbose",
34
+ "docs:api": "npm run build && api-documenter markdown --input temp --output ./docs/api",
35
+ "publish:alpha": "npm run build && npm version prerelease --preid=alpha && npm publish --tag alpha",
36
+ "publish:beta": "npm run build && npm version prerelease --preid=beta && npm publish --tag beta",
37
+ "publish:stable": "npm run build && npm version patch && npm publish --tag latest"
38
+ },
39
+ "files": [
40
+ "dist",
41
+ "index.d.ts",
42
+ "providers.d.ts",
43
+ "README.md",
44
+ "LICENSE"
45
+ ],
46
+ "devDependencies": {
47
+ "@eslint/js": "^9.35.0",
48
+ "@microsoft/api-extractor": "^7.52.11",
49
+ "@types/jest": "^29.5.12",
50
+ "@types/node": "^24.3.1",
51
+ "@types/node-cache": "^4.1.3",
52
+ "@typescript-eslint/eslint-plugin": "^8.42.0",
53
+ "@typescript-eslint/parser": "^8.42.0",
54
+ "eslint": "^9.35.0",
55
+ "globals": "^16.3.0",
56
+ "jest": "^29.7.0",
57
+ "ts-jest": "^29.1.2",
58
+ "typescript": "^5.9.2"
59
+ },
60
+ "dependencies": {
61
+ "node-cache": "^5.1.2"
62
+ }
63
+ }