@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.
- package/LICENSE +21 -0
- package/README.md +240 -0
- package/dist/dao/state-cache.d.ts +10 -0
- package/dist/dao/state-cache.d.ts.map +1 -0
- package/dist/dao/state-cache.js +18 -0
- package/dist/dao/state-cache.js.map +1 -0
- package/dist/dao/types.d.ts +6 -0
- package/dist/dao/types.d.ts.map +1 -0
- package/dist/dao/types.js +2 -0
- package/dist/dao/types.js.map +1 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +10 -0
- package/dist/index.js.map +1 -0
- package/dist/lixa.d.ts +160 -0
- package/dist/lixa.d.ts.map +1 -0
- package/dist/lixa.js +283 -0
- package/dist/lixa.js.map +1 -0
- package/dist/providers/IProvider.d.ts +18 -0
- package/dist/providers/IProvider.d.ts.map +1 -0
- package/dist/providers/IProvider.js +2 -0
- package/dist/providers/IProvider.js.map +1 -0
- package/dist/providers/github.d.ts +9 -0
- package/dist/providers/github.d.ts.map +1 -0
- package/dist/providers/github.js +8 -0
- package/dist/providers/github.js.map +1 -0
- package/dist/providers/google.d.ts +9 -0
- package/dist/providers/google.d.ts.map +1 -0
- package/dist/providers/google.js +8 -0
- package/dist/providers/google.js.map +1 -0
- package/dist/providers/index.d.ts +4 -0
- package/dist/providers/index.d.ts.map +1 -0
- package/dist/providers/index.js +3 -0
- package/dist/providers/index.js.map +1 -0
- package/dist/types.d.ts +67 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/dist/utils/constants.d.ts +4 -0
- package/dist/utils/constants.d.ts.map +1 -0
- package/dist/utils/constants.js +4 -0
- package/dist/utils/constants.js.map +1 -0
- package/index.d.ts +261 -0
- package/package.json +63 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) [2025] [@vunexa/lixa]
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
# @vunexa/lixa
|
|
2
|
+
|
|
3
|
+
> Package is in very early stage and can have frequent breaking changes. DO NOT USE it for production
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/@vunexa/lixa)
|
|
6
|
+
[](https://www.npmjs.com/package/@vunexa/lixa)
|
|
7
|
+
[](https://opensource.org/licenses/MIT)
|
|
8
|
+
|
|
9
|
+
A flexible, provider-agnostic OAuth 2.0 and OpenID Connect (OIDC) client library for backend applications.
|
|
10
|
+
@vunexa/lixa simplifies multi-provider authentication flows (e.g., Google, GitHub), supports extensible session management, and enables custom provider registration.
|
|
11
|
+
|
|
12
|
+
---
|
|
13
|
+
|
|
14
|
+
## Features
|
|
15
|
+
|
|
16
|
+
- Multi-provider OAuth/OIDC support with unified API
|
|
17
|
+
- Built-in support for popular providers like Google and GitHub
|
|
18
|
+
- Custom provider registration with extensible provider interface
|
|
19
|
+
- Extensible session management via pluggable strategies
|
|
20
|
+
- PKCE (Proof Key for Code Exchange) support
|
|
21
|
+
- TypeScript-first with strong typing and async/await support
|
|
22
|
+
- 100% test coverage with comprehensive error handling
|
|
23
|
+
|
|
24
|
+
---
|
|
25
|
+
|
|
26
|
+
## Installation
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
npm install @vunexa/lixa
|
|
30
|
+
# or
|
|
31
|
+
yarn add @vunexa/lixa
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Quick Start
|
|
35
|
+
|
|
36
|
+
### 1. Configure lixa with multiple providers
|
|
37
|
+
|
|
38
|
+
```typescript
|
|
39
|
+
import { Lixa } from '@vunexa/lixa';
|
|
40
|
+
|
|
41
|
+
const lixa = new Lixa({
|
|
42
|
+
providers: {
|
|
43
|
+
google: {
|
|
44
|
+
clientId: process.env.GOOGLE_CLIENT_ID!,
|
|
45
|
+
clientSecret: process.env.GOOGLE_CLIENT_SECRET!,
|
|
46
|
+
redirectUri: 'https://yourapp.com/auth/google/callback',
|
|
47
|
+
scopes: ['openid', 'email', 'profile'],
|
|
48
|
+
extraConfig: {
|
|
49
|
+
prompt: 'consent',
|
|
50
|
+
access_type: 'offline',
|
|
51
|
+
},
|
|
52
|
+
},
|
|
53
|
+
github: {
|
|
54
|
+
clientId: process.env.GITHUB_CLIENT_ID!,
|
|
55
|
+
clientSecret: process.env.GITHUB_CLIENT_SECRET!,
|
|
56
|
+
redirectUri: 'https://yourapp.com/auth/github/callback',
|
|
57
|
+
scopes: ['read:user', 'user:email'],
|
|
58
|
+
extraConfig: {},
|
|
59
|
+
},
|
|
60
|
+
},
|
|
61
|
+
|
|
62
|
+
// Optional: custom session strategy
|
|
63
|
+
sessionStrategy: {
|
|
64
|
+
createSession: async (tokenData) => {
|
|
65
|
+
// Custom session creation logic
|
|
66
|
+
return {
|
|
67
|
+
token: 'custom-session-token',
|
|
68
|
+
raw: tokenData
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
},
|
|
72
|
+
});
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
### 2. Redirect users to the provider's authorization URL
|
|
76
|
+
|
|
77
|
+
```typescript
|
|
78
|
+
app.get("/login", (req, res) => {
|
|
79
|
+
const provider = req.query.provider as string; // 'google' or 'github'
|
|
80
|
+
const state = lixa.generateRandomState();
|
|
81
|
+
|
|
82
|
+
// Store state in session for validation
|
|
83
|
+
req.session.oauthState = state;
|
|
84
|
+
|
|
85
|
+
const authUrl = lixa.getAuthUrl(provider.toUpperCase(), state);
|
|
86
|
+
res.redirect(authUrl);
|
|
87
|
+
});
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
### 3. Handle the provider callback and establish a session
|
|
91
|
+
|
|
92
|
+
```typescript
|
|
93
|
+
app.get("/auth/:provider/callback", async (req, res) => {
|
|
94
|
+
const { code, state } = req.query;
|
|
95
|
+
const provider = req.params.provider;
|
|
96
|
+
|
|
97
|
+
try {
|
|
98
|
+
// Validate state parameter
|
|
99
|
+
if (state !== req.session.oauthState) {
|
|
100
|
+
throw new Error('Invalid state parameter');
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const session = await lixa.handleCallback({
|
|
104
|
+
provider,
|
|
105
|
+
code: code as string,
|
|
106
|
+
state: state as string,
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
// Session established
|
|
110
|
+
res.cookie("session_token", session.token, {
|
|
111
|
+
httpOnly: true,
|
|
112
|
+
secure: true,
|
|
113
|
+
sameSite: 'strict'
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
res.redirect("/dashboard");
|
|
117
|
+
} catch (error) {
|
|
118
|
+
console.error("Authentication error:", error);
|
|
119
|
+
res.status(500).send("Authentication failed");
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
## Advanced Usage
|
|
125
|
+
|
|
126
|
+
### Custom Provider Registration
|
|
127
|
+
|
|
128
|
+
You can register custom OAuth providers by implementing the `IProvider` interface:
|
|
129
|
+
|
|
130
|
+
```typescript
|
|
131
|
+
import { Lixa, IProvider } from '@vunexa/lixa';
|
|
132
|
+
|
|
133
|
+
class CustomProvider implements IProvider {
|
|
134
|
+
authorizationEndpoint = 'https://custom-provider.com/oauth/authorize';
|
|
135
|
+
tokenEndpoint = 'https://custom-provider.com/oauth/token';
|
|
136
|
+
userInfoEndpoint = 'https://custom-provider.com/api/user';
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// Register the custom provider
|
|
140
|
+
Lixa.registerProvider({
|
|
141
|
+
custom: new CustomProvider()
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
// Use it in your configuration
|
|
145
|
+
const lixa = new Lixa({
|
|
146
|
+
providers: {
|
|
147
|
+
custom: {
|
|
148
|
+
clientId: 'your-client-id',
|
|
149
|
+
clientSecret: 'your-client-secret',
|
|
150
|
+
redirectUri: 'https://yourapp.com/auth/custom/callback',
|
|
151
|
+
scopes: ['read:user'],
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
});
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
### Check Provider Registration
|
|
158
|
+
|
|
159
|
+
```typescript
|
|
160
|
+
// Check if a provider is registered
|
|
161
|
+
if (Lixa.isProviderRegistered('google')) {
|
|
162
|
+
console.log('Google provider is available');
|
|
163
|
+
}
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
## API Reference
|
|
167
|
+
|
|
168
|
+
### `Lixa` Class
|
|
169
|
+
|
|
170
|
+
#### Constructor
|
|
171
|
+
- `new Lixa(config: LixaConfig)` - Creates a new Lixa instance
|
|
172
|
+
|
|
173
|
+
#### Static Methods
|
|
174
|
+
- `Lixa.registerProvider(providerMap: { [key: string]: IProvider })` - Register custom providers
|
|
175
|
+
- `Lixa.isProviderRegistered(provider: string): boolean` - Check if a provider is registered
|
|
176
|
+
|
|
177
|
+
#### Instance Methods
|
|
178
|
+
- `generateRandomState(): string` - Generate a random state parameter for OAuth flow
|
|
179
|
+
- `getAuthUrl(provider: string, state: string): string` - Get authorization URL for a provider
|
|
180
|
+
- `handleCallback({ provider, code, state }): Promise<Session>` - Handle OAuth callback and create session
|
|
181
|
+
|
|
182
|
+
### Types
|
|
183
|
+
|
|
184
|
+
```typescript
|
|
185
|
+
interface ProviderConfig {
|
|
186
|
+
clientId: string;
|
|
187
|
+
clientSecret: string;
|
|
188
|
+
redirectUri: string;
|
|
189
|
+
scopes: string[];
|
|
190
|
+
extraConfig?: Record<string, any>;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
interface LixaConfig {
|
|
194
|
+
providers: Record<string, ProviderConfig>;
|
|
195
|
+
sessionStrategy?: SessionStrategy;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
interface SessionStrategy {
|
|
199
|
+
createSession(userInfo: any): Promise<Session>;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
interface Session {
|
|
203
|
+
token: string;
|
|
204
|
+
raw: any;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
interface IProvider {
|
|
208
|
+
authorizationEndpoint: string;
|
|
209
|
+
tokenEndpoint: string;
|
|
210
|
+
userInfoEndpoint: string;
|
|
211
|
+
}
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
## Built-in Providers
|
|
215
|
+
|
|
216
|
+
- **Google** - OAuth 2.0 and OpenID Connect
|
|
217
|
+
- **GitHub** - OAuth 2.0
|
|
218
|
+
|
|
219
|
+
## Development
|
|
220
|
+
|
|
221
|
+
```bash
|
|
222
|
+
# Install dependencies
|
|
223
|
+
npm install
|
|
224
|
+
|
|
225
|
+
# Run tests
|
|
226
|
+
npm test
|
|
227
|
+
|
|
228
|
+
# Run tests with coverage
|
|
229
|
+
npm run test:coverage
|
|
230
|
+
|
|
231
|
+
# Build the library
|
|
232
|
+
npm run build
|
|
233
|
+
|
|
234
|
+
# Lint code
|
|
235
|
+
npm run lint
|
|
236
|
+
```
|
|
237
|
+
|
|
238
|
+
## License
|
|
239
|
+
|
|
240
|
+
MIT
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { StateDao } from "./types";
|
|
2
|
+
declare class LocalStateCache implements StateDao {
|
|
3
|
+
private cache;
|
|
4
|
+
constructor(defaultTtlSeconds?: number);
|
|
5
|
+
saveState(state: string, data: any, expiresInSeconds: number): Promise<void>;
|
|
6
|
+
getState(state: string): Promise<any | null>;
|
|
7
|
+
deleteState(state: string): Promise<void>;
|
|
8
|
+
}
|
|
9
|
+
export { LocalStateCache };
|
|
10
|
+
//# sourceMappingURL=state-cache.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"state-cache.d.ts","sourceRoot":"","sources":["../../src/dao/state-cache.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AAEnC,cAAM,eAAgB,YAAW,QAAQ;IACvC,OAAO,CAAC,KAAK,CAAY;gBAEb,iBAAiB,GAAE,MAAY;IAIrC,SAAS,CAAC,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,GAAG,EAAE,gBAAgB,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAI5E,QAAQ,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,GAAG,GAAG,IAAI,CAAC;IAI5C,WAAW,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;CAGhD;AAED,OAAO,EAAE,eAAe,EAAE,CAAC"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import NodeCache from 'node-cache';
|
|
2
|
+
class LocalStateCache {
|
|
3
|
+
cache;
|
|
4
|
+
constructor(defaultTtlSeconds = 600) {
|
|
5
|
+
this.cache = new NodeCache({ stdTTL: defaultTtlSeconds });
|
|
6
|
+
}
|
|
7
|
+
async saveState(state, data, expiresInSeconds) {
|
|
8
|
+
this.cache.set(state, data, expiresInSeconds);
|
|
9
|
+
}
|
|
10
|
+
async getState(state) {
|
|
11
|
+
return this.cache.get(state) || null;
|
|
12
|
+
}
|
|
13
|
+
async deleteState(state) {
|
|
14
|
+
this.cache.del(state);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
export { LocalStateCache };
|
|
18
|
+
//# sourceMappingURL=state-cache.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"state-cache.js","sourceRoot":"","sources":["../../src/dao/state-cache.ts"],"names":[],"mappings":"AAAA,OAAO,SAAS,MAAM,YAAY,CAAC;AAGnC,MAAM,eAAe;IACX,KAAK,CAAY;IAEzB,YAAY,oBAA4B,GAAG;QACzC,IAAI,CAAC,KAAK,GAAG,IAAI,SAAS,CAAC,EAAE,MAAM,EAAE,iBAAiB,EAAE,CAAC,CAAC;IAC5D,CAAC;IAED,KAAK,CAAC,SAAS,CAAC,KAAa,EAAE,IAAS,EAAE,gBAAwB;QAChE,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,KAAK,EAAE,IAAI,EAAE,gBAAgB,CAAC,CAAC;IAChD,CAAC;IAED,KAAK,CAAC,QAAQ,CAAC,KAAa;QAC1B,OAAO,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,KAAK,CAAC,IAAI,IAAI,CAAC;IACvC,CAAC;IAED,KAAK,CAAC,WAAW,CAAC,KAAa;QAC7B,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;IACxB,CAAC;CACF;AAED,OAAO,EAAE,eAAe,EAAE,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/dao/types.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,QAAQ;IACvB,SAAS,CAAC,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,GAAG,EAAE,gBAAgB,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAC7E,QAAQ,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,GAAG,GAAG,IAAI,CAAC,CAAC;IAC7C,WAAW,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CAC3C"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/dao/types.ts"],"names":[],"mappings":""}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
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
|
+
export { Lixa } from "./lixa";
|
|
10
|
+
export { type ProviderConfig, type LixaConfig, type SessionStrategy, type Session, type SafeLixaConfig, } from "./types";
|
|
11
|
+
export { type IProvider } from "./providers";
|
|
12
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAC;AAC9B,OAAO,EACL,KAAK,cAAc,EACnB,KAAK,UAAU,EACf,KAAK,eAAe,EACpB,KAAK,OAAO,EACZ,KAAK,cAAc,GACpB,MAAM,SAAS,CAAC;AACjB,OAAO,EAAE,KAAK,SAAS,EAAE,MAAM,aAAa,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
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
|
+
export { Lixa } from "./lixa";
|
|
10
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAC"}
|
package/dist/lixa.d.ts
ADDED
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import { type LixaConfig, type ProviderConfig, type Session } from "./types";
|
|
2
|
+
import { IProvider } from "./providers";
|
|
3
|
+
/**
|
|
4
|
+
* Type representing the keys of configured providers
|
|
5
|
+
*/
|
|
6
|
+
type ConfiguredProviderKey<T extends LixaConfig<any>> = keyof T['providers'];
|
|
7
|
+
/**
|
|
8
|
+
* A flexible, provider-agnostic OAuth 2.0 and OpenID Connect (OIDC) client library.
|
|
9
|
+
*
|
|
10
|
+
* @remarks
|
|
11
|
+
* Lixa simplifies multi-provider authentication flows and supports extensible session management.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```typescript
|
|
15
|
+
* import { Lixa } from '@vunexa/lixa';
|
|
16
|
+
* import { GoogleProvider } from '@vunexa/lixa/providers';
|
|
17
|
+
*
|
|
18
|
+
* // Register providers before using them
|
|
19
|
+
* Lixa.registerProvider({ google: new GoogleProvider() });
|
|
20
|
+
*
|
|
21
|
+
* const config = Lixa.createConfig({
|
|
22
|
+
* providers: {
|
|
23
|
+
* google: {
|
|
24
|
+
* clientId: 'your-client-id',
|
|
25
|
+
* clientSecret: 'your-client-secret',
|
|
26
|
+
* redirectUri: 'https://yourapp.com/auth/google/callback',
|
|
27
|
+
* scopes: ['openid', 'email', 'profile']
|
|
28
|
+
* }
|
|
29
|
+
* }
|
|
30
|
+
* });
|
|
31
|
+
*
|
|
32
|
+
* const lixa = new Lixa(config);
|
|
33
|
+
* ```
|
|
34
|
+
*
|
|
35
|
+
* @public
|
|
36
|
+
*/
|
|
37
|
+
declare class Lixa<TConfig extends LixaConfig<any> = LixaConfig> {
|
|
38
|
+
private static CONFIGURED_PROVIDERS;
|
|
39
|
+
private static LOCAL_STATE_CACHE;
|
|
40
|
+
private config;
|
|
41
|
+
private stateDao;
|
|
42
|
+
/**
|
|
43
|
+
* Creates a new Lixa instance with the provided configuration.
|
|
44
|
+
*
|
|
45
|
+
* @param config - The configuration object containing provider settings and optional session strategy
|
|
46
|
+
*/
|
|
47
|
+
constructor(config: TConfig);
|
|
48
|
+
/**
|
|
49
|
+
* Checks if a provider is both registered and configured for this instance.
|
|
50
|
+
* This is a type guard that narrows the provider type for use with getAuthUrl.
|
|
51
|
+
*
|
|
52
|
+
* @param provider - The provider name to check (case-insensitive)
|
|
53
|
+
* @returns True if the provider is registered and configured, false otherwise
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* ```typescript
|
|
57
|
+
* if (lixa.isProviderConfigured(provider)) {
|
|
58
|
+
* // TypeScript now knows provider is a valid ConfiguredProviderKey
|
|
59
|
+
* const authUrl = lixa.getAuthUrl(provider, state);
|
|
60
|
+
* }
|
|
61
|
+
* ```
|
|
62
|
+
*/
|
|
63
|
+
isProviderConfigured<T extends string>(provider: T): provider is T & ConfiguredProviderKey<TConfig>;
|
|
64
|
+
/**
|
|
65
|
+
* Registers custom OAuth providers for use with Lixa.
|
|
66
|
+
*
|
|
67
|
+
* @param providerMap - A map of provider names to IProvider implementations
|
|
68
|
+
*
|
|
69
|
+
* @example
|
|
70
|
+
* ```typescript
|
|
71
|
+
* class CustomProvider implements IProvider {
|
|
72
|
+
* authorizationEndpoint = 'https://custom.com/oauth/authorize';
|
|
73
|
+
* tokenEndpoint = 'https://custom.com/oauth/token';
|
|
74
|
+
* userInfoEndpoint = 'https://custom.com/api/user';
|
|
75
|
+
* }
|
|
76
|
+
*
|
|
77
|
+
* Lixa.registerProvider({ custom: new CustomProvider() });
|
|
78
|
+
* ```
|
|
79
|
+
*/
|
|
80
|
+
static registerProvider<T extends Record<string, IProvider>>(providerMap: T): void;
|
|
81
|
+
/**
|
|
82
|
+
* Gets the list of registered provider names.
|
|
83
|
+
*
|
|
84
|
+
* @returns Array of registered provider names
|
|
85
|
+
*/
|
|
86
|
+
static getRegisteredProviders(): string[];
|
|
87
|
+
/**
|
|
88
|
+
* Creates a type-safe configuration that only allows registered providers.
|
|
89
|
+
*
|
|
90
|
+
* @param config - Configuration object with providers that must be registered
|
|
91
|
+
* @returns The same configuration object, but with type safety for registered providers
|
|
92
|
+
*/
|
|
93
|
+
static createConfig<T extends Record<string, ProviderConfig>>(config: LixaConfig<keyof T & string> & {
|
|
94
|
+
providers: T;
|
|
95
|
+
}): LixaConfig<keyof T & string>;
|
|
96
|
+
/**
|
|
97
|
+
* Generates a cryptographically secure random state parameter for OAuth flows.
|
|
98
|
+
*
|
|
99
|
+
* @returns A 32-character hexadecimal string
|
|
100
|
+
*
|
|
101
|
+
* @remarks
|
|
102
|
+
* The state parameter is used to prevent CSRF attacks in OAuth flows.
|
|
103
|
+
*/
|
|
104
|
+
static generateRandomState(): string;
|
|
105
|
+
/**
|
|
106
|
+
* Generates a cryptographically secure code verifier for PKCE flows.
|
|
107
|
+
*
|
|
108
|
+
* @returns A 64-character hexadecimal string
|
|
109
|
+
*
|
|
110
|
+
* @remarks
|
|
111
|
+
* The code verifier is used in PKCE (Proof Key for Code Exchange) to enhance security.
|
|
112
|
+
*/
|
|
113
|
+
private static generateCodeVerifier;
|
|
114
|
+
private static buildCodeChallenge;
|
|
115
|
+
/**
|
|
116
|
+
* Generates the authorization URL for the specified provider.
|
|
117
|
+
*
|
|
118
|
+
* @param provider - The provider name (must be a configured provider key)
|
|
119
|
+
* @param state - The state parameter for CSRF protection
|
|
120
|
+
* @returns The complete authorization URL to redirect users to
|
|
121
|
+
*
|
|
122
|
+
* @throws Error when the provider is not configured
|
|
123
|
+
*
|
|
124
|
+
* @example
|
|
125
|
+
* ```typescript
|
|
126
|
+
* const state = Lixa.generateRandomState();
|
|
127
|
+
* const authUrl = lixa.getAuthUrl('google', state);
|
|
128
|
+
* res.redirect(authUrl);
|
|
129
|
+
* ```
|
|
130
|
+
*/
|
|
131
|
+
getAuthUrl(provider: ConfiguredProviderKey<TConfig> | string, state: string): string;
|
|
132
|
+
/**
|
|
133
|
+
* Handles the OAuth callback and creates a user session.
|
|
134
|
+
*
|
|
135
|
+
* @param provider - The provider name (must be a configured provider key)
|
|
136
|
+
* @param code - The authorization code from the provider
|
|
137
|
+
* @param state - The state parameter for validation
|
|
138
|
+
* @returns A Promise that resolves to a Session object
|
|
139
|
+
*
|
|
140
|
+
* @throws Error when code or state is missing/invalid, or provider is not configured
|
|
141
|
+
*
|
|
142
|
+
* @example
|
|
143
|
+
* ```typescript
|
|
144
|
+
* const session = await lixa.handleCallback({
|
|
145
|
+
* provider: 'google',
|
|
146
|
+
* code: req.query.code,
|
|
147
|
+
* state: req.query.state
|
|
148
|
+
* });
|
|
149
|
+
* ```
|
|
150
|
+
*/
|
|
151
|
+
handleCallback({ provider, code, state, }: {
|
|
152
|
+
provider: ConfiguredProviderKey<TConfig> | string;
|
|
153
|
+
code: string;
|
|
154
|
+
state?: string;
|
|
155
|
+
}): Promise<Session>;
|
|
156
|
+
private exchangeCodeForToken;
|
|
157
|
+
private findProviderByType;
|
|
158
|
+
}
|
|
159
|
+
export { Lixa };
|
|
160
|
+
//# sourceMappingURL=lixa.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"lixa.d.ts","sourceRoot":"","sources":["../src/lixa.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,UAAU,EAAE,KAAK,cAAc,EAAE,KAAK,OAAO,EAAE,MAAM,SAAS,CAAC;AAC7E,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAUxC;;GAEG;AACH,KAAK,qBAAqB,CAAC,CAAC,SAAS,UAAU,CAAC,GAAG,CAAC,IAAI,MAAM,CAAC,CAAC,WAAW,CAAC,CAAC;AAE7E;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AACH,cAAM,IAAI,CAAC,OAAO,SAAS,UAAU,CAAC,GAAG,CAAC,GAAG,UAAU;IACrD,OAAO,CAAC,MAAM,CAAC,oBAAoB,CAAqC;IACxE,OAAO,CAAC,MAAM,CAAC,iBAAiB,CAAyB;IACzD,OAAO,CAAC,MAAM,CAAU;IACxB,OAAO,CAAC,QAAQ,CAAW;IAE3B;;;;OAIG;gBACS,MAAM,EAAE,OAAO;IAe3B;;;;;;;;;;;;;;OAcG;IACI,oBAAoB,CAAC,CAAC,SAAS,MAAM,EAAE,QAAQ,EAAE,CAAC,GAAG,QAAQ,IAAI,CAAC,GAAG,qBAAqB,CAAC,OAAO,CAAC;IAM1G;;;;;;;;;;;;;;;OAeG;WACW,gBAAgB,CAAC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,EAAE,WAAW,EAAE,CAAC,GAAG,IAAI;IAMzF;;;;OAIG;WACW,sBAAsB,IAAI,MAAM,EAAE;IAIhD;;;;;OAKG;WACW,YAAY,CAAC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,cAAc,CAAC,EACjE,MAAM,EAAE,UAAU,CAAC,MAAM,CAAC,GAAG,MAAM,CAAC,GAAG;QAAE,SAAS,EAAE,CAAC,CAAA;KAAE,GACtD,UAAU,CAAC,MAAM,CAAC,GAAG,MAAM,CAAC;IAc/B;;;;;;;OAOG;WACW,mBAAmB,IAAI,MAAM;IAI3C;;;;;;;OAOG;IACH,OAAO,CAAC,MAAM,CAAC,oBAAoB;IAInC,OAAO,CAAC,MAAM,CAAC,kBAAkB;IAUjC;;;;;;;;;;;;;;;OAeG;IACI,UAAU,CAAC,QAAQ,EAAE,qBAAqB,CAAC,OAAO,CAAC,GAAG,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM;IAsC3F;;;;;;;;;;;;;;;;;;OAkBG;IACU,cAAc,CAAC,EAC1B,QAAQ,EACR,IAAI,EACJ,KAAK,GACN,EAAE;QACD,QAAQ,EAAE,qBAAqB,CAAC,OAAO,CAAC,GAAG,MAAM,CAAC;QAClD,IAAI,EAAE,MAAM,CAAC;QACb,KAAK,CAAC,EAAE,MAAM,CAAC;KAChB,GAAG,OAAO,CAAC,OAAO,CAAC;YAmDN,oBAAoB;IAsClC,OAAO,CAAC,kBAAkB;CAG3B;AAED,OAAO,EAAE,IAAI,EAAE,CAAC"}
|