integreat-transporter-http
Version:
HTTP transporter for Integreat
69 lines • 2.24 kB
JavaScript
import { isObject } from '../utils/is.js';
const decode = (auth) => Buffer.from(auth, 'base64').toString('utf-8');
function extractTokenFromAction(action) {
const headers = action.payload.headers;
if (isObject(headers)) {
const header = headers.authorization;
if (typeof header === 'string') {
const [scheme, token] = header.split(/\s+/);
return [scheme, decode(token)];
}
}
return [];
}
function verifyBasicToken(token, key, secret) {
if (typeof key === 'string' && typeof secret === 'string') {
const expectedToken = `${key}:${secret}`;
return token === expectedToken;
}
return false;
}
const authenticator = {
id: 'http',
async authenticate(_options, _action) {
return { status: 'refused', error: 'Not implemented' };
},
isAuthenticated(_authentication, _options, _action) {
return false;
},
async validate(_authentication, options, action) {
if (action) {
const { type, key, secret } = options || {};
const [scheme, token] = extractTokenFromAction(action);
if (token) {
if (typeof type === 'string' && scheme !== type.trim()) {
return {
status: 'autherror',
error: 'Unsupported scheme',
reason: 'invalidauth',
};
}
if (verifyBasicToken(token, key, secret)) {
return { status: 'ok', access: { ident: { id: key } } };
}
else {
return {
status: 'autherror',
error: 'Invalid credentials',
reason: 'invalidauth',
};
}
}
}
return {
status: 'noaccess',
error: 'Authentication required',
reason: 'noauth',
};
},
authentication: {
asObject(_authentication) {
return {};
},
asHttpHeaders(_authentication) {
return {};
},
},
};
export default authenticator;
//# sourceMappingURL=index.js.map