This repository has been archived by the owner on Sep 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
auth-handler.js
157 lines (153 loc) · 4.96 KB
/
auth-handler.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/**
* MOST Web Framework
* A JavaScript Web Framework
* http://themost.io
*
* Copyright (c) 2014, Kyriakos Barbounakis k.barbounakis@gmail.com, Anthi Oikonomou anthioikonomou@gmail.com
*
* Released under the BSD3-Clause license
* Date: 2014-09-06
*/
/**
* @ignore
*/
var web = require('./index');
/**
* @class AuthHandler
* @constructor
*/
function AuthHandler() {
//
}
/**
* @param {IncomingMessage|ClientRequest} request
* @returns {*}
*/
AuthHandler.parseCookies = function(request) {
var list = {},
rc = request.headers.cookie;
rc && rc.split(';').forEach(function( cookie ) {
var parts = cookie.split('=');
list[parts.shift().trim()] = unescape(parts.join('='));
});
return list;
};
AuthHandler.ANONYMOUS_IDENTITY = { name: 'anonymous', authenticationType:'None' };
/**
* Authenticates an HTTP request and sets user or anonymous identity.
* @param {HttpContext} context
* @param {Function} callback
*/
AuthHandler.prototype.authenticateRequest = function (context, callback) {
try {
callback = callback || function() {};
var cookies = {}, model = context.model('User');
var settings = web.current.config.settings ? (web.current.config.settings.auth || { }) : { } ;
settings.name = settings.name || '.MAUTH';
if (context && context.request)
cookies = AuthHandler.parseCookies(context.request);
if (cookies[settings.name]) {
var str = null;
try {
str = web.current.decrypt(cookies[settings.name]);
}
catch (e) {
//log error (on bad cookie)
console.log(e);
}
//and continue
var userName = null;
if (str) {
var authCookie = JSON.parse(str);
//validate authentication cookie
if (authCookie.user)
userName = authCookie.user;
}
if (typeof model === 'undefined' || model === null) {
//no authentication provider is defined
context.user = { name: userName || 'anonymous', authenticationType:'Basic' };
callback(null);
return;
}
//search for user
if (userName) {
//todo::validate that user exists
//set user identity
context.user = model.convert({ name: userName, authenticationType:'Basic' });
callback(null);
}
else {
//an auth cookie was found but user data or user model does not exist
//set anonymous identity
context.user = model.convert(AuthHandler.ANONYMOUS_IDENTITY);
callback(null);
}
}
else {
//set anonymous identity
if (model)
context.user = model.convert(AuthHandler.ANONYMOUS_IDENTITY);
else
context.user = AuthHandler.ANONYMOUS_IDENTITY;
//no auth cookie was found on request
callback(null);
}
}
catch (e) {
callback(e);
}
};
/**
* @param {{context: HttpContext, target: HttpResult}} args
* @param callback
*/
AuthHandler.prototype.preExecuteResult = function (args, callback) {
try {
callback = callback || function() {};
var context = args.context, model = context.model('User');
if (typeof model === 'undefined' || model === null) {
callback();
return;
}
var authenticationType = context.user.authenticationType;
model.where('name').equal(context.user.name).expand('groups').silent().first(function(err, result) {
if (err) { return callback(err); }
if (result) {
//replace context.user with data object
context.user = model.convert(result);
context.user.authenticationType = authenticationType;
return callback();
}
else if (context.user.name!=='anonymous') {
model.where('name').equal('anonymous').expand('groups').silent().first(function(err, result) {
if (err) { return callback(err); }
if (result) {
context.user = model.convert(result);
context.user.authenticationType = authenticationType;
return callback();
}
else {
return callback();
}
});
}
else {
//do nothing
return callback();
}
});
}
catch (e) {
callback(e);
}
};
/**
* Creates a new instance of AuthHandler class
* @returns {AuthHandler}
*/
AuthHandler.createInstance = function() {
return new AuthHandler();
};
if (typeof exports !== 'undefined') {
module.exports.createInstance = AuthHandler.createInstance;
}