@owox/idp-protocol 0.5.0 → 0.6.0-next-20250821085233

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.
@@ -1 +1 @@
1
- {"version":3,"file":"protocol-middleware.d.ts","sourceRoot":"","sources":["../../src/middleware/protocol-middleware.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,EAAmC,MAAM,SAAS,CAAC;AAEnE,OAAO,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAC;AAEnD;;GAEG;AACH,oBAAY,aAAa;IACvB,OAAO,aAAa;IACpB,QAAQ,cAAc;IACtB,YAAY,kBAAkB;IAC9B,IAAI,cAAc;CACnB;AAED;;;;;;;;;;;;;;GAcG;AACH,MAAM,WAAW,4BAA4B;IAC3C,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,MAAM,CAAC,EAAE;QACP,MAAM,CAAC,EAAE,MAAM,CAAC;QAChB,OAAO,CAAC,EAAE,MAAM,CAAC;QACjB,WAAW,CAAC,EAAE,MAAM,CAAC;QACrB,IAAI,CAAC,EAAE,MAAM,CAAC;KACf,CAAC;CACH;AAQD;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,qBAAa,qBAAqB;IAc9B,OAAO,CAAC,QAAQ,CAAC,QAAQ;IAb3B;;OAEG;IACH,SAAgB,iBAAiB,WAAW;IAC5C,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAS;IAClC,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAgE;IAEvF;;;;OAIG;gBAEgB,QAAQ,EAAE,WAAW,EACtC,OAAO,GAAE,4BAAiC;IAa5C;;;;OAIG;IACH,OAAO,CAAC,iBAAiB;IAOzB;;OAEG;IACH,OAAO,CAAC,qBAAqB;IAe7B;;;;OAIG;IACH,OAAO,CAAC,kBAAkB;IAU1B;;;;;;;;OAQG;IACH,QAAQ,CAAC,GAAG,EAAE,OAAO,GAAG,IAAI;CA8B7B"}
1
+ {"version":3,"file":"protocol-middleware.d.ts","sourceRoot":"","sources":["../../src/middleware/protocol-middleware.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,EAAmC,MAAM,SAAS,CAAC;AAEnE,OAAO,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAC;AAEnD;;GAEG;AACH,oBAAY,aAAa;IACvB,OAAO,aAAa;IACpB,QAAQ,cAAc;IACtB,YAAY,kBAAkB;IAC9B,IAAI,cAAc;CACnB;AAED;;;;;;;;;;;;;;GAcG;AACH,MAAM,WAAW,4BAA4B;IAC3C,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,MAAM,CAAC,EAAE;QACP,MAAM,CAAC,EAAE,MAAM,CAAC;QAChB,OAAO,CAAC,EAAE,MAAM,CAAC;QACjB,WAAW,CAAC,EAAE,MAAM,CAAC;QACrB,IAAI,CAAC,EAAE,MAAM,CAAC;KACf,CAAC;CACH;AAQD;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,qBAAa,qBAAqB;IAc9B,OAAO,CAAC,QAAQ,CAAC,QAAQ;IAb3B;;OAEG;IACH,SAAgB,iBAAiB,WAAW;IAC5C,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAS;IAClC,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAgE;IAEvF;;;;OAIG;gBAEgB,QAAQ,EAAE,WAAW,EACtC,OAAO,GAAE,4BAAiC;IAa5C;;;;OAIG;IACH,OAAO,CAAC,iBAAiB;IAOzB;;OAEG;IACH,OAAO,CAAC,qBAAqB;IAe7B;;;;OAIG;IACH,OAAO,CAAC,kBAAkB;IAU1B;;;;;;;;OAQG;IACH,QAAQ,CAAC,GAAG,EAAE,OAAO,GAAG,IAAI;CAgC7B"}
@@ -129,5 +129,6 @@ export class IdpProtocolMiddleware {
129
129
  const fullPath = `${this.basePath}${path}`;
130
130
  app.all(fullPath, this.createRouteHandler(handler));
131
131
  });
132
+ this.provider.registerRoutes(app);
132
133
  }
133
134
  }
@@ -1,6 +1,6 @@
1
1
  import { IdpProvider } from '../types/provider.js';
2
2
  import { AuthResult, Payload } from '../types/models.js';
3
- import { Request, Response, NextFunction } from 'express';
3
+ import { Express, Request, Response, NextFunction } from 'express';
4
4
  /**
5
5
  * NULL IDP Provider - single user, single project
6
6
  * Used for deployments without user management and development
@@ -11,10 +11,11 @@ export declare class NullIdpProvider implements IdpProvider {
11
11
  private defaultRefreshToken;
12
12
  constructor();
13
13
  refreshToken(_refreshToken: string): Promise<AuthResult>;
14
- signInMiddleware(_req: Request, res: Response, _next: NextFunction): Promise<void>;
14
+ signInMiddleware(req: Request, res: Response, _next: NextFunction): Promise<void>;
15
15
  signOutMiddleware(_req: Request, res: Response, _next: NextFunction): Promise<void>;
16
- accessTokenMiddleware(_req: Request, res: Response, _next: NextFunction): Promise<Response>;
17
- userApiMiddleware(_req: Request, res: Response, _next: NextFunction): Promise<Response<Payload>>;
16
+ accessTokenMiddleware(req: Request, res: Response, _next: NextFunction): Promise<Response>;
17
+ userApiMiddleware(req: Request, res: Response, _next: NextFunction): Promise<Response<Payload>>;
18
+ registerRoutes(_app: Express): void;
18
19
  initialize(): Promise<void>;
19
20
  shutdown(): Promise<void>;
20
21
  introspectToken(_token: string): Promise<Payload | null>;
@@ -1 +1 @@
1
- {"version":3,"file":"null-provider.d.ts","sourceRoot":"","sources":["../../src/providers/null-provider.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAC;AACnD,OAAO,EAAE,UAAU,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAC;AACzD,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAE1D;;;GAGG;AACH,qBAAa,eAAgB,YAAW,WAAW;IACjD,OAAO,CAAC,cAAc,CAAU;IAChC,OAAO,CAAC,kBAAkB,CAAS;IACnC,OAAO,CAAC,mBAAmB,CAAS;;IAc9B,YAAY,CAAC,aAAa,EAAE,MAAM,GAAG,OAAO,CAAC,U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,mBAAmB,CAAS;;IAc9B,YAAY,CAAC,aAAa,EAAE,MAAM,GAAG,OAAO,CAAC,UAAU,CAAC;IAM9D,gBAAgB,CAAC,GAAG,EAAE,OAAO,EAAE,GAAG,EAAE,QAAQ,EAAE,KAAK,EAAE,YAAY,GAAG,OAAO,CAAC,IAAI,CAAC;IAYjF,iBAAiB,CAAC,IAAI,EAAE,OAAO,EAAE,GAAG,EAAE,QAAQ,EAAE,KAAK,EAAE,YAAY,GAAG,OAAO,CAAC,IAAI,CAAC;IAKnF,qBAAqB,CAAC,GAAG,EAAE,OAAO,EAAE,GAAG,EAAE,QAAQ,EAAE,KAAK,EAAE,YAAY,GAAG,OAAO,CAAC,QAAQ,CAAC;IAO1F,iBAAiB,CAAC,GAAG,EAAE,OAAO,EAAE,GAAG,EAAE,QAAQ,EAAE,KAAK,EAAE,YAAY,GAAG,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC;IAO/F,cAAc,CAAC,IAAI,EAAE,OAAO,GAAG,IAAI;IAI7B,UAAU,IAAI,OAAO,CAAC,IAAI,CAAC;IAI3B,QAAQ,IAAI,OAAO,CAAC,IAAI,CAAC;IAIzB,eAAe,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,GAAG,IAAI,CAAC;IAIxD,UAAU,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,GAAG,IAAI,CAAC;IAInD,WAAW,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;CAGjD"}
@@ -22,8 +22,8 @@ export class NullIdpProvider {
22
22
  accessToken: this.defaultAccessToken,
23
23
  };
24
24
  }
25
- signInMiddleware(_req, res, _next) {
26
- const isLocalhost = _req.hostname === 'localhost' || _req.hostname === '127.0.0.1';
25
+ signInMiddleware(req, res, _next) {
26
+ const isLocalhost = req.hostname === 'localhost' || req.hostname === '127.0.0.1';
27
27
  res.cookie('refreshToken', this.defaultRefreshToken, {
28
28
  httpOnly: true,
29
29
  secure: !isLocalhost,
@@ -36,18 +36,21 @@ export class NullIdpProvider {
36
36
  res.clearCookie('refreshToken');
37
37
  return Promise.resolve(res.redirect('/'));
38
38
  }
39
- accessTokenMiddleware(_req, res, _next) {
40
- if (!_req.cookies.refreshToken) {
39
+ accessTokenMiddleware(req, res, _next) {
40
+ if (!req.cookies.refreshToken) {
41
41
  return Promise.resolve(res.status(401).json({ message: 'Unauthorized' }));
42
42
  }
43
43
  return Promise.resolve(res.json({ accessToken: this.defaultAccessToken }));
44
44
  }
45
- userApiMiddleware(_req, res, _next) {
46
- if (!_req.cookies.refreshToken) {
45
+ userApiMiddleware(req, res, _next) {
46
+ if (!req.cookies.refreshToken) {
47
47
  return Promise.resolve(res.status(401).json({ message: 'Unauthorized' }));
48
48
  }
49
49
  return Promise.resolve(res.json(this.defaultPayload));
50
50
  }
51
+ registerRoutes(_app) {
52
+ // Nothing to register
53
+ }
51
54
  async initialize() {
52
55
  // Nothing to initialize
53
56
  }
@@ -1,5 +1,5 @@
1
1
  import { Payload, AuthResult } from './models.js';
2
- import { NextFunction, Request, Response } from 'express';
2
+ import { Express, NextFunction, Request, Response } from 'express';
3
3
  /**
4
4
  * Simplified IDP Provider interface.
5
5
  */
@@ -28,6 +28,12 @@ export interface IdpProvider {
28
28
  * If the IDP implementation does not support user, this method should call the `next()` function.
29
29
  */
30
30
  userApiMiddleware(req: Request, res: Response, next: NextFunction): Promise<Response<Payload>>;
31
+ /**
32
+ * Register routes with the express app.
33
+ * @param app - The express app to register the routes with.
34
+ * @param basePath - The base path to register the routes with.
35
+ */
36
+ registerRoutes(app: Express): void;
31
37
  /**
32
38
  * Introspect a token
33
39
  * @param token - The token to introspect
@@ -1 +1 @@
1
- {"version":3,"file":"provider.d.ts","sourceRoot":"","sources":["../../src/types/provider.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAClD,OAAO,EAAE,YAAY,EAAE,OAAO,EA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,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAE1C;;OAEG;IACH,UAAU,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IAE5B;;OAEG;IACH,QAAQ,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CAC3B"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@owox/idp-protocol",
3
- "version": "0.5.0",
3
+ "version": "0.6.0-next-20250821085233",
4
4
  "type": "module",
5
5
  "author": "OWOX",
6
6
  "license": "ELv2",
@@ -41,8 +41,7 @@
41
41
  "typescript": "^5.7.3"
42
42
  },
43
43
  "files": [
44
- "dist",
45
- "src"
44
+ "dist"
46
45
  ],
47
46
  "exports": {
48
47
  ".": {
package/src/index.ts DELETED
@@ -1,8 +0,0 @@
1
- // Core types and interfaces
2
- export * from './types/index.js';
3
-
4
- // Provider implementations (only NULL provider)
5
- export * from './providers/index.js';
6
-
7
- // Middleware
8
- export * from './middleware/index.js';
@@ -1 +0,0 @@
1
- export * from './protocol-middleware.js';
@@ -1,181 +0,0 @@
1
- import cookieParser from 'cookie-parser';
2
- import { Express, Request, Response, NextFunction } from 'express';
3
-
4
- import { IdpProvider } from '../types/provider.js';
5
-
6
- /**
7
- * The routes that are supported by the protocol middleware.
8
- */
9
- export enum ProtocolRoute {
10
- SIGN_IN = '/sign-in',
11
- SIGN_OUT = '/sign-out',
12
- ACCESS_TOKEN = '/access-token',
13
- USER = '/api/user',
14
- }
15
-
16
- /**
17
- * The options for the protocol middleware.
18
- * @property basePath - The base path for the protoc ol middleware.
19
- * @property routes - The routes that are supported by the protocol middleware.
20
- * @example
21
- * {
22
- * basePath: '/',
23
- * routes: {
24
- * signIn: '/signin', // override the default sign in route
25
- * signOut: '/signout', // override the default sign out route
26
- * accessToken: '/accesstoken', // override the default access token route
27
- * user: '/api/userinfo', // override the default user route
28
- * },
29
- * }
30
- */
31
- export interface IdpProtocolMiddlewareOptions {
32
- basePath?: string;
33
- routes?: {
34
- signIn?: string;
35
- signOut?: string;
36
- accessToken?: string;
37
- user?: string;
38
- };
39
- }
40
-
41
- type MiddlewareHandler = (
42
- req: Request,
43
- res: Response,
44
- next: NextFunction
45
- ) => Promise<void | Response>;
46
-
47
- /**
48
- * The protocol middleware for the IDP.
49
- * @param provider - The provider to use for the middleware.
50
- * @param options - The options for the middleware.
51
- * @example // Register the middleware with the default routes
52
- * const idpProtocolMiddleware = new IdpProtocolMiddleware(provider);
53
- * idpProtocolMiddleware.register(app);
54
- *
55
- * @example // Override the default routes
56
- * const app = express();
57
- * const middlewareOptions: IdpProtocolMiddlewareOptions = {
58
- * basePath: '/',
59
- * routes: {
60
- * signIn: '/signin',
61
- * signOut: '/signout',
62
- * accessToken: '/accesstoken',
63
- * user: '/api/userinfo',
64
- * }
65
- * };
66
- * const idpProtocolMiddleware = new IdpProtocolMiddleware(provider, middlewareOptions);
67
- * idpProtocolMiddleware.register(app);
68
- */
69
- export class IdpProtocolMiddleware {
70
- /**
71
- * The default base path for the protocol middleware.
72
- */
73
- public readonly DEFAULT_BASE_PATH = '/auth';
74
- private readonly basePath: string;
75
- private readonly routes: Required<NonNullable<IdpProtocolMiddlewareOptions['routes']>>;
76
-
77
- /**
78
- * The constructor for the protocol middleware.
79
- * @param provider - The provider to use for the middleware.
80
- * @param options - The options for the middleware.
81
- */
82
- constructor(
83
- private readonly provider: IdpProvider,
84
- options: IdpProtocolMiddlewareOptions = {}
85
- ) {
86
- this.basePath = this.normalizeBasePath(options.basePath ?? this.DEFAULT_BASE_PATH);
87
- this.routes = {
88
- signIn: options.routes?.signIn ?? ProtocolRoute.SIGN_IN,
89
- signOut: options.routes?.signOut ?? ProtocolRoute.SIGN_OUT,
90
- accessToken: options.routes?.accessToken ?? ProtocolRoute.ACCESS_TOKEN,
91
- user: options.routes?.user ?? ProtocolRoute.USER,
92
- };
93
-
94
- this.validateConfiguration();
95
- }
96
-
97
- /**
98
- * Normalize the base path.
99
- * @param path - The path to normalize.
100
- * @returns The normalized path.
101
- */
102
- private normalizeBasePath(path: string): string {
103
- if (!path.startsWith('/')) {
104
- throw new Error(`Base path must start with '/': ${path}`);
105
- }
106
- return path.endsWith('/') && path !== '/' ? path.slice(0, -1) : path;
107
- }
108
-
109
- /**
110
- * Validate the configuration.
111
- */
112
- private validateConfiguration(): void {
113
- const routePaths = Object.values(this.routes);
114
- const duplicates = routePaths.filter((path, index) => routePaths.indexOf(path) !== index);
115
-
116
- if (duplicates.length > 0) {
117
- throw new Error(`Duplicate route paths detected: ${duplicates.join(', ')}`);
118
- }
119
-
120
- routePaths.forEach(path => {
121
- if (!path.startsWith('/')) {
122
- throw new Error(`Route path must start with '/': ${path}`);
123
- }
124
- });
125
- }
126
-
127
- /**
128
- * Create a route handler.
129
- * @param handler - The handler to create.
130
- * @returns The created handler.
131
- */
132
- private createRouteHandler(handler: MiddlewareHandler): MiddlewareHandler {
133
- return async (req: Request, res: Response, next: NextFunction) => {
134
- try {
135
- await handler(req, res, next);
136
- } catch (error) {
137
- next(error);
138
- }
139
- };
140
- }
141
-
142
- /**
143
- * Register the protocol middleware routes with the express app.
144
- * @param app - The express app to register the routes with.
145
- * @example
146
- * const app = express();
147
- * const idpProtocolMiddleware = new IdpProtocolMiddleware(provider);
148
- * idpProtocolMiddleware.register(app);
149
- * app.listen(3000);
150
- */
151
- register(app: Express): void {
152
- app.use(cookieParser());
153
- const routeConfigs = [
154
- {
155
- path: this.routes.signIn,
156
- handler: (req: Request, res: Response, next: NextFunction) =>
157
- this.provider.signInMiddleware(req, res, next),
158
- },
159
- {
160
- path: this.routes.signOut,
161
- handler: (req: Request, res: Response, next: NextFunction) =>
162
- this.provider.signOutMiddleware(req, res, next),
163
- },
164
- {
165
- path: this.routes.accessToken,
166
- handler: (req: Request, res: Response, next: NextFunction) =>
167
- this.provider.accessTokenMiddleware(req, res, next),
168
- },
169
- {
170
- path: this.routes.user,
171
- handler: (req: Request, res: Response, next: NextFunction) =>
172
- this.provider.userApiMiddleware(req, res, next),
173
- },
174
- ];
175
-
176
- routeConfigs.forEach(({ path, handler }) => {
177
- const fullPath = `${this.basePath}${path}`;
178
- app.all(fullPath, this.createRouteHandler(handler));
179
- });
180
- }
181
- }
@@ -1 +0,0 @@
1
- export { NullIdpProvider } from './null-provider.js';
@@ -1,82 +0,0 @@
1
- import { IdpProvider } from '../types/provider.js';
2
- import { AuthResult, Payload } from '../types/models.js';
3
- import { Request, Response, NextFunction } from 'express';
4
-
5
- /**
6
- * NULL IDP Provider - single user, single project
7
- * Used for deployments without user management and development
8
- */
9
- export class NullIdpProvider implements IdpProvider {
10
- private defaultPayload: Payload;
11
- private defaultAccessToken: string;
12
- private defaultRefreshToken: string;
13
-
14
- constructor() {
15
- this.defaultPayload = {
16
- userId: '0',
17
- email: 'admin@localhost',
18
- roles: ['admin'],
19
- fullName: 'Admin',
20
- projectId: '0',
21
- };
22
- this.defaultAccessToken = 'accessToken';
23
- this.defaultRefreshToken = 'refreshToken';
24
- }
25
-
26
- async refreshToken(_refreshToken: string): Promise<AuthResult> {
27
- return {
28
- accessToken: this.defaultAccessToken,
29
- };
30
- }
31
-
32
- signInMiddleware(_req: Request, res: Response, _next: NextFunction): Promise<void> {
33
- const isLocalhost = _req.hostname === 'localhost' || _req.hostname === '127.0.0.1';
34
-
35
- res.cookie('refreshToken', this.defaultRefreshToken, {
36
- httpOnly: true,
37
- secure: !isLocalhost,
38
- sameSite: 'lax',
39
- maxAge: 3600000,
40
- });
41
- return Promise.resolve(res.redirect('/'));
42
- }
43
-
44
- signOutMiddleware(_req: Request, res: Response, _next: NextFunction): Promise<void> {
45
- res.clearCookie('refreshToken');
46
- return Promise.resolve(res.redirect('/'));
47
- }
48
-
49
- accessTokenMiddleware(_req: Request, res: Response, _next: NextFunction): Promise<Response> {
50
- if (!_req.cookies.refreshToken) {
51
- return Promise.resolve(res.status(401).json({ message: 'Unauthorized' }));
52
- }
53
- return Promise.resolve(res.json({ accessToken: this.defaultAccessToken }));
54
- }
55
-
56
- userApiMiddleware(_req: Request, res: Response, _next: NextFunction): Promise<Response<Payload>> {
57
- if (!_req.cookies.refreshToken) {
58
- return Promise.resolve(res.status(401).json({ message: 'Unauthorized' }));
59
- }
60
- return Promise.resolve(res.json(this.defaultPayload));
61
- }
62
-
63
- async initialize(): Promise<void> {
64
- // Nothing to initialize
65
- }
66
-
67
- async shutdown(): Promise<void> {
68
- // Nothing to cleanup
69
- }
70
-
71
- async introspectToken(_token: string): Promise<Payload | null> {
72
- return this.defaultPayload;
73
- }
74
-
75
- async parseToken(_token: string): Promise<Payload | null> {
76
- return this.defaultPayload;
77
- }
78
-
79
- async revokeToken(_token: string): Promise<void> {
80
- // No-op for NULL provider
81
- }
82
- }
package/src/types/cli.ts DELETED
@@ -1,30 +0,0 @@
1
- import { Payload } from './models.js';
2
-
3
- /**
4
- * Commands for adding a user to the IDP by app cli.
5
- */
6
- export interface IdpProviderAddUserCommand {
7
- addUser(username: string, password?: string): Promise<AddUserCommandResponse>;
8
- }
9
-
10
- /**
11
- * Commands for listing users from the IDP
12
- */
13
- export interface IdpProviderListUsersCommand {
14
- listUsers(): Promise<Payload[]>;
15
- }
16
-
17
- /**
18
- * Commands for removing a user from the IDP
19
- */
20
- export interface IdpProviderRemoveUserCommand {
21
- removeUser(userId: string): Promise<void>;
22
- }
23
-
24
- /**
25
- * Response for adding a user to the IDP
26
- */
27
- export interface AddUserCommandResponse {
28
- username: string;
29
- magicLink?: string;
30
- }
@@ -1,14 +0,0 @@
1
- /**
2
- * Configuration for the IDP provider
3
- */
4
- export interface IdpConfig {
5
- /**
6
- * Base URL for the IDP provider
7
- */
8
- baseUrl: string;
9
-
10
- /**
11
- * Additional provider-specific configuration
12
- */
13
- [key: string]: unknown;
14
- }
@@ -1,49 +0,0 @@
1
- /**
2
- * Base error class for the IDP protocol
3
- */
4
- export class IdpError extends Error {
5
- constructor(
6
- message: string,
7
- public code: string,
8
- public statusCode: number = 400
9
- ) {
10
- super(message);
11
- this.name = 'IdpError';
12
- }
13
- }
14
-
15
- /**
16
- * Authentication error
17
- */
18
- export class AuthenticationError extends IdpError {
19
- constructor(message: string = 'Authentication failed') {
20
- super(message, 'AUTHENTICATION_ERROR', 401);
21
- }
22
- }
23
-
24
- /**
25
- * Authorization error
26
- */
27
- export class AuthorizationError extends IdpError {
28
- constructor(message: string = 'Insufficient permissions') {
29
- super(message, 'AUTHORIZATION_ERROR', 403);
30
- }
31
- }
32
-
33
- /**
34
- * Token expired error
35
- */
36
- export class TokenExpiredError extends IdpError {
37
- constructor(message: string = 'Token has expired') {
38
- super(message, 'TOKEN_EXPIRED', 401);
39
- }
40
- }
41
-
42
- /**
43
- * Invalid token error
44
- */
45
- export class InvalidTokenError extends IdpError {
46
- constructor(message: string = 'Invalid token') {
47
- super(message, 'INVALID_TOKEN', 401);
48
- }
49
- }
@@ -1,18 +0,0 @@
1
- /**
2
- * Main types export - organized by category
3
- */
4
-
5
- // Core provider interface
6
- export * from './provider.js';
7
-
8
- // Domain models
9
- export * from './models.js';
10
-
11
- // Configuration
12
- export * from './config.js';
13
-
14
- // Error classes
15
- export * from './errors.js';
16
-
17
- // CLI commands
18
- export * from './cli.js';
@@ -1,28 +0,0 @@
1
- /**
2
- * The roles that are supported by the IDP.
3
- */
4
- export type Role = 'admin' | 'editor' | 'viewer';
5
-
6
- /**
7
- * Standardized token payload that all IDP implementations must return when introspecting their native tokens.
8
- */
9
- export interface Payload {
10
- userId: string;
11
- projectId: string;
12
-
13
- email?: string;
14
- fullName?: string;
15
- avatar?: string;
16
-
17
- roles?: Role[];
18
-
19
- projectTitle?: string;
20
- }
21
-
22
- /**
23
- * Authentication result from IDP callback
24
- */
25
- export interface AuthResult {
26
- accessToken: string;
27
- refreshToken?: string;
28
- }
@@ -1,72 +0,0 @@
1
- import { Payload, AuthResult } from './models.js';
2
- import { NextFunction, Request, Response } from 'express';
3
-
4
- /**
5
- * Simplified IDP Provider interface.
6
- */
7
- export interface IdpProvider {
8
- /**
9
- * Sign in middleware. This method is used to handle the sign in request and use response to send the sign in response.
10
- * <br/>
11
- * If the IDP implementation does not support sign in, this method should call the `next()` function.
12
- */
13
- signInMiddleware(req: Request, res: Response, next: NextFunction): Promise<void | Response>;
14
-
15
- /**
16
- * Sign out middleware. This method is used to handle the sign out request and use response to send the sign out response.
17
- * <br/>
18
- * If the IDP implementation does not support sign out, this method should call the `next()` function.
19
- */
20
- signOutMiddleware(req: Request, res: Response, next: NextFunction): Promise<void | Response>;
21
-
22
- /**
23
- * Access token middleware. This method is used to handle the access token request and use response to send the access token response.
24
- * <br/>
25
- * If the IDP implementation does not support access token, this method should call the `next()` function.
26
- */
27
- accessTokenMiddleware(req: Request, res: Response, next: NextFunction): Promise<void | Response>;
28
-
29
- /**
30
- * User api middleware. This method is used to handle the user request and use response to send the user response.
31
- * <br/>
32
- * If the IDP implementation does not support user, this method should call the `next()` function.
33
- */
34
- userApiMiddleware(req: Request, res: Response, next: NextFunction): Promise<Response<Payload>>;
35
-
36
- /**
37
- * Introspect a token
38
- * @param token - The token to introspect
39
- * @returns The token payload
40
- */
41
- introspectToken(token: string): Promise<Payload | null>;
42
-
43
- /**
44
- * Parse a token
45
- * @param token - The token to parse
46
- * @returns The token payload
47
- */
48
- parseToken(token: string): Promise<Payload | null>;
49
-
50
- /**
51
- * Refresh a token
52
- * @param refreshToken - The refresh token to use for the refresh
53
- * @returns The authentication result
54
- */
55
- refreshToken(refreshToken: string): Promise<AuthResult>;
56
-
57
- /**
58
- * Revoke a token. In different IDP implementations, this may have different token types.
59
- * @param token - The token to revoke
60
- */
61
- revokeToken(token: string): Promise<void>;
62
-
63
- /**
64
- * Initialize the IDP. Create resources, connect to databases, etc.
65
- */
66
- initialize(): Promise<void>;
67
-
68
- /**
69
- * Shutdown the IDP, close all connections and release resources
70
- */
71
- shutdown(): Promise<void>;
72
- }