Skip to content
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

API Authentication mocks #19

Merged
merged 3 commits into from
Apr 27, 2021
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
26 changes: 26 additions & 0 deletions api/helpers/security-handlers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module.exports = {
appid: (req, authOrSecDef, scopesOrApiKey, cb) => {
const appid = req.headers[authOrSecDef.name];
if (appid !== 'social-safety-net') {
cb({
message: `Invalid header "${authOrSecDef.name}"`,
statusCode: 401,
});
}
else {
cb();
}
},
appkey: (req, authOrSecDef, scopesOrApiKey, cb) => {
const appkey = req.headers[authOrSecDef.name];
if (appkey !== 'alohomora') {
cb({
message: `Invalid header "${authOrSecDef.name}"`,
statusCode: 401,
});
}
else {
cb();
}
}
};
6 changes: 3 additions & 3 deletions api/swagger/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ paths:
lastName:
type: string
# $ref: '#/definitions/contact'
# security:
# - appid: []
# - appkey: []
security:
- appid: []
- appkey: []
responses:
'200':
description: Contact Response
Expand Down
7 changes: 2 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@
var SwaggerRestify = require('swagger-restify-mw');
var restify = require('restify');
var app = restify.createServer();
const securityHandlers = require('./api/helpers/security-handlers');

module.exports = app; // for testing

var config = {
appRoot: __dirname, // required config
swaggerSecurityHandlers: {
appid: function appidSecurityHandler(req, authOrSecDef, scopesOrApiKey, cb) {
cb();
}
}
swaggerSecurityHandlers: securityHandlers,
};

SwaggerRestify.create(config, function(err, swaggerRestify) {
Expand Down
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@
"swagger-restify-mw": "^0.7.0"
},
"devDependencies": {
"nodemon": "^2.0.7",
"should": "^7.1.0",
"supertest": "^1.0.0"
},
"scripts": {
"start": "node index.js",
"start": "nodemon index.js",
"test": "swagger project test"
},
"nodemonConfig": {
"ext": "js,json,yaml"
}
}
5 changes: 3 additions & 2 deletions test/api/controllers/hello_world.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
var should = require('should');
var request = require('supertest');
var server = require('../../../app');

var server = require('../../../index');
describe('controllers', function() {

describe('hello_world', function() {
Expand Down Expand Up @@ -46,3 +46,4 @@ describe('controllers', function() {
});

});
*/
149 changes: 149 additions & 0 deletions test/api/helpers/security-handlers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
const request = require('supertest');
const should = require('should');
const server = require('../../../index');
const securityHandlers = require('../../../api/helpers/security-handlers');

describe('securityHandlers', function() {
describe('appid', function() {
it('should pass authenticate', function(done) {
securityHandlers.appid(
{
headers: {
'x-appid': 'social-safety-net',
}
},
{
type: 'apikey',
in: 'header',
name: 'x-appid',
},
null,
(args) => {
if (args != null) {
throw new Error(`Authentication didn't succeed`);
}
done();
}
);
});

it('should fail authentication', function(done) {
securityHandlers.appid(
{
headers: {
'x-appid': null,
}
},
{
type: 'apikey',
in: 'header',
name: 'x-appid',
},
null,
(args) => {
if (args == null) {
throw new Error(`Authentication didn't fail`);
}
done();
}
);
});
});

describe('appkey', function() {
it('should pass authenticate', function(done) {
securityHandlers.appkey(
{
headers: {
'x-appkey': 'alohomora',
}
},
{
type: 'apikey',
in: 'header',
name: 'x-appkey',
},
null,
(args) => {
if (args != null) {
throw new Error(`Authentication didn't succeed`);
}
done();
}
);
});

it('should fail authentication', function(done) {
securityHandlers.appkey(
{
headers: {
'x-appkey': null,
}
},
{
type: 'apikey',
in: 'header',
name: 'x-appkey',
},
null,
(args) => {
if (args == null) {
throw new Error(`Authentication didn't fail`);
}
done();
}
);
});
});

describe('api test', function() {
it('should pass authenticate with appkey', function(done) {
request(server)
.post('/contacts')
.send({ firstName: 'Jane', lastName: 'Doe'})
.set('Content-Type', 'application/json')
.set('X-APPKEY', 'alohomora')
.expect('Content-Type', /json/)
.expect(200, done);
});

it('should pass authenticate with appid', function(done) {
request(server)
.post('/contacts')
.send({ firstName: 'Jane', lastName: 'Doe'})
.set('Content-Type', 'application/json')
.set('X-APPID', 'social-safety-net')
.expect('Content-Type', /json/)
.expect(200, done);
});

it('should fail authenticate with wrong appkey', function(done) {
request(server)
.post('/contacts')
.send({ firstName: 'Jane', lastName: 'Doe'})
.set('Content-Type', 'application/json')
.set('X-APPKEY', 'BAD-APPKEY')
.expect('Content-Type', /json/)
.expect(401, done);
});

it('should fail authenticate with wrong appid', function(done) {
request(server)
.post('/contacts')
.send({ firstName: 'Jane', lastName: 'Doe'})
.set('Content-Type', 'application/json')
.set('X-APPID', 'BAD-APPID')
.expect('Content-Type', /json/)
.expect(401, done);
});

it('should fail authenticate with no security headers passed', function(done) {
request(server)
.post('/contacts')
.send({ firstName: 'Jane', lastName: 'Doe'})
.set('Content-Type', 'application/json')
.expect('Content-Type', /json/)
.expect(401, done);
});
});
});