Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/access-context.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,10 @@ function AccessContext(context) {
this.addPrincipal(principalType, principalId, principalName);
}

var token = this.accessToken || {};
const token = this.accessToken;

if (token.userId != null) {
this.addPrincipal(Principal.USER, token.userId);
this.addPrincipal(token.principalType || Principal.USER, token.userId);
}
if (token.appId != null) {
this.addPrincipal(Principal.APPLICATION, token.appId);
Expand Down
56 changes: 53 additions & 3 deletions test/multiple-user-principal-types.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

'use strict';
var expect = require('./helpers/expect');
var request = require('supertest');
var loopback = require('../');
var ctx = require('../lib/access-context');
var extend = require('util')._extend;
Expand All @@ -28,7 +27,7 @@ describe('Multiple users with custom principalType', function() {
// create a local app object that does not share state with other tests
app = loopback({localRegistry: true, loadBuiltinModels: true});
app.set('_verifyAuthModelRelations', false);
app.set('remoting', {errorHandler: {debug: true, log: false}});
app.set('remoting', {errorHandler: false});
app.dataSource('db', {connector: 'memory'});

var userModelOptions = {
Expand Down Expand Up @@ -270,7 +269,7 @@ describe('Multiple users with custom principalType', function() {
}
});

describe('role model', function() {
describe('Role model', function() {
this.timeout(10000);

var RoleMapping, ACL, user;
Expand Down Expand Up @@ -717,6 +716,57 @@ describe('Multiple users with custom principalType', function() {
}
});

describe('authorization', () => {
beforeEach(givenProductModelAllowingOnlyUserRoleAccess);

it('allows users belonging to authorized role', () => {
logServerErrorsOtherThan(200, app);
debugger;
return userFromOneModel.createAccessToken()
.then(token => {
return supertest(app)
.get('/Products')
.set('Authorization', token.id)
.expect(200, []);
});
});

it('rejects other users', () => {
logServerErrorsOtherThan(401, app);
return userFromAnotherModel.createAccessToken()
.then(token => {
return supertest(app)
.get('/Products')
.set('Authorization', token.id)
.expect(401);
});
});

function givenProductModelAllowingOnlyUserRoleAccess() {
const Product = app.registry.createModel({
name: 'Product',
acls: [
{
'principalType': 'ROLE',
'principalId': '$everyone',
'permission': 'DENY',
},
{
'principalType': 'ROLE',
'principalId': userRole.name,
'permission': 'ALLOW',
},
],
});
app.model(Product, {dataSource: 'db'});

return userRole.principals.create({
principalType: OneUser.modelName,
principalId: userFromOneModel.id,
});
}
});

// helpers
function createUserModel(app, name, options) {
var model = app.registry.createModel(Object.assign({name: name}, options));
Expand Down