@typekcz-nocobase-plugins/plugin-oidc-plus 1.0.3 → 1.0.4
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/dist/client/index.js +1 -1
- package/dist/externalVersion.js +9 -9
- package/package.json +1 -1
- package/src/client/OIDCButton.tsx +70 -0
- package/src/client/Options.tsx +359 -0
- package/src/client/index.tsx +19 -0
- package/src/client/locale/index.ts +18 -0
- package/src/constants.ts +7 -0
- package/src/index.ts +2 -0
- package/src/locale/en-US.json +40 -0
- package/src/locale/es-ES.json +25 -0
- package/src/locale/fr-FR.json +21 -0
- package/src/locale/ko_KR.json +28 -0
- package/src/locale/pt-BR.json +21 -0
- package/src/locale/zh-CN.json +28 -0
- package/src/server/__tests__/oidc.test.ts +283 -0
- package/src/server/actions/getAuthUrl.ts +25 -0
- package/src/server/actions/redirect.ts +32 -0
- package/src/server/index.ts +1 -0
- package/src/server/oidc-auth.ts +169 -0
- package/src/server/plugin.ts +63 -0
- package/src/swagger/index.ts +157 -0
- package/dist/node_modules/nanoid/.devcontainer.json +0 -23
- package/dist/node_modules/nanoid/LICENSE +0 -20
- package/dist/node_modules/nanoid/async/index.browser.cjs +0 -69
- package/dist/node_modules/nanoid/async/index.browser.js +0 -69
- package/dist/node_modules/nanoid/async/index.cjs +0 -71
- package/dist/node_modules/nanoid/async/index.d.ts +0 -56
- package/dist/node_modules/nanoid/async/index.js +0 -71
- package/dist/node_modules/nanoid/async/index.native.js +0 -57
- package/dist/node_modules/nanoid/async/package.json +0 -12
- package/dist/node_modules/nanoid/bin/nanoid.cjs +0 -55
- package/dist/node_modules/nanoid/index.browser.cjs +0 -72
- package/dist/node_modules/nanoid/index.browser.js +0 -72
- package/dist/node_modules/nanoid/index.cjs +0 -1
- package/dist/node_modules/nanoid/index.d.cts +0 -91
- package/dist/node_modules/nanoid/index.d.ts +0 -91
- package/dist/node_modules/nanoid/index.js +0 -85
- package/dist/node_modules/nanoid/nanoid.js +0 -1
- package/dist/node_modules/nanoid/non-secure/index.cjs +0 -34
- package/dist/node_modules/nanoid/non-secure/index.d.ts +0 -33
- package/dist/node_modules/nanoid/non-secure/index.js +0 -34
- package/dist/node_modules/nanoid/non-secure/package.json +0 -6
- package/dist/node_modules/nanoid/package.json +0 -1
- package/dist/node_modules/nanoid/url-alphabet/index.cjs +0 -7
- package/dist/node_modules/nanoid/url-alphabet/index.js +0 -7
- package/dist/node_modules/nanoid/url-alphabet/package.json +0 -6
- package/dist/node_modules/openid-client/lib/client.js +0 -1884
- package/dist/node_modules/openid-client/lib/device_flow_handle.js +0 -125
- package/dist/node_modules/openid-client/lib/errors.js +0 -55
- package/dist/node_modules/openid-client/lib/helpers/assert.js +0 -24
- package/dist/node_modules/openid-client/lib/helpers/base64url.js +0 -13
- package/dist/node_modules/openid-client/lib/helpers/client.js +0 -208
- package/dist/node_modules/openid-client/lib/helpers/consts.js +0 -7
- package/dist/node_modules/openid-client/lib/helpers/decode_jwt.js +0 -27
- package/dist/node_modules/openid-client/lib/helpers/deep_clone.js +0 -1
- package/dist/node_modules/openid-client/lib/helpers/defaults.js +0 -27
- package/dist/node_modules/openid-client/lib/helpers/generators.js +0 -14
- package/dist/node_modules/openid-client/lib/helpers/is_key_object.js +0 -4
- package/dist/node_modules/openid-client/lib/helpers/is_plain_object.js +0 -1
- package/dist/node_modules/openid-client/lib/helpers/issuer.js +0 -111
- package/dist/node_modules/openid-client/lib/helpers/keystore.js +0 -298
- package/dist/node_modules/openid-client/lib/helpers/merge.js +0 -24
- package/dist/node_modules/openid-client/lib/helpers/pick.js +0 -9
- package/dist/node_modules/openid-client/lib/helpers/process_response.js +0 -71
- package/dist/node_modules/openid-client/lib/helpers/request.js +0 -200
- package/dist/node_modules/openid-client/lib/helpers/unix_timestamp.js +0 -1
- package/dist/node_modules/openid-client/lib/helpers/weak_cache.js +0 -1
- package/dist/node_modules/openid-client/lib/helpers/webfinger_normalize.js +0 -71
- package/dist/node_modules/openid-client/lib/helpers/www_authenticate_parser.js +0 -14
- package/dist/node_modules/openid-client/lib/index.js +0 -1
- package/dist/node_modules/openid-client/lib/issuer.js +0 -192
- package/dist/node_modules/openid-client/lib/issuer_registry.js +0 -3
- package/dist/node_modules/openid-client/lib/passport_strategy.js +0 -205
- package/dist/node_modules/openid-client/lib/token_set.js +0 -35
- package/dist/node_modules/openid-client/package.json +0 -1
- package/dist/node_modules/openid-client/types/index.d.ts +0 -623
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { Gateway, InstallOptions, Plugin } from '@nocobase/server';
|
|
2
|
+
import { getAuthUrl } from './actions/getAuthUrl';
|
|
3
|
+
import { redirect } from './actions/redirect';
|
|
4
|
+
import { authType } from '../constants';
|
|
5
|
+
import { OIDCAuth } from './oidc-auth';
|
|
6
|
+
import { resolve } from 'path';
|
|
7
|
+
|
|
8
|
+
export class PluginOIDCServer extends Plugin {
|
|
9
|
+
afterAdd() {}
|
|
10
|
+
|
|
11
|
+
beforeLoad() {}
|
|
12
|
+
|
|
13
|
+
async load() {
|
|
14
|
+
this.db.addMigrations({
|
|
15
|
+
namespace: 'auth',
|
|
16
|
+
directory: resolve(__dirname, 'migrations'),
|
|
17
|
+
context: {
|
|
18
|
+
plugin: this,
|
|
19
|
+
},
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
this.app.authManager.registerTypes(authType, {
|
|
23
|
+
auth: OIDCAuth,
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
this.app.resource({
|
|
27
|
+
name: 'oidc',
|
|
28
|
+
actions: {
|
|
29
|
+
getAuthUrl,
|
|
30
|
+
redirect,
|
|
31
|
+
},
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
this.app.acl.allow('oidc', '*', 'public');
|
|
35
|
+
|
|
36
|
+
/* istanbul ignore next -- @preserve */
|
|
37
|
+
Gateway.getInstance().addAppSelectorMiddleware(async (ctx, next) => {
|
|
38
|
+
const { req } = ctx;
|
|
39
|
+
const url = new URL(req.url, `http://${req.headers.host}`);
|
|
40
|
+
const params = url.searchParams;
|
|
41
|
+
const state = params.get('state');
|
|
42
|
+
if (!state) {
|
|
43
|
+
return next();
|
|
44
|
+
}
|
|
45
|
+
const search = new URLSearchParams(state);
|
|
46
|
+
const appName = search.get('app');
|
|
47
|
+
if (appName) {
|
|
48
|
+
ctx.resolvedAppName = appName;
|
|
49
|
+
}
|
|
50
|
+
await next();
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
async install(options?: InstallOptions) {}
|
|
55
|
+
|
|
56
|
+
async afterEnable() {}
|
|
57
|
+
|
|
58
|
+
async afterDisable() {}
|
|
59
|
+
|
|
60
|
+
async remove() {}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export default PluginOIDCServer;
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
const user = {
|
|
2
|
+
type: 'object',
|
|
3
|
+
description: '用户',
|
|
4
|
+
properties: {
|
|
5
|
+
id: {
|
|
6
|
+
type: 'integer',
|
|
7
|
+
description: 'ID',
|
|
8
|
+
},
|
|
9
|
+
nickname: {
|
|
10
|
+
type: 'string',
|
|
11
|
+
description: '昵称',
|
|
12
|
+
},
|
|
13
|
+
email: {
|
|
14
|
+
type: 'string',
|
|
15
|
+
description: '邮箱',
|
|
16
|
+
},
|
|
17
|
+
phone: {
|
|
18
|
+
type: 'string',
|
|
19
|
+
description: '手机号',
|
|
20
|
+
},
|
|
21
|
+
appLang: {
|
|
22
|
+
type: 'string',
|
|
23
|
+
description: '用户使用语言',
|
|
24
|
+
},
|
|
25
|
+
systemSettings: {
|
|
26
|
+
type: 'object',
|
|
27
|
+
description: '系统设置',
|
|
28
|
+
properties: {
|
|
29
|
+
theme: {
|
|
30
|
+
type: 'string',
|
|
31
|
+
description: '用户使用主题',
|
|
32
|
+
},
|
|
33
|
+
},
|
|
34
|
+
},
|
|
35
|
+
createdAt: {
|
|
36
|
+
type: 'string',
|
|
37
|
+
format: 'date-time',
|
|
38
|
+
description: '创建时间',
|
|
39
|
+
},
|
|
40
|
+
updatedAt: {
|
|
41
|
+
type: 'string',
|
|
42
|
+
format: 'date-time',
|
|
43
|
+
description: '更新时间',
|
|
44
|
+
},
|
|
45
|
+
createdById: {
|
|
46
|
+
type: 'integer',
|
|
47
|
+
description: '创建人',
|
|
48
|
+
},
|
|
49
|
+
updatedById: {
|
|
50
|
+
type: 'integer',
|
|
51
|
+
description: '更新人',
|
|
52
|
+
},
|
|
53
|
+
},
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
export default {
|
|
57
|
+
info: {
|
|
58
|
+
title: 'NocoBase API - OIDC plugin',
|
|
59
|
+
},
|
|
60
|
+
paths: {
|
|
61
|
+
'/oidc:getAuthUrl': {
|
|
62
|
+
security: [],
|
|
63
|
+
get: {
|
|
64
|
+
description: 'Get OIDC authorization url',
|
|
65
|
+
tags: ['OIDC'],
|
|
66
|
+
parameters: [
|
|
67
|
+
{
|
|
68
|
+
name: 'X-Authenticator',
|
|
69
|
+
description: '登录方式标识',
|
|
70
|
+
in: 'header',
|
|
71
|
+
schema: {
|
|
72
|
+
type: 'string',
|
|
73
|
+
},
|
|
74
|
+
required: true,
|
|
75
|
+
},
|
|
76
|
+
],
|
|
77
|
+
responses: {
|
|
78
|
+
200: {
|
|
79
|
+
description: 'ok',
|
|
80
|
+
content: {
|
|
81
|
+
'application/json': {
|
|
82
|
+
schema: {
|
|
83
|
+
type: 'string',
|
|
84
|
+
},
|
|
85
|
+
},
|
|
86
|
+
},
|
|
87
|
+
},
|
|
88
|
+
},
|
|
89
|
+
},
|
|
90
|
+
},
|
|
91
|
+
'/auth:signIn': {
|
|
92
|
+
security: [],
|
|
93
|
+
post: {
|
|
94
|
+
description: 'OIDC sign in',
|
|
95
|
+
tags: ['OIDC'],
|
|
96
|
+
parameters: [
|
|
97
|
+
{
|
|
98
|
+
name: 'X-Authenticator',
|
|
99
|
+
description: '登录方式标识',
|
|
100
|
+
in: 'header',
|
|
101
|
+
schema: {
|
|
102
|
+
type: 'string',
|
|
103
|
+
},
|
|
104
|
+
required: true,
|
|
105
|
+
},
|
|
106
|
+
{
|
|
107
|
+
name: 'nocobase_oidc',
|
|
108
|
+
description: 'state校验值',
|
|
109
|
+
in: 'cookie',
|
|
110
|
+
schema: {
|
|
111
|
+
type: 'string',
|
|
112
|
+
},
|
|
113
|
+
required: true,
|
|
114
|
+
},
|
|
115
|
+
],
|
|
116
|
+
requestBody: {
|
|
117
|
+
content: {
|
|
118
|
+
'application/json': {
|
|
119
|
+
schema: {
|
|
120
|
+
type: 'object',
|
|
121
|
+
properties: {
|
|
122
|
+
code: {
|
|
123
|
+
type: 'string',
|
|
124
|
+
},
|
|
125
|
+
state: {
|
|
126
|
+
type: 'string',
|
|
127
|
+
},
|
|
128
|
+
iss: {
|
|
129
|
+
type: 'string',
|
|
130
|
+
},
|
|
131
|
+
},
|
|
132
|
+
},
|
|
133
|
+
},
|
|
134
|
+
},
|
|
135
|
+
},
|
|
136
|
+
responses: {
|
|
137
|
+
200: {
|
|
138
|
+
description: 'ok',
|
|
139
|
+
content: {
|
|
140
|
+
'application/json': {
|
|
141
|
+
schema: {
|
|
142
|
+
type: 'object',
|
|
143
|
+
properties: {
|
|
144
|
+
token: {
|
|
145
|
+
type: 'string',
|
|
146
|
+
},
|
|
147
|
+
user,
|
|
148
|
+
},
|
|
149
|
+
},
|
|
150
|
+
},
|
|
151
|
+
},
|
|
152
|
+
},
|
|
153
|
+
},
|
|
154
|
+
},
|
|
155
|
+
},
|
|
156
|
+
},
|
|
157
|
+
};
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"image": "localhost/ai-opensource:latest",
|
|
3
|
-
"forwardPorts": [],
|
|
4
|
-
"mounts": [
|
|
5
|
-
{
|
|
6
|
-
"source": "pnpm-store",
|
|
7
|
-
"target": "/home/ai/.local/share/pnpm/store",
|
|
8
|
-
"type": "volume"
|
|
9
|
-
},
|
|
10
|
-
{
|
|
11
|
-
"source": "shell-history",
|
|
12
|
-
"target": "/home/ai/.local/share/history/",
|
|
13
|
-
"type": "volume"
|
|
14
|
-
}
|
|
15
|
-
],
|
|
16
|
-
"workspaceMount": "",
|
|
17
|
-
"runArgs": [
|
|
18
|
-
"--userns=keep-id:uid=1000,gid=1000",
|
|
19
|
-
"--volume=${localWorkspaceFolder}:/workspaces/${localWorkspaceFolderBasename}:Z",
|
|
20
|
-
"--network=host",
|
|
21
|
-
"--ulimit=host"
|
|
22
|
-
]
|
|
23
|
-
}
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
The MIT License (MIT)
|
|
2
|
-
|
|
3
|
-
Copyright 2017 Andrey Sitnik <andrey@sitnik.ru>
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
6
|
-
this software and associated documentation files (the "Software"), to deal in
|
|
7
|
-
the Software without restriction, including without limitation the rights to
|
|
8
|
-
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
9
|
-
the Software, and to permit persons to whom the Software is furnished to do so,
|
|
10
|
-
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, FITNESS
|
|
17
|
-
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
18
|
-
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
19
|
-
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
20
|
-
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
@@ -1,69 +0,0 @@
|
|
|
1
|
-
let random = async bytes => crypto.getRandomValues(new Uint8Array(bytes))
|
|
2
|
-
|
|
3
|
-
let customAlphabet = (alphabet, defaultSize = 21) => {
|
|
4
|
-
// First, a bitmask is necessary to generate the ID. The bitmask makes bytes
|
|
5
|
-
// values closer to the alphabet size. The bitmask calculates the closest
|
|
6
|
-
// `2^31 - 1` number, which exceeds the alphabet size.
|
|
7
|
-
// For example, the bitmask for the alphabet size 30 is 31 (00011111).
|
|
8
|
-
// `Math.clz32` is not used, because it is not available in browsers.
|
|
9
|
-
let mask = (2 << (Math.log(alphabet.length - 1) / Math.LN2)) - 1
|
|
10
|
-
// Though, the bitmask solution is not perfect since the bytes exceeding
|
|
11
|
-
// the alphabet size are refused. Therefore, to reliably generate the ID,
|
|
12
|
-
// the random bytes redundancy has to be satisfied.
|
|
13
|
-
|
|
14
|
-
// Note: every hardware random generator call is performance expensive,
|
|
15
|
-
// because the system call for entropy collection takes a lot of time.
|
|
16
|
-
// So, to avoid additional system calls, extra bytes are requested in advance.
|
|
17
|
-
|
|
18
|
-
// Next, a step determines how many random bytes to generate.
|
|
19
|
-
// The number of random bytes gets decided upon the ID size, mask,
|
|
20
|
-
// alphabet size, and magic number 1.6 (using 1.6 peaks at performance
|
|
21
|
-
// according to benchmarks).
|
|
22
|
-
|
|
23
|
-
// `-~f => Math.ceil(f)` if f is a float
|
|
24
|
-
// `-~i => i + 1` if i is an integer
|
|
25
|
-
let step = -~((1.6 * mask * defaultSize) / alphabet.length)
|
|
26
|
-
|
|
27
|
-
return async (size = defaultSize) => {
|
|
28
|
-
let id = ''
|
|
29
|
-
while (true) {
|
|
30
|
-
let bytes = crypto.getRandomValues(new Uint8Array(step))
|
|
31
|
-
// A compact alternative for `for (var i = 0; i < step; i++)`.
|
|
32
|
-
let i = step | 0
|
|
33
|
-
while (i--) {
|
|
34
|
-
// Adding `|| ''` refuses a random byte that exceeds the alphabet size.
|
|
35
|
-
id += alphabet[bytes[i] & mask] || ''
|
|
36
|
-
if (id.length === size) return id
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
let nanoid = async (size = 21) => {
|
|
43
|
-
let id = ''
|
|
44
|
-
let bytes = crypto.getRandomValues(new Uint8Array((size |= 0)))
|
|
45
|
-
|
|
46
|
-
// A compact alternative for `for (var i = 0; i < step; i++)`.
|
|
47
|
-
while (size--) {
|
|
48
|
-
// It is incorrect to use bytes exceeding the alphabet size.
|
|
49
|
-
// The following mask reduces the random byte in the 0-255 value
|
|
50
|
-
// range to the 0-63 value range. Therefore, adding hacks, such
|
|
51
|
-
// as empty string fallback or magic numbers, is unneccessary because
|
|
52
|
-
// the bitmask trims bytes down to the alphabet size.
|
|
53
|
-
let byte = bytes[size] & 63
|
|
54
|
-
if (byte < 36) {
|
|
55
|
-
// `0-9a-z`
|
|
56
|
-
id += byte.toString(36)
|
|
57
|
-
} else if (byte < 62) {
|
|
58
|
-
// `A-Z`
|
|
59
|
-
id += (byte - 26).toString(36).toUpperCase()
|
|
60
|
-
} else if (byte < 63) {
|
|
61
|
-
id += '_'
|
|
62
|
-
} else {
|
|
63
|
-
id += '-'
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
return id
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
module.exports = { nanoid, customAlphabet, random }
|
|
@@ -1,69 +0,0 @@
|
|
|
1
|
-
let random = async bytes => crypto.getRandomValues(new Uint8Array(bytes))
|
|
2
|
-
|
|
3
|
-
let customAlphabet = (alphabet, defaultSize = 21) => {
|
|
4
|
-
// First, a bitmask is necessary to generate the ID. The bitmask makes bytes
|
|
5
|
-
// values closer to the alphabet size. The bitmask calculates the closest
|
|
6
|
-
// `2^31 - 1` number, which exceeds the alphabet size.
|
|
7
|
-
// For example, the bitmask for the alphabet size 30 is 31 (00011111).
|
|
8
|
-
// `Math.clz32` is not used, because it is not available in browsers.
|
|
9
|
-
let mask = (2 << (Math.log(alphabet.length - 1) / Math.LN2)) - 1
|
|
10
|
-
// Though, the bitmask solution is not perfect since the bytes exceeding
|
|
11
|
-
// the alphabet size are refused. Therefore, to reliably generate the ID,
|
|
12
|
-
// the random bytes redundancy has to be satisfied.
|
|
13
|
-
|
|
14
|
-
// Note: every hardware random generator call is performance expensive,
|
|
15
|
-
// because the system call for entropy collection takes a lot of time.
|
|
16
|
-
// So, to avoid additional system calls, extra bytes are requested in advance.
|
|
17
|
-
|
|
18
|
-
// Next, a step determines how many random bytes to generate.
|
|
19
|
-
// The number of random bytes gets decided upon the ID size, mask,
|
|
20
|
-
// alphabet size, and magic number 1.6 (using 1.6 peaks at performance
|
|
21
|
-
// according to benchmarks).
|
|
22
|
-
|
|
23
|
-
// `-~f => Math.ceil(f)` if f is a float
|
|
24
|
-
// `-~i => i + 1` if i is an integer
|
|
25
|
-
let step = -~((1.6 * mask * defaultSize) / alphabet.length)
|
|
26
|
-
|
|
27
|
-
return async (size = defaultSize) => {
|
|
28
|
-
let id = ''
|
|
29
|
-
while (true) {
|
|
30
|
-
let bytes = crypto.getRandomValues(new Uint8Array(step))
|
|
31
|
-
// A compact alternative for `for (var i = 0; i < step; i++)`.
|
|
32
|
-
let i = step | 0
|
|
33
|
-
while (i--) {
|
|
34
|
-
// Adding `|| ''` refuses a random byte that exceeds the alphabet size.
|
|
35
|
-
id += alphabet[bytes[i] & mask] || ''
|
|
36
|
-
if (id.length === size) return id
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
let nanoid = async (size = 21) => {
|
|
43
|
-
let id = ''
|
|
44
|
-
let bytes = crypto.getRandomValues(new Uint8Array((size |= 0)))
|
|
45
|
-
|
|
46
|
-
// A compact alternative for `for (var i = 0; i < step; i++)`.
|
|
47
|
-
while (size--) {
|
|
48
|
-
// It is incorrect to use bytes exceeding the alphabet size.
|
|
49
|
-
// The following mask reduces the random byte in the 0-255 value
|
|
50
|
-
// range to the 0-63 value range. Therefore, adding hacks, such
|
|
51
|
-
// as empty string fallback or magic numbers, is unneccessary because
|
|
52
|
-
// the bitmask trims bytes down to the alphabet size.
|
|
53
|
-
let byte = bytes[size] & 63
|
|
54
|
-
if (byte < 36) {
|
|
55
|
-
// `0-9a-z`
|
|
56
|
-
id += byte.toString(36)
|
|
57
|
-
} else if (byte < 62) {
|
|
58
|
-
// `A-Z`
|
|
59
|
-
id += (byte - 26).toString(36).toUpperCase()
|
|
60
|
-
} else if (byte < 63) {
|
|
61
|
-
id += '_'
|
|
62
|
-
} else {
|
|
63
|
-
id += '-'
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
return id
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
export { nanoid, customAlphabet, random }
|
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
let crypto = require('crypto')
|
|
2
|
-
|
|
3
|
-
let { urlAlphabet } = require('../url-alphabet/index.cjs')
|
|
4
|
-
|
|
5
|
-
// `crypto.randomFill()` is a little faster than `crypto.randomBytes()`,
|
|
6
|
-
// because it is possible to use in combination with `Buffer.allocUnsafe()`.
|
|
7
|
-
let random = bytes =>
|
|
8
|
-
new Promise((resolve, reject) => {
|
|
9
|
-
// `Buffer.allocUnsafe()` is faster because it doesn’t flush the memory.
|
|
10
|
-
// Memory flushing is unnecessary since the buffer allocation itself resets
|
|
11
|
-
// the memory with the new bytes.
|
|
12
|
-
crypto.randomFill(Buffer.allocUnsafe(bytes), (err, buf) => {
|
|
13
|
-
if (err) {
|
|
14
|
-
reject(err)
|
|
15
|
-
} else {
|
|
16
|
-
resolve(buf)
|
|
17
|
-
}
|
|
18
|
-
})
|
|
19
|
-
})
|
|
20
|
-
|
|
21
|
-
let customAlphabet = (alphabet, defaultSize = 21) => {
|
|
22
|
-
// First, a bitmask is necessary to generate the ID. The bitmask makes bytes
|
|
23
|
-
// values closer to the alphabet size. The bitmask calculates the closest
|
|
24
|
-
// `2^31 - 1` number, which exceeds the alphabet size.
|
|
25
|
-
// For example, the bitmask for the alphabet size 30 is 31 (00011111).
|
|
26
|
-
let mask = (2 << (31 - Math.clz32((alphabet.length - 1) | 1))) - 1
|
|
27
|
-
// Though, the bitmask solution is not perfect since the bytes exceeding
|
|
28
|
-
// the alphabet size are refused. Therefore, to reliably generate the ID,
|
|
29
|
-
// the random bytes redundancy has to be satisfied.
|
|
30
|
-
|
|
31
|
-
// Note: every hardware random generator call is performance expensive,
|
|
32
|
-
// because the system call for entropy collection takes a lot of time.
|
|
33
|
-
// So, to avoid additional system calls, extra bytes are requested in advance.
|
|
34
|
-
|
|
35
|
-
// Next, a step determines how many random bytes to generate.
|
|
36
|
-
// The number of random bytes gets decided upon the ID size, mask,
|
|
37
|
-
// alphabet size, and magic number 1.6 (using 1.6 peaks at performance
|
|
38
|
-
// according to benchmarks).
|
|
39
|
-
let step = Math.ceil((1.6 * mask * defaultSize) / alphabet.length)
|
|
40
|
-
|
|
41
|
-
let tick = (id, size = defaultSize) =>
|
|
42
|
-
random(step).then(bytes => {
|
|
43
|
-
// A compact alternative for `for (var i = 0; i < step; i++)`.
|
|
44
|
-
let i = step
|
|
45
|
-
while (i--) {
|
|
46
|
-
// Adding `|| ''` refuses a random byte that exceeds the alphabet size.
|
|
47
|
-
id += alphabet[bytes[i] & mask] || ''
|
|
48
|
-
if (id.length >= size) return id
|
|
49
|
-
}
|
|
50
|
-
return tick(id, size)
|
|
51
|
-
})
|
|
52
|
-
|
|
53
|
-
return size => tick('', size)
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
let nanoid = (size = 21) =>
|
|
57
|
-
random((size |= 0)).then(bytes => {
|
|
58
|
-
let id = ''
|
|
59
|
-
// A compact alternative for `for (var i = 0; i < step; i++)`.
|
|
60
|
-
while (size--) {
|
|
61
|
-
// It is incorrect to use bytes exceeding the alphabet size.
|
|
62
|
-
// The following mask reduces the random byte in the 0-255 value
|
|
63
|
-
// range to the 0-63 value range. Therefore, adding hacks, such
|
|
64
|
-
// as empty string fallback or magic numbers, is unneccessary because
|
|
65
|
-
// the bitmask trims bytes down to the alphabet size.
|
|
66
|
-
id += urlAlphabet[bytes[size] & 63]
|
|
67
|
-
}
|
|
68
|
-
return id
|
|
69
|
-
})
|
|
70
|
-
|
|
71
|
-
module.exports = { nanoid, customAlphabet, random }
|
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Generate secure URL-friendly unique ID. The non-blocking version.
|
|
3
|
-
*
|
|
4
|
-
* By default, the ID will have 21 symbols to have a collision probability
|
|
5
|
-
* similar to UUID v4.
|
|
6
|
-
*
|
|
7
|
-
* ```js
|
|
8
|
-
* import { nanoid } from 'nanoid/async'
|
|
9
|
-
* nanoid().then(id => {
|
|
10
|
-
* model.id = id
|
|
11
|
-
* })
|
|
12
|
-
* ```
|
|
13
|
-
*
|
|
14
|
-
* @param size Size of the ID. The default size is 21.
|
|
15
|
-
* @returns A promise with a random string.
|
|
16
|
-
*/
|
|
17
|
-
export function nanoid(size?: number): Promise<string>
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* A low-level function.
|
|
21
|
-
* Generate secure unique ID with custom alphabet. The non-blocking version.
|
|
22
|
-
*
|
|
23
|
-
* Alphabet must contain 256 symbols or less. Otherwise, the generator
|
|
24
|
-
* will not be secure.
|
|
25
|
-
*
|
|
26
|
-
* @param alphabet Alphabet used to generate the ID.
|
|
27
|
-
* @param defaultSize Size of the ID. The default size is 21.
|
|
28
|
-
* @returns A function that returns a promise with a random string.
|
|
29
|
-
*
|
|
30
|
-
* ```js
|
|
31
|
-
* import { customAlphabet } from 'nanoid/async'
|
|
32
|
-
* const nanoid = customAlphabet('0123456789абвгдеё', 5)
|
|
33
|
-
* nanoid().then(id => {
|
|
34
|
-
* model.id = id //=> "8ё56а"
|
|
35
|
-
* })
|
|
36
|
-
* ```
|
|
37
|
-
*/
|
|
38
|
-
export function customAlphabet(
|
|
39
|
-
alphabet: string,
|
|
40
|
-
defaultSize?: number
|
|
41
|
-
): (size?: number) => Promise<string>
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* Generate an array of random bytes collected from hardware noise.
|
|
45
|
-
*
|
|
46
|
-
* ```js
|
|
47
|
-
* import { random } from 'nanoid/async'
|
|
48
|
-
* random(5).then(bytes => {
|
|
49
|
-
* bytes //=> [10, 67, 212, 67, 89]
|
|
50
|
-
* })
|
|
51
|
-
* ```
|
|
52
|
-
*
|
|
53
|
-
* @param bytes Size of the array.
|
|
54
|
-
* @returns A promise with a random bytes array.
|
|
55
|
-
*/
|
|
56
|
-
export function random(bytes: number): Promise<Uint8Array>
|
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
import crypto from 'crypto'
|
|
2
|
-
|
|
3
|
-
import { urlAlphabet } from '../url-alphabet/index.js'
|
|
4
|
-
|
|
5
|
-
// `crypto.randomFill()` is a little faster than `crypto.randomBytes()`,
|
|
6
|
-
// because it is possible to use in combination with `Buffer.allocUnsafe()`.
|
|
7
|
-
let random = bytes =>
|
|
8
|
-
new Promise((resolve, reject) => {
|
|
9
|
-
// `Buffer.allocUnsafe()` is faster because it doesn’t flush the memory.
|
|
10
|
-
// Memory flushing is unnecessary since the buffer allocation itself resets
|
|
11
|
-
// the memory with the new bytes.
|
|
12
|
-
crypto.randomFill(Buffer.allocUnsafe(bytes), (err, buf) => {
|
|
13
|
-
if (err) {
|
|
14
|
-
reject(err)
|
|
15
|
-
} else {
|
|
16
|
-
resolve(buf)
|
|
17
|
-
}
|
|
18
|
-
})
|
|
19
|
-
})
|
|
20
|
-
|
|
21
|
-
let customAlphabet = (alphabet, defaultSize = 21) => {
|
|
22
|
-
// First, a bitmask is necessary to generate the ID. The bitmask makes bytes
|
|
23
|
-
// values closer to the alphabet size. The bitmask calculates the closest
|
|
24
|
-
// `2^31 - 1` number, which exceeds the alphabet size.
|
|
25
|
-
// For example, the bitmask for the alphabet size 30 is 31 (00011111).
|
|
26
|
-
let mask = (2 << (31 - Math.clz32((alphabet.length - 1) | 1))) - 1
|
|
27
|
-
// Though, the bitmask solution is not perfect since the bytes exceeding
|
|
28
|
-
// the alphabet size are refused. Therefore, to reliably generate the ID,
|
|
29
|
-
// the random bytes redundancy has to be satisfied.
|
|
30
|
-
|
|
31
|
-
// Note: every hardware random generator call is performance expensive,
|
|
32
|
-
// because the system call for entropy collection takes a lot of time.
|
|
33
|
-
// So, to avoid additional system calls, extra bytes are requested in advance.
|
|
34
|
-
|
|
35
|
-
// Next, a step determines how many random bytes to generate.
|
|
36
|
-
// The number of random bytes gets decided upon the ID size, mask,
|
|
37
|
-
// alphabet size, and magic number 1.6 (using 1.6 peaks at performance
|
|
38
|
-
// according to benchmarks).
|
|
39
|
-
let step = Math.ceil((1.6 * mask * defaultSize) / alphabet.length)
|
|
40
|
-
|
|
41
|
-
let tick = (id, size = defaultSize) =>
|
|
42
|
-
random(step).then(bytes => {
|
|
43
|
-
// A compact alternative for `for (var i = 0; i < step; i++)`.
|
|
44
|
-
let i = step
|
|
45
|
-
while (i--) {
|
|
46
|
-
// Adding `|| ''` refuses a random byte that exceeds the alphabet size.
|
|
47
|
-
id += alphabet[bytes[i] & mask] || ''
|
|
48
|
-
if (id.length >= size) return id
|
|
49
|
-
}
|
|
50
|
-
return tick(id, size)
|
|
51
|
-
})
|
|
52
|
-
|
|
53
|
-
return size => tick('', size)
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
let nanoid = (size = 21) =>
|
|
57
|
-
random((size |= 0)).then(bytes => {
|
|
58
|
-
let id = ''
|
|
59
|
-
// A compact alternative for `for (var i = 0; i < step; i++)`.
|
|
60
|
-
while (size--) {
|
|
61
|
-
// It is incorrect to use bytes exceeding the alphabet size.
|
|
62
|
-
// The following mask reduces the random byte in the 0-255 value
|
|
63
|
-
// range to the 0-63 value range. Therefore, adding hacks, such
|
|
64
|
-
// as empty string fallback or magic numbers, is unneccessary because
|
|
65
|
-
// the bitmask trims bytes down to the alphabet size.
|
|
66
|
-
id += urlAlphabet[bytes[size] & 63]
|
|
67
|
-
}
|
|
68
|
-
return id
|
|
69
|
-
})
|
|
70
|
-
|
|
71
|
-
export { nanoid, customAlphabet, random }
|
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
import { getRandomBytesAsync } from 'expo-random'
|
|
2
|
-
|
|
3
|
-
import { urlAlphabet } from '../url-alphabet/index.js'
|
|
4
|
-
|
|
5
|
-
let random = getRandomBytesAsync
|
|
6
|
-
|
|
7
|
-
let customAlphabet = (alphabet, defaultSize = 21) => {
|
|
8
|
-
// First, a bitmask is necessary to generate the ID. The bitmask makes bytes
|
|
9
|
-
// values closer to the alphabet size. The bitmask calculates the closest
|
|
10
|
-
// `2^31 - 1` number, which exceeds the alphabet size.
|
|
11
|
-
// For example, the bitmask for the alphabet size 30 is 31 (00011111).
|
|
12
|
-
let mask = (2 << (31 - Math.clz32((alphabet.length - 1) | 1))) - 1
|
|
13
|
-
// Though, the bitmask solution is not perfect since the bytes exceeding
|
|
14
|
-
// the alphabet size are refused. Therefore, to reliably generate the ID,
|
|
15
|
-
// the random bytes redundancy has to be satisfied.
|
|
16
|
-
|
|
17
|
-
// Note: every hardware random generator call is performance expensive,
|
|
18
|
-
// because the system call for entropy collection takes a lot of time.
|
|
19
|
-
// So, to avoid additional system calls, extra bytes are requested in advance.
|
|
20
|
-
|
|
21
|
-
// Next, a step determines how many random bytes to generate.
|
|
22
|
-
// The number of random bytes gets decided upon the ID size, mask,
|
|
23
|
-
// alphabet size, and magic number 1.6 (using 1.6 peaks at performance
|
|
24
|
-
// according to benchmarks).
|
|
25
|
-
let step = Math.ceil((1.6 * mask * defaultSize) / alphabet.length)
|
|
26
|
-
|
|
27
|
-
let tick = (id, size = defaultSize) =>
|
|
28
|
-
random(step).then(bytes => {
|
|
29
|
-
// A compact alternative for `for (var i = 0; i < step; i++)`.
|
|
30
|
-
let i = step
|
|
31
|
-
while (i--) {
|
|
32
|
-
// Adding `|| ''` refuses a random byte that exceeds the alphabet size.
|
|
33
|
-
id += alphabet[bytes[i] & mask] || ''
|
|
34
|
-
if (id.length >= size) return id
|
|
35
|
-
}
|
|
36
|
-
return tick(id, size)
|
|
37
|
-
})
|
|
38
|
-
|
|
39
|
-
return size => tick('', size)
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
let nanoid = (size = 21) =>
|
|
43
|
-
random((size |= 0)).then(bytes => {
|
|
44
|
-
let id = ''
|
|
45
|
-
// A compact alternative for `for (var i = 0; i < step; i++)`.
|
|
46
|
-
while (size--) {
|
|
47
|
-
// It is incorrect to use bytes exceeding the alphabet size.
|
|
48
|
-
// The following mask reduces the random byte in the 0-255 value
|
|
49
|
-
// range to the 0-63 value range. Therefore, adding hacks, such
|
|
50
|
-
// as empty string fallback or magic numbers, is unneccessary because
|
|
51
|
-
// the bitmask trims bytes down to the alphabet size.
|
|
52
|
-
id += urlAlphabet[bytes[size] & 63]
|
|
53
|
-
}
|
|
54
|
-
return id
|
|
55
|
-
})
|
|
56
|
-
|
|
57
|
-
export { nanoid, customAlphabet, random }
|