@soapjs/soap-auth 0.1.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 +223 -0
- package/build/errors.d.ts +53 -0
- package/build/errors.js +117 -0
- package/build/factories/auth-strategy.factory.d.ts +9 -0
- package/build/factories/auth-strategy.factory.js +16 -0
- package/build/factories/http-auth-strategy.factory.d.ts +5 -0
- package/build/factories/http-auth-strategy.factory.js +42 -0
- package/build/factories/socket-auth-strategy.factory.d.ts +5 -0
- package/build/factories/socket-auth-strategy.factory.js +27 -0
- package/build/index.d.ts +28 -0
- package/build/index.js +44 -0
- package/build/session/file.session-store.d.ts +10 -0
- package/build/session/file.session-store.js +59 -0
- package/build/session/memory.session-store.d.ts +7 -0
- package/build/session/memory.session-store.js +19 -0
- package/build/session/session-handler.d.ts +17 -0
- package/build/session/session-handler.js +145 -0
- package/build/soap-auth.d.ts +16 -0
- package/build/soap-auth.js +75 -0
- package/build/strategies/api-key/api-key.errors.d.ts +9 -0
- package/build/strategies/api-key/api-key.errors.js +24 -0
- package/build/strategies/api-key/api-key.strategy.d.ts +14 -0
- package/build/strategies/api-key/api-key.strategy.js +95 -0
- package/build/strategies/api-key/api-key.types.d.ts +13 -0
- package/build/strategies/api-key/api-key.types.js +2 -0
- package/build/strategies/base-auth.strategy.d.ts +17 -0
- package/build/strategies/base-auth.strategy.js +69 -0
- package/build/strategies/basic/basic.strategy.d.ts +25 -0
- package/build/strategies/basic/basic.strategy.js +53 -0
- package/build/strategies/basic/basic.types.d.ts +10 -0
- package/build/strategies/basic/basic.types.js +2 -0
- package/build/strategies/credential-based-auth.strategy.d.ts +30 -0
- package/build/strategies/credential-based-auth.strategy.js +205 -0
- package/build/strategies/jwt/jwt.strategy.d.ts +10 -0
- package/build/strategies/jwt/jwt.strategy.js +69 -0
- package/build/strategies/jwt/jwt.tools.d.ts +3 -0
- package/build/strategies/jwt/jwt.tools.js +79 -0
- package/build/strategies/jwt/jwt.types.d.ts +33 -0
- package/build/strategies/jwt/jwt.types.js +2 -0
- package/build/strategies/local/local.strategy.d.ts +25 -0
- package/build/strategies/local/local.strategy.js +76 -0
- package/build/strategies/local/local.types.d.ts +3 -0
- package/build/strategies/local/local.types.js +2 -0
- package/build/strategies/oauth2/oauth2.strategy.d.ts +35 -0
- package/build/strategies/oauth2/oauth2.strategy.js +259 -0
- package/build/strategies/oauth2/oauth2.tools.d.ts +4 -0
- package/build/strategies/oauth2/oauth2.tools.js +22 -0
- package/build/strategies/oauth2/oauth2.types.d.ts +29 -0
- package/build/strategies/oauth2/oauth2.types.js +2 -0
- package/build/strategies/token-based-auth.strategy.d.ts +25 -0
- package/build/strategies/token-based-auth.strategy.js +124 -0
- package/build/tools/session.tools.d.ts +6 -0
- package/build/tools/session.tools.js +15 -0
- package/build/tools/token.tools.d.ts +7 -0
- package/build/tools/token.tools.js +32 -0
- package/build/tools/tools.d.ts +3 -0
- package/build/tools/tools.js +23 -0
- package/build/types.d.ts +251 -0
- package/build/types.js +2 -0
- package/jest.config.unit.json +10 -0
- package/ldap.md +62 -0
- package/package.json +33 -0
- package/saml.md +244 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Radosław Kamysz
|
|
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,223 @@
|
|
|
1
|
+
# SoapAuth
|
|
2
|
+
|
|
3
|
+
**SoapAuth** is a flexible authentication and authorization module designed to provide support for various authentication strategies. It is intended to be used alongside a specific framework adapter, serving as the core authentication engine.
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
npm install @soapjs/soap-auth
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
---
|
|
14
|
+
|
|
15
|
+
## Features
|
|
16
|
+
|
|
17
|
+
- Multiple authentication strategies (local, OAuth2, JWT, API key, basic auth)
|
|
18
|
+
- Custom authentication strategy support
|
|
19
|
+
- Built-in session management with pluggable session stores
|
|
20
|
+
- Token handling for JWT authentication
|
|
21
|
+
- Extensible architecture with abstract base classes
|
|
22
|
+
- Compatible with framework adapters for seamless integration
|
|
23
|
+
|
|
24
|
+
---
|
|
25
|
+
|
|
26
|
+
## Core Classes Overview
|
|
27
|
+
|
|
28
|
+
### 1. `SoapAuth`
|
|
29
|
+
This is the main entry point for managing authentication. It initializes different strategies based on the provided configuration and provides methods for authentication, authorization, and session management.
|
|
30
|
+
|
|
31
|
+
### 2. `AuthStrategy`
|
|
32
|
+
The base interface that all strategies must implement. Provides methods such as `authenticate`, `authorize`, `init`, and `logout`.
|
|
33
|
+
|
|
34
|
+
### 3. `SessionHandler`
|
|
35
|
+
Handles session operations like retrieving, storing, and generating session IDs. It supports multiple session storage mechanisms such as in-memory and file-based stores.
|
|
36
|
+
|
|
37
|
+
### 4. `TokenBasedAuthStrategy`
|
|
38
|
+
Abstract class for token-based authentication strategies, providing methods for handling access and refresh tokens.
|
|
39
|
+
|
|
40
|
+
### 5. `CredentialBasedAuthStrategy`
|
|
41
|
+
Abstract class for username-password-based authentication strategies.
|
|
42
|
+
|
|
43
|
+
### 6. `HttpAuthStrategyFactory` & `SocketAuthStrategyFactory`
|
|
44
|
+
Factories responsible for creating instances of HTTP and socket-based authentication strategies respectively.
|
|
45
|
+
|
|
46
|
+
---
|
|
47
|
+
|
|
48
|
+
## Available Authentication Strategies
|
|
49
|
+
|
|
50
|
+
### 1. Local Strategy
|
|
51
|
+
|
|
52
|
+
**When to use:**
|
|
53
|
+
- Ideal for applications that require username and password authentication stored locally.
|
|
54
|
+
|
|
55
|
+
**Configuration example:**
|
|
56
|
+
```typescript
|
|
57
|
+
const authConfig = {
|
|
58
|
+
http: {
|
|
59
|
+
local: {
|
|
60
|
+
usernameField: "email",
|
|
61
|
+
passwordField: "password",
|
|
62
|
+
validateUser: async (username, password) => {
|
|
63
|
+
return username === "user@example.com" && password === "securepass" ? { id: 1, name: "John Doe" } : null;
|
|
64
|
+
},
|
|
65
|
+
},
|
|
66
|
+
},
|
|
67
|
+
};
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
### 2. OAuth2 Strategy
|
|
71
|
+
|
|
72
|
+
**When to use:**
|
|
73
|
+
- Suitable for social logins (Google, Facebook, Twitter, etc.).
|
|
74
|
+
|
|
75
|
+
**Configuration example:**
|
|
76
|
+
```typescript
|
|
77
|
+
const authConfig = {
|
|
78
|
+
http: {
|
|
79
|
+
oauth2: {
|
|
80
|
+
google: {
|
|
81
|
+
clientId: "your-client-id",
|
|
82
|
+
clientSecret: "your-client-secret",
|
|
83
|
+
redirectUri: "https://yourapp.com/auth/callback",
|
|
84
|
+
scope: "openid profile email",
|
|
85
|
+
endpoints: {
|
|
86
|
+
authorizationUrl: "https://accounts.google.com/o/oauth2/auth",
|
|
87
|
+
tokenUrl: "https://oauth2.googleapis.com/token",
|
|
88
|
+
},
|
|
89
|
+
},
|
|
90
|
+
},
|
|
91
|
+
},
|
|
92
|
+
};
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
### 3. JWT Strategy
|
|
96
|
+
|
|
97
|
+
**When to use:**
|
|
98
|
+
- Suitable for stateless authentication where tokens are passed between the client and server.
|
|
99
|
+
|
|
100
|
+
**Configuration example:**
|
|
101
|
+
```typescript
|
|
102
|
+
const authConfig = {
|
|
103
|
+
http: {
|
|
104
|
+
jwt: {
|
|
105
|
+
access: {
|
|
106
|
+
secretKey: "your-secret-key",
|
|
107
|
+
expiresIn: "1h",
|
|
108
|
+
signOptions: { algorithm: "HS256" },
|
|
109
|
+
},
|
|
110
|
+
},
|
|
111
|
+
},
|
|
112
|
+
};
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
### 4. API Key Strategy
|
|
116
|
+
|
|
117
|
+
**When to use:**
|
|
118
|
+
- Best for authenticating external services using static API keys.
|
|
119
|
+
|
|
120
|
+
**Configuration example:**
|
|
121
|
+
```typescript
|
|
122
|
+
const authConfig = {
|
|
123
|
+
http: {
|
|
124
|
+
apiKey: {
|
|
125
|
+
extractApiKey: (context) => context.headers["x-api-key"],
|
|
126
|
+
retrieveUserByApiKey: async (apiKey) => {
|
|
127
|
+
return apiKey === "valid-api-key" ? { id: 1, role: "admin" } : null;
|
|
128
|
+
},
|
|
129
|
+
},
|
|
130
|
+
},
|
|
131
|
+
};
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
### 5. Basic Authentication
|
|
135
|
+
|
|
136
|
+
**When to use:**
|
|
137
|
+
- Useful for simple username-password authentication with minimal overhead.
|
|
138
|
+
|
|
139
|
+
**Configuration example:**
|
|
140
|
+
```typescript
|
|
141
|
+
const authConfig = {
|
|
142
|
+
http: {
|
|
143
|
+
basic: {
|
|
144
|
+
validateUser: async (username, password) => {
|
|
145
|
+
return username === "admin" && password === "password" ? { id: 1, role: "admin" } : null;
|
|
146
|
+
},
|
|
147
|
+
},
|
|
148
|
+
},
|
|
149
|
+
};
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
---
|
|
153
|
+
|
|
154
|
+
## Using SoapAuth
|
|
155
|
+
|
|
156
|
+
```typescript
|
|
157
|
+
import { SoapAuth } from "@soapjs/soap-auth";
|
|
158
|
+
|
|
159
|
+
const auth = new SoapAuth(authConfig);
|
|
160
|
+
await auth.init();
|
|
161
|
+
|
|
162
|
+
const user = await auth.authenticate("local", { email: "user@example.com", password: "securepass" });
|
|
163
|
+
console.log("Authenticated user:", user);
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
---
|
|
167
|
+
|
|
168
|
+
## Creating a Custom Authentication Strategy
|
|
169
|
+
|
|
170
|
+
To create a custom strategy, extend the appropriate base class depending on the authentication type:
|
|
171
|
+
|
|
172
|
+
1. **For token-based strategies:** Extend `TokenBasedAuthStrategy`.
|
|
173
|
+
2. **For credential-based strategies:** Extend `CredentialBasedAuthStrategy`.
|
|
174
|
+
3. **For generic implementations:** Extend `BaseAuthStrategy` directly.
|
|
175
|
+
|
|
176
|
+
**Example:**
|
|
177
|
+
```typescript
|
|
178
|
+
class CustomAuthStrategy extends TokenBasedAuthStrategy {
|
|
179
|
+
async authenticate(context) {
|
|
180
|
+
const token = context.headers.authorization;
|
|
181
|
+
if (token === "valid-token") {
|
|
182
|
+
return { user: { id: 1, role: "admin" }, tokens: { accessToken: token } };
|
|
183
|
+
}
|
|
184
|
+
throw new Error("Invalid token");
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
---
|
|
190
|
+
|
|
191
|
+
## Session Management
|
|
192
|
+
|
|
193
|
+
SoapAuth supports session management through the `SessionHandler` class. The session can be stored using memory, files, or external databases via adapters.
|
|
194
|
+
|
|
195
|
+
**Example Session Configuration:**
|
|
196
|
+
```typescript
|
|
197
|
+
const authConfig = {
|
|
198
|
+
session: {
|
|
199
|
+
secret: "your-session-secret",
|
|
200
|
+
sessionKey: "my-session-id",
|
|
201
|
+
store: new MemorySessionStore(),
|
|
202
|
+
},
|
|
203
|
+
};
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
---
|
|
207
|
+
|
|
208
|
+
## Framework Integration
|
|
209
|
+
|
|
210
|
+
SoapAuth is designed to work with various frameworks such as Express, Koa, Fastify, and more through dedicated adapters.
|
|
211
|
+
|
|
212
|
+
```typescript
|
|
213
|
+
// basic example
|
|
214
|
+
app.use(async (req, res, next) => {
|
|
215
|
+
try {
|
|
216
|
+
const user = await auth.authenticate("jwt", req);
|
|
217
|
+
req.user = user;
|
|
218
|
+
next();
|
|
219
|
+
} catch (error) {
|
|
220
|
+
res.status(401).send("Unauthorized");
|
|
221
|
+
}
|
|
222
|
+
});
|
|
223
|
+
```
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
export declare class UnauthorizedRoleError extends Error {
|
|
2
|
+
constructor();
|
|
3
|
+
}
|
|
4
|
+
export declare class MissingAuthorizationCodeError extends Error {
|
|
5
|
+
constructor();
|
|
6
|
+
}
|
|
7
|
+
export declare class RateLimitExceededError extends Error {
|
|
8
|
+
constructor();
|
|
9
|
+
}
|
|
10
|
+
export declare class AccountLockedError extends Error {
|
|
11
|
+
constructor();
|
|
12
|
+
}
|
|
13
|
+
export declare class MissingCredentialsError extends Error {
|
|
14
|
+
constructor();
|
|
15
|
+
}
|
|
16
|
+
export declare class UserNotFoundError extends Error {
|
|
17
|
+
constructor();
|
|
18
|
+
}
|
|
19
|
+
export declare class InvalidCredentialsError extends Error {
|
|
20
|
+
constructor();
|
|
21
|
+
}
|
|
22
|
+
export declare class MissingTokenError extends Error {
|
|
23
|
+
readonly tokenType: "Access" | "Refresh";
|
|
24
|
+
constructor(tokenType?: "Access" | "Refresh");
|
|
25
|
+
}
|
|
26
|
+
export declare class EmptyPayloadError extends Error {
|
|
27
|
+
constructor();
|
|
28
|
+
}
|
|
29
|
+
export declare class UndefinedTokenError extends Error {
|
|
30
|
+
readonly tokenType: "Access" | "Refresh";
|
|
31
|
+
constructor(tokenType?: "Access" | "Refresh");
|
|
32
|
+
}
|
|
33
|
+
export declare class InvalidTokenError extends Error {
|
|
34
|
+
readonly tokenType: "Access" | "Refresh";
|
|
35
|
+
constructor(tokenType?: "Access" | "Refresh");
|
|
36
|
+
}
|
|
37
|
+
export declare class UndefinedTokenSecretError extends Error {
|
|
38
|
+
readonly tokenType: "Access" | "Refresh";
|
|
39
|
+
constructor(tokenType?: "Access" | "Refresh");
|
|
40
|
+
}
|
|
41
|
+
export declare class UndefinedTokenHandlerError extends Error {
|
|
42
|
+
readonly tokenType: "Access" | "Refresh";
|
|
43
|
+
readonly handler: string;
|
|
44
|
+
constructor(tokenType: "Access" | "Refresh", handler: string);
|
|
45
|
+
}
|
|
46
|
+
export declare class ExpiredTokenError extends Error {
|
|
47
|
+
readonly tokenType: "Access" | "Refresh";
|
|
48
|
+
constructor(tokenType?: "Access" | "Refresh");
|
|
49
|
+
}
|
|
50
|
+
export declare class AuthError extends Error {
|
|
51
|
+
readonly error: Error;
|
|
52
|
+
constructor(error: Error, message: string);
|
|
53
|
+
}
|
package/build/errors.js
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AuthError = exports.ExpiredTokenError = exports.UndefinedTokenHandlerError = exports.UndefinedTokenSecretError = exports.InvalidTokenError = exports.UndefinedTokenError = exports.EmptyPayloadError = exports.MissingTokenError = exports.InvalidCredentialsError = exports.UserNotFoundError = exports.MissingCredentialsError = exports.AccountLockedError = exports.RateLimitExceededError = exports.MissingAuthorizationCodeError = exports.UnauthorizedRoleError = void 0;
|
|
4
|
+
class UnauthorizedRoleError extends Error {
|
|
5
|
+
constructor() {
|
|
6
|
+
super("User does not have the required role.");
|
|
7
|
+
this.name = "UnauthorizedRoleError";
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
exports.UnauthorizedRoleError = UnauthorizedRoleError;
|
|
11
|
+
class MissingAuthorizationCodeError extends Error {
|
|
12
|
+
constructor() {
|
|
13
|
+
super("Authorization code is required.");
|
|
14
|
+
this.name = "MissingAuthorizationCodeError";
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
exports.MissingAuthorizationCodeError = MissingAuthorizationCodeError;
|
|
18
|
+
class RateLimitExceededError extends Error {
|
|
19
|
+
constructor() {
|
|
20
|
+
super("Rate limit exceeded for the provided credentials.");
|
|
21
|
+
this.name = "RateLimitExceededError";
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
exports.RateLimitExceededError = RateLimitExceededError;
|
|
25
|
+
class AccountLockedError extends Error {
|
|
26
|
+
constructor() {
|
|
27
|
+
super("Account is locked due to too many failed attempts.");
|
|
28
|
+
this.name = "AccountLockedError";
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
exports.AccountLockedError = AccountLockedError;
|
|
32
|
+
class MissingCredentialsError extends Error {
|
|
33
|
+
constructor() {
|
|
34
|
+
super("Missing credentials: Username and password are required.");
|
|
35
|
+
this.name = "MissingCredentialsError";
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
exports.MissingCredentialsError = MissingCredentialsError;
|
|
39
|
+
class UserNotFoundError extends Error {
|
|
40
|
+
constructor() {
|
|
41
|
+
super("User not found.");
|
|
42
|
+
this.name = "UserNotFound";
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
exports.UserNotFoundError = UserNotFoundError;
|
|
46
|
+
class InvalidCredentialsError extends Error {
|
|
47
|
+
constructor() {
|
|
48
|
+
super("Invalid credentials: Authentication failed.");
|
|
49
|
+
this.name = "InvalidCredentialsError";
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
exports.InvalidCredentialsError = InvalidCredentialsError;
|
|
53
|
+
class MissingTokenError extends Error {
|
|
54
|
+
tokenType;
|
|
55
|
+
constructor(tokenType = "Access") {
|
|
56
|
+
super(`${tokenType} token is empty or not defined`);
|
|
57
|
+
this.tokenType = tokenType;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
exports.MissingTokenError = MissingTokenError;
|
|
61
|
+
class EmptyPayloadError extends Error {
|
|
62
|
+
constructor() {
|
|
63
|
+
super(`Payload is empty or not defined`);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
exports.EmptyPayloadError = EmptyPayloadError;
|
|
67
|
+
class UndefinedTokenError extends Error {
|
|
68
|
+
tokenType;
|
|
69
|
+
constructor(tokenType = "Access") {
|
|
70
|
+
super(`${tokenType} token not defined`);
|
|
71
|
+
this.tokenType = tokenType;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
exports.UndefinedTokenError = UndefinedTokenError;
|
|
75
|
+
class InvalidTokenError extends Error {
|
|
76
|
+
tokenType;
|
|
77
|
+
constructor(tokenType = "Access") {
|
|
78
|
+
super(`Invalid ${tokenType} token`);
|
|
79
|
+
this.tokenType = tokenType;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
exports.InvalidTokenError = InvalidTokenError;
|
|
83
|
+
class UndefinedTokenSecretError extends Error {
|
|
84
|
+
tokenType;
|
|
85
|
+
constructor(tokenType = "Access") {
|
|
86
|
+
super(`${tokenType} token secret not defined`);
|
|
87
|
+
this.tokenType = tokenType;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
exports.UndefinedTokenSecretError = UndefinedTokenSecretError;
|
|
91
|
+
class UndefinedTokenHandlerError extends Error {
|
|
92
|
+
tokenType;
|
|
93
|
+
handler;
|
|
94
|
+
constructor(tokenType = "Access", handler) {
|
|
95
|
+
super(`${tokenType} token "${handler}" not defined`);
|
|
96
|
+
this.tokenType = tokenType;
|
|
97
|
+
this.handler = handler;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
exports.UndefinedTokenHandlerError = UndefinedTokenHandlerError;
|
|
101
|
+
class ExpiredTokenError extends Error {
|
|
102
|
+
tokenType;
|
|
103
|
+
constructor(tokenType = "Access") {
|
|
104
|
+
super(`${tokenType} token expired`);
|
|
105
|
+
this.tokenType = tokenType;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
exports.ExpiredTokenError = ExpiredTokenError;
|
|
109
|
+
class AuthError extends Error {
|
|
110
|
+
error;
|
|
111
|
+
constructor(error, message) {
|
|
112
|
+
super(message || error.message);
|
|
113
|
+
this.error = error;
|
|
114
|
+
this.name = "AuthError";
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
exports.AuthError = AuthError;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import * as Soap from "@soapjs/soap";
|
|
2
|
+
import { AuthStrategy, SessionConfig, SoapAuthConfig } from "../types";
|
|
3
|
+
import { SessionHandler } from "../session/session-handler";
|
|
4
|
+
export declare abstract class AuthStrategyFactory {
|
|
5
|
+
protected logger?: Soap.Logger;
|
|
6
|
+
abstract createStrategies(config: SoapAuthConfig): Map<string, AuthStrategy>;
|
|
7
|
+
constructor(logger?: Soap.Logger);
|
|
8
|
+
protected getSessionHandler(strategyConfig: SessionConfig, globalConfig: SessionConfig): SessionHandler<unknown, any>;
|
|
9
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AuthStrategyFactory = void 0;
|
|
4
|
+
const tools_1 = require("../tools/tools");
|
|
5
|
+
const session_handler_1 = require("../session/session-handler");
|
|
6
|
+
class AuthStrategyFactory {
|
|
7
|
+
logger;
|
|
8
|
+
constructor(logger) {
|
|
9
|
+
this.logger = logger;
|
|
10
|
+
}
|
|
11
|
+
getSessionHandler(strategyConfig, globalConfig) {
|
|
12
|
+
const sessionConfig = (0, tools_1.resolveConfig)(strategyConfig, globalConfig);
|
|
13
|
+
return sessionConfig ? new session_handler_1.SessionHandler(sessionConfig) : undefined;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
exports.AuthStrategyFactory = AuthStrategyFactory;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { AuthStrategy, SoapAuthConfig } from "../types";
|
|
2
|
+
import { AuthStrategyFactory } from "./auth-strategy.factory";
|
|
3
|
+
export declare class HttpAuthStrategyFactory extends AuthStrategyFactory {
|
|
4
|
+
createStrategies(config: SoapAuthConfig): Map<string, AuthStrategy>;
|
|
5
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.HttpAuthStrategyFactory = void 0;
|
|
4
|
+
const oauth2_strategy_1 = require("../strategies/oauth2/oauth2.strategy");
|
|
5
|
+
const api_key_strategy_1 = require("../strategies/api-key/api-key.strategy");
|
|
6
|
+
const jwt_strategy_1 = require("../strategies/jwt/jwt.strategy");
|
|
7
|
+
const basic_strategy_1 = require("../strategies/basic/basic.strategy");
|
|
8
|
+
const auth_strategy_factory_1 = require("./auth-strategy.factory");
|
|
9
|
+
const tools_1 = require("../tools/tools");
|
|
10
|
+
const local_strategy_1 = require("../strategies/local/local.strategy");
|
|
11
|
+
class HttpAuthStrategyFactory extends auth_strategy_factory_1.AuthStrategyFactory {
|
|
12
|
+
createStrategies(config) {
|
|
13
|
+
const strategies = new Map();
|
|
14
|
+
if (!config.http) {
|
|
15
|
+
return strategies;
|
|
16
|
+
}
|
|
17
|
+
if (config.http.oauth2) {
|
|
18
|
+
for (const provider in config.http.oauth2) {
|
|
19
|
+
strategies.set(provider, new oauth2_strategy_1.OAuth2Strategy(config.http.oauth2[provider], (0, tools_1.resolveConfig)(config.http.oauth2[provider].tokens.access, config.tokens.access), (0, tools_1.resolveConfig)(config.http.oauth2[provider].tokens.refresh, config.tokens.refresh), this.getSessionHandler(config.http.oauth2[provider].session, config.session), config.logger));
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
if (config.http.apiKey) {
|
|
23
|
+
strategies.set("apiKey", new api_key_strategy_1.ApiKeyStrategy(config.http.apiKey, this.logger));
|
|
24
|
+
}
|
|
25
|
+
if (config.http.basic) {
|
|
26
|
+
strategies.set("basic", new basic_strategy_1.BasicStrategy(config.http.basic, this.getSessionHandler(config.http.basic.session, config.session), config.logger));
|
|
27
|
+
}
|
|
28
|
+
if (config.http.local) {
|
|
29
|
+
strategies.set("local", new local_strategy_1.LocalStrategy(config.http.local, this.getSessionHandler(config.http.local.session, config.session), config.logger));
|
|
30
|
+
}
|
|
31
|
+
if (config.http.jwt) {
|
|
32
|
+
strategies.set("jwt", new jwt_strategy_1.JwtStrategy(config.http.jwt, this.getSessionHandler(config.http.jwt.session, config.session), this.logger));
|
|
33
|
+
}
|
|
34
|
+
if (config.http.custom) {
|
|
35
|
+
Object.entries(config.http.custom).forEach(([key, strategy]) => {
|
|
36
|
+
strategies.set(key, strategy);
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
return strategies;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
exports.HttpAuthStrategyFactory = HttpAuthStrategyFactory;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { AuthStrategy, SoapAuthConfig } from "../types";
|
|
2
|
+
import { AuthStrategyFactory } from "./auth-strategy.factory";
|
|
3
|
+
export declare class SocketAuthStrategyFactory extends AuthStrategyFactory {
|
|
4
|
+
createStrategies(config: SoapAuthConfig): Map<string, AuthStrategy>;
|
|
5
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SocketAuthStrategyFactory = void 0;
|
|
4
|
+
const api_key_strategy_1 = require("../strategies/api-key/api-key.strategy");
|
|
5
|
+
const jwt_strategy_1 = require("../strategies/jwt/jwt.strategy");
|
|
6
|
+
const auth_strategy_factory_1 = require("./auth-strategy.factory");
|
|
7
|
+
class SocketAuthStrategyFactory extends auth_strategy_factory_1.AuthStrategyFactory {
|
|
8
|
+
createStrategies(config) {
|
|
9
|
+
const strategies = new Map();
|
|
10
|
+
if (!config.socket) {
|
|
11
|
+
return strategies;
|
|
12
|
+
}
|
|
13
|
+
if (config.socket.apiKey) {
|
|
14
|
+
strategies.set("apiKey", new api_key_strategy_1.ApiKeyStrategy(config.socket.apiKey, this.logger));
|
|
15
|
+
}
|
|
16
|
+
if (config.socket.jwt) {
|
|
17
|
+
strategies.set("jwt", new jwt_strategy_1.JwtStrategy(config.socket.jwt, this.getSessionHandler(config.socket.jwt.session, config.session), this.logger));
|
|
18
|
+
}
|
|
19
|
+
if (config.socket.custom) {
|
|
20
|
+
Object.entries(config.socket.custom).forEach(([key, strategy]) => {
|
|
21
|
+
strategies.set(key, strategy);
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
return strategies;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
exports.SocketAuthStrategyFactory = SocketAuthStrategyFactory;
|
package/build/index.d.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
export * from "./factories/auth-strategy.factory";
|
|
2
|
+
export * from "./factories/http-auth-strategy.factory";
|
|
3
|
+
export * from "./factories/socket-auth-strategy.factory";
|
|
4
|
+
export * from "./session/file.session-store";
|
|
5
|
+
export * from "./session/memory.session-store";
|
|
6
|
+
export * from "./session/session-handler";
|
|
7
|
+
export * from "./strategies/api-key/api-key.errors";
|
|
8
|
+
export * from "./strategies/api-key/api-key.strategy";
|
|
9
|
+
export * from "./strategies/api-key/api-key.types";
|
|
10
|
+
export * from "./strategies/base-auth.strategy";
|
|
11
|
+
export * from "./strategies/basic/basic.strategy";
|
|
12
|
+
export * from "./strategies/basic/basic.types";
|
|
13
|
+
export * from "./strategies/credential-based-auth.strategy";
|
|
14
|
+
export * from "./strategies/jwt/jwt.strategy";
|
|
15
|
+
export * from "./strategies/jwt/jwt.tools";
|
|
16
|
+
export * from "./strategies/jwt/jwt.types";
|
|
17
|
+
export * from "./strategies/local/local.strategy";
|
|
18
|
+
export * from "./strategies/local/local.types";
|
|
19
|
+
export * from "./strategies/oauth2/oauth2.strategy";
|
|
20
|
+
export * from "./strategies/oauth2/oauth2.tools";
|
|
21
|
+
export * from "./strategies/oauth2/oauth2.types";
|
|
22
|
+
export * from "./strategies/token-based-auth.strategy";
|
|
23
|
+
export * from "./tools/session.tools";
|
|
24
|
+
export * from "./tools/token.tools";
|
|
25
|
+
export * from "./tools/tools";
|
|
26
|
+
export * from "./errors";
|
|
27
|
+
export * from "./types";
|
|
28
|
+
export * from "./soap-auth";
|
package/build/index.js
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./factories/auth-strategy.factory"), exports);
|
|
18
|
+
__exportStar(require("./factories/http-auth-strategy.factory"), exports);
|
|
19
|
+
__exportStar(require("./factories/socket-auth-strategy.factory"), exports);
|
|
20
|
+
__exportStar(require("./session/file.session-store"), exports);
|
|
21
|
+
__exportStar(require("./session/memory.session-store"), exports);
|
|
22
|
+
__exportStar(require("./session/session-handler"), exports);
|
|
23
|
+
__exportStar(require("./strategies/api-key/api-key.errors"), exports);
|
|
24
|
+
__exportStar(require("./strategies/api-key/api-key.strategy"), exports);
|
|
25
|
+
__exportStar(require("./strategies/api-key/api-key.types"), exports);
|
|
26
|
+
__exportStar(require("./strategies/base-auth.strategy"), exports);
|
|
27
|
+
__exportStar(require("./strategies/basic/basic.strategy"), exports);
|
|
28
|
+
__exportStar(require("./strategies/basic/basic.types"), exports);
|
|
29
|
+
__exportStar(require("./strategies/credential-based-auth.strategy"), exports);
|
|
30
|
+
__exportStar(require("./strategies/jwt/jwt.strategy"), exports);
|
|
31
|
+
__exportStar(require("./strategies/jwt/jwt.tools"), exports);
|
|
32
|
+
__exportStar(require("./strategies/jwt/jwt.types"), exports);
|
|
33
|
+
__exportStar(require("./strategies/local/local.strategy"), exports);
|
|
34
|
+
__exportStar(require("./strategies/local/local.types"), exports);
|
|
35
|
+
__exportStar(require("./strategies/oauth2/oauth2.strategy"), exports);
|
|
36
|
+
__exportStar(require("./strategies/oauth2/oauth2.tools"), exports);
|
|
37
|
+
__exportStar(require("./strategies/oauth2/oauth2.types"), exports);
|
|
38
|
+
__exportStar(require("./strategies/token-based-auth.strategy"), exports);
|
|
39
|
+
__exportStar(require("./tools/session.tools"), exports);
|
|
40
|
+
__exportStar(require("./tools/token.tools"), exports);
|
|
41
|
+
__exportStar(require("./tools/tools"), exports);
|
|
42
|
+
__exportStar(require("./errors"), exports);
|
|
43
|
+
__exportStar(require("./types"), exports);
|
|
44
|
+
__exportStar(require("./soap-auth"), exports);
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { SessionData, SessionStore } from "../types";
|
|
2
|
+
export declare class FileSessionStore implements SessionStore {
|
|
3
|
+
private sessionsDir;
|
|
4
|
+
constructor(sessionsDir: string);
|
|
5
|
+
init(): Promise<void>;
|
|
6
|
+
getSession<SessionData>(sid: string): Promise<SessionData | null>;
|
|
7
|
+
setSession<SessionData>(sessionId: string, sessionData: SessionData): Promise<void>;
|
|
8
|
+
destroySession(sessionId: string): Promise<void>;
|
|
9
|
+
touchSession(sessionId: string, session: SessionData): Promise<void>;
|
|
10
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.FileSessionStore = void 0;
|
|
7
|
+
const promises_1 = __importDefault(require("fs/promises"));
|
|
8
|
+
const path_1 = __importDefault(require("path"));
|
|
9
|
+
class FileSessionStore {
|
|
10
|
+
sessionsDir;
|
|
11
|
+
constructor(sessionsDir) {
|
|
12
|
+
this.sessionsDir = sessionsDir;
|
|
13
|
+
}
|
|
14
|
+
async init() {
|
|
15
|
+
try {
|
|
16
|
+
await promises_1.default.mkdir(this.sessionsDir, { recursive: true });
|
|
17
|
+
}
|
|
18
|
+
catch (error) {
|
|
19
|
+
console.error("Error initializing session directory:", error);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
async getSession(sid) {
|
|
23
|
+
const sessionPath = path_1.default.join(this.sessionsDir, sid);
|
|
24
|
+
try {
|
|
25
|
+
const data = await promises_1.default.readFile(sessionPath, "utf8");
|
|
26
|
+
return JSON.parse(data);
|
|
27
|
+
}
|
|
28
|
+
catch (error) {
|
|
29
|
+
console.error("Error getting session:", error);
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
async setSession(sessionId, sessionData) {
|
|
34
|
+
const sessionPath = path_1.default.join(this.sessionsDir, sessionId);
|
|
35
|
+
try {
|
|
36
|
+
await promises_1.default.writeFile(sessionPath, JSON.stringify(sessionData), "utf8");
|
|
37
|
+
}
|
|
38
|
+
catch (error) {
|
|
39
|
+
console.error("Error writing session file:", error);
|
|
40
|
+
throw error;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
async destroySession(sessionId) {
|
|
44
|
+
const sessionPath = path_1.default.join(this.sessionsDir, sessionId);
|
|
45
|
+
try {
|
|
46
|
+
await promises_1.default.unlink(sessionPath);
|
|
47
|
+
}
|
|
48
|
+
catch (error) {
|
|
49
|
+
if (error.code !== "ENOENT") {
|
|
50
|
+
console.error("Error destroying session:", error);
|
|
51
|
+
throw error;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
async touchSession(sessionId, session) {
|
|
56
|
+
await this.setSession(sessionId, session);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
exports.FileSessionStore = FileSessionStore;
|