node-authorization-header
Authorization Header middleware for Express and Sails.js
Validates and extracts token
value from Authorization Header of a given type, e.g. Bearer
.
Install
$ npm install authorization-header --save
Overview
authorizationHeader(options, [callback])
options
type
The type of Authorization, e.g.Bearer
,Basic
,Digest
, etc.attachTo
Where the token value extracted will be attach to, defaults totoken
.compareTo
This options allows user to pass a value to compare against the extractedtoken
.
Usage in Express
Default behavior
const authorizationHeader = ; app;
Usage of type
and attachTo
options.
const authorizationHeader = ; app
Usage of compareTo
option.
app;
Usage in Sails.js
Default behavior
// Will return 401 HTTP status code if any errors occurred.// policies/authorizationHeader.jsmoduleexports = type: 'Digest' ;
Default behavior
// policies/authorizationHeader.jsmoduleexports = { if !err return ; return res;};
Error handling
Possible thrown errors
AuthorizationHeaderError
message | code |
---|---|
No Authorization header is present. | E_AUTHORIZATION_REQUIRED |
Formats should be Authorization: <type> <token> . |
E_AUTHORIZATION_INVALID_FORMAT |
Authorization of type <type> was expected. |
E_AUTHORIZATION_INVALID_TYPE |
Token provided is invalid. | E_AUTHORIZATION_INVALID_TOKEN |
Suppose E_AUTHORIZATION_INVALID_TYPE
error was thrown
app;
Test
$ npm test