-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
new file: lib/services/identity/controllers/identity-viewer-controller.js
- Loading branch information
Showing
1 changed file
with
62 additions
and
0 deletions.
There are no files selected for viewing
62 changes: 62 additions & 0 deletions
62
lib/services/identity/controllers/identity-viewer-controller.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
'use strict'; | ||
|
||
const _ = require('lodash'); | ||
const Boom = require('boom'); | ||
|
||
const Controller = require('../../../controllers/controller'); | ||
const IdentityItemController = require('./identity-item-controller'); | ||
|
||
class IdentityViewerController extends IdentityItemController { | ||
get(req, res, next) { | ||
const err = this.checkViewerAccess(req); | ||
if (err) { | ||
return next(err); | ||
} | ||
|
||
return super.get(req, res, next); | ||
} | ||
|
||
patch(req, res, next) { | ||
const err = this.checkViewerAccess(req); | ||
if (err) { | ||
return next(err); | ||
} | ||
|
||
return super.patch(req, res, next); | ||
} | ||
|
||
delete(req, res, next) { | ||
const err = this.checkViewerAccess(req); | ||
if (err) { | ||
return next(err); | ||
} | ||
|
||
return super.delete(req, res, next); | ||
} | ||
|
||
checkViewerAccess(req) { | ||
if (this.isAdminRequest(req)) { | ||
return null; | ||
} | ||
|
||
const viewerId = _.get(req, 'identity.viewer.id'); | ||
if (req.params.id !== viewerId) { | ||
return Boom.unauthorized('Viewer specified in JWT does not match requested viewer.'); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
static create(spec) { | ||
if (!spec.bus || !_.isObject(spec.bus)) { | ||
throw new Error('IdentityViewerController spec.bus is required'); | ||
} | ||
|
||
return Controller.create(new IdentityViewerController({ | ||
bus: spec.bus, | ||
type: 'viewer' | ||
})); | ||
} | ||
} | ||
|
||
module.exports = IdentityViewerController; |