-
Notifications
You must be signed in to change notification settings - Fork 5
/
service.js
88 lines (74 loc) · 2.34 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
var express = require('express');
var url = require('url');
var fs = require('fs');
var qlikauth = require('qlik-auth');
var google = require('googleapis');
var api = google.oauth2('v2');
var OAuth2 = google.auth.OAuth2;
var app = express();
var arg = process.argv.slice(2);
var settings = {};
arg.forEach(function(a) {
var key = a.split("=");
switch(key[0]) {
case "domain":
settings.domain = key[1];
break;
case "user_directory":
settings.userDirectory = key[1];
break;
case "client_id":
settings.clientId = key[1];
break;
case "client_secret":
settings.clientSecret = key[1];
break;
case "redirect_uris":
settings.redirectUri = key[1];
settings.port = url.parse(settings.redirectUri).port || 80
settings.path = url.parse(settings.redirectUri).path || "/oauth2callback"
break;
}
});
//Create oauth2 client
oauth2Client = new OAuth2(settings.clientId, settings.clientSecret, settings.redirectUri);
app.get('/', function (req, res) {
qlikauth.init(req, res);
//Generate authentication url with email scope
var authUrl = oauth2Client.generateAuthUrl({
access_type: 'online',
scope: 'email'
});
//Redirect to generated url
res.redirect(authUrl);
});
app.get(settings.path, function (req, res) {
//Get token from returned code parameter
oauth2Client.getToken(req.query.code, function(err, token) {
if (err) {res.send(err); return};
//Set credentials from token
oauth2Client.setCredentials(token);
//Get user details
api.userinfo.get({ auth: oauth2Client }, function(err, response) {
if (err) {res.send(err); return};
//Make sure authenticated user belongs to the right domain
if(!response.email.endsWith(settings.domain)) {
res.send("Invalid domain address");
return;
}
//Define user directory, user identity and attributes
var profile = {
'UserDirectory': settings.userDirectory,
'UserId': response.email,
'Attributes': []
}
//Make call for ticket request
qlikauth.requestTicket(req, res, profile);
});
});
});
//Create web server
var server = app.listen(settings.port, function () {});
String.prototype.endsWith = function(suffix) {
return this.indexOf(suffix, this.length - suffix.length) !== -1;
};