authored 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +3 -0
- package/index.d.ts +60 -0
- package/index.js +216 -0
- package/jsconfig.json +27 -0
- package/package.json +36 -0
- package/types/index.d.ts +60 -0
- package/types/types.d.ts +6 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Mark Joseph Yalung
|
|
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
package/index.d.ts
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { Client } from "pg";
|
|
2
|
+
export declare const $$: {
|
|
3
|
+
p: any;
|
|
4
|
+
textD: TextDecoder;
|
|
5
|
+
};
|
|
6
|
+
export declare const O: {
|
|
7
|
+
vals: {
|
|
8
|
+
<T>(o: {
|
|
9
|
+
[s: string]: T;
|
|
10
|
+
} | ArrayLike<T>): T[];
|
|
11
|
+
(o: {}): any[];
|
|
12
|
+
};
|
|
13
|
+
keys: {
|
|
14
|
+
(o: object): string[];
|
|
15
|
+
(o: {}): string[];
|
|
16
|
+
};
|
|
17
|
+
items: {
|
|
18
|
+
<T>(o: {
|
|
19
|
+
[s: string]: T;
|
|
20
|
+
} | ArrayLike<T>): [string, T][];
|
|
21
|
+
(o: {}): [string, any][];
|
|
22
|
+
};
|
|
23
|
+
has: (o: object, v: PropertyKey) => boolean;
|
|
24
|
+
define: <T>(o: T, p: PropertyKey, attributes: PropertyDescriptor & ThisType<any>) => T;
|
|
25
|
+
ass: {
|
|
26
|
+
<T extends {}, U>(target: T, source: U): T & U;
|
|
27
|
+
<T extends {}, U, V>(target: T, source1: U, source2: V): T & U & V;
|
|
28
|
+
<T extends {}, U, V, W>(target: T, source1: U, source2: V, source3: W): T & U & V & W;
|
|
29
|
+
(target: object, ...sources: any[]): any;
|
|
30
|
+
};
|
|
31
|
+
length: (ob: Object) => number;
|
|
32
|
+
};
|
|
33
|
+
type authConfig = {
|
|
34
|
+
COOKIE_NAME: string;
|
|
35
|
+
COOKIE_DOMAIN: string;
|
|
36
|
+
COOKIE_PATH: string;
|
|
37
|
+
COOKIE_HTTPONLY: boolean;
|
|
38
|
+
COOKIE_SECURE: boolean;
|
|
39
|
+
REFRESH_EACH_REQUEST: boolean;
|
|
40
|
+
COOKIE_SAMESITE: string;
|
|
41
|
+
KEY_PREFIX: string;
|
|
42
|
+
PERMANENT: boolean;
|
|
43
|
+
USE_SIGNER: boolean;
|
|
44
|
+
ID_LENGTH: number;
|
|
45
|
+
FILE_THRESHOLD: number;
|
|
46
|
+
LIFETIME: number;
|
|
47
|
+
MAX_COOKIE_SIZE: number;
|
|
48
|
+
INTERFACE: dbs;
|
|
49
|
+
STORAGE: string;
|
|
50
|
+
JWT_STORAGE: string;
|
|
51
|
+
JWT_LIFETIME: number;
|
|
52
|
+
};
|
|
53
|
+
type dbs = "fs" | "postgres";
|
|
54
|
+
export declare class Auth {
|
|
55
|
+
postgresClient?: Client;
|
|
56
|
+
config: authConfig;
|
|
57
|
+
constructor(type: dbs);
|
|
58
|
+
initStorage(path?: string): void;
|
|
59
|
+
}
|
|
60
|
+
export {};
|
package/index.js
ADDED
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
import { randomBytes, createCipheriv, createDecipheriv } from "node:crypto";
|
|
2
|
+
import { CryptoHasher } from "bun";
|
|
3
|
+
export const $$ = {
|
|
4
|
+
set p(a) {
|
|
5
|
+
if (Array.isArray(a)) {
|
|
6
|
+
console.log(...a);
|
|
7
|
+
}
|
|
8
|
+
else {
|
|
9
|
+
console.log(a);
|
|
10
|
+
}
|
|
11
|
+
},
|
|
12
|
+
textD: new TextDecoder(),
|
|
13
|
+
};
|
|
14
|
+
export const O = {
|
|
15
|
+
vals: Object.values,
|
|
16
|
+
keys: Object.keys,
|
|
17
|
+
items: Object.entries,
|
|
18
|
+
has: Object.hasOwn,
|
|
19
|
+
define: Object.defineProperty,
|
|
20
|
+
ass: Object.assign,
|
|
21
|
+
length: (ob) => {
|
|
22
|
+
return Object.keys(ob).length;
|
|
23
|
+
},
|
|
24
|
+
};
|
|
25
|
+
export class Auth {
|
|
26
|
+
postgresClient;
|
|
27
|
+
config = {
|
|
28
|
+
COOKIE_NAME: "session",
|
|
29
|
+
COOKIE_DOMAIN: "127.0.0.1",
|
|
30
|
+
COOKIE_PATH: "/",
|
|
31
|
+
COOKIE_HTTPONLY: true,
|
|
32
|
+
COOKIE_SECURE: true,
|
|
33
|
+
REFRESH_EACH_REQUEST: false,
|
|
34
|
+
COOKIE_SAMESITE: "Strict",
|
|
35
|
+
KEY_PREFIX: "session:",
|
|
36
|
+
PERMANENT: true,
|
|
37
|
+
USE_SIGNER: false,
|
|
38
|
+
ID_LENGTH: 32,
|
|
39
|
+
FILE_THRESHOLD: 500,
|
|
40
|
+
LIFETIME: 31,
|
|
41
|
+
MAX_COOKIE_SIZE: 4093,
|
|
42
|
+
INTERFACE: "fs",
|
|
43
|
+
STORAGE: ".sessions",
|
|
44
|
+
JWT_STORAGE: ".jwt",
|
|
45
|
+
JWT_LIFETIME: 5,
|
|
46
|
+
};
|
|
47
|
+
constructor(type) {
|
|
48
|
+
this.config.INTERFACE = type;
|
|
49
|
+
}
|
|
50
|
+
initStorage(path = "./") {
|
|
51
|
+
this.config.STORAGE = path + this.config.STORAGE;
|
|
52
|
+
this.config.JWT_STORAGE = path + this.config.JWT_STORAGE;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
class callBack {
|
|
56
|
+
data;
|
|
57
|
+
modified;
|
|
58
|
+
new = true;
|
|
59
|
+
length = 0;
|
|
60
|
+
constructor(initial = {}) {
|
|
61
|
+
this.modified = true;
|
|
62
|
+
this.data = {};
|
|
63
|
+
this.length = O.length(initial);
|
|
64
|
+
if (this.length) {
|
|
65
|
+
this.new = false;
|
|
66
|
+
}
|
|
67
|
+
O.ass(this.data, initial);
|
|
68
|
+
}
|
|
69
|
+
set(target, prop, val) {
|
|
70
|
+
if (!this.readonly && target.data[prop] != val) {
|
|
71
|
+
this.modified = true;
|
|
72
|
+
target.data[prop] = val;
|
|
73
|
+
if (!(prop in target.data)) {
|
|
74
|
+
this.length++;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
return target;
|
|
78
|
+
}
|
|
79
|
+
get(target, prop) {
|
|
80
|
+
if (prop in target) {
|
|
81
|
+
return target[prop];
|
|
82
|
+
}
|
|
83
|
+
return target.data[prop];
|
|
84
|
+
}
|
|
85
|
+
has(target, prop) {
|
|
86
|
+
if (prop in target.data) {
|
|
87
|
+
return true;
|
|
88
|
+
}
|
|
89
|
+
return false;
|
|
90
|
+
}
|
|
91
|
+
deleteProperty(target, val) {
|
|
92
|
+
if (!this.readonly && val in target.data) {
|
|
93
|
+
this.modified = true;
|
|
94
|
+
delete target.data[val];
|
|
95
|
+
}
|
|
96
|
+
return true;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
class ServerSide extends callBack {
|
|
100
|
+
modified;
|
|
101
|
+
sid;
|
|
102
|
+
readonly;
|
|
103
|
+
constructor(sid = "", initial = {}, readonly = false) {
|
|
104
|
+
super(initial);
|
|
105
|
+
this.modified = false;
|
|
106
|
+
this.sid = sid;
|
|
107
|
+
this.readonly = readonly;
|
|
108
|
+
}
|
|
109
|
+
get session() {
|
|
110
|
+
return new Proxy(this, this);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
function hmacDigest(salt) {
|
|
114
|
+
const hmac = new Bun.CryptoHasher("sha256", salt);
|
|
115
|
+
return hmac.digest();
|
|
116
|
+
}
|
|
117
|
+
function str2Buffer(str) {
|
|
118
|
+
return Buffer.from(str);
|
|
119
|
+
}
|
|
120
|
+
function decode(str) {
|
|
121
|
+
return $$.textD.decode(str);
|
|
122
|
+
}
|
|
123
|
+
class Signator {
|
|
124
|
+
salt;
|
|
125
|
+
constructor(salt) {
|
|
126
|
+
this.salt = salt;
|
|
127
|
+
}
|
|
128
|
+
getSignature(val) {
|
|
129
|
+
const vals = str2Buffer(val);
|
|
130
|
+
return hmacDigest(this.salt).toString("base64");
|
|
131
|
+
}
|
|
132
|
+
deriveKey() {
|
|
133
|
+
return hmacDigest(this.salt);
|
|
134
|
+
}
|
|
135
|
+
sign(val) {
|
|
136
|
+
const sig = this.getSignature(val);
|
|
137
|
+
const vals = str2Buffer(val + "." + sig);
|
|
138
|
+
return decode(vals);
|
|
139
|
+
}
|
|
140
|
+
unsign(signedVal) {
|
|
141
|
+
if (!(signedVal.indexOf(".") > -1)) {
|
|
142
|
+
throw Error("No sep found");
|
|
143
|
+
}
|
|
144
|
+
const isept = signedVal.indexOf(".");
|
|
145
|
+
const val = signedVal.slice(0, isept);
|
|
146
|
+
const sig = signedVal.slice(isept + 1);
|
|
147
|
+
return this.verifySignature(val, sig);
|
|
148
|
+
}
|
|
149
|
+
loadUnsign(vals) {
|
|
150
|
+
if (this.unsign(vals)) {
|
|
151
|
+
const sval = str2Buffer(vals);
|
|
152
|
+
const sept = str2Buffer(".").toString()[0];
|
|
153
|
+
if (!(sept in sval)) {
|
|
154
|
+
throw Error("No sep found");
|
|
155
|
+
}
|
|
156
|
+
const isept = sval.indexOf(sept);
|
|
157
|
+
const val = sval.subarray(0, isept);
|
|
158
|
+
return Buffer.from(val.toString(), "base64").toString("utf-8");
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
verifySignature(val, sig) {
|
|
162
|
+
return this.getSignature(val) == sig ? true : false;
|
|
163
|
+
}
|
|
164
|
+
generate(len = 21) {
|
|
165
|
+
const rbyte = randomBytes(len);
|
|
166
|
+
let lbyte = rbyte.toString("base64");
|
|
167
|
+
if (lbyte.endsWith("=")) {
|
|
168
|
+
lbyte = lbyte.slice(0, -1);
|
|
169
|
+
}
|
|
170
|
+
return this.sign(lbyte);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
class AuthInterface extends Signator {
|
|
174
|
+
constructor(salt) {
|
|
175
|
+
super(salt ?? "salty");
|
|
176
|
+
}
|
|
177
|
+
async openSession(sid) {
|
|
178
|
+
if (this.unsign(sid))
|
|
179
|
+
return await this.fetchSession(sid);
|
|
180
|
+
return this.new;
|
|
181
|
+
}
|
|
182
|
+
async fetchSession(sid) {
|
|
183
|
+
return this.new;
|
|
184
|
+
}
|
|
185
|
+
async saveSession() {
|
|
186
|
+
return;
|
|
187
|
+
}
|
|
188
|
+
get new() {
|
|
189
|
+
return new ServerSide(this.generate(), {}).session;
|
|
190
|
+
}
|
|
191
|
+
get readonly() {
|
|
192
|
+
return new ServerSide(this.generate(), {}, true).session;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
const sk = "helloworld";
|
|
196
|
+
const CH = new CryptoHasher("sha256");
|
|
197
|
+
function dSecret() {
|
|
198
|
+
return CH.copy().update(sk).digest();
|
|
199
|
+
}
|
|
200
|
+
function encrypt(text) {
|
|
201
|
+
const secretKey = dSecret();
|
|
202
|
+
const iv = randomBytes(16);
|
|
203
|
+
const cipher = createCipheriv("aes-256-cbc", secretKey, iv);
|
|
204
|
+
let encrypted = cipher.update(text, "utf8", "hex");
|
|
205
|
+
encrypted += cipher.final("hex");
|
|
206
|
+
return `${iv.toString("hex")}:${encrypted}`;
|
|
207
|
+
}
|
|
208
|
+
function decrypt(encryptedData) {
|
|
209
|
+
const secretKey = dSecret();
|
|
210
|
+
const [ivHex, encryptedText] = encryptedData.split(":");
|
|
211
|
+
const iv = Buffer.from(ivHex, "hex");
|
|
212
|
+
const decipher = createDecipheriv("aes-256-cbc", secretKey, iv);
|
|
213
|
+
let decrypted = decipher.update(encryptedText, "hex", "utf8");
|
|
214
|
+
decrypted += decipher.final("utf8");
|
|
215
|
+
return decrypted;
|
|
216
|
+
}
|
package/jsconfig.json
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
// Enable latest features
|
|
4
|
+
"lib": ["ESNext", "DOM"],
|
|
5
|
+
"target": "ESNext",
|
|
6
|
+
"module": "ESNext",
|
|
7
|
+
"moduleDetection": "force",
|
|
8
|
+
"jsx": "react-jsx",
|
|
9
|
+
"allowJs": true,
|
|
10
|
+
|
|
11
|
+
// Bundler mode
|
|
12
|
+
"moduleResolution": "bundler",
|
|
13
|
+
"allowImportingTsExtensions": true,
|
|
14
|
+
"verbatimModuleSyntax": true,
|
|
15
|
+
"noEmit": true,
|
|
16
|
+
|
|
17
|
+
// Best practices
|
|
18
|
+
"strict": true,
|
|
19
|
+
"skipLibCheck": true,
|
|
20
|
+
"noFallthroughCasesInSwitch": true,
|
|
21
|
+
|
|
22
|
+
// Some stricter flags (disabled by default)
|
|
23
|
+
"noUnusedLocals": false,
|
|
24
|
+
"noUnusedParameters": false,
|
|
25
|
+
"noPropertyAccessFromIndexSignature": false
|
|
26
|
+
}
|
|
27
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "authored",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "authored",
|
|
5
|
+
"author": "Marky <markjotep@gmail.com>",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"types": "./types/index.d.ts",
|
|
8
|
+
"main": "index.js",
|
|
9
|
+
"keywords": [
|
|
10
|
+
"ts",
|
|
11
|
+
"bun"
|
|
12
|
+
],
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "git+https://github.com/MARKjotep/authored.git"
|
|
16
|
+
},
|
|
17
|
+
"bugs": {
|
|
18
|
+
"url": "https://github.com/MARKjotep/authored/issues"
|
|
19
|
+
},
|
|
20
|
+
"homepage": "https://github.com/MARKjotep/authored#readme",
|
|
21
|
+
"scripts": {
|
|
22
|
+
"build": "tsc ; dist=../../DIST/authored/ ; cp -r package.json $dist ; cp -r ./types/types.d.ts $dist/types; bun install --cwd $dist; "
|
|
23
|
+
},
|
|
24
|
+
"dependencies": {
|
|
25
|
+
"jsonwebtoken": "^9.0.2",
|
|
26
|
+
"pg": "^8.13.1"
|
|
27
|
+
},
|
|
28
|
+
"devDependencies": {
|
|
29
|
+
"@types/jsonwebtoken": "^9.0.7",
|
|
30
|
+
"@types/pg": "^8.11.10",
|
|
31
|
+
"@types/bun": "latest"
|
|
32
|
+
},
|
|
33
|
+
"peerDependencies": {
|
|
34
|
+
"typescript": "^5.0.0"
|
|
35
|
+
}
|
|
36
|
+
}
|
package/types/index.d.ts
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { Client } from "pg";
|
|
2
|
+
export declare const $$: {
|
|
3
|
+
p: any;
|
|
4
|
+
textD: TextDecoder;
|
|
5
|
+
};
|
|
6
|
+
export declare const O: {
|
|
7
|
+
vals: {
|
|
8
|
+
<T>(o: {
|
|
9
|
+
[s: string]: T;
|
|
10
|
+
} | ArrayLike<T>): T[];
|
|
11
|
+
(o: {}): any[];
|
|
12
|
+
};
|
|
13
|
+
keys: {
|
|
14
|
+
(o: object): string[];
|
|
15
|
+
(o: {}): string[];
|
|
16
|
+
};
|
|
17
|
+
items: {
|
|
18
|
+
<T>(o: {
|
|
19
|
+
[s: string]: T;
|
|
20
|
+
} | ArrayLike<T>): [string, T][];
|
|
21
|
+
(o: {}): [string, any][];
|
|
22
|
+
};
|
|
23
|
+
has: (o: object, v: PropertyKey) => boolean;
|
|
24
|
+
define: <T>(o: T, p: PropertyKey, attributes: PropertyDescriptor & ThisType<any>) => T;
|
|
25
|
+
ass: {
|
|
26
|
+
<T extends {}, U>(target: T, source: U): T & U;
|
|
27
|
+
<T extends {}, U, V>(target: T, source1: U, source2: V): T & U & V;
|
|
28
|
+
<T extends {}, U, V, W>(target: T, source1: U, source2: V, source3: W): T & U & V & W;
|
|
29
|
+
(target: object, ...sources: any[]): any;
|
|
30
|
+
};
|
|
31
|
+
length: (ob: Object) => number;
|
|
32
|
+
};
|
|
33
|
+
type authConfig = {
|
|
34
|
+
COOKIE_NAME: string;
|
|
35
|
+
COOKIE_DOMAIN: string;
|
|
36
|
+
COOKIE_PATH: string;
|
|
37
|
+
COOKIE_HTTPONLY: boolean;
|
|
38
|
+
COOKIE_SECURE: boolean;
|
|
39
|
+
REFRESH_EACH_REQUEST: boolean;
|
|
40
|
+
COOKIE_SAMESITE: string;
|
|
41
|
+
KEY_PREFIX: string;
|
|
42
|
+
PERMANENT: boolean;
|
|
43
|
+
USE_SIGNER: boolean;
|
|
44
|
+
ID_LENGTH: number;
|
|
45
|
+
FILE_THRESHOLD: number;
|
|
46
|
+
LIFETIME: number;
|
|
47
|
+
MAX_COOKIE_SIZE: number;
|
|
48
|
+
INTERFACE: dbs;
|
|
49
|
+
STORAGE: string;
|
|
50
|
+
JWT_STORAGE: string;
|
|
51
|
+
JWT_LIFETIME: number;
|
|
52
|
+
};
|
|
53
|
+
type dbs = "fs" | "postgres";
|
|
54
|
+
export declare class Auth {
|
|
55
|
+
postgresClient?: Client;
|
|
56
|
+
config: authConfig;
|
|
57
|
+
constructor(type: dbs);
|
|
58
|
+
initStorage(path?: string): void;
|
|
59
|
+
}
|
|
60
|
+
export {};
|