2016-05-04 00:10:46 +00:00
|
|
|
// Copyright IBM Corp. 2014,2016. All Rights Reserved.
|
|
|
|
// Node module: loopback
|
|
|
|
// This file is licensed under the MIT License.
|
|
|
|
// License text available at https://opensource.org/licenses/MIT
|
|
|
|
|
2014-10-15 14:42:46 +00:00
|
|
|
var assert = require('assert');
|
2014-10-09 15:32:03 +00:00
|
|
|
var loopback = require('./loopback');
|
2013-12-17 02:12:13 +00:00
|
|
|
var debug = require('debug')('loopback:security:access-context');
|
2013-12-12 00:03:48 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Access context represents the context for a request to access protected
|
|
|
|
* resources
|
|
|
|
*
|
2013-12-20 01:49:47 +00:00
|
|
|
* @class
|
2014-04-23 22:20:18 +00:00
|
|
|
* @options {Object} context The context object
|
2013-12-12 00:03:48 +00:00
|
|
|
* @property {Principal[]} principals An array of principals
|
|
|
|
* @property {Function} model The model class
|
|
|
|
* @property {String} modelName The model name
|
|
|
|
* @property {String} modelId The model id
|
|
|
|
* @property {String} property The model property/method/relation name
|
|
|
|
* @property {String} method The model method to be invoked
|
|
|
|
* @property {String} accessType The access type
|
|
|
|
* @property {AccessToken} accessToken The access token
|
|
|
|
*
|
|
|
|
* @returns {AccessContext}
|
|
|
|
* @constructor
|
|
|
|
*/
|
|
|
|
function AccessContext(context) {
|
|
|
|
if (!(this instanceof AccessContext)) {
|
|
|
|
return new AccessContext(context);
|
|
|
|
}
|
|
|
|
context = context || {};
|
|
|
|
|
|
|
|
this.principals = context.principals || [];
|
|
|
|
var model = context.model;
|
|
|
|
model = ('string' === typeof model) ? loopback.getModel(model) : model;
|
|
|
|
this.model = model;
|
|
|
|
this.modelName = model && model.modelName;
|
|
|
|
|
|
|
|
this.modelId = context.id || context.modelId;
|
|
|
|
this.property = context.property || AccessContext.ALL;
|
|
|
|
|
|
|
|
this.method = context.method;
|
2014-05-31 02:29:30 +00:00
|
|
|
this.sharedMethod = context.sharedMethod;
|
|
|
|
this.sharedClass = this.sharedMethod && this.sharedMethod.sharedClass;
|
2014-10-30 20:49:47 +00:00
|
|
|
if (this.sharedMethod) {
|
2014-10-16 22:54:40 +00:00
|
|
|
this.methodNames = this.sharedMethod.aliases.concat([this.sharedMethod.name]);
|
2014-05-31 02:29:30 +00:00
|
|
|
} else {
|
|
|
|
this.methodNames = [];
|
|
|
|
}
|
2014-10-16 22:54:40 +00:00
|
|
|
|
2014-10-30 20:49:47 +00:00
|
|
|
if (this.sharedMethod) {
|
2014-10-16 22:54:40 +00:00
|
|
|
this.accessType = this.model._getAccessTypeForMethod(this.sharedMethod);
|
2014-06-02 20:41:14 +00:00
|
|
|
}
|
2014-10-16 22:54:40 +00:00
|
|
|
|
2013-12-12 00:03:48 +00:00
|
|
|
this.accessType = context.accessType || AccessContext.ALL;
|
2014-10-13 08:23:35 +00:00
|
|
|
assert(loopback.AccessToken,
|
|
|
|
'AccessToken model must be defined before AccessContext model');
|
|
|
|
this.accessToken = context.accessToken || loopback.AccessToken.ANONYMOUS;
|
2013-12-12 00:03:48 +00:00
|
|
|
|
|
|
|
var principalType = context.principalType || Principal.USER;
|
|
|
|
var principalId = context.principalId || undefined;
|
|
|
|
var principalName = context.principalName || undefined;
|
|
|
|
if (principalId) {
|
|
|
|
this.addPrincipal(principalType, principalId, principalName);
|
|
|
|
}
|
|
|
|
|
|
|
|
var token = this.accessToken || {};
|
|
|
|
|
|
|
|
if (token.userId) {
|
|
|
|
this.addPrincipal(Principal.USER, token.userId);
|
|
|
|
}
|
|
|
|
if (token.appId) {
|
|
|
|
this.addPrincipal(Principal.APPLICATION, token.appId);
|
|
|
|
}
|
2014-08-08 05:19:27 +00:00
|
|
|
this.remotingContext = context.remotingContext;
|
2013-12-12 00:03:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Define constant for the wildcard
|
|
|
|
AccessContext.ALL = '*';
|
|
|
|
|
|
|
|
// Define constants for access types
|
|
|
|
AccessContext.READ = 'READ'; // Read operation
|
2015-04-03 14:41:32 +00:00
|
|
|
AccessContext.REPLICATE = 'REPLICATE'; // Replicate (pull) changes
|
2013-12-12 00:03:48 +00:00
|
|
|
AccessContext.WRITE = 'WRITE'; // Write operation
|
|
|
|
AccessContext.EXECUTE = 'EXECUTE'; // Execute operation
|
|
|
|
|
|
|
|
AccessContext.DEFAULT = 'DEFAULT'; // Not specified
|
|
|
|
AccessContext.ALLOW = 'ALLOW'; // Allow
|
|
|
|
AccessContext.ALARM = 'ALARM'; // Warn - send an alarm
|
|
|
|
AccessContext.AUDIT = 'AUDIT'; // Audit - record the access
|
|
|
|
AccessContext.DENY = 'DENY'; // Deny
|
|
|
|
|
|
|
|
AccessContext.permissionOrder = {
|
|
|
|
DEFAULT: 0,
|
|
|
|
ALLOW: 1,
|
|
|
|
ALARM: 2,
|
|
|
|
AUDIT: 3,
|
|
|
|
DENY: 4
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a principal to the context
|
|
|
|
* @param {String} principalType The principal type
|
|
|
|
* @param {*} principalId The principal id
|
|
|
|
* @param {String} [principalName] The principal name
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
2014-10-30 20:49:47 +00:00
|
|
|
AccessContext.prototype.addPrincipal = function(principalType, principalId, principalName) {
|
2013-12-12 00:03:48 +00:00
|
|
|
var principal = new Principal(principalType, principalId, principalName);
|
|
|
|
for (var i = 0; i < this.principals.length; i++) {
|
|
|
|
var p = this.principals[i];
|
|
|
|
if (p.equals(principal)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.principals.push(principal);
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the user id
|
|
|
|
* @returns {*}
|
|
|
|
*/
|
|
|
|
AccessContext.prototype.getUserId = function() {
|
|
|
|
for (var i = 0; i < this.principals.length; i++) {
|
|
|
|
var p = this.principals[i];
|
|
|
|
if (p.type === Principal.USER) {
|
|
|
|
return p.id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the application id
|
|
|
|
* @returns {*}
|
|
|
|
*/
|
|
|
|
AccessContext.prototype.getAppId = function() {
|
|
|
|
for (var i = 0; i < this.principals.length; i++) {
|
|
|
|
var p = this.principals[i];
|
|
|
|
if (p.type === Principal.APPLICATION) {
|
|
|
|
return p.id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the access context has authenticated principals
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
AccessContext.prototype.isAuthenticated = function() {
|
|
|
|
return !!(this.getUserId() || this.getAppId());
|
|
|
|
};
|
|
|
|
|
2014-11-04 00:26:33 +00:00
|
|
|
/*!
|
2013-12-17 02:12:13 +00:00
|
|
|
* Print debug info for access context.
|
|
|
|
*/
|
|
|
|
|
|
|
|
AccessContext.prototype.debug = function() {
|
2014-10-30 20:49:47 +00:00
|
|
|
if (debug.enabled) {
|
2013-12-17 02:12:13 +00:00
|
|
|
debug('---AccessContext---');
|
2014-10-30 20:49:47 +00:00
|
|
|
if (this.principals && this.principals.length) {
|
2014-10-16 22:54:40 +00:00
|
|
|
debug('principals:');
|
2013-12-17 02:12:13 +00:00
|
|
|
this.principals.forEach(function(principal) {
|
2014-10-16 22:54:40 +00:00
|
|
|
debug('principal: %j', principal);
|
2013-12-17 02:12:13 +00:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
debug('principals: %j', this.principals);
|
|
|
|
}
|
|
|
|
debug('modelName %s', this.modelName);
|
|
|
|
debug('modelId %s', this.modelId);
|
|
|
|
debug('property %s', this.property);
|
|
|
|
debug('method %s', this.method);
|
|
|
|
debug('accessType %s', this.accessType);
|
2014-10-30 20:49:47 +00:00
|
|
|
if (this.accessToken) {
|
2014-10-16 22:54:40 +00:00
|
|
|
debug('accessToken:');
|
2013-12-17 02:12:13 +00:00
|
|
|
debug(' id %j', this.accessToken.id);
|
|
|
|
debug(' ttl %j', this.accessToken.ttl);
|
|
|
|
}
|
|
|
|
debug('getUserId() %s', this.getUserId());
|
|
|
|
debug('isAuthenticated() %s', this.isAuthenticated());
|
|
|
|
}
|
2014-10-16 22:54:40 +00:00
|
|
|
};
|
2013-12-17 02:12:13 +00:00
|
|
|
|
2013-12-12 00:03:48 +00:00
|
|
|
/**
|
|
|
|
* This class represents the abstract notion of a principal, which can be used
|
|
|
|
* to represent any entity, such as an individual, a corporation, and a login id
|
|
|
|
* @param {String} type The principal type
|
|
|
|
* @param {*} id The princiapl id
|
|
|
|
* @param {String} [name] The principal name
|
|
|
|
* @returns {Principal}
|
2013-12-20 01:49:47 +00:00
|
|
|
* @class
|
2013-12-12 00:03:48 +00:00
|
|
|
*/
|
|
|
|
function Principal(type, id, name) {
|
|
|
|
if (!(this instanceof Principal)) {
|
|
|
|
return new Principal(type, id, name);
|
|
|
|
}
|
|
|
|
this.type = type;
|
|
|
|
this.id = id;
|
|
|
|
this.name = name;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Define constants for principal types
|
|
|
|
Principal.USER = 'USER';
|
|
|
|
Principal.APP = Principal.APPLICATION = 'APP';
|
|
|
|
Principal.ROLE = 'ROLE';
|
|
|
|
Principal.SCOPE = 'SCOPE';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compare if two principals are equal
|
2014-03-14 00:25:21 +00:00
|
|
|
* Returns true if argument principal is equal to this principal.
|
2014-10-30 20:49:47 +00:00
|
|
|
* @param {Object} p The other principal
|
2013-12-12 00:03:48 +00:00
|
|
|
*/
|
2014-10-30 20:49:47 +00:00
|
|
|
Principal.prototype.equals = function(p) {
|
2013-12-12 00:03:48 +00:00
|
|
|
if (p instanceof Principal) {
|
|
|
|
return this.type === p.type && String(this.id) === String(p.id);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2014-03-14 00:25:21 +00:00
|
|
|
* A request to access protected resources.
|
2013-12-12 00:03:48 +00:00
|
|
|
* @param {String} model The model name
|
|
|
|
* @param {String} property
|
|
|
|
* @param {String} accessType The access type
|
2014-03-14 00:25:21 +00:00
|
|
|
* @param {String} permission The requested permission
|
2013-12-12 00:03:48 +00:00
|
|
|
* @returns {AccessRequest}
|
2013-12-20 01:49:47 +00:00
|
|
|
* @class
|
2013-12-12 00:03:48 +00:00
|
|
|
*/
|
2014-05-31 02:29:30 +00:00
|
|
|
function AccessRequest(model, property, accessType, permission, methodNames) {
|
2013-12-12 00:03:48 +00:00
|
|
|
if (!(this instanceof AccessRequest)) {
|
|
|
|
return new AccessRequest(model, property, accessType);
|
|
|
|
}
|
2014-03-19 22:09:20 +00:00
|
|
|
if (arguments.length === 1 && typeof model === 'object') {
|
|
|
|
// The argument is an object that contains all required properties
|
|
|
|
var obj = model || {};
|
|
|
|
this.model = obj.model || AccessContext.ALL;
|
|
|
|
this.property = obj.property || AccessContext.ALL;
|
|
|
|
this.accessType = obj.accessType || AccessContext.ALL;
|
|
|
|
this.permission = obj.permission || AccessContext.DEFAULT;
|
2014-05-31 02:29:30 +00:00
|
|
|
this.methodNames = methodNames || [];
|
2014-03-19 22:09:20 +00:00
|
|
|
} else {
|
|
|
|
this.model = model || AccessContext.ALL;
|
|
|
|
this.property = property || AccessContext.ALL;
|
|
|
|
this.accessType = accessType || AccessContext.ALL;
|
|
|
|
this.permission = permission || AccessContext.DEFAULT;
|
2014-05-31 02:29:30 +00:00
|
|
|
this.methodNames = methodNames || [];
|
2013-12-17 02:12:13 +00:00
|
|
|
}
|
2013-12-12 00:03:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-06-02 20:41:14 +00:00
|
|
|
* Does the request contain any wildcards?
|
|
|
|
*
|
|
|
|
* @returns {Boolean}
|
2013-12-12 00:03:48 +00:00
|
|
|
*/
|
2014-10-30 20:49:47 +00:00
|
|
|
AccessRequest.prototype.isWildcard = function() {
|
2013-12-12 00:03:48 +00:00
|
|
|
return this.model === AccessContext.ALL ||
|
|
|
|
this.property === AccessContext.ALL ||
|
|
|
|
this.accessType === AccessContext.ALL;
|
|
|
|
};
|
|
|
|
|
2014-05-31 02:29:30 +00:00
|
|
|
/**
|
|
|
|
* Does the given `ACL` apply to this `AccessRequest`.
|
|
|
|
*
|
|
|
|
* @param {ACL} acl
|
|
|
|
*/
|
|
|
|
|
|
|
|
AccessRequest.prototype.exactlyMatches = function(acl) {
|
|
|
|
var matchesModel = acl.model === this.model;
|
|
|
|
var matchesProperty = acl.property === this.property;
|
|
|
|
var matchesMethodName = this.methodNames.indexOf(acl.property) !== -1;
|
2014-06-02 20:41:14 +00:00
|
|
|
var matchesAccessType = acl.accessType === this.accessType;
|
2014-05-31 02:29:30 +00:00
|
|
|
|
2014-10-30 20:49:47 +00:00
|
|
|
if (matchesModel && matchesAccessType) {
|
2014-05-31 02:29:30 +00:00
|
|
|
return matchesProperty || matchesMethodName;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2014-10-16 22:54:40 +00:00
|
|
|
};
|
2014-05-31 02:29:30 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Is the request for access allowed?
|
2014-10-16 22:54:40 +00:00
|
|
|
*
|
2014-05-31 02:29:30 +00:00
|
|
|
* @returns {Boolean}
|
|
|
|
*/
|
|
|
|
|
|
|
|
AccessRequest.prototype.isAllowed = function() {
|
2014-10-13 08:55:08 +00:00
|
|
|
return this.permission !== loopback.ACL.DENY;
|
2014-10-16 22:54:40 +00:00
|
|
|
};
|
2014-05-31 02:29:30 +00:00
|
|
|
|
|
|
|
AccessRequest.prototype.debug = function() {
|
2014-10-30 20:49:47 +00:00
|
|
|
if (debug.enabled) {
|
2014-05-31 02:29:30 +00:00
|
|
|
debug('---AccessRequest---');
|
|
|
|
debug(' model %s', this.model);
|
|
|
|
debug(' property %s', this.property);
|
|
|
|
debug(' accessType %s', this.accessType);
|
|
|
|
debug(' permission %s', this.permission);
|
|
|
|
debug(' isWildcard() %s', this.isWildcard());
|
|
|
|
debug(' isAllowed() %s', this.isAllowed());
|
|
|
|
}
|
2014-10-16 22:54:40 +00:00
|
|
|
};
|
2014-05-31 02:29:30 +00:00
|
|
|
|
2013-12-12 00:03:48 +00:00
|
|
|
module.exports.AccessContext = AccessContext;
|
|
|
|
module.exports.Principal = Principal;
|
|
|
|
module.exports.AccessRequest = AccessRequest;
|