@stacksjs/security 0.37.3
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.md +21 -0
- package/README.md +59 -0
- package/dist/crypt.d.ts +3 -0
- package/dist/crypt.mjs +10 -0
- package/dist/hash.d.ts +8 -0
- package/dist/hash.mjs +38 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.mjs +3 -0
- package/dist/key.d.ts +1 -0
- package/dist/key.mjs +9 -0
- package/package.json +55 -0
- package/src/crypt.ts +14 -0
- package/src/hash.ts +52 -0
- package/src/index.ts +3 -0
- package/src/key.ts +11 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 Open Web Foundation
|
|
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,59 @@
|
|
|
1
|
+
# Stacks Router
|
|
2
|
+
|
|
3
|
+
This package contains the Stacks Security.
|
|
4
|
+
|
|
5
|
+
## โ๏ธ Features
|
|
6
|
+
|
|
7
|
+
wip
|
|
8
|
+
|
|
9
|
+
- โก๏ธ
|
|
10
|
+
|
|
11
|
+
wip
|
|
12
|
+
|
|
13
|
+
## ๐ค Usage
|
|
14
|
+
|
|
15
|
+
wip
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
pnpm i -D @stacksjs/security
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
Now, you can use it in your project:
|
|
22
|
+
|
|
23
|
+
```js
|
|
24
|
+
import * as security from '@stacksjs/security'
|
|
25
|
+
|
|
26
|
+
// wip
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
Learn more in the docs.
|
|
30
|
+
|
|
31
|
+
## ๐งช Testing
|
|
32
|
+
|
|
33
|
+
```bash
|
|
34
|
+
pnpm test
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## ๐ Changelog
|
|
38
|
+
|
|
39
|
+
Please see our [releases](https://github.com/stacksjs/stacksjs/releases) page for more information on what has changed recently.
|
|
40
|
+
|
|
41
|
+
## ๐ช๐ผ Contributing
|
|
42
|
+
|
|
43
|
+
Please see [CONTRIBUTING](../../.github/CONTRIBUTING.md) for details.
|
|
44
|
+
|
|
45
|
+
## ๐ Community
|
|
46
|
+
|
|
47
|
+
For help, discussion about best practices, or any other conversation that would benefit from being searchable:
|
|
48
|
+
|
|
49
|
+
[Discussions on GitHub](https://github.com/stacksjs/stacks/discussions)
|
|
50
|
+
|
|
51
|
+
For casual chit-chat with others using this package:
|
|
52
|
+
|
|
53
|
+
[Join the Open Web Discord Server](https://discord.ow3.org)
|
|
54
|
+
|
|
55
|
+
## ๐ License
|
|
56
|
+
|
|
57
|
+
The MIT License (MIT). Please see [LICENSE](https://github.com/stacksjs/stacks/tree/main/LICENSE.md) for more information.
|
|
58
|
+
|
|
59
|
+
Made with โค๏ธ
|
package/dist/crypt.d.ts
ADDED
package/dist/crypt.mjs
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import aes from "crypto-js/aes";
|
|
2
|
+
import utf8 from "crypto-js/enc-utf8";
|
|
3
|
+
const passphrase = import.meta.env.APP_KEY;
|
|
4
|
+
function encrypt(message) {
|
|
5
|
+
return aes.encrypt(message, passphrase).toString();
|
|
6
|
+
}
|
|
7
|
+
function decrypt(encrypted) {
|
|
8
|
+
return aes.decrypt(encrypted, passphrase).toString(utf8);
|
|
9
|
+
}
|
|
10
|
+
export { encrypt, decrypt };
|
package/dist/hash.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
declare function make(password: string, algorithm?: string): Promise<any>;
|
|
2
|
+
declare function verify(password: string, hash: string, algorithm?: string): Promise<any>;
|
|
3
|
+
declare function bcryptEncode(password: string): Promise<any>;
|
|
4
|
+
declare function bcryptVerify(password: string, hash: string): Promise<any>;
|
|
5
|
+
declare function base64Encode(password: string): Promise<string>;
|
|
6
|
+
declare function base64Verify(password: string, hash: string): Promise<boolean>;
|
|
7
|
+
declare function md5Encode(password: string): Promise<any>;
|
|
8
|
+
export { make as makeHash, verify as verifyHash, base64Encode, base64Verify, bcryptEncode, bcryptVerify, md5Encode };
|
package/dist/hash.mjs
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import bcryptjs from "bcryptjs";
|
|
2
|
+
import { Base64 } from "js-base64";
|
|
3
|
+
import md5 from "crypto-js/md5";
|
|
4
|
+
import { hashing } from "../../../config";
|
|
5
|
+
async function make(password, algorithm = "bcrypt") {
|
|
6
|
+
if (algorithm === "bcrypt")
|
|
7
|
+
return bcryptEncode(password);
|
|
8
|
+
if (algorithm === "base64")
|
|
9
|
+
return base64Encode(password);
|
|
10
|
+
throw new Error("Unsupported algorithm");
|
|
11
|
+
}
|
|
12
|
+
async function verify(password, hash, algorithm = "bcrypt") {
|
|
13
|
+
if (algorithm === "bcrypt")
|
|
14
|
+
return bcryptVerify(password, hash);
|
|
15
|
+
if (algorithm === "base64")
|
|
16
|
+
return base64Verify(password, hash);
|
|
17
|
+
throw new Error("Unsupported algorithm");
|
|
18
|
+
}
|
|
19
|
+
async function bcryptEncode(password) {
|
|
20
|
+
if (!hashing.bcrypt)
|
|
21
|
+
throw new Error("Bcrypt hashing is not configured");
|
|
22
|
+
const salt = await bcryptjs.genSaltSync(hashing.bcrypt.rounds);
|
|
23
|
+
const hash = await bcryptjs.hash(password, salt);
|
|
24
|
+
return hash;
|
|
25
|
+
}
|
|
26
|
+
async function bcryptVerify(password, hash) {
|
|
27
|
+
return await bcryptjs.compare(password, hash);
|
|
28
|
+
}
|
|
29
|
+
async function base64Encode(password) {
|
|
30
|
+
return await Base64.encode(password);
|
|
31
|
+
}
|
|
32
|
+
async function base64Verify(password, hash) {
|
|
33
|
+
return Base64.decode(hash) === password;
|
|
34
|
+
}
|
|
35
|
+
async function md5Encode(password) {
|
|
36
|
+
return md5(password);
|
|
37
|
+
}
|
|
38
|
+
export { make as makeHash, verify as verifyHash, base64Encode, base64Verify, bcryptEncode, bcryptVerify, md5Encode };
|
package/dist/index.d.ts
ADDED
package/dist/index.mjs
ADDED
package/dist/key.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function generateAppKey(): Promise<string>;
|
package/dist/key.mjs
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { getRandomValues } from "node:crypto";
|
|
2
|
+
import utf8 from "crypto-js/enc-utf8";
|
|
3
|
+
import base64 from "crypto-js/enc-base64";
|
|
4
|
+
export async function generateAppKey() {
|
|
5
|
+
const random = getRandomValues(new Uint8Array(32));
|
|
6
|
+
const encodedWord = utf8.parse(random.toString());
|
|
7
|
+
const key = base64.stringify(encodedWord);
|
|
8
|
+
return `base64:${key}`;
|
|
9
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@stacksjs/security",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "0.37.3",
|
|
5
|
+
"packageManager": "pnpm@7.13.6",
|
|
6
|
+
"description": "The Stacks framework security.",
|
|
7
|
+
"author": "Chris Breuer",
|
|
8
|
+
"license": "MIT",
|
|
9
|
+
"funding": "https://github.com/sponsors/chrisbbreuer",
|
|
10
|
+
"homepage": "https://github.com/stacksjs/stacks/tree/main/.stacks/core/security#readme",
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "git+https://github.com/stacksjs/stacks.git",
|
|
14
|
+
"directory": "./.stacks/core/security"
|
|
15
|
+
},
|
|
16
|
+
"bugs": {
|
|
17
|
+
"url": "https://github.com/stacksjs/stacks/issues"
|
|
18
|
+
},
|
|
19
|
+
"keywords": [
|
|
20
|
+
"security",
|
|
21
|
+
"framework",
|
|
22
|
+
"typescript",
|
|
23
|
+
"javascript"
|
|
24
|
+
],
|
|
25
|
+
"main": "dist/index.mjs",
|
|
26
|
+
"module": "dist/index.mjs",
|
|
27
|
+
"types": "dist/index.d.ts",
|
|
28
|
+
"contributors": [
|
|
29
|
+
"Chris Breuer <chris@ow3.org>"
|
|
30
|
+
],
|
|
31
|
+
"files": [
|
|
32
|
+
"dist",
|
|
33
|
+
"src",
|
|
34
|
+
"README.md"
|
|
35
|
+
],
|
|
36
|
+
"engines": {
|
|
37
|
+
"node": ">=v18.10.0",
|
|
38
|
+
"pnpm": ">=7.13.6"
|
|
39
|
+
},
|
|
40
|
+
"dependencies": {
|
|
41
|
+
"@types/bcryptjs": "^2.4.2",
|
|
42
|
+
"@types/crypto-js": "^4.1.1",
|
|
43
|
+
"bcryptjs": "^2.4.3",
|
|
44
|
+
"crypto-js": "^4.1.1",
|
|
45
|
+
"js-base64": "^3.7.2"
|
|
46
|
+
},
|
|
47
|
+
"devDependencies": {
|
|
48
|
+
"mkdist": "^0.3.13"
|
|
49
|
+
},
|
|
50
|
+
"scripts": {
|
|
51
|
+
"build": "mkdist -d",
|
|
52
|
+
"dev": "mkdist -d",
|
|
53
|
+
"typecheck": "tsc --noEmit"
|
|
54
|
+
}
|
|
55
|
+
}
|
package/src/crypt.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import aes from 'crypto-js/aes'
|
|
2
|
+
import utf8 from 'crypto-js/enc-utf8'
|
|
3
|
+
|
|
4
|
+
const passphrase = import.meta.env.APP_KEY as string
|
|
5
|
+
|
|
6
|
+
function encrypt(message: string): string {
|
|
7
|
+
return aes.encrypt(message, passphrase).toString()
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
function decrypt(encrypted: string): string {
|
|
11
|
+
return aes.decrypt(encrypted, passphrase).toString(utf8)
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export { encrypt, decrypt }
|
package/src/hash.ts
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import bcryptjs from 'bcryptjs'
|
|
2
|
+
import { Base64 } from 'js-base64'
|
|
3
|
+
import md5 from 'crypto-js/md5'
|
|
4
|
+
import { hashing } from '../../../config'
|
|
5
|
+
|
|
6
|
+
async function make(password: string, algorithm = 'bcrypt') {
|
|
7
|
+
if (algorithm === 'bcrypt')
|
|
8
|
+
return bcryptEncode(password)
|
|
9
|
+
|
|
10
|
+
if (algorithm === 'base64')
|
|
11
|
+
return base64Encode(password)
|
|
12
|
+
|
|
13
|
+
throw new Error('Unsupported algorithm')
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
async function verify(password: string, hash: string, algorithm = 'bcrypt') {
|
|
17
|
+
if (algorithm === 'bcrypt')
|
|
18
|
+
return bcryptVerify(password, hash)
|
|
19
|
+
|
|
20
|
+
if (algorithm === 'base64')
|
|
21
|
+
return base64Verify(password, hash)
|
|
22
|
+
|
|
23
|
+
throw new Error('Unsupported algorithm')
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
async function bcryptEncode(password: string) {
|
|
27
|
+
if (!hashing.bcrypt)
|
|
28
|
+
throw new Error('Bcrypt hashing is not configured')
|
|
29
|
+
|
|
30
|
+
const salt = await bcryptjs.genSaltSync(hashing.bcrypt.rounds)
|
|
31
|
+
const hash = await bcryptjs.hash(password, salt)
|
|
32
|
+
|
|
33
|
+
return hash
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
async function bcryptVerify(password: string, hash: string) {
|
|
37
|
+
return await bcryptjs.compare(password, hash)
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
async function base64Encode(password: string) {
|
|
41
|
+
return await Base64.encode(password)
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
async function base64Verify(password: string, hash: string) {
|
|
45
|
+
return Base64.decode(hash) === password
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
async function md5Encode(password: string) {
|
|
49
|
+
return md5(password)
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export { make as makeHash, verify as verifyHash, base64Encode, base64Verify, bcryptEncode, bcryptVerify, md5Encode }
|
package/src/index.ts
ADDED
package/src/key.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { getRandomValues } from 'node:crypto'
|
|
2
|
+
import utf8 from 'crypto-js/enc-utf8'
|
|
3
|
+
import base64 from 'crypto-js/enc-base64'
|
|
4
|
+
|
|
5
|
+
export async function generateAppKey() {
|
|
6
|
+
const random = getRandomValues(new Uint8Array(32))
|
|
7
|
+
const encodedWord = utf8.parse(random.toString())
|
|
8
|
+
const key = base64.stringify(encodedWord)
|
|
9
|
+
|
|
10
|
+
return `base64:${key}`
|
|
11
|
+
}
|