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
/
restrict-access-handler.js
176 lines (171 loc) · 6.27 KB
/
restrict-access-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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/**
* MOST Web Framework
* A JavaScript Web Framework
* http://themost.io
* Created by Kyriakos Barbounakis<k.barbounakis@gmail.com> on 2015-03-12.
*
* Copyright (c) 2014, Kyriakos Barbounakis k.barbounakis@gmail.com
Anthi Oikonomou anthioikonomou@gmail.com
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of MOST Web Framework nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/**
* Created by Kyriakos on 25/9/2014.
*/
var web = require('./index'), url = require('url');
/**
* @class LocationSetting
* @constructor
*/
function LocationSetting() {
/**
* Gets or sets a string that represents the description of this object
* @type {string}
*/
this.description = null;
/**
* Gets or sets a string that represents the target path associated with access settings.
* @type {*}
*/
this.path = null;
/**
* Gets or sets a comma delimited string that represents the collection of users or groups where this access setting will be applied. A wildcard (*) may be used.
* @type {*}
*/
this.allow = null;
/**
* Gets or sets a string that represents the collection of users or groups where this access setting will be applied. A wildcard (*) may be used.
* @type {*}
*/
this.deny = null;
}
/**
* @class RestrictHandler
* @constructor
* @augments HttpHandler
*/
function RestrictHandler() {
//
}
/**
* Authenticates an HTTP request and sets user or anonymous identity.
* @param {HttpContext} context
* @param {Function} callback
*/
RestrictHandler.prototype.authorizeRequest = function (context, callback) {
try {
if (context.is('OPTIONS')) { return callback(); }
if (context.user.name=='anonymous')
{
RestrictHandler.prototype.isRestricted(context, function(err, result) {
if (err) {
web.common.log(err);
callback(new web.common.HttpUnauthorizedException('Access denied'));
}
else if (result) {
var er = new web.common.HttpUnauthorizedException();
context.application.errors.unauthorized(context,er,function(err) {
if (err) {
return callback(err);
}
context.response.end();
return callback(er);
});
}
else {
callback();
}
});
}
else {
callback();
}
}
catch (e) {
callback(e);
}
};
RestrictHandler.prototype.isNotRestricted = function(context, callback) {
try {
if (web.common.isNullOrUndefined(context)) {
return callback(new web.common.HttpBadRequest());
}
if (web.common.isNullOrUndefined(context.request)) {
return callback(new web.common.HttpBadRequest());
}
//ensure settings (and auth settings)
context.application.config.settings = context.application.config.settings || {};
/**
* @type {{loginPage:string=,locations:Array}|*}
*/
context.application.config.settings.auth = context.application.config.settings.auth || {};
//get login page, request url and locations
var loginPage = context.application.config.settings.auth.loginPage || '/login.html',
requestUrl = url.parse(context.request.url),
locations = context.application.config.settings.auth.locations || [];
if (requestUrl.pathname===loginPage) {
return callback(null, true);
}
for (var i = 0; i < locations.length; i++) {
/**
* @type {*|LocationSetting}
*/
var location = locations[i];
if (/\*$/.test(location.path)) {
//wildcard search /something/*
if ((requestUrl.pathname.indexOf(location.path.replace(/\*$/,''))==0) && (location.allow=='*')) {
return callback(null, true);
}
}
else {
if ((requestUrl.pathname===location.path) && (location.allow=='*')) {
return callback(null, true);
}
}
}
return callback(null, false);
}
catch(e) {
web.common.log(e);
return callback(null, false);
}
};
RestrictHandler.prototype.isRestricted = function(context, callback) {
RestrictHandler.prototype.isNotRestricted(context, function(err, result) {
if (err) { return callback(err); }
callback(null, !result);
});
};
/**
* Creates a new instance of AuthHandler class
* @returns {RestrictHandler}
*/
RestrictHandler.createInstance = function() {
return new RestrictHandler();
};
if (typeof exports !== 'undefined') {
module.exports.createInstance = RestrictHandler.createInstance;
/**
* @constructs {RestrictHandler}
*/
module.exports.RestrictHandler = RestrictHandler;
}