Skip to content

Commit

Permalink
support security validation for cookie auth
Browse files Browse the repository at this point in the history
  • Loading branch information
Carmine DiMascio committed Apr 25, 2020
1 parent 03ba8d6 commit b7d7afc
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 85 deletions.
7 changes: 5 additions & 2 deletions src/middlewares/openapi.security.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ class SecuritySchemes {
? defaultSecurityHandler
: null;

const promises = this.securities.map(async s => {
const promises = this.securities.map(async (s) => {
try {
if (Util.isEmptyObject(s)) {
// anonumous security
Expand Down Expand Up @@ -242,8 +242,11 @@ class AuthValidator {
if (!req.query[scheme.name]) {
throw Error(`query parameter '${scheme.name}' required`);
}
} else if (scheme.in === 'cookie') {
if (!req.cookies[scheme.name]) {
throw Error(`cookie '${scheme.name}' required`);
}
}
// TODO scheme in cookie

this.dissallowScopes();
}
Expand Down
50 changes: 32 additions & 18 deletions test/resources/security.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
openapi: '3.0.2'
openapi: "3.0.2"
info:
version: 1.0.0
title: requestBodies $ref
Expand All @@ -8,20 +8,20 @@ servers:
- url: /v1/

paths:
/no_security:
/no_security:
get:
responses:
'200':
"200":
description: OK

/api_key:
get:
security:
- ApiKeyAuth: []
responses:
'200':
"200":
description: OK
'401':
"401":
description: unauthorized

/api_key_or_anonymous:
Expand All @@ -31,41 +31,51 @@ paths:
- {}
- ApiKeyAuth: []
responses:
'200':
"200":
description: OK
'401':
"401":
description: unauthorized

# This api key with scopes should fail validation and return 500
# scopes are only allowed for oauth2 and openidconnect
/api_key_with_scopes:
get:
security:
- ApiKeyAuth: ['read', 'write']
- ApiKeyAuth: ["read", "write"]
responses:
'200':
"200":
description: OK
'401':
"401":
description: unauthorized

/bearer:
get:
security:
- BearerAuth: []
responses:
'200':
"200":
description: OK
'401':
"401":
description: unauthorized

/basic:
get:
security:
- BasicAuth: []
responses:
'200':
"200":
description: OK
'401':
"401":
description: unauthorized

/cookie_auth:
get:
security:
- CookieAuth: []
responses:
"200":
description: OK
"401":
description: unauthorized

/oauth2:
Expand All @@ -75,9 +85,9 @@ paths:
- scope1
- scope2
responses:
'200':
"200":
description: OK
'401':
"401":
description: unauthorized

/openid:
Expand All @@ -87,9 +97,9 @@ paths:
- scope1
- scope2
responses:
'200':
"200":
description: OK
'401':
"401":
description: unauthorized

components:
Expand All @@ -104,6 +114,10 @@ components:
type: apiKey
in: header
name: X-API-Key
CookieAuth:
type: apiKey
in: cookie
name: JSESSIONID # cookie name
OpenID:
type: openIdConnect
openIdConnectUrl: https://example.com/.well-known/openid-configuration
Expand Down
24 changes: 18 additions & 6 deletions test/security.defaults.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ describe('security.defaults', () => {
express
.Router()
.get(`/api_key`, (req, res) => res.json({ logged_in: true }))
.get(`/cookie_auth`, (req, res) => res.json({ logged_in: true }))
.get(`/bearer`, (req, res) => res.json({ logged_in: true }))
.get(`/basic`, (req, res) => res.json({ logged_in: true }))
.get('/no_security', (req, res) => res.json({ logged_in: true })),
Expand All @@ -29,15 +30,14 @@ describe('security.defaults', () => {
});

it('should return 200 if no security', async () =>
request(app)
.get(`${basePath}/no_security`)
.expect(200));
request(app).get(`${basePath}/no_security`).expect(200));

it('should skip validation, even if auth header is missing for basic auth', async () => {
return request(app)
.get(`${basePath}/basic`)
.expect(401)
.then(r => {
.then((r) => {
console.log(r.body);
expect(r.body)
.to.have.property('message')
.that.equals('Authorization header required');
Expand All @@ -47,10 +47,22 @@ describe('security.defaults', () => {
it('should skip security validation, even if auth header is missing for bearer auth', async () => {
return request(app)
.get(`${basePath}/bearer`)
.expect(401).then(r => {
.expect(401)
.then((r) => {
expect(r.body)
.to.have.property('message')
.that.equals('Authorization header required');
})
});
});

it('should return 401 if cookie auth property is missing', async () => {
return request(app)
.get(`${basePath}/cookie_auth`)
.expect(401)
.then((r) => {
expect(r.body)
.to.have.property('message')
.that.equals('cookie \'JSESSIONID\' required');
});
});
});
Loading

0 comments on commit b7d7afc

Please sign in to comment.