-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.js
More file actions
121 lines (105 loc) · 4.78 KB
/
auth.js
File metadata and controls
121 lines (105 loc) · 4.78 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
// 'use strict';
// var crypto = require('crypto');
// var AuthenticationContext = require('adal-node').AuthenticationContext;
// module.exports = {
// Create:function(params){
// var authObj = {
// tenant:params.tenant, // OK.
// clientId:params.clientId,
// secret:params.secret,
// redirectUri:params.redirectUri,
// resource:params.resource
// };
// authObj.authorityHostUrl = "https://login.windows.net";
// authObj.authorityUrl = authObj.authorityHostUrl + "/" + authObj.tenant;
// authObj.resource = "00000002-0000-0000-c000-000000000000";
// authObj.templateAuthzUrl = 'https://login.windows.net/' + authObj.tenant + '/oauth2/authorize?response_type=code&client_id=<client_id>&redirect_uri=<redirect_uri>&state=<state>&resource=<resource>';
// authObj.loginIfNotAuth = function(req,res,action){
// if(isAuthored(req))
// {
// console.log("is Authored.");
// if(isExpire(req))
// {
// console.log("is expired.");
// authObj.refreshToken(req,res,action);
// }
// else{
// console.log("not expired.");
// action();
// }
// }
// else
// {
// console.log("is not authored.");
// authWithAzureAD(res);
// }
// };
// authObj.receiveToken = function(req,res,action){
// if (req.cookies.authstate !== req.query.state) {
// res.send('error: state does not match');
// return;
// }
// var authenticationContext = new AuthenticationContext(authObj.authorityUrl);
// authenticationContext.acquireTokenWithAuthorizationCode(req.query.code, authObj.redirectUri, authObj.resource, authObj.clientId, authObj.secret, function(err, response) {
// console.log("auth.js -> session");
// var message = '';
// if (err) {
// message = 'error: ' + err.message;
// console.log(response)
// res.send(message);
// return;
// }
// response.requestOn = Date.now();
// //set token to session
// req.session.authInfo = response;
// //do the action
// if(action){
// action();
// }
// });
// };
// authObj.refreshToken = function(req,res,action) {
// var authenticationContext = new AuthenticationContext(authObj.authorityUrl);
// authenticationContext.acquireTokenWithRefreshToken(req.session.authInfo.refreshToken, authObj.clientId, authObj.secret, authObj.resource, function(refreshErr, refreshResponse) {
// if (refreshErr) {
// var message = 'refreshError: ' + refreshErr.message;
// res.send(message);
// return;
// }
// refreshResponse.requestOn = Date.now();
// //set token to session
// req.session.authInfo = refreshResponse;
// //do the action
// if(action){
// action();
// }
// });
// };
// function authWithAzureAD(res){
// crypto.randomBytes(48, function(ex, buf) {
// var token = buf.toString('base64').replace(/\//g,'_').replace(/\+/g,'-');
// res.cookie('authstate', token);
// var authorizationUrl = createAuthorizationUrl(token);
// console.log(authorizationUrl);
// res.redirect(authorizationUrl);
// });
// }
// function isAuthored(req){
// return req.session.authInfo;
// }
// function isExpire(req){
// var now = Date.now();
// var requestOn = req.session.authInfo.requestOn;
// var expiresIn = req.session.authInfo.expiresIn * 1000;
// return requestOn + expiresIn >= Date.now();
// }
// function createAuthorizationUrl(state) {
// var authorizationUrl = authObj.templateAuthzUrl.replace('<client_id>', authObj.clientId)
// .replace('<redirect_uri>',authObj.redirectUri)
// .replace('<state>', state)
// .replace('<resource>', authObj.resource);
// return authorizationUrl;
// }
// return authObj;
// }
// };