-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Authorizations and Access Control to hide API methods and check access #14
Closed
Closed
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
9ef2681
Added authorizations to route definition, access control, route filte…
dc8cce2
Updated docs to include new authorizations functionality
6530242
Added authorizations to route definition sample
a17e344
Move req.params.api_key usage from swagger-doc to main application ac…
c76552b
Resolved issues reported by Travis
0873d45
Resolved issues reported by Travis
fcb9432
Resolved issues reported by Travis
f01bf9a
Remove inline require, add tests
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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 |
---|---|---|
|
@@ -5,12 +5,108 @@ node-restify-swagger | |
[![Coverage Status](https://coveralls.io/repos/z0mt3c/node-restify-swagger/badge.png?branch=master)](https://coveralls.io/r/z0mt3c/node-restify-swagger?branch=master) | ||
[![Dependency Status](https://gemnasium.com/z0mt3c/node-restify-swagger.png)](https://gemnasium.com/z0mt3c/node-restify-swagger) | ||
|
||
Swagger resource generation for [Restify](https://github.com/mcavage/node-restify). | ||
|
||
Install | ||
Uses [node-restify-validation](https://github.com/z0mt3c/node-restify-validation) for query, path, body validations. | ||
|
||
Includes an optional access control scheme which allows api methods to be hidden and access granted from main application. | ||
|
||
|
||
Installation | ||
------- | ||
|
||
Install the module. | ||
|
||
npm install node-restify-swagger | ||
|
||
|
||
Include a copy of [Swagger-UI](https://github.com/swagger-api/swagger-ui) at the root of your project. | ||
|
||
|
||
Demo project | ||
------- | ||
|
||
A simple demo project can be cloned from [node-restify-demo](https://github.com/z0mt3c/node-restify-demo). | ||
|
||
|
||
Sample App.js | ||
------- | ||
|
||
var restify = require('restify'); | ||
var restifyValidation = require('node-restify-validation'); | ||
var restifySwagger = require('node-restify-swagger'); | ||
|
||
var server = module.exports.server = restify.createServer(); | ||
server.use(restify.queryParser()); | ||
server.use(restify.bodyParser()); | ||
server.use(restifyValidation.validationPlugin({ errorsAsArray: false })); | ||
|
||
restifySwagger.configure(server, { | ||
info: { | ||
contact: '[email protected]', | ||
description: 'Description text', | ||
license: 'MIT', | ||
licenseUrl: 'http://opensource.org/licenses/MIT', | ||
termsOfServiceUrl: 'http://opensource.org/licenses/MIT', | ||
title: 'Node Restify Swagger Demo' | ||
}, | ||
apiDescriptions: { | ||
'get':'GET-Api Resourcen' | ||
} | ||
}); | ||
|
||
// accessControl function which will be used by the restifySwagger middleware | ||
// and resource generation functionality to authorize access to the methods (optional) | ||
function accessControl(req, res) { | ||
|
||
var api_key = ''; | ||
if (typeof req.params.api_key == 'string') { | ||
api_key = req.params.api_key; | ||
} | ||
|
||
// if api_key is valid then return a space seperated list of authorizations | ||
// the authorizations will be defined on the routes | ||
return ((api_key == '12345') ? 'public account' : ''); | ||
|
||
} | ||
|
||
// verify authentication and authorization (optional) | ||
restifySwagger.authorizeAccess(accessControl); | ||
|
||
// Test Controller | ||
server.get({url: '/get/:name', | ||
authorizations: 'account', | ||
swagger: { | ||
summary: 'My hello call description', | ||
notes: 'My hello call notes', | ||
nickname: 'sayHelloCall' | ||
}, | ||
validation: { | ||
name: { isRequired: true, isIn: ['foo', 'bar'], scope: 'path', description: 'Your unreal name' }, | ||
status: { isRequired: true, isIn: ['foo', 'bar'], scope: 'query', description: 'Are you foo or bar?' }, | ||
email: { isRequired: false, isEmail: true, scope: 'query', description: 'Your real email address' }, | ||
age: { isRequired: true, isInt: true, scope: 'query', description: 'Your age' }, | ||
accept: { isRequired: true, isIn: ['true', 'false'], scope: 'query', swaggerType: 'boolean', description: 'Are you foo or bar?' }, | ||
password: { isRequired: true, description: 'New password' }, | ||
passwordRepeat: { equalTo: 'password', description: 'Repeated password'} | ||
}}, function (req, res, next) { | ||
res.send(req.params); | ||
}); | ||
|
||
// Serve static swagger resources | ||
server.get(/^\/docs\/?.*/, restify.serveStatic({directory: './swagger-ui'})); | ||
server.get('/', function (req, res, next) { | ||
res.header('Location', '/docs/index.html'); | ||
res.send(302); | ||
return next(false); | ||
}); | ||
|
||
restifySwagger.loadRestifyRoutes(accessControl); | ||
|
||
// Start server | ||
server.listen(8001, function () { | ||
console.log('%s listening at %s', server.name, server.url); | ||
}); | ||
|
||
|
||
License | ||
------- | ||
|
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
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please don't require inline and remember restify is(was) a dev dependency.