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
150 lines (127 loc) · 6.35 KB
/
Copy pathConfig.ts
File metadata and controls
150 lines (127 loc) · 6.35 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
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 protocol: string = Config.getEnv("protocol", "http");
public static nodered_domain_schema: string = Config.getEnv("nodered_domain_schema", "");
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 {
var matches = Config.nodered_id.match(/\d+/);
if (matches !== null && matches !== undefined) {
if (matches.length > 0) {
Config.nodered_id = matches[matches.length - 1]; // Just grab the last number
}
}
if (Config.nodered_domain_schema != "") {
Config.domain = Config.nodered_domain_schema.replace("$nodered_id$", Config.nodered_id)
}
// if (Config.tls_crt != '' && Config.tls_key != '') {
// return "https://" + Config.domain + ":" + Config.port + "/";
// }
// return "http://" + Config.domain + ":" + Config.port + "/";
var result: string = "";
if (Config.tls_crt != '' && Config.tls_key != '') {
result = "https://" + Config.domain;
} else {
result = Config.protocol + "://" + Config.domain;
}
if (Config.port != 80 && Config.port != 443) {
result = result + ":" + Config.port + "/";
} else { result = result + "/"; }
return result;
}
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 {
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;
}
static DumpConfig() {
console.log("baseurl" + Config.baseurl());
console.log("nodered_id: " + Config.nodered_id);
console.log("consumer_key: " + Config.consumer_key);
console.log("consumer_secret: " + Config.consumer_secret);
console.log("saml_federation_metadata: " + Config.saml_federation_metadata);
console.log("saml_issuer: " + Config.saml_issuer);
console.log("saml_entrypoint: " + Config.saml_entrypoint);
console.log("saml_crt: " + Config.saml_crt);
console.log("port: " + Config.port);
console.log("domain: " + Config.domain);
console.log("nodered_domain_schema: " + Config.nodered_domain_schema);
console.log("api_ws_url: " + Config.api_ws_url);
console.log("amqp_url: " + Config.amqp_url);
console.log("api_credential_cache_seconds: " + Config.api_credential_cache_seconds);
console.log("api_allow_anonymous: " + Config.api_allow_anonymous);
// console.log("tls_crt: " + Config.tls_crt);
// console.log("tls_key: " + Config.tls_key);
// console.log("tls_ca: " + Config.tls_ca);
// console.log("tls_passphrase: " + Config.tls_passphrase);
// console.log("aes_secret: " + Config.aes_secret);
}
}