@typekcz-nocobase-plugins/plugin-oidc-plus 1.0.2 → 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 -3
- package/dist/externalVersion.js +9 -9
- package/package.json +10 -10
- 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/LICENSE +0 -20
- package/dist/node_modules/nanoid/async/index.browser.cjs +0 -34
- package/dist/node_modules/nanoid/async/index.browser.js +0 -34
- package/dist/node_modules/nanoid/async/index.cjs +0 -35
- package/dist/node_modules/nanoid/async/index.d.ts +0 -56
- package/dist/node_modules/nanoid/async/index.js +0 -35
- package/dist/node_modules/nanoid/async/index.native.js +0 -26
- 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 -34
- package/dist/node_modules/nanoid/index.browser.js +0 -34
- package/dist/node_modules/nanoid/index.cjs +0 -1
- package/dist/node_modules/nanoid/index.d.ts +0 -91
- package/dist/node_modules/nanoid/index.js +0 -45
- package/dist/node_modules/nanoid/nanoid.js +0 -1
- package/dist/node_modules/nanoid/non-secure/index.cjs +0 -21
- package/dist/node_modules/nanoid/non-secure/index.d.ts +0 -33
- package/dist/node_modules/nanoid/non-secure/index.js +0 -21
- 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 -3
- package/dist/node_modules/nanoid/url-alphabet/index.js +0 -3
- package/dist/node_modules/nanoid/url-alphabet/package.json +0 -6
- package/dist/node_modules/openid-client/lib/client.js +0 -1849
- 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 -211
- 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 -191
- 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 -622
|
@@ -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,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,34 +0,0 @@
|
|
|
1
|
-
let random = async bytes => crypto.getRandomValues(new Uint8Array(bytes))
|
|
2
|
-
let customAlphabet = (alphabet, defaultSize = 21) => {
|
|
3
|
-
let mask = (2 << (Math.log(alphabet.length - 1) / Math.LN2)) - 1
|
|
4
|
-
let step = -~((1.6 * mask * defaultSize) / alphabet.length)
|
|
5
|
-
return async (size = defaultSize) => {
|
|
6
|
-
let id = ''
|
|
7
|
-
while (true) {
|
|
8
|
-
let bytes = crypto.getRandomValues(new Uint8Array(step))
|
|
9
|
-
let i = step
|
|
10
|
-
while (i--) {
|
|
11
|
-
id += alphabet[bytes[i] & mask] || ''
|
|
12
|
-
if (id.length === size) return id
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
let nanoid = async (size = 21) => {
|
|
18
|
-
let id = ''
|
|
19
|
-
let bytes = crypto.getRandomValues(new Uint8Array(size))
|
|
20
|
-
while (size--) {
|
|
21
|
-
let byte = bytes[size] & 63
|
|
22
|
-
if (byte < 36) {
|
|
23
|
-
id += byte.toString(36)
|
|
24
|
-
} else if (byte < 62) {
|
|
25
|
-
id += (byte - 26).toString(36).toUpperCase()
|
|
26
|
-
} else if (byte < 63) {
|
|
27
|
-
id += '_'
|
|
28
|
-
} else {
|
|
29
|
-
id += '-'
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
return id
|
|
33
|
-
}
|
|
34
|
-
module.exports = { nanoid, customAlphabet, random }
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
let random = async bytes => crypto.getRandomValues(new Uint8Array(bytes))
|
|
2
|
-
let customAlphabet = (alphabet, defaultSize = 21) => {
|
|
3
|
-
let mask = (2 << (Math.log(alphabet.length - 1) / Math.LN2)) - 1
|
|
4
|
-
let step = -~((1.6 * mask * defaultSize) / alphabet.length)
|
|
5
|
-
return async (size = defaultSize) => {
|
|
6
|
-
let id = ''
|
|
7
|
-
while (true) {
|
|
8
|
-
let bytes = crypto.getRandomValues(new Uint8Array(step))
|
|
9
|
-
let i = step
|
|
10
|
-
while (i--) {
|
|
11
|
-
id += alphabet[bytes[i] & mask] || ''
|
|
12
|
-
if (id.length === size) return id
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
let nanoid = async (size = 21) => {
|
|
18
|
-
let id = ''
|
|
19
|
-
let bytes = crypto.getRandomValues(new Uint8Array(size))
|
|
20
|
-
while (size--) {
|
|
21
|
-
let byte = bytes[size] & 63
|
|
22
|
-
if (byte < 36) {
|
|
23
|
-
id += byte.toString(36)
|
|
24
|
-
} else if (byte < 62) {
|
|
25
|
-
id += (byte - 26).toString(36).toUpperCase()
|
|
26
|
-
} else if (byte < 63) {
|
|
27
|
-
id += '_'
|
|
28
|
-
} else {
|
|
29
|
-
id += '-'
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
return id
|
|
33
|
-
}
|
|
34
|
-
export { nanoid, customAlphabet, random }
|
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
let crypto = require('crypto')
|
|
2
|
-
let { urlAlphabet } = require('../url-alphabet/index.cjs')
|
|
3
|
-
let random = bytes =>
|
|
4
|
-
new Promise((resolve, reject) => {
|
|
5
|
-
crypto.randomFill(Buffer.allocUnsafe(bytes), (err, buf) => {
|
|
6
|
-
if (err) {
|
|
7
|
-
reject(err)
|
|
8
|
-
} else {
|
|
9
|
-
resolve(buf)
|
|
10
|
-
}
|
|
11
|
-
})
|
|
12
|
-
})
|
|
13
|
-
let customAlphabet = (alphabet, defaultSize = 21) => {
|
|
14
|
-
let mask = (2 << (31 - Math.clz32((alphabet.length - 1) | 1))) - 1
|
|
15
|
-
let step = Math.ceil((1.6 * mask * defaultSize) / alphabet.length)
|
|
16
|
-
let tick = (id, size = defaultSize) =>
|
|
17
|
-
random(step).then(bytes => {
|
|
18
|
-
let i = step
|
|
19
|
-
while (i--) {
|
|
20
|
-
id += alphabet[bytes[i] & mask] || ''
|
|
21
|
-
if (id.length === size) return id
|
|
22
|
-
}
|
|
23
|
-
return tick(id, size)
|
|
24
|
-
})
|
|
25
|
-
return size => tick('', size)
|
|
26
|
-
}
|
|
27
|
-
let nanoid = (size = 21) =>
|
|
28
|
-
random(size).then(bytes => {
|
|
29
|
-
let id = ''
|
|
30
|
-
while (size--) {
|
|
31
|
-
id += urlAlphabet[bytes[size] & 63]
|
|
32
|
-
}
|
|
33
|
-
return id
|
|
34
|
-
})
|
|
35
|
-
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,35 +0,0 @@
|
|
|
1
|
-
import crypto from 'crypto'
|
|
2
|
-
import { urlAlphabet } from '../url-alphabet/index.js'
|
|
3
|
-
let random = bytes =>
|
|
4
|
-
new Promise((resolve, reject) => {
|
|
5
|
-
crypto.randomFill(Buffer.allocUnsafe(bytes), (err, buf) => {
|
|
6
|
-
if (err) {
|
|
7
|
-
reject(err)
|
|
8
|
-
} else {
|
|
9
|
-
resolve(buf)
|
|
10
|
-
}
|
|
11
|
-
})
|
|
12
|
-
})
|
|
13
|
-
let customAlphabet = (alphabet, defaultSize = 21) => {
|
|
14
|
-
let mask = (2 << (31 - Math.clz32((alphabet.length - 1) | 1))) - 1
|
|
15
|
-
let step = Math.ceil((1.6 * mask * defaultSize) / alphabet.length)
|
|
16
|
-
let tick = (id, size = defaultSize) =>
|
|
17
|
-
random(step).then(bytes => {
|
|
18
|
-
let i = step
|
|
19
|
-
while (i--) {
|
|
20
|
-
id += alphabet[bytes[i] & mask] || ''
|
|
21
|
-
if (id.length === size) return id
|
|
22
|
-
}
|
|
23
|
-
return tick(id, size)
|
|
24
|
-
})
|
|
25
|
-
return size => tick('', size)
|
|
26
|
-
}
|
|
27
|
-
let nanoid = (size = 21) =>
|
|
28
|
-
random(size).then(bytes => {
|
|
29
|
-
let id = ''
|
|
30
|
-
while (size--) {
|
|
31
|
-
id += urlAlphabet[bytes[size] & 63]
|
|
32
|
-
}
|
|
33
|
-
return id
|
|
34
|
-
})
|
|
35
|
-
export { nanoid, customAlphabet, random }
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import { getRandomBytesAsync } from 'expo-random'
|
|
2
|
-
import { urlAlphabet } from '../url-alphabet/index.js'
|
|
3
|
-
let random = getRandomBytesAsync
|
|
4
|
-
let customAlphabet = (alphabet, defaultSize = 21) => {
|
|
5
|
-
let mask = (2 << (31 - Math.clz32((alphabet.length - 1) | 1))) - 1
|
|
6
|
-
let step = Math.ceil((1.6 * mask * defaultSize) / alphabet.length)
|
|
7
|
-
let tick = (id, size = defaultSize) =>
|
|
8
|
-
random(step).then(bytes => {
|
|
9
|
-
let i = step
|
|
10
|
-
while (i--) {
|
|
11
|
-
id += alphabet[bytes[i] & mask] || ''
|
|
12
|
-
if (id.length === size) return id
|
|
13
|
-
}
|
|
14
|
-
return tick(id, size)
|
|
15
|
-
})
|
|
16
|
-
return size => tick('', size)
|
|
17
|
-
}
|
|
18
|
-
let nanoid = (size = 21) =>
|
|
19
|
-
random(size).then(bytes => {
|
|
20
|
-
let id = ''
|
|
21
|
-
while (size--) {
|
|
22
|
-
id += urlAlphabet[bytes[size] & 63]
|
|
23
|
-
}
|
|
24
|
-
return id
|
|
25
|
-
})
|
|
26
|
-
export { nanoid, customAlphabet, random }
|
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
let { nanoid, customAlphabet } = require('..')
|
|
4
|
-
|
|
5
|
-
function print(msg) {
|
|
6
|
-
process.stdout.write(msg + '\n')
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
function error(msg) {
|
|
10
|
-
process.stderr.write(msg + '\n')
|
|
11
|
-
process.exit(1)
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
if (process.argv.includes('--help') || process.argv.includes('-h')) {
|
|
15
|
-
print(`
|
|
16
|
-
Usage
|
|
17
|
-
$ nanoid [options]
|
|
18
|
-
|
|
19
|
-
Options
|
|
20
|
-
-s, --size Generated ID size
|
|
21
|
-
-a, --alphabet Alphabet to use
|
|
22
|
-
-h, --help Show this help
|
|
23
|
-
|
|
24
|
-
Examples
|
|
25
|
-
$ nanoid --s 15
|
|
26
|
-
S9sBF77U6sDB8Yg
|
|
27
|
-
|
|
28
|
-
$ nanoid --size 10 --alphabet abc
|
|
29
|
-
bcabababca`)
|
|
30
|
-
process.exit()
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
let alphabet, size
|
|
34
|
-
for (let i = 2; i < process.argv.length; i++) {
|
|
35
|
-
let arg = process.argv[i]
|
|
36
|
-
if (arg === '--size' || arg === '-s') {
|
|
37
|
-
size = Number(process.argv[i + 1])
|
|
38
|
-
i += 1
|
|
39
|
-
if (Number.isNaN(size) || size <= 0) {
|
|
40
|
-
error('Size must be positive integer')
|
|
41
|
-
}
|
|
42
|
-
} else if (arg === '--alphabet' || arg === '-a') {
|
|
43
|
-
alphabet = process.argv[i + 1]
|
|
44
|
-
i += 1
|
|
45
|
-
} else {
|
|
46
|
-
error('Unknown argument ' + arg)
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
if (alphabet) {
|
|
51
|
-
let customNanoid = customAlphabet(alphabet, size)
|
|
52
|
-
print(customNanoid())
|
|
53
|
-
} else {
|
|
54
|
-
print(nanoid(size))
|
|
55
|
-
}
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
let { urlAlphabet } = require('./url-alphabet/index.cjs')
|
|
2
|
-
let random = bytes => crypto.getRandomValues(new Uint8Array(bytes))
|
|
3
|
-
let customRandom = (alphabet, defaultSize, getRandom) => {
|
|
4
|
-
let mask = (2 << (Math.log(alphabet.length - 1) / Math.LN2)) - 1
|
|
5
|
-
let step = -~((1.6 * mask * defaultSize) / alphabet.length)
|
|
6
|
-
return (size = defaultSize) => {
|
|
7
|
-
let id = ''
|
|
8
|
-
while (true) {
|
|
9
|
-
let bytes = getRandom(step)
|
|
10
|
-
let j = step
|
|
11
|
-
while (j--) {
|
|
12
|
-
id += alphabet[bytes[j] & mask] || ''
|
|
13
|
-
if (id.length === size) return id
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
let customAlphabet = (alphabet, size = 21) =>
|
|
19
|
-
customRandom(alphabet, size, random)
|
|
20
|
-
let nanoid = (size = 21) =>
|
|
21
|
-
crypto.getRandomValues(new Uint8Array(size)).reduce((id, byte) => {
|
|
22
|
-
byte &= 63
|
|
23
|
-
if (byte < 36) {
|
|
24
|
-
id += byte.toString(36)
|
|
25
|
-
} else if (byte < 62) {
|
|
26
|
-
id += (byte - 26).toString(36).toUpperCase()
|
|
27
|
-
} else if (byte > 62) {
|
|
28
|
-
id += '-'
|
|
29
|
-
} else {
|
|
30
|
-
id += '_'
|
|
31
|
-
}
|
|
32
|
-
return id
|
|
33
|
-
}, '')
|
|
34
|
-
module.exports = { nanoid, customAlphabet, customRandom, urlAlphabet, random }
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import { urlAlphabet } from './url-alphabet/index.js'
|
|
2
|
-
let random = bytes => crypto.getRandomValues(new Uint8Array(bytes))
|
|
3
|
-
let customRandom = (alphabet, defaultSize, getRandom) => {
|
|
4
|
-
let mask = (2 << (Math.log(alphabet.length - 1) / Math.LN2)) - 1
|
|
5
|
-
let step = -~((1.6 * mask * defaultSize) / alphabet.length)
|
|
6
|
-
return (size = defaultSize) => {
|
|
7
|
-
let id = ''
|
|
8
|
-
while (true) {
|
|
9
|
-
let bytes = getRandom(step)
|
|
10
|
-
let j = step
|
|
11
|
-
while (j--) {
|
|
12
|
-
id += alphabet[bytes[j] & mask] || ''
|
|
13
|
-
if (id.length === size) return id
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
let customAlphabet = (alphabet, size = 21) =>
|
|
19
|
-
customRandom(alphabet, size, random)
|
|
20
|
-
let nanoid = (size = 21) =>
|
|
21
|
-
crypto.getRandomValues(new Uint8Array(size)).reduce((id, byte) => {
|
|
22
|
-
byte &= 63
|
|
23
|
-
if (byte < 36) {
|
|
24
|
-
id += byte.toString(36)
|
|
25
|
-
} else if (byte < 62) {
|
|
26
|
-
id += (byte - 26).toString(36).toUpperCase()
|
|
27
|
-
} else if (byte > 62) {
|
|
28
|
-
id += '-'
|
|
29
|
-
} else {
|
|
30
|
-
id += '_'
|
|
31
|
-
}
|
|
32
|
-
return id
|
|
33
|
-
}, '')
|
|
34
|
-
export { nanoid, customAlphabet, customRandom, urlAlphabet, random }
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
(function(){var e={113:function(e){"use strict";e.exports=require("crypto")},592:function(e,t,r){let l=r(113);let{urlAlphabet:n}=r(651);const a=128;let u,i;let fillPool=e=>{if(!u||u.length<e){u=Buffer.allocUnsafe(e*a);l.randomFillSync(u);i=0}else if(i+e>u.length){l.randomFillSync(u);i=0}i+=e};let random=e=>{fillPool(e-=0);return u.subarray(i-e,i)};let customRandom=(e,t,r)=>{let l=(2<<31-Math.clz32(e.length-1|1))-1;let n=Math.ceil(1.6*l*t/e.length);return(a=t)=>{let u="";while(true){let t=r(n);let i=n;while(i--){u+=e[t[i]&l]||"";if(u.length===a)return u}}}};let customAlphabet=(e,t=21)=>customRandom(e,t,random);let nanoid=(e=21)=>{fillPool(e-=0);let t="";for(let r=i-e;r<i;r++){t+=n[u[r]&63]}return t};e.exports={nanoid:nanoid,customAlphabet:customAlphabet,customRandom:customRandom,urlAlphabet:n,random:random}},651:function(e){let t="useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict";e.exports={urlAlphabet:t}}};var t={};function __nccwpck_require__(r){var l=t[r];if(l!==undefined){return l.exports}var n=t[r]={exports:{}};var a=true;try{e[r](n,n.exports,__nccwpck_require__);a=false}finally{if(a)delete t[r]}return n.exports}if(typeof __nccwpck_require__!=="undefined")__nccwpck_require__.ab=__dirname+"/";var r=__nccwpck_require__(592);module.exports=r})();
|