orange-auth 0.0.0 → 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/.prettierignore +153 -0
- package/.prettierrc +8 -0
- package/LICENSE +21 -0
- package/README.md +118 -0
- package/dist/@types/globals.d.ts +7 -0
- package/dist/@types/globals.d.ts.map +1 -0
- package/dist/@types/globals.js +1 -0
- package/dist/functions/jwt.d.ts +4 -0
- package/dist/functions/jwt.d.ts.map +1 -0
- package/dist/functions/jwt.js +11 -0
- package/dist/lib.d.ts +18 -0
- package/dist/lib.d.ts.map +1 -0
- package/dist/lib.js +65 -0
- package/dist/providers/Credentials.d.ts +15 -0
- package/dist/providers/Credentials.d.ts.map +1 -0
- package/dist/providers/Credentials.js +24 -0
- package/dist/providers/IProvider.d.ts +12 -0
- package/dist/providers/IProvider.d.ts.map +1 -0
- package/dist/providers/IProvider.js +10 -0
- package/dist/strategies/IStrategy.d.ts +9 -0
- package/dist/strategies/IStrategy.d.ts.map +1 -0
- package/dist/strategies/IStrategy.js +4 -0
- package/dist/strategies/jwt.d.ts +13 -0
- package/dist/strategies/jwt.d.ts.map +1 -0
- package/dist/strategies/jwt.js +19 -0
- package/eslint.config.d.ts +3 -0
- package/eslint.config.d.ts.map +1 -0
- package/eslint.config.js +99 -0
- package/package.json +31 -9
- package/tsconfig.json +46 -0
- package/lib.js +0 -3
package/.prettierignore
ADDED
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
# Logs
|
|
2
|
+
logs
|
|
3
|
+
*.log
|
|
4
|
+
npm-debug.log*
|
|
5
|
+
yarn-debug.log*
|
|
6
|
+
yarn-error.log*
|
|
7
|
+
lerna-debug.log*
|
|
8
|
+
.pnpm-debug.log*
|
|
9
|
+
|
|
10
|
+
# Diagnostic reports (https://nodejs.org/api/report.html)
|
|
11
|
+
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
|
|
12
|
+
|
|
13
|
+
# Runtime data
|
|
14
|
+
pids
|
|
15
|
+
*.pid
|
|
16
|
+
*.seed
|
|
17
|
+
*.pid.lock
|
|
18
|
+
|
|
19
|
+
# Directory for instrumented libs generated by jscoverage/JSCover
|
|
20
|
+
lib-cov
|
|
21
|
+
|
|
22
|
+
# Coverage directory used by tools like istanbul
|
|
23
|
+
coverage
|
|
24
|
+
*.lcov
|
|
25
|
+
|
|
26
|
+
# nyc test coverage
|
|
27
|
+
.nyc_output
|
|
28
|
+
|
|
29
|
+
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
|
|
30
|
+
.grunt
|
|
31
|
+
|
|
32
|
+
# Bower dependency directory (https://bower.io/)
|
|
33
|
+
bower_components
|
|
34
|
+
|
|
35
|
+
# node-waf configuration
|
|
36
|
+
.lock-wscript
|
|
37
|
+
|
|
38
|
+
# Compiled binary addons (https://nodejs.org/api/addons.html)
|
|
39
|
+
build/Release
|
|
40
|
+
|
|
41
|
+
# Dependency directories
|
|
42
|
+
node_modules/
|
|
43
|
+
jspm_packages/
|
|
44
|
+
|
|
45
|
+
# Snowpack dependency directory (https://snowpack.dev/)
|
|
46
|
+
web_modules/
|
|
47
|
+
|
|
48
|
+
# TypeScript cache
|
|
49
|
+
*.tsbuildinfo
|
|
50
|
+
|
|
51
|
+
# Optional npm cache directory
|
|
52
|
+
.npm
|
|
53
|
+
|
|
54
|
+
# Optional eslint cache
|
|
55
|
+
.eslintcache
|
|
56
|
+
|
|
57
|
+
# Optional stylelint cache
|
|
58
|
+
.stylelintcache
|
|
59
|
+
|
|
60
|
+
# Microbundle cache
|
|
61
|
+
.rpt2_cache/
|
|
62
|
+
.rts2_cache_cjs/
|
|
63
|
+
.rts2_cache_es/
|
|
64
|
+
.rts2_cache_umd/
|
|
65
|
+
|
|
66
|
+
# Optional REPL history
|
|
67
|
+
.node_repl_history
|
|
68
|
+
|
|
69
|
+
# Output of 'npm pack'
|
|
70
|
+
*.tgz
|
|
71
|
+
|
|
72
|
+
# Yarn Integrity file
|
|
73
|
+
.yarn-integrity
|
|
74
|
+
|
|
75
|
+
# dotenv environment variable files
|
|
76
|
+
.env
|
|
77
|
+
.env.development.local
|
|
78
|
+
.env.test.local
|
|
79
|
+
.env.production.local
|
|
80
|
+
.env.local
|
|
81
|
+
|
|
82
|
+
# parcel-bundler cache (https://parceljs.org/)
|
|
83
|
+
.cache
|
|
84
|
+
.parcel-cache
|
|
85
|
+
|
|
86
|
+
# firebase-admin service-account
|
|
87
|
+
firebase
|
|
88
|
+
|
|
89
|
+
# Next.js build output
|
|
90
|
+
.next
|
|
91
|
+
out
|
|
92
|
+
|
|
93
|
+
# Nuxt.js build / generate output
|
|
94
|
+
.nuxt
|
|
95
|
+
dist
|
|
96
|
+
|
|
97
|
+
# Gatsby files
|
|
98
|
+
.cache/
|
|
99
|
+
# Comment in the public line in if your project uses Gatsby and not Next.js
|
|
100
|
+
# https://nextjs.org/blog/next-9-1#public-directory-support
|
|
101
|
+
# public
|
|
102
|
+
|
|
103
|
+
# vuepress build output
|
|
104
|
+
.vuepress/dist
|
|
105
|
+
|
|
106
|
+
# vuepress v2.x temp and cache directory
|
|
107
|
+
.temp
|
|
108
|
+
|
|
109
|
+
# Docusaurus cache and generated files
|
|
110
|
+
.docusaurus
|
|
111
|
+
|
|
112
|
+
# Serverless directories
|
|
113
|
+
.serverless/
|
|
114
|
+
|
|
115
|
+
# FuseBox cache
|
|
116
|
+
.fusebox/
|
|
117
|
+
|
|
118
|
+
# DynamoDB Local files
|
|
119
|
+
.dynamodb/
|
|
120
|
+
|
|
121
|
+
# TernJS port file
|
|
122
|
+
.tern-port
|
|
123
|
+
|
|
124
|
+
# Stores VSCode versions used for testing VSCode extensions
|
|
125
|
+
.vscode-test
|
|
126
|
+
|
|
127
|
+
# yarn v2
|
|
128
|
+
.yarn/cache
|
|
129
|
+
.yarn/unplugged
|
|
130
|
+
.yarn/build-state.yml
|
|
131
|
+
.yarn/install-state.gz
|
|
132
|
+
.pnp.*
|
|
133
|
+
|
|
134
|
+
# Cloudflare
|
|
135
|
+
.wrangler/
|
|
136
|
+
|
|
137
|
+
# Vercel
|
|
138
|
+
.vercel/
|
|
139
|
+
|
|
140
|
+
# Sentry Vite Plugin
|
|
141
|
+
.env.sentry-build-plugin
|
|
142
|
+
|
|
143
|
+
# aws-cdk
|
|
144
|
+
.cdk.staging
|
|
145
|
+
cdk.out
|
|
146
|
+
|
|
147
|
+
## Panda
|
|
148
|
+
styled-system
|
|
149
|
+
styled-system-studio
|
|
150
|
+
|
|
151
|
+
# Shadcn
|
|
152
|
+
lib/components/ui/
|
|
153
|
+
server/dbClient/
|
package/.prettierrc
ADDED
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) Mathieu Dery.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE
|
package/README.md
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
# 🍊 Orange Auth
|
|
2
|
+
|
|
3
|
+
### THIS IS A VERY EARLY WIP, AND SHOULD NOT BE USED
|
|
4
|
+
|
|
5
|
+
A lightweight authentication handler built for [@universal-middleware/core](https://www.npmjs.com/package/@universal-middleware/core), with support for plug-and-play providers and strategies. This package manages login/logout and session deserialization through HTTP handlers and secure cookies.
|
|
6
|
+
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
## ✨ Features
|
|
10
|
+
|
|
11
|
+
- Provider-based authentication (e.g., Credentials, OAuth)
|
|
12
|
+
- Strategy-based token serialization and deserialization (e.g., JWT)
|
|
13
|
+
- Secure, HTTP-only cookie session management
|
|
14
|
+
- Framework-agnostic and middleware-compatible
|
|
15
|
+
- Written in TypeScript
|
|
16
|
+
|
|
17
|
+
---
|
|
18
|
+
|
|
19
|
+
## 📦 Installation
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
npm install universal-auth
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
📁 Project Structure
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
src/
|
|
29
|
+
├── @types/ # Custom type definitions (e.g., Session)
|
|
30
|
+
├── functions/ # Utility functions
|
|
31
|
+
├── providers/ # Implementations of IProvider
|
|
32
|
+
├── strategies/ # Implementations of IStrategy
|
|
33
|
+
├── lib.ts # Main exports
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
## 🚀 Usage
|
|
37
|
+
|
|
38
|
+
### 1. Define your auth configuration:
|
|
39
|
+
|
|
40
|
+
```ts
|
|
41
|
+
import { CreateAuth } from "universal-auth";
|
|
42
|
+
import { JwtStrategy } from "./strategies/jwt";
|
|
43
|
+
import { CredentialsProvider } from "./providers/Credentials";
|
|
44
|
+
|
|
45
|
+
const handler = CreateAuth({
|
|
46
|
+
providers: [CredentialsProvider],
|
|
47
|
+
secret: "your-secret-key",
|
|
48
|
+
cookieName: "my-auth-cookie", // optional (default: "orange.auth")
|
|
49
|
+
strategy: JwtStrategy,
|
|
50
|
+
basePath: "/api/auth",
|
|
51
|
+
});
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
This will expose two routes:
|
|
55
|
+
|
|
56
|
+
`GET /api/auth/login/:provider`
|
|
57
|
+
|
|
58
|
+
`GET /api/auth/logout/:provider`
|
|
59
|
+
|
|
60
|
+
You must implement a matching `provider.ID`, e.g. `"credentials"`.
|
|
61
|
+
|
|
62
|
+
### 2. Use in a universal middleware router:`
|
|
63
|
+
|
|
64
|
+
```ts
|
|
65
|
+
import { router } from "@universal-middleware/core";
|
|
66
|
+
import { handler as authHandler } from "./path-to-your-auth";
|
|
67
|
+
|
|
68
|
+
export const app = router();
|
|
69
|
+
app.use(authHandler);
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
### 3. Getting the current session:
|
|
73
|
+
|
|
74
|
+
```ts
|
|
75
|
+
import { getSession } from "universal-auth";
|
|
76
|
+
|
|
77
|
+
const session = await getSession(req);
|
|
78
|
+
if (session) {
|
|
79
|
+
console.log("Logged in as", session.user);
|
|
80
|
+
}
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
## 🧩 Interfaces
|
|
84
|
+
|
|
85
|
+
`IProvider`
|
|
86
|
+
|
|
87
|
+
Defines how to log in a user and return a token:
|
|
88
|
+
|
|
89
|
+
```ts
|
|
90
|
+
interface IProvider {
|
|
91
|
+
ID: string;
|
|
92
|
+
logIn(req: Request, config: ConfigOptions): Promise<string>;
|
|
93
|
+
}
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
`IStrategy`
|
|
97
|
+
|
|
98
|
+
Defines how to serialize and deserialize tokens:
|
|
99
|
+
|
|
100
|
+
```ts
|
|
101
|
+
interface IStrategy {
|
|
102
|
+
deserialize(token: string, config: ConfigOptions): Promise<Session | null>;
|
|
103
|
+
logOut(req: Request, config: ConfigOptions): Promise<void>;
|
|
104
|
+
}
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
## 🔐 Security
|
|
108
|
+
Cookies are set with:
|
|
109
|
+
|
|
110
|
+
`HttpOnly: true`
|
|
111
|
+
|
|
112
|
+
`SameSite: "Lax"`
|
|
113
|
+
|
|
114
|
+
`Secure: true`
|
|
115
|
+
|
|
116
|
+
`Max-Age: 600` (10 minutes)
|
|
117
|
+
|
|
118
|
+
You may customize these by modifying the `CreateAuth` implementation.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"globals.d.ts","sourceRoot":"","sources":["../../src/@types/globals.ts"],"names":[],"mappings":"AAAA,OAAO,CAAC,MAAM,CAAC;IACX,KAAK,YAAY,CAAC,CAAC,IAAI,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC;CACzC;AAED,MAAM,WAAW,OAAQ,SAAQ,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;IACpD,EAAE,EAAE,MAAM,CAAC;CACd"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { type JwtPayload, type PublicKey, type Secret, type VerifyOptions } from "jsonwebtoken";
|
|
2
|
+
export { sign } from "jsonwebtoken";
|
|
3
|
+
export declare function verify<T extends JwtPayload = JwtPayload>(token: string, secretOrPublicKey: Secret | PublicKey, options?: VerifyOptions): Promise<T | null>;
|
|
4
|
+
//# sourceMappingURL=jwt.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"jwt.d.ts","sourceRoot":"","sources":["../../src/functions/jwt.ts"],"names":[],"mappings":"AAAA,OAAO,EAEH,KAAK,UAAU,EACf,KAAK,SAAS,EACd,KAAK,MAAM,EACX,KAAK,aAAa,EACrB,MAAM,cAAc,CAAC;AAEtB,OAAO,EAAE,IAAI,EAAE,MAAM,cAAc,CAAC;AAEpC,wBAAgB,MAAM,CAAC,CAAC,SAAS,UAAU,GAAG,UAAU,EACpD,KAAK,EAAE,MAAM,EACb,iBAAiB,EAAE,MAAM,GAAG,SAAS,EACrC,OAAO,CAAC,EAAE,aAAa,qBAQ1B"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { verify as baseVerify, } from "jsonwebtoken";
|
|
2
|
+
export { sign } from "jsonwebtoken";
|
|
3
|
+
export function verify(token, secretOrPublicKey, options) {
|
|
4
|
+
return new Promise((resolve) => {
|
|
5
|
+
baseVerify(token, secretOrPublicKey, { ...options, complete: false }, (err, payload) => {
|
|
6
|
+
if (err)
|
|
7
|
+
resolve(null);
|
|
8
|
+
resolve(payload);
|
|
9
|
+
});
|
|
10
|
+
});
|
|
11
|
+
}
|
package/dist/lib.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { Session } from "./@types/globals";
|
|
2
|
+
import type { IStrategy } from "./strategies/IStrategy";
|
|
3
|
+
import type { IProvider } from "./providers/IProvider";
|
|
4
|
+
type Maybe<T> = T | null | undefined;
|
|
5
|
+
export type ConfigOptionsProps = Readonly<{
|
|
6
|
+
providers: IProvider[];
|
|
7
|
+
secret: string;
|
|
8
|
+
cookieName?: string;
|
|
9
|
+
strategy: IStrategy;
|
|
10
|
+
basePath: string;
|
|
11
|
+
}>;
|
|
12
|
+
export type ConfigOptions = Required<Omit<ConfigOptionsProps, "basePath">>;
|
|
13
|
+
export declare const CreateAuth: (config: ConfigOptionsProps) => (req: Request, _: Universal.Context, runtime: import("@universal-middleware/core").RuntimeAdapter) => Promise<Response>;
|
|
14
|
+
export declare const getSession: <T extends Session = Session>(req: {
|
|
15
|
+
headers: Maybe<Headers | Record<string, string>>;
|
|
16
|
+
}) => Promise<T | null> | null;
|
|
17
|
+
export {};
|
|
18
|
+
//# sourceMappingURL=lib.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"lib.d.ts","sourceRoot":"","sources":["../src/lib.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,kBAAkB,CAAC;AAChD,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,wBAAwB,CAAC;AACxD,OAAO,KAAK,EAAW,SAAS,EAAE,MAAM,uBAAuB,CAAC;AAGhE,KAAK,KAAK,CAAC,CAAC,IAAI,CAAC,GAAG,IAAI,GAAG,SAAS,CAAC;AAErC,MAAM,MAAM,kBAAkB,GAAG,QAAQ,CAAC;IACtC,SAAS,EAAE,SAAS,EAAE,CAAC;IACvB,MAAM,EAAE,MAAM,CAAC;IACf,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,QAAQ,EAAE,SAAS,CAAC;IACpB,QAAQ,EAAE,MAAM,CAAC;CACpB,CAAC,CAAC;AAEH,MAAM,MAAM,aAAa,GAAG,QAAQ,CAAC,IAAI,CAAC,kBAAkB,EAAE,UAAU,CAAC,CAAC,CAAC;AAI3E,eAAO,MAAM,UAAU,WAAa,kBAAkB,4HAgEU,CAAC;AAEjE,eAAO,MAAM,UAAU,GAAI,CAAC,SAAS,OAAO,GAAG,OAAO,EAAE,KAAK;IAAE,OAAO,EAAE,KAAK,CAAC,OAAO,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAAA;CAAE,6BAYhH,CAAC"}
|
package/dist/lib.js
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import Cookies from "universal-cookie";
|
|
2
|
+
import { find, isNil } from "lodash-es";
|
|
3
|
+
import { serialize as cookie } from "cookie";
|
|
4
|
+
import { params } from "@universal-middleware/core";
|
|
5
|
+
let globalCfg;
|
|
6
|
+
export const CreateAuth = ((config) => async (req, _, runtime) => {
|
|
7
|
+
const { secret, strategy, cookieName, providers, basePath } = config;
|
|
8
|
+
const routeParams = params(req, runtime, basePath);
|
|
9
|
+
if (isNil(routeParams?.["action"]) || isNil(routeParams["provider"])) {
|
|
10
|
+
throw new Error('[ERROR]: Base path is missing! Make sure to set the "basePath" variable in the auth\'s config.');
|
|
11
|
+
}
|
|
12
|
+
if (isNil(secret)) {
|
|
13
|
+
throw new Error('[ERROR]: Auth secret missing! Make sure to set the "secret" variable in the auth\'s config.');
|
|
14
|
+
}
|
|
15
|
+
if (isNil(strategy)) {
|
|
16
|
+
throw new Error('[ERROR]: No strategy chosen! Make sure to set the "strategy" variable in the auth\'s config.');
|
|
17
|
+
}
|
|
18
|
+
globalCfg = {
|
|
19
|
+
cookieName: cookieName ?? "orange.auth",
|
|
20
|
+
providers: providers ?? [],
|
|
21
|
+
secret,
|
|
22
|
+
strategy,
|
|
23
|
+
};
|
|
24
|
+
const path = routeParams["provider"];
|
|
25
|
+
const provider = find(providers, (p) => p.ID === path);
|
|
26
|
+
if (isNil(provider)) {
|
|
27
|
+
return new Response("Page not found", { status: 404 });
|
|
28
|
+
}
|
|
29
|
+
switch (routeParams["action"]) {
|
|
30
|
+
case "login": {
|
|
31
|
+
const token = await provider.logIn(req, globalCfg).catch(() => null);
|
|
32
|
+
if (isNil(token))
|
|
33
|
+
return new Response(null, { status: 400 });
|
|
34
|
+
const headers = new Headers();
|
|
35
|
+
headers.set("Set-Cookie", cookie(globalCfg.cookieName, token, {
|
|
36
|
+
path: "/",
|
|
37
|
+
httpOnly: true,
|
|
38
|
+
sameSite: "lax",
|
|
39
|
+
secure: true,
|
|
40
|
+
maxAge: 600,
|
|
41
|
+
}));
|
|
42
|
+
return new Response(null, { status: 200, headers });
|
|
43
|
+
}
|
|
44
|
+
case "logout": {
|
|
45
|
+
await globalCfg.strategy.logOut(req, globalCfg);
|
|
46
|
+
const headers = new Headers();
|
|
47
|
+
headers.set("Set-Cookie", cookie(globalCfg.cookieName, ""));
|
|
48
|
+
return new Response(null, { status: 200, headers });
|
|
49
|
+
}
|
|
50
|
+
default:
|
|
51
|
+
return new Response("Page not found", { status: 404 });
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
export const getSession = (req) => {
|
|
55
|
+
if (isNil(req.headers))
|
|
56
|
+
return null;
|
|
57
|
+
const cookieHeader = req.headers instanceof Headers ? req.headers.get("cookie") : req.headers["cookie"];
|
|
58
|
+
const cookie = new Cookies(cookieHeader);
|
|
59
|
+
if (isNil(cookie))
|
|
60
|
+
return null;
|
|
61
|
+
const token = cookie.get(globalCfg.cookieName);
|
|
62
|
+
if (isNil(token))
|
|
63
|
+
return null;
|
|
64
|
+
return globalCfg.strategy.deserialize(token, globalCfg);
|
|
65
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { IProvider } from "./IProvider";
|
|
2
|
+
import type { ConfigOptions } from "../lib";
|
|
3
|
+
import type { Session } from "../@types/globals";
|
|
4
|
+
export type CredentialsConfig<TCredentials extends string> = Readonly<{
|
|
5
|
+
name?: "credentials" | (string & {});
|
|
6
|
+
credentials: TCredentials[];
|
|
7
|
+
authorize: (credentials: Record<TCredentials, string>) => MaybePromise<Session | null>;
|
|
8
|
+
}>;
|
|
9
|
+
export declare class Credentials<TCredentials extends string = string> extends IProvider {
|
|
10
|
+
private config;
|
|
11
|
+
constructor(config: CredentialsConfig<TCredentials>);
|
|
12
|
+
getSession(req: Request, globalCfg: ConfigOptions): Promise<Session | null>;
|
|
13
|
+
logIn(req: Request, globalCfg: ConfigOptions): Promise<string | null>;
|
|
14
|
+
}
|
|
15
|
+
//# sourceMappingURL=Credentials.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Credentials.d.ts","sourceRoot":"","sources":["../../src/providers/Credentials.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AACxC,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,QAAQ,CAAC;AAC5C,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAC;AAEjD,MAAM,MAAM,iBAAiB,CAAC,YAAY,SAAS,MAAM,IAAI,QAAQ,CAAC;IAClE,IAAI,CAAC,EAAE,aAAa,GAAG,CAAC,MAAM,GAAG,EAAE,CAAC,CAAC;IACrC,WAAW,EAAE,YAAY,EAAE,CAAC;IAC5B,SAAS,EAAE,CAAC,WAAW,EAAE,MAAM,CAAC,YAAY,EAAE,MAAM,CAAC,KAAK,YAAY,CAAC,OAAO,GAAG,IAAI,CAAC,CAAC;CAC1F,CAAC,CAAC;AAEH,qBAAa,WAAW,CAAC,YAAY,SAAS,MAAM,GAAG,MAAM,CAAE,SAAQ,SAAS;IAC5E,OAAO,CAAC,MAAM,CAAkC;gBAEpC,MAAM,EAAE,iBAAiB,CAAC,YAAY,CAAC;IAK7B,UAAU,CAAC,GAAG,EAAE,OAAO,EAAE,SAAS,EAAE,aAAa,GAAG,OAAO,CAAC,OAAO,GAAG,IAAI,CAAC;IAS3E,KAAK,CAAC,GAAG,EAAE,OAAO,EAAE,SAAS,EAAE,aAAa,GAAG,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC;CAQ9F"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { isNil } from "lodash-es";
|
|
2
|
+
import Cookies from "universal-cookie";
|
|
3
|
+
import { IProvider } from "./IProvider";
|
|
4
|
+
export class Credentials extends IProvider {
|
|
5
|
+
config;
|
|
6
|
+
constructor(config) {
|
|
7
|
+
super("credentials");
|
|
8
|
+
this.config = config;
|
|
9
|
+
}
|
|
10
|
+
async getSession(req, globalCfg) {
|
|
11
|
+
const cookies = new Cookies(req.headers.get("cookie"));
|
|
12
|
+
const token = cookies.get(globalCfg.cookieName);
|
|
13
|
+
if (token == null)
|
|
14
|
+
return null;
|
|
15
|
+
return globalCfg.strategy.deserialize(token, globalCfg);
|
|
16
|
+
}
|
|
17
|
+
async logIn(req, globalCfg) {
|
|
18
|
+
const body = (await req.json());
|
|
19
|
+
const session = await this.config.authorize(body);
|
|
20
|
+
if (isNil(session))
|
|
21
|
+
return null;
|
|
22
|
+
return globalCfg.strategy.serialize(session, globalCfg);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { ConfigOptions } from "../lib";
|
|
2
|
+
import type { Session } from "../@types/globals";
|
|
3
|
+
export type Actions = "login" | "logout";
|
|
4
|
+
declare abstract class IProvider {
|
|
5
|
+
private __ID;
|
|
6
|
+
constructor(ID: string);
|
|
7
|
+
get ID(): string;
|
|
8
|
+
abstract getSession(req: Request, globalCfg: ConfigOptions): Promise<Session | null>;
|
|
9
|
+
abstract logIn(req: Request, globalCfg: ConfigOptions): Promise<string | null>;
|
|
10
|
+
}
|
|
11
|
+
export { IProvider };
|
|
12
|
+
//# sourceMappingURL=IProvider.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"IProvider.d.ts","sourceRoot":"","sources":["../../src/providers/IProvider.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,QAAQ,CAAC;AAC5C,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAC;AAEjD,MAAM,MAAM,OAAO,GAAG,OAAO,GAAG,QAAQ,CAAC;AAEzC,uBAAe,SAAS;IACpB,OAAO,CAAC,IAAI,CAAS;gBAET,EAAE,EAAE,MAAM;IAItB,IAAW,EAAE,IAAI,MAAM,CAEtB;aAEe,UAAU,CAAC,GAAG,EAAE,OAAO,EAAE,SAAS,EAAE,aAAa,GAAG,OAAO,CAAC,OAAO,GAAG,IAAI,CAAC;aAC3E,KAAK,CAAC,GAAG,EAAE,OAAO,EAAE,SAAS,EAAE,aAAa,GAAG,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC;CACxF;AAED,OAAO,EAAE,SAAS,EAAE,CAAC"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { ConfigOptions } from "../lib";
|
|
2
|
+
import { type Session } from "../@types/globals";
|
|
3
|
+
declare abstract class IStrategy {
|
|
4
|
+
abstract serialize(session: Session, globalCfg: ConfigOptions): Promise<string>;
|
|
5
|
+
abstract deserialize(token: string, globalCfg: ConfigOptions): Promise<Session | null>;
|
|
6
|
+
abstract logOut(req: Request, globalCfg: ConfigOptions): Promise<void>;
|
|
7
|
+
}
|
|
8
|
+
export { IStrategy };
|
|
9
|
+
//# sourceMappingURL=IStrategy.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"IStrategy.d.ts","sourceRoot":"","sources":["../../src/strategies/IStrategy.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,QAAQ,CAAC;AAC5C,OAAO,EAAE,KAAK,OAAO,EAAE,MAAM,mBAAmB,CAAC;AAEjD,uBAAe,SAAS;aACJ,SAAS,CAAC,OAAO,EAAE,OAAO,EAAE,SAAS,EAAE,aAAa,GAAG,OAAO,CAAC,MAAM,CAAC;aACtE,WAAW,CAAC,KAAK,EAAE,MAAM,EAAE,SAAS,EAAE,aAAa,GAAG,OAAO,CAAC,OAAO,GAAG,IAAI,CAAC;aAC7E,MAAM,CAAC,GAAG,EAAE,OAAO,EAAE,SAAS,EAAE,aAAa,GAAG,OAAO,CAAC,IAAI,CAAC;CAChF;AAED,OAAO,EAAE,SAAS,EAAE,CAAC"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { IStrategy } from "./IStrategy";
|
|
2
|
+
import type { ConfigOptions } from "../lib";
|
|
3
|
+
import type { SignOptions } from "jsonwebtoken";
|
|
4
|
+
import type { Session } from "../@types/globals";
|
|
5
|
+
declare class JWT extends IStrategy {
|
|
6
|
+
private signOptions;
|
|
7
|
+
constructor(options?: SignOptions);
|
|
8
|
+
serialize(session: Session, globalCfg: ConfigOptions): Promise<string>;
|
|
9
|
+
deserialize(token: string, globalCfg: ConfigOptions): Promise<Session | null>;
|
|
10
|
+
logOut(): Promise<void>;
|
|
11
|
+
}
|
|
12
|
+
export { JWT };
|
|
13
|
+
//# sourceMappingURL=jwt.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"jwt.d.ts","sourceRoot":"","sources":["../../src/strategies/jwt.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AACxC,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,QAAQ,CAAC;AAE5C,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAChD,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAC;AAEjD,cAAM,GAAI,SAAQ,SAAS;IACvB,OAAO,CAAC,WAAW,CAAc;gBAErB,OAAO,GAAE,WAAiC;IAMtC,SAAS,CAAC,OAAO,EAAE,OAAO,EAAE,SAAS,EAAE,aAAa,GAAG,OAAO,CAAC,MAAM,CAAC;IAItE,WAAW,CAAC,KAAK,EAAE,MAAM,EAAE,SAAS,EAAE,aAAa,GAAG,OAAO,CAAC,OAAO,GAAG,IAAI,CAAC;IAI7E,MAAM,IAAI,OAAO,CAAC,IAAI,CAAC;CAG1C;AAED,OAAO,EAAE,GAAG,EAAE,CAAC"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { IStrategy } from "./IStrategy";
|
|
2
|
+
import { verify, sign } from "../functions/jwt";
|
|
3
|
+
class JWT extends IStrategy {
|
|
4
|
+
signOptions;
|
|
5
|
+
constructor(options = { expiresIn: "1h" }) {
|
|
6
|
+
super();
|
|
7
|
+
this.signOptions = options;
|
|
8
|
+
}
|
|
9
|
+
serialize(session, globalCfg) {
|
|
10
|
+
return Promise.resolve(sign(session, globalCfg.secret, this.signOptions));
|
|
11
|
+
}
|
|
12
|
+
deserialize(token, globalCfg) {
|
|
13
|
+
return verify(token, globalCfg.secret);
|
|
14
|
+
}
|
|
15
|
+
logOut() {
|
|
16
|
+
return Promise.resolve();
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
export { JWT };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"eslint.config.d.ts","sourceRoot":"","sources":["eslint.config.ts"],"names":[],"mappings":";AAIA,wBAyGE"}
|
package/eslint.config.js
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import eslint from "@eslint/js";
|
|
2
|
+
import tseslint from "typescript-eslint";
|
|
3
|
+
import prettier from "eslint-plugin-prettier/recommended";
|
|
4
|
+
export default tseslint.config({
|
|
5
|
+
ignores: [
|
|
6
|
+
"dist/*",
|
|
7
|
+
"**/*.ts.build-*.mjs",
|
|
8
|
+
"*.js",
|
|
9
|
+
"*.cjs",
|
|
10
|
+
"*.mjs",
|
|
11
|
+
],
|
|
12
|
+
}, eslint.configs.recommended, ...tseslint.configs.recommended, {
|
|
13
|
+
languageOptions: {
|
|
14
|
+
parserOptions: {
|
|
15
|
+
warnOnUnsupportedTypeScriptVersion: false,
|
|
16
|
+
sourceType: "module",
|
|
17
|
+
ecmaVersion: "latest",
|
|
18
|
+
},
|
|
19
|
+
},
|
|
20
|
+
}, {
|
|
21
|
+
rules: {
|
|
22
|
+
"arrow-spacing": [
|
|
23
|
+
"warn",
|
|
24
|
+
{
|
|
25
|
+
before: true,
|
|
26
|
+
after: true,
|
|
27
|
+
},
|
|
28
|
+
],
|
|
29
|
+
"brace-style": [
|
|
30
|
+
"error",
|
|
31
|
+
"stroustrup",
|
|
32
|
+
{
|
|
33
|
+
allowSingleLine: true,
|
|
34
|
+
},
|
|
35
|
+
],
|
|
36
|
+
"comma-dangle": ["error", "always-multiline"],
|
|
37
|
+
"comma-spacing": "error",
|
|
38
|
+
"comma-style": "error",
|
|
39
|
+
curly: ["error", "multi-line", "consistent"],
|
|
40
|
+
"dot-location": ["error", "property"],
|
|
41
|
+
"handle-callback-err": "off",
|
|
42
|
+
indent: ["error", 4],
|
|
43
|
+
"keyword-spacing": "error",
|
|
44
|
+
"max-nested-callbacks": [
|
|
45
|
+
"error",
|
|
46
|
+
{
|
|
47
|
+
max: 4,
|
|
48
|
+
},
|
|
49
|
+
],
|
|
50
|
+
"max-statements-per-line": [
|
|
51
|
+
"error",
|
|
52
|
+
{
|
|
53
|
+
max: 2,
|
|
54
|
+
},
|
|
55
|
+
],
|
|
56
|
+
"no-console": "off",
|
|
57
|
+
"no-empty-function": "error",
|
|
58
|
+
"no-floating-decimal": "error",
|
|
59
|
+
"no-inline-comments": "error",
|
|
60
|
+
"no-lonely-if": "error",
|
|
61
|
+
"no-multi-spaces": "error",
|
|
62
|
+
"no-multiple-empty-lines": [
|
|
63
|
+
"error",
|
|
64
|
+
{
|
|
65
|
+
max: 2,
|
|
66
|
+
maxEOF: 1,
|
|
67
|
+
maxBOF: 0,
|
|
68
|
+
},
|
|
69
|
+
],
|
|
70
|
+
"no-shadow": [
|
|
71
|
+
"error",
|
|
72
|
+
{
|
|
73
|
+
allow: ["err", "resolve", "reject"],
|
|
74
|
+
},
|
|
75
|
+
],
|
|
76
|
+
"no-trailing-spaces": ["error"],
|
|
77
|
+
"no-var": "error",
|
|
78
|
+
"object-curly-spacing": ["error", "always"],
|
|
79
|
+
"prefer-const": "error",
|
|
80
|
+
quotes: ["error", "double"],
|
|
81
|
+
semi: ["error", "always"],
|
|
82
|
+
"space-before-blocks": "error",
|
|
83
|
+
"space-before-function-paren": [
|
|
84
|
+
"error",
|
|
85
|
+
{
|
|
86
|
+
anonymous: "never",
|
|
87
|
+
named: "never",
|
|
88
|
+
asyncArrow: "always",
|
|
89
|
+
},
|
|
90
|
+
],
|
|
91
|
+
"space-in-parens": "error",
|
|
92
|
+
"space-infix-ops": "error",
|
|
93
|
+
"space-unary-ops": "error",
|
|
94
|
+
"spaced-comment": "error",
|
|
95
|
+
yoda: "error",
|
|
96
|
+
"@typescript-eslint/no-misused-promises": "off",
|
|
97
|
+
"@typescript-eslint/no-unused-vars": "warn",
|
|
98
|
+
},
|
|
99
|
+
}, prettier);
|
package/package.json
CHANGED
|
@@ -1,11 +1,33 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
2
|
+
"name": "orange-auth",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "Simple modular auth library",
|
|
6
|
+
"main": "dist/lib.js",
|
|
7
|
+
"types": "dist/lib.d.ts",
|
|
8
|
+
"scripts": {
|
|
9
|
+
"build": "tsc"
|
|
10
|
+
},
|
|
11
|
+
"author": "Mathieu Dery mathieu.dery@bananastreaming.ca",
|
|
12
|
+
"license": "MIT",
|
|
13
|
+
"repository": {
|
|
14
|
+
"url": "https://github.com/Unimat45/OrangeAuth.git"
|
|
15
|
+
},
|
|
16
|
+
"devDependencies": {
|
|
17
|
+
"@eslint/js": "^9.31.0",
|
|
18
|
+
"@types/jsonwebtoken": "^9.0.10",
|
|
19
|
+
"@types/lodash-es": "^4.17.12",
|
|
20
|
+
"@types/node": "^24.0.15",
|
|
21
|
+
"eslint-plugin-prettier": "^5.5.3",
|
|
22
|
+
"tslib": "^2.8.1",
|
|
23
|
+
"typescript": "^5.8.3",
|
|
24
|
+
"typescript-eslint": "^8.37.0"
|
|
25
|
+
},
|
|
26
|
+
"dependencies": {
|
|
27
|
+
"@universal-middleware/core": "^0.4.8",
|
|
28
|
+
"cookie": "^1.0.2",
|
|
29
|
+
"jsonwebtoken": "^9.0.2",
|
|
30
|
+
"lodash-es": "^4.17.21",
|
|
31
|
+
"universal-cookie": "^8.0.1"
|
|
32
|
+
}
|
|
11
33
|
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"incremental": true,
|
|
4
|
+
|
|
5
|
+
"target": "es2022",
|
|
6
|
+
"lib": ["dom", "dom.iterable", "ES2022"],
|
|
7
|
+
|
|
8
|
+
"module": "es2022",
|
|
9
|
+
"moduleResolution": "bundler",
|
|
10
|
+
"resolveJsonModule": true,
|
|
11
|
+
|
|
12
|
+
"checkJs": true,
|
|
13
|
+
|
|
14
|
+
"declaration": true,
|
|
15
|
+
"declarationMap": true,
|
|
16
|
+
"outDir": "dist",
|
|
17
|
+
"rootDir": "src",
|
|
18
|
+
"removeComments": true,
|
|
19
|
+
"importHelpers": true,
|
|
20
|
+
"newLine": "lf",
|
|
21
|
+
|
|
22
|
+
"verbatimModuleSyntax": true,
|
|
23
|
+
"erasableSyntaxOnly": true,
|
|
24
|
+
"esModuleInterop": true,
|
|
25
|
+
"forceConsistentCasingInFileNames": true,
|
|
26
|
+
|
|
27
|
+
"strict": true,
|
|
28
|
+
"noImplicitAny": true,
|
|
29
|
+
"strictNullChecks": true,
|
|
30
|
+
"strictFunctionTypes": true,
|
|
31
|
+
"strictPropertyInitialization": true,
|
|
32
|
+
"noImplicitThis": true,
|
|
33
|
+
"useUnknownInCatchVariables": true,
|
|
34
|
+
"alwaysStrict": true,
|
|
35
|
+
"noUnusedLocals": true,
|
|
36
|
+
"noUnusedParameters": true,
|
|
37
|
+
"noImplicitReturns": true,
|
|
38
|
+
"noFallthroughCasesInSwitch": true,
|
|
39
|
+
"noUncheckedIndexedAccess": true,
|
|
40
|
+
"noImplicitOverride": true,
|
|
41
|
+
"noPropertyAccessFromIndexSignature": true,
|
|
42
|
+
|
|
43
|
+
"skipLibCheck": true
|
|
44
|
+
},
|
|
45
|
+
"include": ["src/**/*.ts", "eslint.config.ts"]
|
|
46
|
+
}
|
package/lib.js
DELETED