forked from hapi-swagger/hapi-swagger
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5820d63
commit fa17e18
Showing
32 changed files
with
5,579 additions
and
5,585 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,68 +1,70 @@ | ||
var Hapi = require('hapi'), | ||
Inert = require('inert'), | ||
Vision = require('vision'), | ||
Blipp = require('blipp'), | ||
H2o2 = require('h2o2'), | ||
BearerToken = require('hapi-auth-bearer-token'), | ||
HapiSwagger = require('../'), | ||
Pack = require('../package'), | ||
Routes = require('./routes'); | ||
'use strict'; | ||
|
||
var BearerToken = require('hapi-auth-bearer-token'), | ||
Blipp = require('blipp'), | ||
H2o2 = require('h2o2'), | ||
Hapi = require('hapi'), | ||
Inert = require('inert'), | ||
Vision = require('vision'); | ||
|
||
var HapiSwagger = require('../'), | ||
Pack = require('../package'), | ||
Routes = require('./routes'); | ||
|
||
|
||
var server = new Hapi.Server(); | ||
server.connection({ | ||
host: 'localhost', | ||
port: 3000 | ||
}); | ||
server.connection({ | ||
host: 'localhost', | ||
port: 3000 | ||
}); | ||
|
||
var swaggerOptions = { | ||
info: { | ||
'title': 'Test API Documentation', | ||
'description': 'This is a sample example of API documentation.', | ||
'version': Pack.version, | ||
'termsOfService': 'https://github.com/glennjones/hapi-swagger/', | ||
'contact': { | ||
'email': '[email protected]' | ||
}, | ||
'license': { | ||
'name': 'MIT', | ||
'url': 'https://raw.githubusercontent.com/glennjones/hapi-swagger/master/license.txt' | ||
} | ||
info: { | ||
'title': 'Test API Documentation', | ||
'description': 'This is a sample example of API documentation.', | ||
'version': Pack.version, | ||
'termsOfService': 'https://github.com/glennjones/hapi-swagger/', | ||
'contact': { | ||
'email': '[email protected]' | ||
}, | ||
tags: [{ | ||
'name': 'store', | ||
'description': 'Storing a sum', | ||
'externalDocs': { | ||
'description': 'Find out more', | ||
'url': 'http://example.org' | ||
} | ||
},{ | ||
'name': 'sum', | ||
'description': 'API of sums', | ||
'externalDocs': { | ||
'description': 'Find out more', | ||
'url': 'http://example.org' | ||
'license': { | ||
'name': 'MIT', | ||
'url': 'https://raw.githubusercontent.com/glennjones/hapi-swagger/master/license.txt' | ||
} | ||
}, | ||
tags: [{ | ||
'name': 'store', | ||
'description': 'Storing a sum', | ||
'externalDocs': { | ||
'description': 'Find out more', | ||
'url': 'http://example.org' | ||
} | ||
}, { | ||
'name': 'sum', | ||
'description': 'API of sums', | ||
'externalDocs': { | ||
'description': 'Find out more', | ||
'url': 'http://example.org' | ||
} | ||
}], | ||
securityDefinitions: { | ||
'petstore_auth': { | ||
'type': 'oauth2', | ||
'authorizationUrl': 'http://petstore.swagger.io/api/oauth/dialog', | ||
'flow': 'implicit', | ||
'scopes': { | ||
'write:pets': 'modify pets in your account', | ||
'read:pets': 'read your pets' | ||
} | ||
}], | ||
securityDefinitions: { | ||
'petstore_auth': { | ||
'type': 'oauth2', | ||
'authorizationUrl': 'http://petstore.swagger.io/api/oauth/dialog', | ||
'flow': 'implicit', | ||
'scopes': { | ||
'write:pets': 'modify pets in your account', | ||
'read:pets': 'read your pets' | ||
} | ||
}, | ||
'api_key': { | ||
'type': 'apiKey', | ||
'name': 'api_key', | ||
'in': 'header' | ||
} | ||
}, | ||
security: [{'api_key': []}] | ||
}; | ||
|
||
|
||
}, | ||
'api_key': { | ||
'type': 'apiKey', | ||
'name': 'api_key', | ||
'in': 'header' | ||
} | ||
}, | ||
security: [{ 'api_key': [] }] | ||
}; | ||
|
||
|
||
server.register([ | ||
|
@@ -75,32 +77,39 @@ server.register([ | |
register: HapiSwagger, | ||
options: swaggerOptions | ||
}], function (err) { | ||
|
||
server.auth.strategy('bearer', 'bearer-access-token', { | ||
'accessTokenName': 'access_token', | ||
'validateFunc': validateBearer | ||
}); | ||
server.start(function(){ | ||
server.start(function () { | ||
|
||
console.log('server running at:', server.info.uri); | ||
}); | ||
}); | ||
|
||
server.route(Routes); | ||
|
||
|
||
// validation function for bearer strategy | ||
function validateBearer(token, callback) { | ||
if(token === '12345'){ | ||
/** | ||
* validation function for bearer strategy | ||
* | ||
* @param {Object} token | ||
* @param {Function} callback | ||
*/ | ||
var validateBearer = function (token, callback) { | ||
|
||
if (token === '12345') { | ||
callback(null, true, { | ||
token: token, | ||
user: { | ||
username: 'glennjones', | ||
name: 'Glenn Jones', | ||
groups: ['admin','user'] | ||
groups: ['admin', 'user'] | ||
} | ||
}); | ||
}else{ | ||
} else { | ||
// for bad token keep err as null | ||
callback(null, false, {}); | ||
callback(null, false, {}); | ||
} | ||
} | ||
|
||
}; |
Oops, something went wrong.