-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmicroservice-auth-anonymous.js
135 lines (124 loc) · 3.24 KB
/
microservice-auth-anonymous.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/**
* Profile Stats MicroService.
*/
'use strict';
const framework = '@microservice-framework';
const Cluster = require(framework + '/microservice-cluster');
const Microservice = require(framework + '/microservice');
const MicroserviceRouterRegister = require(framework + '/microservice-router-register').register;
const clientViaRouter = require(framework + '/microservice-router-register').clientViaRouter;
const debugF = require('debug');
const fs = require('fs');
const ANONYMOUS = 'anonymous';
var debug = {
log: debugF('auth-anonymous:log'),
debug: debugF('auth-anonymous:debug')
};
require('dotenv').config();
let permissionsPath = './permissions.json';
if (process.env.PERMISSION_PATH) {
permissionsPath = process.env.PERMISSION_PATH;
}
var mservice = new Microservice({
mongoUrl: '',
mongoTable: '',
secureKey: process.env.SECURE_KEY,
schema: process.env.SCHEMA
});
var mControlCluster = new Cluster({
pid: process.env.PIDFILE,
port: process.env.PORT,
hostname: process.env.HOSTNAME,
count: process.env.WORKERS,
callbacks: {
init: microserviceAuthAnonymousINIT,
POST: microserviceAuthAnonymousPOST,
OPTIONS: mservice.options
}
});
/**
* Init Handler.
*/
function microserviceAuthAnonymousINIT(cluster, worker, address) {
if (worker.id == 1) {
var mserviceRegister = new MicroserviceRouterRegister({
server: {
url: process.env.ROUTER_URL,
secureKey: process.env.ROUTER_SECRET,
period: process.env.ROUTER_PERIOD,
},
route: {
path: [process.env.SELF_PATH],
url: process.env.SELF_URL,
secureKey: process.env.SECURE_KEY,
},
cluster: cluster
});
}
}
/**
* POST handler.
*/
function microserviceAuthAnonymousPOST(jsonData, requestDetails, callback) {
try {
// Validate jsonData.code for XSS
mservice.validateJson(jsonData);
} catch (e) {
return callback(e, null);
}
getScope(function(err, anonymousJSON) {
if (err) {
return callback(err);
}
// scope it and return access token
let scopeRequest = {
credentials: {
login: ANONYMOUS
},
scope: anonymousJSON
}
if (process.env.DEFAULT_TTL) {
scopeRequest.ttl = parseInt(process.env.DEFAULT_TTL);
}
if(jsonData.ttl) {
scopeRequest.ttl = jsonData.ttl;
}
clientViaRouter('auth', function(err, authServer) {
if (err) {
return callback(err);
}
authServer.post(scopeRequest, function(err, authAnswer) {
if (err) {
debug.debug('authServer.post err %O', err);
debug.log('authServer.post failed with error.');
return callback(err);
}
let handlerAnswer = {
code: 200,
answer: {
accessToken: authAnswer.accessToken,
expireAt: authAnswer.expireAt
}
}
return callback(err, handlerAnswer);
});
});
})
}
/**
* Read scope from filesystem.
*/
function getScope(callback) {
let scopeJSON;
fs.readFile(permissionsPath, function(err, data){
if (err) {
return callback(err);
}
try {
scopeJSON = JSON.parse(data);
callback(null, scopeJSON);
} catch (e) {
return callback(new Error('Failed to load role permissions'));
}
})
}