-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Expand file tree
/
Copy pathAPIKeyHandler.ts
More file actions
35 lines (28 loc) · 995 Bytes
/
APIKeyHandler.ts
File metadata and controls
35 lines (28 loc) · 995 Bytes
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
import * as absolutePaths from '../utils/AbsolutePaths';
import fs from 'fs';
import log4js from 'log4js';
import randomString from '../utils/randomstring';
import {argv} from '../utils/Cli'
import settings from '../utils/Settings';
const apiHandlerLogger = log4js.getLogger('APIHandler');
export type APIFields = {
apikey: string;
api_key: string;
padID: string;
padName: string;
authorization: string;
}
// ensure we have an apikey
export let apikey:string|null = null;
const apikeyFilename = absolutePaths.makeAbsolute(argv.apikey || './APIKEY.txt');
if(settings.authenticationMethod === 'apikey') {
try {
apikey = fs.readFileSync(apikeyFilename, 'utf8');
apiHandlerLogger.info(`Api key file read from: "${apikeyFilename}"`);
} catch (e) {
apiHandlerLogger.info(
`Api key file "${apikeyFilename}" not found. Creating with random contents.`);
apikey = randomString(32);
fs.writeFileSync(apikeyFilename, apikey!, 'utf8');
}
}