-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfiguration.ts
More file actions
86 lines (74 loc) · 2.65 KB
/
configuration.ts
File metadata and controls
86 lines (74 loc) · 2.65 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
import joi from 'joi';
import * as jose from 'jose';
import {ConfigurationError} from './errors';
import {FACTSET_WELL_KNOWN_URI, PACKAGE_NAME} from './constants';
import {readFileSync} from 'fs';
import debugModule from 'debug';
const debug = debugModule(`${PACKAGE_NAME}:configuration`);
export type ConfidentialClientJwk = jose.JWK;
export type ConfidentialClientConfiguration = {
name: string;
clientId: string;
clientAuthType: string;
owners: Array<string>;
wellKnownUri: string;
jwk: ConfidentialClientJwk;
};
const schema = joi
.object({
name: joi.string().required(),
clientId: joi.string().required(),
clientAuthType: joi.string().required(),
owners: joi.array().items(joi.string()).min(1).required(),
wellKnownUri: joi.string().uri().default(FACTSET_WELL_KNOWN_URI),
jwk: joi
.object({
kty: joi.string().required(),
use: joi.string().required(),
alg: joi.string().required(),
kid: joi.string().required(),
d: joi.string().required(),
n: joi.string().required(),
e: joi.string().required(),
p: joi.string().required(),
q: joi.string().required(),
dp: joi.string().required(),
dq: joi.string().required(),
qi: joi.string().required(),
})
.required(),
})
.unknown(true);
export class Configuration {
public static validateConfig(config: unknown): ConfidentialClientConfiguration {
debug('Validating the config');
const result = schema.validate(config, {abortEarly: false, errors: {}});
if (result.error !== undefined) {
throw new ConfigurationError(`Configuration is not valid: ${result.error.message}`);
}
debug('Config is vaild');
return result.value;
}
public static loadConfig(param: ConfidentialClientConfiguration | string): ConfidentialClientConfiguration {
debug('Trying to load the config');
if (typeof param === 'object') {
debug('Config is an object');
return Configuration.validateConfig(param);
} else if (typeof param === 'string') {
try {
debug('Config is a string, trying to load from a file: %s', param);
const configString = readFileSync(param, 'utf8');
const parsedConfig = JSON.parse(configString);
return Configuration.validateConfig(parsedConfig);
} catch (error) {
if (error instanceof ConfigurationError) {
throw error;
}
throw new ConfigurationError(`Could not load config: ${param} (${error})`, error);
}
}
throw new ConfigurationError(
'Invalid parameter type, needs to be a path (string) or configuration (ConfidentialClientConfiguration)"'
);
}
}