orange-auth 0.1.2 → 1.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/README.md +177 -118
- package/dist/@types/globals.d.ts +90 -3
- package/dist/@types/globals.d.ts.map +1 -1
- package/dist/@types/internals.d.ts +13 -0
- package/dist/@types/internals.d.ts.map +1 -0
- package/dist/@types/internals.js +1 -0
- package/dist/functions/index.d.ts +3 -0
- package/dist/functions/index.d.ts.map +1 -0
- package/dist/functions/index.js +2 -0
- package/dist/functions/jwt.d.ts.map +1 -1
- package/dist/functions/jwt.js +1 -1
- package/dist/functions/urlencodedToJson.d.ts +2 -0
- package/dist/functions/urlencodedToJson.d.ts.map +1 -0
- package/dist/functions/urlencodedToJson.js +8 -0
- package/dist/index.d.ts +3 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -2
- package/dist/lib.d.ts +37 -51
- package/dist/lib.d.ts.map +1 -1
- package/dist/lib.js +133 -68
- package/dist/providers/Credentials.d.ts +3 -4
- package/dist/providers/Credentials.d.ts.map +1 -1
- package/dist/providers/Credentials.js +20 -6
- package/dist/providers/IProvider.d.ts +2 -5
- package/dist/providers/IProvider.d.ts.map +1 -1
- package/dist/strategies/IStrategy.d.ts +4 -4
- package/dist/strategies/IStrategy.d.ts.map +1 -1
- package/dist/strategies/jwt.d.ts +3 -3
- package/dist/strategies/jwt.d.ts.map +1 -1
- package/dist/strategies/jwt.js +15 -2
- package/package.json +16 -3
package/README.md
CHANGED
|
@@ -1,118 +1,177 @@
|
|
|
1
|
-
# 🍊 Orange Auth
|
|
2
|
-
|
|
3
|
-
### THIS IS A VERY EARLY WIP, AND SHOULD NOT BE USED
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
-
|
|
12
|
-
-
|
|
13
|
-
-
|
|
14
|
-
- Framework-agnostic
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
});
|
|
52
|
-
```
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
```
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
1
|
+
# 🍊 Orange Auth
|
|
2
|
+
|
|
3
|
+
### THIS IS A VERY EARLY WIP, AND SHOULD NOT BE USED IN PRODUCTION
|
|
4
|
+
|
|
5
|
+
Authentication middleware for [@universal-middleware/core](https://www.npmjs.com/package/@universal-middleware/core), supporting provider-based login/logout and strategy-driven session handling via secure cookies.
|
|
6
|
+
|
|
7
|
+
## 🚀 Features
|
|
8
|
+
|
|
9
|
+
- Provider-based login/logout flow
|
|
10
|
+
- Pluggable strategies (e.g. JWT)
|
|
11
|
+
- Secure cookie-based session storage
|
|
12
|
+
- Simple, composable handler with session retrieval
|
|
13
|
+
- Optional cookie settings
|
|
14
|
+
- Framework-agnostic
|
|
15
|
+
|
|
16
|
+
## 📦 Install
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
npm install orange-auth
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## 🧠 Usage
|
|
23
|
+
|
|
24
|
+
### Setup
|
|
25
|
+
|
|
26
|
+
```ts
|
|
27
|
+
import { CreateAuth } from "orange-auth";
|
|
28
|
+
import { JWT } from "orange-auth/strategies";
|
|
29
|
+
import { Credentials } from "orange-auth/providers";
|
|
30
|
+
|
|
31
|
+
const { handler, getSession } = CreateAuth({
|
|
32
|
+
providers: [new Credentials({ ... })],
|
|
33
|
+
strategy: new JWT({ ... }),
|
|
34
|
+
secret: "your-secret",
|
|
35
|
+
basePath: "/api/auth/:action/:provider",
|
|
36
|
+
});
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
You can now use:
|
|
40
|
+
- `POST /api/auth/login/:provider`
|
|
41
|
+
- `POST /api/auth/logout/:provider`
|
|
42
|
+
|
|
43
|
+
### Example (express middleware router)
|
|
44
|
+
|
|
45
|
+
```ts
|
|
46
|
+
import express from "express";
|
|
47
|
+
import { handler } from "./auth";
|
|
48
|
+
import { createHandler } from "@universal-middleware/express";
|
|
49
|
+
|
|
50
|
+
const app = express();
|
|
51
|
+
app.get("/api/auth/{*auth}", createHandler(handler)());
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
## 🧾 Session Access
|
|
55
|
+
|
|
56
|
+
```ts
|
|
57
|
+
import { getSession } from "./auth";
|
|
58
|
+
|
|
59
|
+
const session = await getSession(req);
|
|
60
|
+
|
|
61
|
+
if (session) {
|
|
62
|
+
console.log("Logged in user:", session.user);
|
|
63
|
+
}
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
## 🧩 Config Options
|
|
67
|
+
|
|
68
|
+
```ts
|
|
69
|
+
type ConfigOptionsProps = {
|
|
70
|
+
/**
|
|
71
|
+
* All the available providers.
|
|
72
|
+
* If multiple instance of a single provider are used with the same name, the order does matter.
|
|
73
|
+
*/
|
|
74
|
+
providers: IProvider[];
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Your secret key.
|
|
78
|
+
*/
|
|
79
|
+
secret: string;
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* A custom name for the cookie.
|
|
83
|
+
* Otherwise, the default name will be `orange.auth`
|
|
84
|
+
*/
|
|
85
|
+
cookieName?: string;
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* The strategy to be used.
|
|
89
|
+
*/
|
|
90
|
+
strategy: IStrategy;
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* This should be the url path that your auth is set up on, including the action and provider variables.
|
|
94
|
+
* @example
|
|
95
|
+
* ```js
|
|
96
|
+
* const app = express();
|
|
97
|
+
*
|
|
98
|
+
* const { handler } = CreateAuth({
|
|
99
|
+
* basePath: "/api/auth/:action/:provider",
|
|
100
|
+
* ...
|
|
101
|
+
* });
|
|
102
|
+
*
|
|
103
|
+
* app.all("/api/auth/{*auth}", createHandler(handler)());
|
|
104
|
+
* ```
|
|
105
|
+
*/
|
|
106
|
+
basePath: string;
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Cookie serialization options. see [MDN Cookie](https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/Cookies)
|
|
110
|
+
*/
|
|
111
|
+
cookieSettings?: SerializeOptions;
|
|
112
|
+
};
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
## 🧱 Interfaces
|
|
116
|
+
|
|
117
|
+
### IProvider
|
|
118
|
+
|
|
119
|
+
```ts
|
|
120
|
+
abstract class IProvider {
|
|
121
|
+
/**
|
|
122
|
+
* Custom name for a provider
|
|
123
|
+
*/
|
|
124
|
+
ID: string;
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Login function. This is used to call all the login flows of each provider.
|
|
128
|
+
* For now, the request's body **MUST** be JSON.
|
|
129
|
+
* @param req The request object.
|
|
130
|
+
* @param globalCfg The global auth config.
|
|
131
|
+
*/
|
|
132
|
+
logIn(req: Request, globalCfg: ConfigOptions): Promise<string | null>;
|
|
133
|
+
}
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
### IStrategy
|
|
137
|
+
|
|
138
|
+
```ts
|
|
139
|
+
abstract class IStrategy {
|
|
140
|
+
/**
|
|
141
|
+
* Handles how a session token is generated.
|
|
142
|
+
* @param session The validated session object.
|
|
143
|
+
* @param globalCfg The global auth config.
|
|
144
|
+
* @returns A newly generated token that will be sent as a cookie.
|
|
145
|
+
*/
|
|
146
|
+
serialize(session: Session, globalCfg: ConfigOptions): Promise<string>;
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Handles how a token is validated and deserialized into a session object.
|
|
150
|
+
* @param token A user's token.
|
|
151
|
+
* @param globalCfg The global auth config.
|
|
152
|
+
* @returns A user's session if validated and found, else `null`.
|
|
153
|
+
*/
|
|
154
|
+
deserialize(token: string, globalCfg: ConfigOptions): Promise<Session | null>;
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Handles how a session is destroyed when a user is logging out.
|
|
158
|
+
* @param req The request object.
|
|
159
|
+
* @param globalCfg The global auth config.
|
|
160
|
+
*/
|
|
161
|
+
logOut(req: Request, globalCfg: ConfigOptions): Promise<void>;
|
|
162
|
+
}
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
---
|
|
166
|
+
|
|
167
|
+
## 🔐 Cookie Defaults
|
|
168
|
+
|
|
169
|
+
By default, the cookie is:
|
|
170
|
+
|
|
171
|
+
- `httpOnly: true`
|
|
172
|
+
- `secure: true`
|
|
173
|
+
- `sameSite: "lax"`
|
|
174
|
+
- `path: "/"`
|
|
175
|
+
- `maxAge: 3600` (1 hour)
|
|
176
|
+
|
|
177
|
+
This can be customized via `cookieSettings` in the initial config.
|
package/dist/@types/globals.d.ts
CHANGED
|
@@ -1,10 +1,97 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
}
|
|
1
|
+
import type { SerializeOptions } from "cookie";
|
|
2
|
+
import type { IProvider } from "../providers/IProvider";
|
|
3
|
+
import type { IStrategy } from "../strategies/IStrategy";
|
|
4
|
+
/**
|
|
5
|
+
* This is a Promise, or not...
|
|
6
|
+
*/
|
|
7
|
+
export type MaybePromise<T> = T | Promise<T>;
|
|
4
8
|
/**
|
|
5
9
|
* General session type. This should be augmented to include your session's fields.
|
|
6
10
|
*/
|
|
7
11
|
export interface Session extends Record<string, unknown> {
|
|
8
12
|
id: string;
|
|
9
13
|
}
|
|
14
|
+
/**
|
|
15
|
+
* Parameters for the custom callbacks
|
|
16
|
+
*/
|
|
17
|
+
type CallbackParams = {
|
|
18
|
+
/**
|
|
19
|
+
* The current token
|
|
20
|
+
*/
|
|
21
|
+
token: string;
|
|
22
|
+
/**
|
|
23
|
+
* The current deserialized token
|
|
24
|
+
*/
|
|
25
|
+
session: Session;
|
|
26
|
+
/**
|
|
27
|
+
* TThe request's headers
|
|
28
|
+
*/
|
|
29
|
+
headers: Headers;
|
|
30
|
+
};
|
|
31
|
+
/**
|
|
32
|
+
* Auth Configuration props.
|
|
33
|
+
*/
|
|
34
|
+
export type ConfigOptionsProps = Readonly<{
|
|
35
|
+
/**
|
|
36
|
+
* All the available providers.
|
|
37
|
+
* If multiple instance of a single provider are used, the order does matter.
|
|
38
|
+
*/
|
|
39
|
+
providers: IProvider[];
|
|
40
|
+
/**
|
|
41
|
+
* Your secret key.
|
|
42
|
+
*/
|
|
43
|
+
secret: string | {
|
|
44
|
+
publicKey: string;
|
|
45
|
+
privateKey: string;
|
|
46
|
+
};
|
|
47
|
+
/**
|
|
48
|
+
* A custom name for the cookie.
|
|
49
|
+
* Otherwise, the default name will be `orange.auth`
|
|
50
|
+
*/
|
|
51
|
+
cookieName?: string;
|
|
52
|
+
/**
|
|
53
|
+
* The strategy to be used.
|
|
54
|
+
*/
|
|
55
|
+
strategy: IStrategy;
|
|
56
|
+
/**
|
|
57
|
+
* This should be the url path that your auth is set up on, including the action and provider variables.
|
|
58
|
+
* @example
|
|
59
|
+
* ```js
|
|
60
|
+
* const app = express();
|
|
61
|
+
*
|
|
62
|
+
* const { handler } = CreateAuth({
|
|
63
|
+
* basePath: "/api/auth/:action/:provider",
|
|
64
|
+
* ...
|
|
65
|
+
* });
|
|
66
|
+
*
|
|
67
|
+
* app.all("/api/auth/{*auth}", createHandler(handler)());
|
|
68
|
+
* ```
|
|
69
|
+
*/
|
|
70
|
+
basePath: string;
|
|
71
|
+
/**
|
|
72
|
+
* Cookie serialization options. see [MDN Cookie](https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/Cookies)
|
|
73
|
+
*/
|
|
74
|
+
cookieSettings?: SerializeOptions;
|
|
75
|
+
/**
|
|
76
|
+
* Custom callbacks
|
|
77
|
+
*/
|
|
78
|
+
callbacks?: {
|
|
79
|
+
/**
|
|
80
|
+
* Custom login callback. This is ran after logging in with the provider.
|
|
81
|
+
* This can accept 2 return types: a boolean that indicates if the login is valid,
|
|
82
|
+
* or a url which will redirect the user.
|
|
83
|
+
* @param params An object containing the token, session and headers of a request.
|
|
84
|
+
* @returns a boolean that indicates if the login is valid,
|
|
85
|
+
* or a url which will redirect the user.
|
|
86
|
+
*/
|
|
87
|
+
login?: (params: CallbackParams) => MaybePromise<boolean | string>;
|
|
88
|
+
/**
|
|
89
|
+
* Custom logout callback. This is ran before logging out with the strategy.
|
|
90
|
+
* @param params An object containing the token, session and headers of a request.
|
|
91
|
+
* @returns Nothing, or an empty promise.
|
|
92
|
+
*/
|
|
93
|
+
logout?: (params: CallbackParams) => MaybePromise<void>;
|
|
94
|
+
};
|
|
95
|
+
}>;
|
|
96
|
+
export {};
|
|
10
97
|
//# sourceMappingURL=globals.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"globals.d.ts","sourceRoot":"","sources":["../../src/@types/globals.ts"],"names":[],"mappings":"AAAA,OAAO,CAAC,MAAM,CAAC;
|
|
1
|
+
{"version":3,"file":"globals.d.ts","sourceRoot":"","sources":["../../src/@types/globals.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,QAAQ,CAAC;AAC/C,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,wBAAwB,CAAC;AACxD,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAC;AAEzD;;GAEG;AACH,MAAM,MAAM,YAAY,CAAC,CAAC,IAAI,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC;AAE7C;;GAEG;AACH,MAAM,WAAW,OAAQ,SAAQ,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;IACpD,EAAE,EAAE,MAAM,CAAC;CACd;AAED;;GAEG;AACH,KAAK,cAAc,GAAG;IAClB;;OAEG;IACH,KAAK,EAAE,MAAM,CAAC;IACd;;OAEG;IACH,OAAO,EAAE,OAAO,CAAC;IACjB;;OAEG;IACH,OAAO,EAAE,OAAO,CAAC;CACpB,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,kBAAkB,GAAG,QAAQ,CAAC;IACtC;;;OAGG;IACH,SAAS,EAAE,SAAS,EAAE,CAAC;IAEvB;;OAEG;IACH,MAAM,EAAE,MAAM,GAAG;QAAE,SAAS,EAAE,MAAM,CAAC;QAAC,UAAU,EAAE,MAAM,CAAA;KAAE,CAAC;IAE3D;;;OAGG;IACH,UAAU,CAAC,EAAE,MAAM,CAAC;IAEpB;;OAEG;IACH,QAAQ,EAAE,SAAS,CAAC;IAEpB;;;;;;;;;;;;;OAaG;IACH,QAAQ,EAAE,MAAM,CAAC;IAEjB;;OAEG;IACH,cAAc,CAAC,EAAE,gBAAgB,CAAC;IAElC;;OAEG;IACH,SAAS,CAAC,EAAE;QACR;;;;;;;WAOG;QACH,KAAK,CAAC,EAAE,CAAC,MAAM,EAAE,cAAc,KAAK,YAAY,CAAC,OAAO,GAAG,MAAM,CAAC,CAAC;QAEnE;;;;WAIG;QACH,MAAM,CAAC,EAAE,CAAC,MAAM,EAAE,cAAc,KAAK,YAAY,CAAC,IAAI,CAAC,CAAC;KAC3D,CAAC;CACL,CAAC,CAAC"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { RequiredDeep } from "type-fest";
|
|
2
|
+
import type { ConfigOptionsProps } from "./globals";
|
|
3
|
+
/**
|
|
4
|
+
* Internally used version of the options
|
|
5
|
+
*/
|
|
6
|
+
export type ConfigOptions = Omit<RequiredDeep<Omit<ConfigOptionsProps, "basePath">>, "cookieSettings"> & {
|
|
7
|
+
cookieSettings: NonNullable<ConfigOptionsProps["cookieSettings"]>;
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* Maybe there is a value, maybe not 🤷♂️
|
|
11
|
+
*/
|
|
12
|
+
export type Maybe<T> = T | null | undefined;
|
|
13
|
+
//# sourceMappingURL=internals.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"internals.d.ts","sourceRoot":"","sources":["../../src/@types/internals.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,WAAW,CAAC;AAC9C,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,WAAW,CAAC;AAEpD;;GAEG;AACH,MAAM,MAAM,aAAa,GAAG,IAAI,CAAC,YAAY,CAAC,IAAI,CAAC,kBAAkB,EAAE,UAAU,CAAC,CAAC,EAAE,gBAAgB,CAAC,GAAG;IAErG,cAAc,EAAE,WAAW,CAAC,kBAAkB,CAAC,gBAAgB,CAAC,CAAC,CAAC;CACrE,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,KAAK,CAAC,CAAC,IAAI,CAAC,GAAG,IAAI,GAAG,SAAS,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/functions/index.ts"],"names":[],"mappings":"AAAA,cAAc,OAAO,CAAC;AACtB,cAAc,oBAAoB,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"jwt.d.ts","sourceRoot":"","sources":["../../src/functions/jwt.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"jwt.d.ts","sourceRoot":"","sources":["../../src/functions/jwt.ts"],"names":[],"mappings":"AAAA,OAAO,EAAwB,KAAK,UAAU,EAAE,KAAK,SAAS,EAAE,KAAK,MAAM,EAAE,KAAK,aAAa,EAAE,MAAM,cAAc,CAAC;AAGtH,OAAO,EAAE,IAAI,EAAE,MAAM,cAAc,CAAC;AAEpC;;;;;;GAMG;AACH,wBAAgB,MAAM,CAAC,CAAC,SAAS,UAAU,GAAG,UAAU,EACpD,KAAK,EAAE,MAAM,EACb,iBAAiB,EAAE,MAAM,GAAG,SAAS,EACrC,OAAO,CAAC,EAAE,aAAa,qBAS1B"}
|
package/dist/functions/jwt.js
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"urlencodedToJson.d.ts","sourceRoot":"","sources":["../../src/functions/urlencodedToJson.ts"],"names":[],"mappings":"AAAA,wBAAgB,gBAAgB,CAAC,CAAC,SAAS,MAAM,GAAG,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,CAAC,CAS5E"}
|
package/dist/index.d.ts
CHANGED
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,OAAO,CAAC;AACtB,cAAc,aAAa,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,OAAO,CAAC;AACtB,cAAc,kBAAkB,CAAC;AACjC,OAAO,KAAK,SAAS,MAAM,aAAa,CAAC;AACzC,OAAO,KAAK,UAAU,MAAM,cAAc,CAAC"}
|
package/dist/index.js
CHANGED
package/dist/lib.d.ts
CHANGED
|
@@ -1,60 +1,46 @@
|
|
|
1
|
+
import type { Maybe } from "./@types/internals";
|
|
1
2
|
import type { Session } from "./@types/globals";
|
|
2
|
-
import type { IStrategy } from "./strategies/IStrategy";
|
|
3
|
-
import type { IProvider } from "./providers/IProvider";
|
|
4
|
-
import { type SerializeOptions } from "cookie";
|
|
5
|
-
type Maybe<T> = T | null | undefined;
|
|
6
3
|
/**
|
|
7
|
-
*
|
|
4
|
+
* Initializes the auth. This should be called once per backend.
|
|
5
|
+
* @param req Something that has a `headers` field; either a Headers instance, or just a plain object.
|
|
6
|
+
* @returns A session if found and valid, or `null`.
|
|
8
7
|
*/
|
|
9
|
-
export
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* Your secret key.
|
|
17
|
-
*/
|
|
18
|
-
secret: string;
|
|
19
|
-
/**
|
|
20
|
-
* A custom name for the cookie.
|
|
21
|
-
* Otherwise, the default name will be `orange.auth`
|
|
22
|
-
*/
|
|
8
|
+
export declare const CreateAuth: (config: Readonly<{
|
|
9
|
+
providers: import("./providers").IProvider[];
|
|
10
|
+
secret: string | {
|
|
11
|
+
publicKey: string;
|
|
12
|
+
privateKey: string;
|
|
13
|
+
};
|
|
23
14
|
cookieName?: string;
|
|
15
|
+
strategy: import("./strategies").IStrategy;
|
|
16
|
+
basePath: string;
|
|
17
|
+
cookieSettings?: import("cookie").SerializeOptions;
|
|
18
|
+
callbacks?: {
|
|
19
|
+
login?: (params: {
|
|
20
|
+
token: string;
|
|
21
|
+
session: Session;
|
|
22
|
+
headers: Headers;
|
|
23
|
+
}) => import(".").MaybePromise<boolean | string>;
|
|
24
|
+
logout?: (params: {
|
|
25
|
+
token: string;
|
|
26
|
+
session: Session;
|
|
27
|
+
headers: Headers;
|
|
28
|
+
}) => import(".").MaybePromise<void>;
|
|
29
|
+
};
|
|
30
|
+
}>) => {
|
|
24
31
|
/**
|
|
25
|
-
*
|
|
26
|
-
|
|
27
|
-
strategy: IStrategy;
|
|
28
|
-
/**
|
|
29
|
-
* This should be the url path that your auth is set up on, including the action and provider variables.
|
|
30
|
-
* @example
|
|
31
|
-
* ```js
|
|
32
|
-
* const app = express();
|
|
33
|
-
*
|
|
34
|
-
* app.all("/api/auth/{*auth}", createHandler(CreateAuth)({
|
|
35
|
-
* basePath: "/api/auth/:action/:provider",
|
|
36
|
-
* ...
|
|
37
|
-
* }))
|
|
38
|
-
* ```
|
|
32
|
+
* Universal handler route. You can use this with the `createHandler()` method
|
|
33
|
+
* @returns
|
|
39
34
|
*/
|
|
40
|
-
|
|
35
|
+
handler: () => (req: Request, _: Universal.Context, runtime: import("@universal-middleware/core").RuntimeAdapter) => Promise<Response>;
|
|
41
36
|
/**
|
|
42
|
-
*
|
|
37
|
+
* Deserialize a user's session.
|
|
38
|
+
* @param globalCfg The global auth config
|
|
39
|
+
* @param req An object having a headers field
|
|
40
|
+
* @returns A user's token and session, if found and valid
|
|
43
41
|
*/
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
*/
|
|
49
|
-
export type __internal__Options = Required<Omit<ConfigOptionsProps, "basePath">>;
|
|
50
|
-
export declare const CreateAuth: (config: ConfigOptionsProps) => (req: Request, _: Universal.Context, runtime: import("@universal-middleware/core").RuntimeAdapter) => Promise<Response>;
|
|
51
|
-
/**
|
|
52
|
-
* Access a user's session.
|
|
53
|
-
* @param req Something that has a `headers` field; either a Headers instance, or just a plain object.
|
|
54
|
-
* @returns A session if found and valid, or `null`.
|
|
55
|
-
*/
|
|
56
|
-
export declare const getSession: <T extends Session = Session>(req: {
|
|
57
|
-
headers: Maybe<Headers | Record<string, string>>;
|
|
58
|
-
}) => Promise<T | null> | null;
|
|
59
|
-
export {};
|
|
42
|
+
getSession: <T extends Session = Session>(req: {
|
|
43
|
+
headers: Maybe<Headers | Record<string, string>>;
|
|
44
|
+
}) => Promise<T | null>;
|
|
45
|
+
};
|
|
60
46
|
//# sourceMappingURL=lib.d.ts.map
|
package/dist/lib.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"lib.d.ts","sourceRoot":"","sources":["../src/lib.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"lib.d.ts","sourceRoot":"","sources":["../src/lib.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAiB,KAAK,EAAE,MAAM,oBAAoB,CAAC;AAE/D,OAAO,KAAK,EAAsB,OAAO,EAAE,MAAM,kBAAkB,CAAC;AA4CpE;;;;GAIG;AACH,eAAO,MAAM,UAAU;;;;;;;;;;;aAYuB,CAAA;;;;;cAUvB,CAAC;;;;;;;IAOhB;;;OAGG;;IAwGH;;;;;OAKG;iBACU,CAAC,SAAS,OAAO,iBAAiB;QAAE,OAAO,EAAE,KAAK,CAAC,OAAO,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAAA;KAAE,KAEnC,OAAO,CAAC,CAAC,GAAG,IAAI,CAAC;CAUxF,CAAC"}
|
package/dist/lib.js
CHANGED
|
@@ -1,17 +1,47 @@
|
|
|
1
1
|
import Cookies from "universal-cookie";
|
|
2
|
-
import { assign, find, isNil } from "lodash-es";
|
|
3
2
|
import { serialize as cookie } from "cookie";
|
|
3
|
+
import { assign, find, isNil, isString, merge } from "lodash-es";
|
|
4
4
|
import { params } from "@universal-middleware/core";
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
5
|
+
/**
|
|
6
|
+
* Deserialize a user's session based of the headers
|
|
7
|
+
* @param globalCfg The global auth config
|
|
8
|
+
* @param req An object having a headers field
|
|
9
|
+
* @returns A user's token and session, if found and valid
|
|
10
|
+
*/
|
|
11
|
+
const getSession = async (globalCfg, req) => {
|
|
12
|
+
if (isNil(req.headers))
|
|
13
|
+
return {
|
|
14
|
+
session: null,
|
|
15
|
+
token: null,
|
|
16
|
+
};
|
|
17
|
+
// Find the correct cookie header
|
|
18
|
+
const cookieHeader = req.headers instanceof Headers ? req.headers.get("cookie") : req.headers["cookie"];
|
|
19
|
+
const cookie = new Cookies(cookieHeader);
|
|
20
|
+
if (isNil(cookie))
|
|
21
|
+
return {
|
|
22
|
+
session: null,
|
|
23
|
+
token: null,
|
|
24
|
+
};
|
|
25
|
+
// Tries to extract the specific cookie.
|
|
26
|
+
const token = cookie.get(globalCfg.cookieName);
|
|
27
|
+
if (isNil(token))
|
|
28
|
+
return {
|
|
29
|
+
session: null,
|
|
30
|
+
token: null,
|
|
31
|
+
};
|
|
32
|
+
// Tries to deserialize it
|
|
33
|
+
return {
|
|
34
|
+
session: (await globalCfg.strategy.deserialize(token, globalCfg)),
|
|
35
|
+
token: token,
|
|
36
|
+
};
|
|
12
37
|
};
|
|
38
|
+
/**
|
|
39
|
+
* Initializes the auth. This should be called once per backend.
|
|
40
|
+
* @param req Something that has a `headers` field; either a Headers instance, or just a plain object.
|
|
41
|
+
* @returns A session if found and valid, or `null`.
|
|
42
|
+
*/
|
|
13
43
|
export const CreateAuth = ((config) => {
|
|
14
|
-
const { secret, strategy, cookieName, providers, cookieSettings, basePath } = config;
|
|
44
|
+
const { secret, strategy, cookieName, providers, cookieSettings, basePath, callbacks } = config;
|
|
15
45
|
if (isNil(secret)) {
|
|
16
46
|
throw new Error('[ERROR]: Auth secret missing! Make sure to set the "secret" variable in the auth\'s config.');
|
|
17
47
|
}
|
|
@@ -20,7 +50,7 @@ export const CreateAuth = ((config) => {
|
|
|
20
50
|
}
|
|
21
51
|
// We set the global config on startup, and not on the route handler,
|
|
22
52
|
// otherwise a session cannot be accessed until someone logs in
|
|
23
|
-
|
|
53
|
+
const globalCfg = {
|
|
24
54
|
cookieName: cookieName ?? "orange.auth",
|
|
25
55
|
providers: providers ?? [],
|
|
26
56
|
secret,
|
|
@@ -32,65 +62,100 @@ export const CreateAuth = ((config) => {
|
|
|
32
62
|
secure: true,
|
|
33
63
|
maxAge: 3600,
|
|
34
64
|
},
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
}
|
|
48
|
-
// Handles each action independently
|
|
49
|
-
switch (routeParams["action"]) {
|
|
50
|
-
case "login": {
|
|
51
|
-
// Use the found provider to login
|
|
52
|
-
const token = await provider.logIn(req, globalCfg).catch(() => null);
|
|
53
|
-
// If failed, return Bad Request response
|
|
54
|
-
if (isNil(token))
|
|
55
|
-
return new Response(null, { status: 400 });
|
|
56
|
-
// Creates the set-cookie header
|
|
57
|
-
const headers = new Headers();
|
|
58
|
-
headers.set("Set-Cookie", cookie(globalCfg.cookieName, token, globalCfg.cookieSettings));
|
|
59
|
-
// And returns it
|
|
60
|
-
return new Response(null, { status: 200, headers });
|
|
61
|
-
}
|
|
62
|
-
case "logout": {
|
|
63
|
-
// Use the strategy to logout
|
|
64
|
-
await globalCfg.strategy.logOut(req, globalCfg);
|
|
65
|
-
// Clears the header.
|
|
66
|
-
const headers = new Headers();
|
|
67
|
-
headers.set("Set-Cookie", cookie(globalCfg.cookieName, ""));
|
|
68
|
-
// And send them
|
|
69
|
-
return new Response(null, { status: 200, headers });
|
|
65
|
+
callbacks: merge({}, { login: () => ({}), logout: () => ({}) }, callbacks),
|
|
66
|
+
};
|
|
67
|
+
return {
|
|
68
|
+
/**
|
|
69
|
+
* Universal handler route. You can use this with the `createHandler()` method
|
|
70
|
+
* @returns
|
|
71
|
+
*/
|
|
72
|
+
handler: () => async (req, _, runtime) => {
|
|
73
|
+
// Tries to get the action and provider info from the url
|
|
74
|
+
const routeParams = params(req, runtime, basePath);
|
|
75
|
+
if (isNil(routeParams?.["action"]) || isNil(routeParams["provider"])) {
|
|
76
|
+
throw new Error('[ERROR]: Base path is missing! Make sure to set the "basePath" variable in the auth\'s config.');
|
|
70
77
|
}
|
|
71
|
-
|
|
72
|
-
|
|
78
|
+
// Finds the requested provider by name
|
|
79
|
+
const path = routeParams["provider"];
|
|
80
|
+
const provider = find(providers, (p) => p.ID === path);
|
|
81
|
+
if (isNil(provider)) {
|
|
73
82
|
return new Response("Page not found", { status: 404 });
|
|
74
|
-
|
|
83
|
+
}
|
|
84
|
+
// Handles each action independently
|
|
85
|
+
switch (routeParams["action"]) {
|
|
86
|
+
case "login": {
|
|
87
|
+
// Use the found provider to login
|
|
88
|
+
const token = await provider.logIn(req, globalCfg).catch(() => null);
|
|
89
|
+
// If failed, return Bad Request response
|
|
90
|
+
if (isNil(token))
|
|
91
|
+
return new Response(null, { status: 400 });
|
|
92
|
+
const params = await getSession(globalCfg, {
|
|
93
|
+
// The cookie header is faked here, since the request does not have any token yet.
|
|
94
|
+
headers: { cookie: cookie(globalCfg.cookieName, token) },
|
|
95
|
+
});
|
|
96
|
+
// If there is no session at this point, something as gone wrong
|
|
97
|
+
if (isNil(params.session) || isNil(params.token)) {
|
|
98
|
+
console.error("[AUTH ERROR]: Missing session after login");
|
|
99
|
+
return new Response("internal server error", { status: 500 });
|
|
100
|
+
}
|
|
101
|
+
// Run the login callback
|
|
102
|
+
const customRes = await globalCfg.callbacks.login({
|
|
103
|
+
headers: req.headers,
|
|
104
|
+
token: params.token,
|
|
105
|
+
session: params.session,
|
|
106
|
+
});
|
|
107
|
+
// If the result is false, fail the login
|
|
108
|
+
if (customRes === false) {
|
|
109
|
+
return new Response("Bad Request", { status: 400 });
|
|
110
|
+
}
|
|
111
|
+
// If the result is a string, assume it is a redirection path
|
|
112
|
+
if (isString(customRes)) {
|
|
113
|
+
const headers = new Headers();
|
|
114
|
+
headers.set("Location", customRes);
|
|
115
|
+
return new Response(null, { status: 308, headers });
|
|
116
|
+
}
|
|
117
|
+
// Creates the set-cookie header
|
|
118
|
+
const headers = new Headers();
|
|
119
|
+
headers.set("Set-Cookie", cookie(globalCfg.cookieName, token, globalCfg.cookieSettings));
|
|
120
|
+
// And return it
|
|
121
|
+
return new Response(null, { status: 200, headers });
|
|
122
|
+
}
|
|
123
|
+
case "logout": {
|
|
124
|
+
const params = await getSession(globalCfg, req);
|
|
125
|
+
// If there is no session, no need to call the callback
|
|
126
|
+
if (!isNil(params.session) && !isNil(params.token)) {
|
|
127
|
+
await globalCfg.callbacks.logout({
|
|
128
|
+
headers: req.headers,
|
|
129
|
+
token: params.token,
|
|
130
|
+
session: params.session,
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
// Use the strategy to logout
|
|
134
|
+
await globalCfg.strategy.logOut(req, globalCfg);
|
|
135
|
+
// Clears the header.
|
|
136
|
+
const headers = new Headers();
|
|
137
|
+
headers.set("Set-Cookie", cookie(globalCfg.cookieName, "deleted",
|
|
138
|
+
// Use the same cookie config, but make sure it is expired
|
|
139
|
+
assign({}, globalCfg.cookieSettings, {
|
|
140
|
+
expires: new Date(0),
|
|
141
|
+
maxAge: undefined,
|
|
142
|
+
})));
|
|
143
|
+
// And send them
|
|
144
|
+
return new Response(null, { status: 200, headers });
|
|
145
|
+
}
|
|
146
|
+
default:
|
|
147
|
+
// If a wrong action is requested, return a 404
|
|
148
|
+
return new Response("Page not found", { status: 404 });
|
|
149
|
+
}
|
|
150
|
+
},
|
|
151
|
+
/**
|
|
152
|
+
* Deserialize a user's session.
|
|
153
|
+
* @param globalCfg The global auth config
|
|
154
|
+
* @param req An object having a headers field
|
|
155
|
+
* @returns A user's token and session, if found and valid
|
|
156
|
+
*/
|
|
157
|
+
getSession: (req) =>
|
|
158
|
+
// Only returns the session
|
|
159
|
+
getSession(globalCfg, req).then((doc) => doc.session),
|
|
75
160
|
};
|
|
76
161
|
});
|
|
77
|
-
/**
|
|
78
|
-
* Access a user's session.
|
|
79
|
-
* @param req Something that has a `headers` field; either a Headers instance, or just a plain object.
|
|
80
|
-
* @returns A session if found and valid, or `null`.
|
|
81
|
-
*/
|
|
82
|
-
export const getSession = (req) => {
|
|
83
|
-
if (isNil(req.headers))
|
|
84
|
-
return null;
|
|
85
|
-
// Find the correct cookie header
|
|
86
|
-
const cookieHeader = req.headers instanceof Headers ? req.headers.get("cookie") : req.headers["cookie"];
|
|
87
|
-
const cookie = new Cookies(cookieHeader);
|
|
88
|
-
if (isNil(cookie))
|
|
89
|
-
return null;
|
|
90
|
-
// Tries to extract the specific cookie.
|
|
91
|
-
const token = cookie.get(globalCfg.cookieName);
|
|
92
|
-
if (isNil(token))
|
|
93
|
-
return null;
|
|
94
|
-
// Tries to deserialize it
|
|
95
|
-
return globalCfg.strategy.deserialize(token, globalCfg);
|
|
96
|
-
};
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { IProvider } from "./IProvider";
|
|
2
|
-
import type {
|
|
3
|
-
import type { Session } from "../@types/globals";
|
|
2
|
+
import type { ConfigOptions } from "../@types/internals";
|
|
3
|
+
import type { Session, MaybePromise } from "../@types/globals";
|
|
4
4
|
/**
|
|
5
5
|
* Configuration options of the Credentials provider
|
|
6
6
|
*/
|
|
@@ -28,7 +28,6 @@ export type CredentialsConfig<TCredentials extends string> = Readonly<{
|
|
|
28
28
|
export declare class Credentials<TCredentials extends string = string> extends IProvider {
|
|
29
29
|
private config;
|
|
30
30
|
constructor(config: CredentialsConfig<TCredentials>);
|
|
31
|
-
|
|
32
|
-
logIn(req: Request, globalCfg: __internal__Options): Promise<string | null>;
|
|
31
|
+
logIn(req: Request, globalCfg: ConfigOptions): Promise<string | null>;
|
|
33
32
|
}
|
|
34
33
|
//# sourceMappingURL=Credentials.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Credentials.d.ts","sourceRoot":"","sources":["../../src/providers/Credentials.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;
|
|
1
|
+
{"version":3,"file":"Credentials.d.ts","sourceRoot":"","sources":["../../src/providers/Credentials.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAExC,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AACzD,OAAO,KAAK,EAAE,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AAE/D;;GAEG;AACH,MAAM,MAAM,iBAAiB,CAAC,YAAY,SAAS,MAAM,IAAI,QAAQ,CAAC;IAClE;;;OAGG;IACH,IAAI,CAAC,EAAE,aAAa,GAAG,CAAC,MAAM,GAAG,EAAE,CAAC,CAAC;IACrC;;OAEG;IACH,WAAW,EAAE,YAAY,EAAE,CAAC;IAC5B;;;;;OAKG;IACH,SAAS,EAAE,CAAC,WAAW,EAAE,MAAM,CAAC,YAAY,EAAE,MAAM,CAAC,KAAK,YAAY,CAAC,OAAO,GAAG,IAAI,CAAC,CAAC;CAC1F,CAAC,CAAC;AAEH;;GAEG;AACH,qBAAa,WAAW,CAAC,YAAY,SAAS,MAAM,GAAG,MAAM,CAAE,SAAQ,SAAS;IAC5E,OAAO,CAAC,MAAM,CAAkC;gBAEpC,MAAM,EAAE,iBAAiB,CAAC,YAAY,CAAC;IAK7B,KAAK,CAAC,GAAG,EAAE,OAAO,EAAE,SAAS,EAAE,aAAa,GAAG,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC;CA6B9F"}
|
|
@@ -1,20 +1,34 @@
|
|
|
1
1
|
import { isNil } from "lodash-es";
|
|
2
2
|
import { IProvider } from "./IProvider";
|
|
3
|
+
import { urlencodedToJson } from "../functions";
|
|
3
4
|
/**
|
|
4
5
|
* Provider used to login a user using basic credentials.
|
|
5
6
|
*/
|
|
6
7
|
export class Credentials extends IProvider {
|
|
7
8
|
config;
|
|
8
9
|
constructor(config) {
|
|
9
|
-
super("credentials");
|
|
10
|
+
super(config.name ?? "credentials");
|
|
10
11
|
this.config = config;
|
|
11
12
|
}
|
|
12
|
-
async getSession() {
|
|
13
|
-
throw new Error("This should never be used");
|
|
14
|
-
}
|
|
15
13
|
async logIn(req, globalCfg) {
|
|
16
|
-
|
|
17
|
-
|
|
14
|
+
const contentType = req.headers.get("Content-Type")?.split(";")[0] ?? "text/plain";
|
|
15
|
+
let body;
|
|
16
|
+
switch (contentType) {
|
|
17
|
+
case "application/json":
|
|
18
|
+
body = await req.json();
|
|
19
|
+
break;
|
|
20
|
+
case "application/x-www-urlencoded":
|
|
21
|
+
body = await req.text().then((urlencodedToJson));
|
|
22
|
+
break;
|
|
23
|
+
case "multipart/form-data":
|
|
24
|
+
const data = await req.formData();
|
|
25
|
+
body = Object.fromEntries(data);
|
|
26
|
+
break;
|
|
27
|
+
// fields should come from a form, so every un-supported types will be failing.
|
|
28
|
+
case "text/plain":
|
|
29
|
+
default:
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
18
32
|
// Calls the user defined authorize callback
|
|
19
33
|
const session = await this.config.authorize(body);
|
|
20
34
|
if (isNil(session))
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type { Session } from "../@types/globals";
|
|
1
|
+
import type { ConfigOptions } from "../@types/internals";
|
|
3
2
|
/**
|
|
4
3
|
* Available url callback actions.
|
|
5
4
|
*/
|
|
@@ -18,15 +17,13 @@ declare abstract class IProvider {
|
|
|
18
17
|
* The provider ID.
|
|
19
18
|
*/
|
|
20
19
|
get ID(): string;
|
|
21
|
-
/** @deprecated You can use the top level `getSession` instead. */
|
|
22
|
-
abstract getSession(req: Request, globalCfg: __internal__Options): Promise<Session | null>;
|
|
23
20
|
/**
|
|
24
21
|
* Login function. This is used to call all the login flows of each provider.
|
|
25
22
|
* For now, the request's body **MUST** be JSON.
|
|
26
23
|
* @param req The request object.
|
|
27
24
|
* @param globalCfg The global auth config.
|
|
28
25
|
*/
|
|
29
|
-
abstract logIn(req: Request, globalCfg:
|
|
26
|
+
abstract logIn(req: Request, globalCfg: ConfigOptions): Promise<string | null>;
|
|
30
27
|
}
|
|
31
28
|
export { IProvider };
|
|
32
29
|
//# sourceMappingURL=IProvider.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"IProvider.d.ts","sourceRoot":"","sources":["../../src/providers/IProvider.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,
|
|
1
|
+
{"version":3,"file":"IProvider.d.ts","sourceRoot":"","sources":["../../src/providers/IProvider.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAEzD;;GAEG;AACH,MAAM,MAAM,OAAO,GAAG,OAAO,GAAG,QAAQ,CAAC;AAEzC;;;GAGG;AACH,uBAAe,SAAS;IACpB;;OAEG;IACH,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAS;gBAElB,EAAE,EAAE,MAAM;IAItB;;OAEG;IACH,IAAW,EAAE,IAAI,MAAM,CAEtB;IAED;;;;;OAKG;aACa,KAAK,CAAC,GAAG,EAAE,OAAO,EAAE,SAAS,EAAE,aAAa,GAAG,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC;CACxF;AAED,OAAO,EAAE,SAAS,EAAE,CAAC"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { __internal__Options } from "../lib";
|
|
2
1
|
import { type Session } from "../@types/globals";
|
|
2
|
+
import type { ConfigOptions } from "../@types/internals";
|
|
3
3
|
/**
|
|
4
4
|
* A strategy is used to handle the creation, validation and accessing a user's session.
|
|
5
5
|
*/
|
|
@@ -10,20 +10,20 @@ declare abstract class IStrategy {
|
|
|
10
10
|
* @param globalCfg The global auth config.
|
|
11
11
|
* @returns A newly generated token that will be sent as a cookie.
|
|
12
12
|
*/
|
|
13
|
-
abstract serialize(session: Session, globalCfg:
|
|
13
|
+
abstract serialize(session: Session, globalCfg: ConfigOptions): Promise<string>;
|
|
14
14
|
/**
|
|
15
15
|
* Handles how a token is validated and deserialized into a session object.
|
|
16
16
|
* @param token A user's token.
|
|
17
17
|
* @param globalCfg The global auth config.
|
|
18
18
|
* @returns A user's session if validated and found, else `null`.
|
|
19
19
|
*/
|
|
20
|
-
abstract deserialize(token: string, globalCfg:
|
|
20
|
+
abstract deserialize(token: string, globalCfg: ConfigOptions): Promise<Session | null>;
|
|
21
21
|
/**
|
|
22
22
|
* Handles how a session is destroyed when a user is logging out.
|
|
23
23
|
* @param req The request object.
|
|
24
24
|
* @param globalCfg The global auth config.
|
|
25
25
|
*/
|
|
26
|
-
abstract logOut(req: Request, globalCfg:
|
|
26
|
+
abstract logOut(req: Request, globalCfg: ConfigOptions): Promise<void>;
|
|
27
27
|
}
|
|
28
28
|
export { IStrategy };
|
|
29
29
|
//# sourceMappingURL=IStrategy.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"IStrategy.d.ts","sourceRoot":"","sources":["../../src/strategies/IStrategy.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"IStrategy.d.ts","sourceRoot":"","sources":["../../src/strategies/IStrategy.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,OAAO,EAAE,MAAM,mBAAmB,CAAC;AACjD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAEzD;;GAEG;AACH,uBAAe,SAAS;IACpB;;;;;OAKG;aACa,SAAS,CAAC,OAAO,EAAE,OAAO,EAAE,SAAS,EAAE,aAAa,GAAG,OAAO,CAAC,MAAM,CAAC;IAEtF;;;;;OAKG;aACa,WAAW,CAAC,KAAK,EAAE,MAAM,EAAE,SAAS,EAAE,aAAa,GAAG,OAAO,CAAC,OAAO,GAAG,IAAI,CAAC;IAE7F;;;;OAIG;aACa,MAAM,CAAC,GAAG,EAAE,OAAO,EAAE,SAAS,EAAE,aAAa,GAAG,OAAO,CAAC,IAAI,CAAC;CAChF;AAED,OAAO,EAAE,SAAS,EAAE,CAAC"}
|
package/dist/strategies/jwt.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { IStrategy } from "./IStrategy";
|
|
2
|
-
import type { __internal__Options } from "../lib";
|
|
3
2
|
import type { SignOptions } from "jsonwebtoken";
|
|
4
3
|
import type { Session } from "../@types/globals";
|
|
4
|
+
import type { ConfigOptions } from "../@types/internals";
|
|
5
5
|
/**
|
|
6
6
|
* Basic JWT strategy
|
|
7
7
|
*/
|
|
@@ -11,8 +11,8 @@ declare class JWT extends IStrategy {
|
|
|
11
11
|
*/
|
|
12
12
|
private signOptions;
|
|
13
13
|
constructor(options?: SignOptions);
|
|
14
|
-
serialize(session: Session, globalCfg:
|
|
15
|
-
deserialize(token: string, globalCfg:
|
|
14
|
+
serialize(session: Session, globalCfg: ConfigOptions): Promise<string>;
|
|
15
|
+
deserialize(token: string, globalCfg: ConfigOptions): Promise<Session | null>;
|
|
16
16
|
logOut(): Promise<void>;
|
|
17
17
|
}
|
|
18
18
|
export { JWT };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"jwt.d.ts","sourceRoot":"","sources":["../../src/strategies/jwt.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"jwt.d.ts","sourceRoot":"","sources":["../../src/strategies/jwt.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAExC,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAChD,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAC;AACjD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAgBzD;;GAEG;AACH,cAAM,GAAI,SAAQ,SAAS;IACvB;;OAEG;IACH,OAAO,CAAC,WAAW,CAAc;gBAErB,OAAO,GAAE,WAAiC;IAMtC,SAAS,CAAC,OAAO,EAAE,OAAO,EAAE,SAAS,EAAE,aAAa,GAAG,OAAO,CAAC,MAAM,CAAC;IAKtE,WAAW,CAAC,KAAK,EAAE,MAAM,EAAE,SAAS,EAAE,aAAa,GAAG,OAAO,CAAC,OAAO,GAAG,IAAI,CAAC;IAK7E,MAAM,IAAI,OAAO,CAAC,IAAI,CAAC;CAI1C;AAED,OAAO,EAAE,GAAG,EAAE,CAAC"}
|
package/dist/strategies/jwt.js
CHANGED
|
@@ -1,5 +1,18 @@
|
|
|
1
|
+
import { isString } from "lodash-es";
|
|
1
2
|
import { IStrategy } from "./IStrategy";
|
|
2
3
|
import { verify, sign } from "../functions/jwt";
|
|
4
|
+
/**
|
|
5
|
+
* Retrieves either the secret or a private key, depending on the used JWT algorithm
|
|
6
|
+
* @param secret Secret key, or key pair
|
|
7
|
+
* @returns The secret or private key
|
|
8
|
+
*/
|
|
9
|
+
const secretOrPrivateKey = (secret) => isString(secret) ? secret : secret.privateKey;
|
|
10
|
+
/**
|
|
11
|
+
* Retrieves either the secret or a public key, depending on the used JWT algorithm
|
|
12
|
+
* @param secret Secret key, or key pair
|
|
13
|
+
* @returns The secret or public key
|
|
14
|
+
*/
|
|
15
|
+
const secretOrPublicKey = (secret) => isString(secret) ? secret : secret.publicKey;
|
|
3
16
|
/**
|
|
4
17
|
* Basic JWT strategy
|
|
5
18
|
*/
|
|
@@ -14,11 +27,11 @@ class JWT extends IStrategy {
|
|
|
14
27
|
}
|
|
15
28
|
serialize(session, globalCfg) {
|
|
16
29
|
// Directly call the sign function, but make it async.
|
|
17
|
-
return Promise.resolve(sign(session, globalCfg.secret, this.signOptions));
|
|
30
|
+
return Promise.resolve(sign(session, secretOrPrivateKey(globalCfg.secret), this.signOptions));
|
|
18
31
|
}
|
|
19
32
|
deserialize(token, globalCfg) {
|
|
20
33
|
// The verify function does everything for us, in this case.
|
|
21
|
-
return verify(token, globalCfg.secret);
|
|
34
|
+
return verify(token, secretOrPublicKey(globalCfg.secret));
|
|
22
35
|
}
|
|
23
36
|
logOut() {
|
|
24
37
|
// Since a JWT does not have any data in a DB, there is nothing to do here.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "orange-auth",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Simple modular auth library",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -8,8 +8,19 @@
|
|
|
8
8
|
"files": [
|
|
9
9
|
"dist"
|
|
10
10
|
],
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"require": "./dist/index.js",
|
|
14
|
+
"default": "./dist/index.js",
|
|
15
|
+
"types": "./dist/index.d.ts"
|
|
16
|
+
},
|
|
17
|
+
"./strategies": "./dist/strategies/index.js",
|
|
18
|
+
"./providers": "./dist/providers/index.js",
|
|
19
|
+
"./functions": "./dist/functions/index.js"
|
|
20
|
+
},
|
|
11
21
|
"scripts": {
|
|
12
|
-
"build": "tsc"
|
|
22
|
+
"build": "tsc",
|
|
23
|
+
"test": "vitest"
|
|
13
24
|
},
|
|
14
25
|
"author": "Mathieu Dery mathieu.dery@bananastreaming.ca",
|
|
15
26
|
"license": "MIT",
|
|
@@ -23,8 +34,10 @@
|
|
|
23
34
|
"@types/node": "^24.0.15",
|
|
24
35
|
"eslint-plugin-prettier": "^5.5.3",
|
|
25
36
|
"tslib": "^2.8.1",
|
|
37
|
+
"type-fest": "^4.41.0",
|
|
26
38
|
"typescript": "^5.8.3",
|
|
27
|
-
"typescript-eslint": "^8.37.0"
|
|
39
|
+
"typescript-eslint": "^8.37.0",
|
|
40
|
+
"vitest": "^3.2.4"
|
|
28
41
|
},
|
|
29
42
|
"dependencies": {
|
|
30
43
|
"@universal-middleware/core": "^0.4.8",
|