create lib for getting api key token from request
This commit is contained in:
parent
32986e3ebd
commit
963737d3fb
62
lib/cartodb/api/get_api_key_token_from_request.js
Normal file
62
lib/cartodb/api/get_api_key_token_from_request.js
Normal file
@ -0,0 +1,62 @@
|
||||
'use strict';
|
||||
|
||||
const basicAuth = require('basic-auth');
|
||||
|
||||
module.exports = function getApiKeyTokenFromRequest(req) {
|
||||
let apiKeyToken = null;
|
||||
|
||||
for (var getter of apiKeyGetters) {
|
||||
(apiKeyToken = getter(req));
|
||||
if (apiKeyTokenFound(apiKeyToken)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return apiKeyToken;
|
||||
};
|
||||
|
||||
//--------------------------------------------------------------------------------
|
||||
|
||||
const apiKeyGetters = [
|
||||
getApikeyTokenFromHeaderAuthorization,
|
||||
getApikeyTokenFromRequestQueryString,
|
||||
getApikeyTokenFromRequestBody,
|
||||
];
|
||||
|
||||
function getApikeyTokenFromHeaderAuthorization(req) {
|
||||
const credentials = basicAuth(req);
|
||||
|
||||
if (credentials) {
|
||||
return credentials.pass;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
function getApikeyTokenFromRequestQueryString(req) {
|
||||
if (req.query.api_key) {
|
||||
return req.query.api_key;
|
||||
}
|
||||
|
||||
if (req.query.map_key) {
|
||||
return req.query.map_key;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
function getApikeyTokenFromRequestBody(req) {
|
||||
if (req.body && req.body.api_key) {
|
||||
return req.body.api_key;
|
||||
}
|
||||
|
||||
if (req.body && req.body.map_key) {
|
||||
return req.body.map_key;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
function apiKeyTokenFound(apiKeyToken) {
|
||||
return !!apiKeyToken;
|
||||
}
|
Loading…
Reference in New Issue
Block a user