forked from stormpath/idsite-src
-
Notifications
You must be signed in to change notification settings - Fork 1
/
localtunnel.js
121 lines (96 loc) · 2.78 KB
/
localtunnel.js
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
var localtunnel = require('localtunnel');
var stormpath = require('stormpath');
var callbckUri = 'http://stormpath.localhost:8001/idSiteCallback';
var client = new stormpath.Client();
var doCleanup = false;
var previousDomainName = null;
var host = null;
/*eslint no-console: 0*/
function prepeareIdSiteModel(client, idSiteApplicationHost, callbckUri, cb) {
client.getCurrentTenant(function(err, tenant) {
if (err) {
throw err;
}
client.getResource(tenant.href + '/idSites', function(err, collection) {
if (err) {
throw err;
}
var idSiteModel = collection.items[0];
previousDomainName = idSiteModel.domainName;
idSiteModel.domainName = idSiteApplicationHost.split('//')[1];
if (idSiteModel.authorizedOriginUris.indexOf(idSiteApplicationHost) === -1) {
idSiteModel.authorizedOriginUris.push(idSiteApplicationHost);
}
if (idSiteModel.authorizedRedirectUris.indexOf(callbckUri) === -1) {
idSiteModel.authorizedRedirectUris.push(callbckUri);
}
idSiteModel.save(cb);
});
});
}
function revertIdSiteModel(client, idSiteApplicationHost, callbckUri, cb) {
client.getCurrentTenant(function(err, tenant) {
if (err) {
throw err;
}
client.getResource(tenant.href + '/idSites', function(err, collection) {
if (err) {
throw err;
}
var idSiteModel = collection.items[0];
idSiteModel.domainName = previousDomainName;
idSiteModel.authorizedOriginUris = idSiteModel.authorizedOriginUris.filter(function(uri) {
return !uri.match(idSiteApplicationHost);
});
idSiteModel.authorizedRedirectUris = idSiteModel.authorizedRedirectUris.filter(function(uri) {
return !uri.match(callbckUri);
});
idSiteModel.save(cb);
});
});
}
function cleanup(cb){
if(doCleanup){
revertIdSiteModel(client,host,callbckUri,function (err) {
if (err) {
throw err;
}
console.log('ID Site Model Restored');
cb();
});
}
}
console.log(process.env.PORT);
var tunnel = localtunnel(process.env.PORT || 9000, function(err, tunnel) {
if (err) {
console.error(err);
return process.exit(1);
}
host = tunnel.url;
console.log(host);
prepeareIdSiteModel(client,host,callbckUri,function(err){
if (err) {
throw err;
}
console.log('ID Site Model Ready');
setInterval(function () { },1000);
doCleanup = true;
});
});
tunnel.on('error', function (err) {
console.error(err);
cleanup();
});
tunnel.on('close', cleanup);
process.on('SIGTERM', function() {
console.log('\nCaught termination signal');
cleanup(function (){
process.exit();
});
});
process.on('SIGINT', function() {
console.log('\nCaught interrupt signal');
cleanup(function (){
process.exit();
});
});