-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice.js
100 lines (83 loc) · 2.94 KB
/
service.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
var express = require('express'),
app = express(),
fs = require('fs'),
https = require("https"),
qlikAuth = require('qlik-auth'),
o365 = require('./o365.js'),
config = require("./config.js");
var arg = process.argv.slice(2);
arg.forEach( function(a) {
var key = a.split("=");
switch( key[0] ) {
case "user_directory":
config.prefix = key[1];
break;
case "certificates_path":
config.certPath = key[1];
break;
case "client_id":
config.office365.client_id = key[1];
break;
case "client_secret":
config.office365.client_secret = key[1];
break;
case "auth_port":
config.port = key[1];
break;
}
} );
app.get('/', function ( req, res ) {
//Init sense auth module
qlikAuth.init(req, res);
//Redirect to Office 365 Auth url
var hostUrl = req.protocol+"://"+req.get('host');
res.redirect( o365.getAuthUrl(hostUrl, config) );
});
app.get('/oauth2callback', function ( req, res ) {
if ( req.query.code !== undefined && req.query.state !== undefined ) {
var hostUrl = req.protocol+"://"+req.get('host');
o365.getTokenFromCode( req.query.code, req.query.state, hostUrl, config, function ( e, accessToken, refreshToken ) {
if ( e ) {
res.send( { "error": e } );
return;
}
o365.getUser( accessToken, function( err, user ) {
if ( !err && user ) {
o365.getUserGroups( accessToken, function( err, groups ) {
if (err) {
res.send( { "error": err } );
return;
}
var attributes = groups.value.map( function(g) {
return {"Group": g.displayName};
} );
attributes.push( { "name": user.displayName } );
qlikAuth.requestTicket(req, res, {
'UserDirectory': config.prefix,
'UserId': user.userPrincipalName,
'Attributes': attributes
});
} );
//Make call for ticket request
/*
qlikAuth.requestTicket(req, res, {
'UserDirectory': config.prefix,
'UserId': user.userPrincipalName,
'Attributes': []
});*/
} else {
res.send( { "error": err } );
}
});
});
} else {
res.send( {"error": "missing code"} );
}
});
var options = {
key: fs.readFileSync( config.certificates().client_key ),
cert: fs.readFileSync( config.certificates().client ),
};
//Server application
var server = https.createServer( options, app );
server.listen( config.port );