forked from openiap/opencore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfig.ts
More file actions
96 lines (85 loc) · 4.21 KB
/
Copy pathConfig.ts
File metadata and controls
96 lines (85 loc) · 4.21 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
87
88
89
90
91
92
93
94
95
96
import * as https from "https";
import * as retry from "async-retry";
import { fetch, toPassportConfig } from "passport-saml-metadata";
export class Config {
public static nodered_id: string = Config.getEnv("nodered_id", "1");
public static consumer_key: string = Config.getEnv("consumer_key", "");
public static consumer_secret: string = Config.getEnv("consumer_secret", "");
public static saml_federation_metadata: string = Config.getEnv("saml_federation_metadata", "");
public static saml_issuer: string = Config.getEnv("saml_issuer", "");
public static saml_entrypoint: string = Config.getEnv("saml_entrypoint", "");
public static saml_crt: string = Config.getEnv("saml_crt", "");
public static tls_crt: string = Config.getEnv("tls_crt", "");
public static tls_key: string = Config.getEnv("tls_key", "");
public static tls_ca: string = Config.getEnv("tls_ca", "");
public static tls_passphrase: string = Config.getEnv("tls_passphrase", "");
public static port: number = parseInt(Config.getEnv("port", "1880"));
public static domain: string = Config.getEnv("domain", "localhost");
public static api_ws_url: string = Config.getEnv("api_ws_url", "ws://localhost:3000");
public static amqp_url: string = Config.getEnv("amqp_url", "amqp://localhost");
public static api_credential_cache_seconds: number = parseInt(Config.getEnv("api_credential_cache_seconds", "300"));
public static api_allow_anonymous: boolean = Config.parseBoolean(Config.getEnv("api_allow_anonymous", "false"));
public static aes_secret: string = Config.getEnv("aes_secret", "");
public static baseurl(): string {
if (Config.tls_crt != '' && Config.tls_key != '') {
return "https://" + Config.domain + ":" + Config.port + "/";
}
return "http://" + Config.domain + ":" + Config.port + "/";
}
public static getEnv(name: string, defaultvalue: string): string {
var value: any = process.env[name];
if (!value || value === "") { value = defaultvalue; }
return value;
}
public static parseBoolean(s:any):boolean {
var val:string = "false";
if (typeof s === "number") {
val = s.toString();
} else if (typeof s === "string") {
val = s.toLowerCase().trim();
} else if (typeof s === "boolean") {
val = s.toString();
} else {
throw new Error("Unknown type!");
}
switch(val) {
case "true": case "yes": case "1": return true;
case "false": case "no": case "0": case null: return false;
default: return Boolean(s);
}
}
public static async parse_federation_metadata(url: string): Promise<any> {
try {
// rootCas.addFile(path.join(__dirname, '../config/ssl/gd_bundle-g2-g1.crt'));
if (Config.tls_ca !== "") {
// var tls_ca: string = Buffer.from(Config.tls_ca, 'base64').toString('ascii')
// var rootCas = require('ssl-root-cas/latest').create();
// rootCas.push(tls_ca);
// // rootCas.addFile( tls_ca );
// https.globalAgent.options.ca = rootCas;
// require('https').globalAgent.options.ca = rootCas;
}
} catch (error) {
console.log(error);
}
// if anything throws, we retry
var metadata: any = await retry(async bail => {
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
var reader: any = await fetch({ url });
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "1";
if (reader === null || reader === undefined) { bail(new Error("Failed getting result")); return; }
var config: any = toPassportConfig(reader);
// we need this, for Office 365 :-/
if (reader.signingCerts && reader.signingCerts.length > 1) {
config.cert = reader.signingCerts;
}
return config;
}, {
retries: 50,
onRetry: function (error: Error, count: number): void {
console.log("retry " + count + " error " + error.message + " getting " + url);
}
});
return metadata;
}
}