This repository was archived by the owner on Feb 16, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathcreateOAuthClient.js
More file actions
86 lines (78 loc) · 2.16 KB
/
createOAuthClient.js
File metadata and controls
86 lines (78 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/**
* @module createOAuthClient
* @description Create OAuth client. For use by Jenkins, it creates an OAuth
* Client with randomized data so they can be securely and randomly generated
* on our production website.
*
* docker-compose exec api node ./commands/createOAuthClient.js
*/
const mongoose = require('mongoose');
const args = require('yargs').argv;
const Client = require('../api/models/Client');
const env = require('../config/env');
const { logger } = env;
// Connect to DB.
const store = env.database.store;
mongoose.connect(store.uri, store.options);
// Generate a random secret each time we run the command
const idChars = 'abcdefghijklmnopqrstuvwxyz';
const secretChars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()';
let suffix = '';
let secret = '';
for (let i = 0; i < 36; i++) {
suffix += idChars.charAt(Math.floor(Math.random() * idChars.length));
secret += secretChars.charAt(Math.floor(Math.random() * secretChars.length));
}
async function run() {
const clientInfo = {
id: `change-me-${suffix.substring(0, 6)}`,
name: 'CHANGE ME',
secret,
url: 'https://example.com/',
redirectUri: 'https://example.com/user/login/hid/callback',
};
// Customize the input if arguments were sent
if (args.id) {
clientInfo.id = args.id;
}
if (args.name) {
clientInfo.name = args.name;
}
if (args.url) {
clientInfo.url = args.url;
}
if (args.redirectUri) {
clientInfo.redirectUri = args.redirectUri;
}
// Attempt to create new OAuth client and log the result.
await Client.create(clientInfo).then((data) => {
logger.info(
'[commands->createOAuthClient] created new OAuth client',
{
security: true,
oauth: {
client_id: data.id,
},
},
);
}).catch((err) => {
logger.warn(
'[commands->createOAuthClient] failed to create new OAuth client',
{
security: true,
fail: true,
oauth: {
client_id: clientInfo.id,
},
stack_trace: err.stack,
},
);
});
process.exit();
}
(async function iife() {
await run();
}()).catch((e) => {
console.log(e);
process.exit(1);
});