@owlmeans/server-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 +490 -0
- package/build/.gitkeep +0 -0
- package/build/actions/index.d.ts +2 -0
- package/build/actions/index.d.ts.map +1 -0
- package/build/actions/index.js +2 -0
- package/build/actions/index.js.map +1 -0
- package/build/actions/service.d.ts +4 -0
- package/build/actions/service.d.ts.map +1 -0
- package/build/actions/service.js +7 -0
- package/build/actions/service.js.map +1 -0
- package/build/consts.d.ts +5 -0
- package/build/consts.d.ts.map +1 -0
- package/build/consts.js +5 -0
- package/build/consts.js.map +1 -0
- package/build/index.d.ts +6 -0
- package/build/index.d.ts.map +1 -0
- package/build/index.js +5 -0
- package/build/index.js.map +1 -0
- package/build/manager/actions/index.d.ts +2 -0
- package/build/manager/actions/index.d.ts.map +1 -0
- package/build/manager/actions/index.js +2 -0
- package/build/manager/actions/index.js.map +1 -0
- package/build/manager/actions/manager.d.ts +6 -0
- package/build/manager/actions/manager.d.ts.map +1 -0
- package/build/manager/actions/manager.js +10 -0
- package/build/manager/actions/manager.js.map +1 -0
- package/build/manager/consts.d.ts +3 -0
- package/build/manager/consts.d.ts.map +1 -0
- package/build/manager/consts.js +3 -0
- package/build/manager/consts.js.map +1 -0
- package/build/manager/context.d.ts +3 -0
- package/build/manager/context.d.ts.map +1 -0
- package/build/manager/context.js +26 -0
- package/build/manager/context.js.map +1 -0
- package/build/manager/exports.d.ts +16 -0
- package/build/manager/exports.d.ts.map +1 -0
- package/build/manager/exports.js +13 -0
- package/build/manager/exports.js.map +1 -0
- package/build/manager/index.d.ts +11 -0
- package/build/manager/index.d.ts.map +1 -0
- package/build/manager/index.js +10 -0
- package/build/manager/index.js.map +1 -0
- package/build/manager/main.d.ts +3 -0
- package/build/manager/main.d.ts.map +1 -0
- package/build/manager/main.js +7 -0
- package/build/manager/main.js.map +1 -0
- package/build/manager/model.d.ts +3 -0
- package/build/manager/model.d.ts.map +1 -0
- package/build/manager/model.js +68 -0
- package/build/manager/model.js.map +1 -0
- package/build/manager/modules.d.ts +2 -0
- package/build/manager/modules.d.ts.map +1 -0
- package/build/manager/modules.js +21 -0
- package/build/manager/modules.js.map +1 -0
- package/build/manager/plugins/basic-ed25519.d.ts +4 -0
- package/build/manager/plugins/basic-ed25519.d.ts.map +1 -0
- package/build/manager/plugins/basic-ed25519.js +42 -0
- package/build/manager/plugins/basic-ed25519.js.map +1 -0
- package/build/manager/plugins/basic-rely.d.ts +4 -0
- package/build/manager/plugins/basic-rely.d.ts.map +1 -0
- package/build/manager/plugins/basic-rely.js +152 -0
- package/build/manager/plugins/basic-rely.js.map +1 -0
- package/build/manager/plugins/export.d.ts +6 -0
- package/build/manager/plugins/export.d.ts.map +1 -0
- package/build/manager/plugins/export.js +5 -0
- package/build/manager/plugins/export.js.map +1 -0
- package/build/manager/plugins/index.d.ts +4 -0
- package/build/manager/plugins/index.d.ts.map +1 -0
- package/build/manager/plugins/index.js +9 -0
- package/build/manager/plugins/index.js.map +1 -0
- package/build/manager/plugins/re-captcha.d.ts +4 -0
- package/build/manager/plugins/re-captcha.d.ts.map +1 -0
- package/build/manager/plugins/re-captcha.js +49 -0
- package/build/manager/plugins/re-captcha.js.map +1 -0
- package/build/manager/plugins/types.d.ts +25 -0
- package/build/manager/plugins/types.d.ts.map +1 -0
- package/build/manager/plugins/types.js +2 -0
- package/build/manager/plugins/types.js.map +1 -0
- package/build/manager/plugins/utils.d.ts +8 -0
- package/build/manager/plugins/utils.d.ts.map +1 -0
- package/build/manager/plugins/utils.js +18 -0
- package/build/manager/plugins/utils.js.map +1 -0
- package/build/manager/rely/flow.d.ts +5 -0
- package/build/manager/rely/flow.d.ts.map +1 -0
- package/build/manager/rely/flow.js +127 -0
- package/build/manager/rely/flow.js.map +1 -0
- package/build/manager/rely/index.d.ts +2 -0
- package/build/manager/rely/index.d.ts.map +1 -0
- package/build/manager/rely/index.js +2 -0
- package/build/manager/rely/index.js.map +1 -0
- package/build/manager/rely.d.ts +3 -0
- package/build/manager/rely.d.ts.map +1 -0
- package/build/manager/rely.js +63 -0
- package/build/manager/rely.js.map +1 -0
- package/build/manager/types.d.ts +33 -0
- package/build/manager/types.d.ts.map +1 -0
- package/build/manager/types.js +2 -0
- package/build/manager/types.js.map +1 -0
- package/build/manager/utils/index.d.ts +2 -0
- package/build/manager/utils/index.d.ts.map +1 -0
- package/build/manager/utils/index.js +2 -0
- package/build/manager/utils/index.js.map +1 -0
- package/build/manager/utils/trusted.d.ts +5 -0
- package/build/manager/utils/trusted.d.ts.map +1 -0
- package/build/manager/utils/trusted.js +11 -0
- package/build/manager/utils/trusted.js.map +1 -0
- package/build/model/index.d.ts +2 -0
- package/build/model/index.d.ts.map +1 -0
- package/build/model/index.js +2 -0
- package/build/model/index.js.map +1 -0
- package/build/model/rely/consumer.d.ts +3 -0
- package/build/model/rely/consumer.d.ts.map +1 -0
- package/build/model/rely/consumer.js +9 -0
- package/build/model/rely/consumer.js.map +1 -0
- package/build/model/rely/index.d.ts +5 -0
- package/build/model/rely/index.d.ts.map +1 -0
- package/build/model/rely/index.js +4 -0
- package/build/model/rely/index.js.map +1 -0
- package/build/model/rely/peer.d.ts +5 -0
- package/build/model/rely/peer.d.ts.map +1 -0
- package/build/model/rely/peer.js +36 -0
- package/build/model/rely/peer.js.map +1 -0
- package/build/model/rely/provider.d.ts +3 -0
- package/build/model/rely/provider.d.ts.map +1 -0
- package/build/model/rely/provider.js +6 -0
- package/build/model/rely/provider.js.map +1 -0
- package/build/model/rely/types.d.ts +8 -0
- package/build/model/rely/types.d.ts.map +1 -0
- package/build/model/rely/types.js +2 -0
- package/build/model/rely/types.js.map +1 -0
- package/build/modules.d.ts +3 -0
- package/build/modules.d.ts.map +1 -0
- package/build/modules.js +11 -0
- package/build/modules.js.map +1 -0
- package/build/service.d.ts +7 -0
- package/build/service.d.ts.map +1 -0
- package/build/service.js +90 -0
- package/build/service.js.map +1 -0
- package/build/types.d.ts +13 -0
- package/build/types.d.ts.map +1 -0
- package/build/types.js +2 -0
- package/build/types.js.map +1 -0
- package/package.json +76 -0
- package/src/actions/index.ts +2 -0
- package/src/actions/service.ts +16 -0
- package/src/consts.ts +8 -0
- package/src/index.ts +6 -0
- package/src/manager/README.md +507 -0
- package/src/manager/actions/index.ts +2 -0
- package/src/manager/actions/manager.ts +23 -0
- package/src/manager/consts.ts +4 -0
- package/src/manager/context.ts +33 -0
- package/src/manager/exports.ts +17 -0
- package/src/manager/index.ts +11 -0
- package/src/manager/main.ts +9 -0
- package/src/manager/model.ts +93 -0
- package/src/manager/modules.ts +29 -0
- package/src/manager/plugins/basic-ed25519.ts +56 -0
- package/src/manager/plugins/basic-rely.ts +167 -0
- package/src/manager/plugins/export.ts +6 -0
- package/src/manager/plugins/index.ts +13 -0
- package/src/manager/plugins/re-captcha.ts +62 -0
- package/src/manager/plugins/types.ts +27 -0
- package/src/manager/plugins/utils.ts +21 -0
- package/src/manager/rely/flow.ts +135 -0
- package/src/manager/rely/index.ts +2 -0
- package/src/manager/rely.ts +90 -0
- package/src/manager/types.ts +42 -0
- package/src/manager/utils/index.ts +2 -0
- package/src/manager/utils/trusted.ts +16 -0
- package/src/model/index.ts +2 -0
- package/src/model/rely/consumer.ts +11 -0
- package/src/model/rely/index.ts +5 -0
- package/src/model/rely/peer.ts +43 -0
- package/src/model/rely/provider.ts +9 -0
- package/src/model/rely/types.ts +8 -0
- package/src/modules.ts +17 -0
- package/src/service.ts +126 -0
- package/src/types.ts +15 -0
- package/tsconfig.json +15 -0
- package/tsconfig.tsbuildinfo +1 -0
|
@@ -0,0 +1,507 @@
|
|
|
1
|
+
# OwlMeans Auth Manager App
|
|
2
|
+
|
|
3
|
+
The **OwlMeans Auth Manager App** is a complete server application for managing authentication and authorization services within the OwlMeans Common Libraries ecosystem. This manager application serves as the core authentication service providing secure authentication, token management, and relay functionality for distributed microservices architectures.
|
|
4
|
+
|
|
5
|
+
## Purpose
|
|
6
|
+
|
|
7
|
+
This manager application provides a complete authentication service implementation designed for:
|
|
8
|
+
|
|
9
|
+
- **Authentication Management**: Secure user authentication with multiple credential types
|
|
10
|
+
- **Token Management**: JWT token generation, validation, and refresh mechanisms
|
|
11
|
+
- **Relay Service**: Cross-service authentication relay for distributed architectures
|
|
12
|
+
- **API Security**: Comprehensive API endpoint protection with guards and gates
|
|
13
|
+
- **Socket Communication**: Real-time authentication over WebSocket connections
|
|
14
|
+
- **Microservice Integration**: Seamless integration with other OwlMeans services
|
|
15
|
+
|
|
16
|
+
## Key Concepts
|
|
17
|
+
|
|
18
|
+
### Authentication Model
|
|
19
|
+
The manager implements a comprehensive authentication model that handles:
|
|
20
|
+
- **Initialization**: Setup and configuration of authentication providers
|
|
21
|
+
- **Authentication**: User credential validation and token generation
|
|
22
|
+
- **Relay**: Cross-service token validation and relay functionality
|
|
23
|
+
|
|
24
|
+
### Rely Service
|
|
25
|
+
A specialized guard service that enables authentication token relay between microservices, allowing for:
|
|
26
|
+
- **One-Time Tokens**: Secure single-use authentication tokens
|
|
27
|
+
- **Wallet DID Authentication**: Decentralized identity authentication support
|
|
28
|
+
- **Challenge Validation**: Anti-replay protection using challenge-response mechanisms
|
|
29
|
+
|
|
30
|
+
### Module-Based Architecture
|
|
31
|
+
The manager is built on the OwlMeans module system, providing:
|
|
32
|
+
- **Route Management**: Standardized API endpoints for authentication operations
|
|
33
|
+
- **Handler Registration**: Modular request handlers for different authentication flows
|
|
34
|
+
- **Guard Integration**: Automatic security enforcement at the module level
|
|
35
|
+
|
|
36
|
+
## Installation
|
|
37
|
+
|
|
38
|
+
This manager app is part of the `@owlmeans/server-auth` package:
|
|
39
|
+
|
|
40
|
+
```bash
|
|
41
|
+
npm install @owlmeans/server-auth
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## API Reference
|
|
45
|
+
|
|
46
|
+
### Types
|
|
47
|
+
|
|
48
|
+
#### `AppConfig`
|
|
49
|
+
Configuration interface for the Auth Manager App.
|
|
50
|
+
|
|
51
|
+
```typescript
|
|
52
|
+
interface AppConfig extends ServerConfig, KlusterConfig {
|
|
53
|
+
services: Record<string, ServiceRoute>
|
|
54
|
+
}
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
Extends base server configuration with Kluster support and service routing definitions.
|
|
58
|
+
|
|
59
|
+
#### `AppContext<C extends AppConfig>`
|
|
60
|
+
Main application context interface extending server context with additional capabilities.
|
|
61
|
+
|
|
62
|
+
```typescript
|
|
63
|
+
interface AppContext<C extends AppConfig = AppConfig> extends ServerContext<C>,
|
|
64
|
+
ApiServerAppend,
|
|
65
|
+
StaticResourceAppend { }
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
Provides comprehensive context management with:
|
|
69
|
+
- **ServerContext**: Base server functionality
|
|
70
|
+
- **ApiServerAppend**: HTTP API server capabilities
|
|
71
|
+
- **StaticResourceAppend**: Static resource serving
|
|
72
|
+
|
|
73
|
+
#### `AuthModel`
|
|
74
|
+
Core authentication model interface defining authentication operations.
|
|
75
|
+
|
|
76
|
+
```typescript
|
|
77
|
+
interface AuthModel {
|
|
78
|
+
init: (request: AllowanceRequest) => Promise<AllowanceResponse>
|
|
79
|
+
authenticate: (credential: AuthCredentials) => Promise<AuthToken>
|
|
80
|
+
rely: (conn: Connection, source?: Auth | null) => Promise<void>
|
|
81
|
+
}
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
**Methods:**
|
|
85
|
+
- **`init`**: Initialize authentication session and return allowance parameters
|
|
86
|
+
- **`authenticate`**: Validate credentials and return authentication token
|
|
87
|
+
- **`rely`**: Establish authentication relay over WebSocket connection
|
|
88
|
+
|
|
89
|
+
#### `RelyService`
|
|
90
|
+
Guard service interface for handling authentication relay.
|
|
91
|
+
|
|
92
|
+
```typescript
|
|
93
|
+
interface RelyService extends GuardService {
|
|
94
|
+
}
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
Extends the base GuardService to provide authentication relay capabilities.
|
|
98
|
+
|
|
99
|
+
#### `RelyAllowanceRequest`
|
|
100
|
+
Extended allowance request interface for relay operations.
|
|
101
|
+
|
|
102
|
+
```typescript
|
|
103
|
+
interface RelyAllowanceRequest extends AllowanceRequest {
|
|
104
|
+
auth?: Auth
|
|
105
|
+
provideRely?: RelyLinker
|
|
106
|
+
conn?: Connection
|
|
107
|
+
}
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
**Properties:**
|
|
111
|
+
- **`auth`**: Optional authentication context
|
|
112
|
+
- **`provideRely`**: Function to link rely tokens
|
|
113
|
+
- **`conn`**: WebSocket connection for real-time relay
|
|
114
|
+
|
|
115
|
+
#### `RelyLinker`
|
|
116
|
+
Function interface for linking rely tokens between services.
|
|
117
|
+
|
|
118
|
+
```typescript
|
|
119
|
+
interface RelyLinker {
|
|
120
|
+
(rely: RelyToken, source: RelyToken, notify?: boolean): Promise<void>
|
|
121
|
+
}
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
#### `RelyCarrier`
|
|
125
|
+
Container for relay token relationships.
|
|
126
|
+
|
|
127
|
+
```typescript
|
|
128
|
+
interface RelyCarrier {
|
|
129
|
+
source: RelyToken
|
|
130
|
+
rely: RelyToken
|
|
131
|
+
}
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
### Factory Functions
|
|
135
|
+
|
|
136
|
+
#### `makeContext<C extends AppConfig, T extends AppContext<C>>(cfg: C, customize?: boolean): T`
|
|
137
|
+
|
|
138
|
+
Creates a fully configured application context for the Auth Manager App.
|
|
139
|
+
|
|
140
|
+
```typescript
|
|
141
|
+
import { makeContext } from '@owlmeans/server-auth/manager'
|
|
142
|
+
|
|
143
|
+
const config = {
|
|
144
|
+
service: 'auth-manager',
|
|
145
|
+
port: 3000,
|
|
146
|
+
services: {
|
|
147
|
+
redis: { host: 'localhost', port: 6379 }
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
const context = makeContext(config)
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
**Parameters:**
|
|
155
|
+
- `cfg`: Application configuration
|
|
156
|
+
- `customize`: Optional flag to skip default service registration
|
|
157
|
+
|
|
158
|
+
**Returns:** Configured application context with all necessary services
|
|
159
|
+
|
|
160
|
+
#### `createRelyService(alias?: string): RelyService`
|
|
161
|
+
|
|
162
|
+
Creates a rely service for authentication token relay.
|
|
163
|
+
|
|
164
|
+
```typescript
|
|
165
|
+
import { createRelyService } from '@owlmeans/server-auth/manager'
|
|
166
|
+
|
|
167
|
+
const relyService = createRelyService('custom-rely')
|
|
168
|
+
context.registerService(relyService)
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
**Parameters:**
|
|
172
|
+
- `alias`: Optional service alias (defaults to 'auth-rely')
|
|
173
|
+
|
|
174
|
+
**Returns:** Configured rely service
|
|
175
|
+
|
|
176
|
+
### Core Functions
|
|
177
|
+
|
|
178
|
+
#### `main<C extends AppConfig, T extends AppContext<C>>(ctx: T): Promise<void>`
|
|
179
|
+
|
|
180
|
+
Main application entry point that configures and starts the Auth Manager App.
|
|
181
|
+
|
|
182
|
+
```typescript
|
|
183
|
+
import { main, makeContext } from '@owlmeans/server-auth/manager'
|
|
184
|
+
|
|
185
|
+
const context = makeContext(config)
|
|
186
|
+
await main(context)
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
**Functionality:**
|
|
190
|
+
- Registers all authentication modules
|
|
191
|
+
- Configures and initializes the context
|
|
192
|
+
- Starts the API server and begins listening for requests
|
|
193
|
+
|
|
194
|
+
### Authentication Handlers
|
|
195
|
+
|
|
196
|
+
#### `authenticationInit`
|
|
197
|
+
Handler for authentication initialization requests.
|
|
198
|
+
|
|
199
|
+
```typescript
|
|
200
|
+
const authenticationInit: RefedModuleHandler<AllowanceRequest>
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
Processes initialization requests to set up authentication parameters and return allowance information.
|
|
204
|
+
|
|
205
|
+
#### `authenticate`
|
|
206
|
+
Handler for credential authentication requests.
|
|
207
|
+
|
|
208
|
+
```typescript
|
|
209
|
+
const authenticate: RefedModuleHandler<AuthCredentials>
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
Validates user credentials and returns authentication tokens.
|
|
213
|
+
|
|
214
|
+
#### `rely`
|
|
215
|
+
Handler for authentication relay over WebSocket connections.
|
|
216
|
+
|
|
217
|
+
```typescript
|
|
218
|
+
const rely: RefedModuleHandler<void>
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
Establishes authentication relay for real-time communication channels.
|
|
222
|
+
|
|
223
|
+
### Constants
|
|
224
|
+
|
|
225
|
+
#### `DEFAULT_RELY`
|
|
226
|
+
Default alias for the rely service.
|
|
227
|
+
|
|
228
|
+
```typescript
|
|
229
|
+
const DEFAULT_RELY = 'auth-rely'
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
#### `RELY_TUNNEL`
|
|
233
|
+
Identifier for rely tunnel connections.
|
|
234
|
+
|
|
235
|
+
```typescript
|
|
236
|
+
const RELY_TUNNEL = 'rely-tunnel'
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
## Usage Examples
|
|
240
|
+
|
|
241
|
+
### Basic Server Setup
|
|
242
|
+
|
|
243
|
+
```typescript
|
|
244
|
+
import { main, makeContext } from '@owlmeans/server-auth/manager'
|
|
245
|
+
|
|
246
|
+
// Create application configuration
|
|
247
|
+
const config = {
|
|
248
|
+
service: 'auth-manager',
|
|
249
|
+
port: 3000,
|
|
250
|
+
layer: Layer.Service,
|
|
251
|
+
type: AppType.Backend,
|
|
252
|
+
services: {
|
|
253
|
+
redis: {
|
|
254
|
+
host: process.env.REDIS_HOST || 'localhost',
|
|
255
|
+
port: parseInt(process.env.REDIS_PORT || '6379')
|
|
256
|
+
},
|
|
257
|
+
postgres: {
|
|
258
|
+
host: process.env.DB_HOST || 'localhost',
|
|
259
|
+
port: parseInt(process.env.DB_PORT || '5432')
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
// Create and start the application
|
|
265
|
+
const context = makeContext(config)
|
|
266
|
+
await main(context)
|
|
267
|
+
|
|
268
|
+
console.log('Auth Manager App is running on port 3000')
|
|
269
|
+
```
|
|
270
|
+
|
|
271
|
+
### Custom Context Configuration
|
|
272
|
+
|
|
273
|
+
```typescript
|
|
274
|
+
import { makeContext, createRelyService } from '@owlmeans/server-auth/manager'
|
|
275
|
+
|
|
276
|
+
// Create custom context without default services
|
|
277
|
+
const context = makeContext(config, true)
|
|
278
|
+
|
|
279
|
+
// Add custom rely service
|
|
280
|
+
const customRelyService = createRelyService('custom-auth-relay')
|
|
281
|
+
context.registerService(customRelyService)
|
|
282
|
+
|
|
283
|
+
// Add custom resources or services
|
|
284
|
+
context.registerResource(customCacheResource)
|
|
285
|
+
|
|
286
|
+
// Configure and initialize
|
|
287
|
+
await context.configure().init()
|
|
288
|
+
```
|
|
289
|
+
|
|
290
|
+
### Authentication Model Usage
|
|
291
|
+
|
|
292
|
+
```typescript
|
|
293
|
+
import { makeAuthModel } from '@owlmeans/server-auth'
|
|
294
|
+
|
|
295
|
+
const context = makeContext(config)
|
|
296
|
+
const authModel = makeAuthModel(context)
|
|
297
|
+
|
|
298
|
+
// Initialize authentication
|
|
299
|
+
const allowanceRequest = {
|
|
300
|
+
type: AuthenticationType.PasswordLogin,
|
|
301
|
+
challenge: 'unique-challenge-string'
|
|
302
|
+
}
|
|
303
|
+
const allowance = await authModel.init(allowanceRequest)
|
|
304
|
+
|
|
305
|
+
// Authenticate user
|
|
306
|
+
const credentials = {
|
|
307
|
+
username: 'user@example.com',
|
|
308
|
+
password: 'secure-password',
|
|
309
|
+
challenge: allowance.challenge
|
|
310
|
+
}
|
|
311
|
+
const authToken = await authModel.authenticate(credentials)
|
|
312
|
+
|
|
313
|
+
// Handle WebSocket relay
|
|
314
|
+
const connection = getWebSocketConnection()
|
|
315
|
+
await authModel.rely(connection, authToken.auth)
|
|
316
|
+
```
|
|
317
|
+
|
|
318
|
+
### API Integration
|
|
319
|
+
|
|
320
|
+
```typescript
|
|
321
|
+
import { modules } from '@owlmeans/server-auth/manager'
|
|
322
|
+
|
|
323
|
+
// The manager automatically registers these API endpoints:
|
|
324
|
+
|
|
325
|
+
// POST /api/auth/init - Authentication initialization
|
|
326
|
+
// POST /api/auth/authenticate - User authentication
|
|
327
|
+
// WebSocket /api/auth/rely - Authentication relay
|
|
328
|
+
|
|
329
|
+
// Access modules for custom routing
|
|
330
|
+
modules.forEach(module => {
|
|
331
|
+
console.log(`Route: ${module.getPath()}`)
|
|
332
|
+
console.log(`Method: ${module.route.method}`)
|
|
333
|
+
})
|
|
334
|
+
```
|
|
335
|
+
|
|
336
|
+
## API Endpoints
|
|
337
|
+
|
|
338
|
+
The Auth Manager App automatically exposes these authentication endpoints:
|
|
339
|
+
|
|
340
|
+
### POST `/api/auth/init`
|
|
341
|
+
Initialize authentication session.
|
|
342
|
+
|
|
343
|
+
**Request Body:**
|
|
344
|
+
```typescript
|
|
345
|
+
{
|
|
346
|
+
type: AuthenticationType,
|
|
347
|
+
challenge?: string
|
|
348
|
+
}
|
|
349
|
+
```
|
|
350
|
+
|
|
351
|
+
**Response:**
|
|
352
|
+
```typescript
|
|
353
|
+
{
|
|
354
|
+
challenge: string,
|
|
355
|
+
allowance: AllowanceParameters
|
|
356
|
+
}
|
|
357
|
+
```
|
|
358
|
+
|
|
359
|
+
### POST `/api/auth/authenticate`
|
|
360
|
+
Authenticate user credentials.
|
|
361
|
+
|
|
362
|
+
**Request Body:**
|
|
363
|
+
```typescript
|
|
364
|
+
{
|
|
365
|
+
username: string,
|
|
366
|
+
password: string,
|
|
367
|
+
challenge: string
|
|
368
|
+
}
|
|
369
|
+
```
|
|
370
|
+
|
|
371
|
+
**Response:**
|
|
372
|
+
```typescript
|
|
373
|
+
{
|
|
374
|
+
token: string,
|
|
375
|
+
auth: Auth,
|
|
376
|
+
refresh?: string
|
|
377
|
+
}
|
|
378
|
+
```
|
|
379
|
+
|
|
380
|
+
### WebSocket `/api/auth/rely`
|
|
381
|
+
Establish authentication relay connection.
|
|
382
|
+
|
|
383
|
+
**Query Parameters:**
|
|
384
|
+
- `auth`: Authentication token for relay validation
|
|
385
|
+
|
|
386
|
+
## Security Features
|
|
387
|
+
|
|
388
|
+
### Guard System
|
|
389
|
+
The manager implements comprehensive request guards:
|
|
390
|
+
- **Authentication Guards**: Validate user authentication status
|
|
391
|
+
- **Rely Guards**: Secure cross-service authentication relay
|
|
392
|
+
- **Challenge Validation**: Anti-replay protection mechanisms
|
|
393
|
+
|
|
394
|
+
### Token Management
|
|
395
|
+
- **JWT Tokens**: Secure JSON Web Token generation and validation
|
|
396
|
+
- **One-Time Tokens**: Single-use authentication tokens for enhanced security
|
|
397
|
+
- **Token Relay**: Secure token passing between microservices
|
|
398
|
+
|
|
399
|
+
### Wallet DID Support
|
|
400
|
+
- **Decentralized Identity**: Support for blockchain-based authentication
|
|
401
|
+
- **Cryptographic Validation**: Secure signature verification
|
|
402
|
+
- **Challenge-Response**: Anti-replay protection for DID authentication
|
|
403
|
+
|
|
404
|
+
## Error Handling
|
|
405
|
+
|
|
406
|
+
The manager handles various authentication errors:
|
|
407
|
+
|
|
408
|
+
### `AuthenFailed`
|
|
409
|
+
Thrown when authentication validation fails.
|
|
410
|
+
|
|
411
|
+
```typescript
|
|
412
|
+
try {
|
|
413
|
+
await authModel.authenticate(credentials)
|
|
414
|
+
} catch (error) {
|
|
415
|
+
if (error instanceof AuthenFailed) {
|
|
416
|
+
console.error('Authentication failed:', error.message)
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
```
|
|
420
|
+
|
|
421
|
+
### Common Error Scenarios
|
|
422
|
+
- **Invalid Credentials**: User provides incorrect username/password
|
|
423
|
+
- **Expired Challenge**: Authentication challenge has expired
|
|
424
|
+
- **Invalid Token**: Malformed or expired authentication token
|
|
425
|
+
- **Rely Failure**: Cross-service authentication relay failed
|
|
426
|
+
|
|
427
|
+
## Integration Patterns
|
|
428
|
+
|
|
429
|
+
### Microservice Architecture
|
|
430
|
+
|
|
431
|
+
```typescript
|
|
432
|
+
// Auth Manager as central authentication service
|
|
433
|
+
const authManager = makeContext({
|
|
434
|
+
service: 'auth-manager',
|
|
435
|
+
port: 3001
|
|
436
|
+
})
|
|
437
|
+
|
|
438
|
+
// Other services rely on auth manager
|
|
439
|
+
const userService = makeContext({
|
|
440
|
+
service: 'user-service',
|
|
441
|
+
port: 3002,
|
|
442
|
+
services: {
|
|
443
|
+
'auth-manager': {
|
|
444
|
+
host: 'auth-manager',
|
|
445
|
+
port: 3001
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
})
|
|
449
|
+
```
|
|
450
|
+
|
|
451
|
+
### Load Balancing and High Availability
|
|
452
|
+
|
|
453
|
+
```typescript
|
|
454
|
+
const config = {
|
|
455
|
+
service: 'auth-manager',
|
|
456
|
+
port: process.env.PORT || 3000,
|
|
457
|
+
services: {
|
|
458
|
+
redis: {
|
|
459
|
+
host: process.env.REDIS_CLUSTER || 'redis-cluster',
|
|
460
|
+
port: 6379
|
|
461
|
+
},
|
|
462
|
+
postgres: {
|
|
463
|
+
host: process.env.DB_CLUSTER || 'postgres-cluster',
|
|
464
|
+
port: 5432
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
```
|
|
469
|
+
|
|
470
|
+
### Container Deployment
|
|
471
|
+
|
|
472
|
+
```dockerfile
|
|
473
|
+
FROM node:18-alpine
|
|
474
|
+
WORKDIR /app
|
|
475
|
+
COPY package*.json ./
|
|
476
|
+
RUN npm ci --only=production
|
|
477
|
+
COPY . .
|
|
478
|
+
EXPOSE 3000
|
|
479
|
+
CMD ["node", "dist/manager/main.js"]
|
|
480
|
+
```
|
|
481
|
+
|
|
482
|
+
## Best Practices
|
|
483
|
+
|
|
484
|
+
1. **Secure Configuration**: Store sensitive configuration in environment variables or secrets
|
|
485
|
+
2. **Service Discovery**: Use proper service discovery mechanisms in microservice deployments
|
|
486
|
+
3. **Token Rotation**: Implement regular token refresh and rotation
|
|
487
|
+
4. **Monitoring**: Add comprehensive logging and monitoring for authentication events
|
|
488
|
+
5. **Rate Limiting**: Implement rate limiting to prevent brute force attacks
|
|
489
|
+
6. **HTTPS Only**: Always use HTTPS in production environments
|
|
490
|
+
|
|
491
|
+
## Dependencies
|
|
492
|
+
|
|
493
|
+
This manager app depends on:
|
|
494
|
+
- `@owlmeans/auth` - Core authentication functionality
|
|
495
|
+
- `@owlmeans/auth-common` - Shared authentication components
|
|
496
|
+
- `@owlmeans/server-context` - Server context management
|
|
497
|
+
- `@owlmeans/server-api` - HTTP API server functionality
|
|
498
|
+
- `@owlmeans/server-socket` - WebSocket server support
|
|
499
|
+
- `@owlmeans/static-resource` - Static resource management
|
|
500
|
+
- `@owlmeans/api-config-server` - API configuration server
|
|
501
|
+
|
|
502
|
+
## Related Packages
|
|
503
|
+
|
|
504
|
+
- [`@owlmeans/server-auth`](../../../README.md) - Parent server authentication package
|
|
505
|
+
- [`@owlmeans/auth`](../../../auth/README.md) - Core authentication library
|
|
506
|
+
- [`@owlmeans/client-auth`](../../../client-auth/README.md) - Client-side authentication
|
|
507
|
+
- [`@owlmeans/context`](../../../context/README.md) - Context management system
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { AllowanceRequest, AuthCredentials } from '@owlmeans/auth'
|
|
2
|
+
import { handleBody } from '@owlmeans/server-api'
|
|
3
|
+
import { makeAuthModel } from '../model.js'
|
|
4
|
+
import type { RefedModuleHandler } from '@owlmeans/server-module'
|
|
5
|
+
import type { AppContext, AppConfig } from '../types.js'
|
|
6
|
+
import { handleConnection } from '@owlmeans/server-socket'
|
|
7
|
+
|
|
8
|
+
export const authenticationInit: RefedModuleHandler<AllowanceRequest> = handleBody(
|
|
9
|
+
async (payload: AllowanceRequest, ctx) =>
|
|
10
|
+
await makeAuthModel(ctx as AppContext<AppConfig>).init(payload)
|
|
11
|
+
)
|
|
12
|
+
|
|
13
|
+
export const authenticate: RefedModuleHandler<AuthCredentials> = handleBody(
|
|
14
|
+
async (payload: AuthCredentials, ctx) =>
|
|
15
|
+
await makeAuthModel(ctx as AppContext<AppConfig>).authenticate(payload)
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
export const rely: RefedModuleHandler<void> = handleConnection(
|
|
19
|
+
// @TODO Request will contain information is there requrest
|
|
20
|
+
// privileged or not (privileged request implies auth provider)
|
|
21
|
+
async (conn, ctx, req) =>
|
|
22
|
+
await makeAuthModel(ctx as AppContext<AppConfig>).rely(conn, req.auth)
|
|
23
|
+
)
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { makeServerContext } from '@owlmeans/server-context'
|
|
2
|
+
import type { AppContext, AppConfig } from './types.js'
|
|
3
|
+
import { appendApiServer } from '@owlmeans/server-api'
|
|
4
|
+
import { appendApiClient } from '@owlmeans/api'
|
|
5
|
+
import { appendSocketService, createSocketMiddleware } from '@owlmeans/server-socket'
|
|
6
|
+
import { DEFAULT_RELY } from './consts.js'
|
|
7
|
+
import { createRelyService } from './rely.js'
|
|
8
|
+
import { AUTH_CACHE } from '../consts.js'
|
|
9
|
+
import { appendStaticResource } from '@owlmeans/static-resource'
|
|
10
|
+
|
|
11
|
+
export const makeContext = <C extends AppConfig, T extends AppContext<C>>(cfg: C, customize: boolean = false): T => {
|
|
12
|
+
const context = makeServerContext(cfg) as T
|
|
13
|
+
|
|
14
|
+
appendApiServer<C, T>(context)
|
|
15
|
+
appendApiClient<C, T>(context)
|
|
16
|
+
appendSocketService<C, T>(context)
|
|
17
|
+
|
|
18
|
+
context.registerMiddleware(createSocketMiddleware())
|
|
19
|
+
|
|
20
|
+
// @TODO figure out rely service via config?
|
|
21
|
+
if (!customize && !context.hasService(DEFAULT_RELY)) {
|
|
22
|
+
context.registerService(createRelyService(DEFAULT_RELY))
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
appendStaticResource<C, T>(context)
|
|
26
|
+
if (!customize && !context.hasResource(AUTH_CACHE)) {
|
|
27
|
+
appendStaticResource<C, T>(context, AUTH_CACHE)
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
context.makeContext = (cfg => makeContext(cfg, customize)) as typeof context.makeContext
|
|
31
|
+
|
|
32
|
+
return context as T
|
|
33
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
|
|
2
|
+
export { config } from '@owlmeans/server-context'
|
|
3
|
+
export type { TrustedRecord } from '@owlmeans/auth-common'
|
|
4
|
+
export { addWebService } from '@owlmeans/client-config'
|
|
5
|
+
export { service, TRUSTED } from '@owlmeans/config'
|
|
6
|
+
export { AppType, assertContext } from '@owlmeans/context'
|
|
7
|
+
export { klusterize } from '@owlmeans/kluster'
|
|
8
|
+
export { elevate } from '@owlmeans/server-module'
|
|
9
|
+
export { elevate as celevate, stab } from '@owlmeans/client-module'
|
|
10
|
+
export type { RefedModuleHandler } from '@owlmeans/server-module'
|
|
11
|
+
export { handleIntermediate, handleBody, handleRequest, handleParams } from '@owlmeans/server-api'
|
|
12
|
+
export type { AbstractRequest, AbstractResponse } from '@owlmeans/module'
|
|
13
|
+
export { backend } from '@owlmeans/route'
|
|
14
|
+
|
|
15
|
+
export { AUTHEN, AUTHEN_AUTHEN, AUTHEN_INIT } from '@owlmeans/auth'
|
|
16
|
+
export { GUARD_ED25519, BED255_CASHE_RESOURCE } from '@owlmeans/auth-common'
|
|
17
|
+
export { filterObject } from '@owlmeans/resource'
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export type * from './types.js'
|
|
2
|
+
|
|
3
|
+
export * from './modules.js'
|
|
4
|
+
export * from './exports.js'
|
|
5
|
+
export * from './context.js'
|
|
6
|
+
export * from './main.js'
|
|
7
|
+
export * from './rely.js'
|
|
8
|
+
export * from './consts.js'
|
|
9
|
+
export * from './rely/index.js'
|
|
10
|
+
export * from './actions/index.js'
|
|
11
|
+
export * from './plugins/index.js'
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
|
|
2
|
+
import { modules } from './modules.js'
|
|
3
|
+
import type { AppContext, AppConfig } from './types.js'
|
|
4
|
+
|
|
5
|
+
export const main = async <C extends AppConfig, T extends AppContext<C>>(ctx: T) => {
|
|
6
|
+
ctx.registerModules(modules)
|
|
7
|
+
await ctx.configure().init()
|
|
8
|
+
await ctx.getApiServer().listen()
|
|
9
|
+
}
|