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
Hapi multiple auth strategy not working as expected #3444
Comments
You can't have two strategies using the same header scheme. You have a single incoming authorization credential that can match one of two sources (client, admin). You need to construct one strategy that checks one, then the other. hapi has no way of knowing why the first one failed (e.g. it was the wrong kind of credentials). |
@hueniverse How would one go about doing this? Does this require a custom scheme as well? My issue is that I'd like to use two strategies on a route that both look at the |
You would need to write a new scheme that uses the auth |
For anyone who ends up here, this is a draft for how the use case of an ambiguous bearer token can be implemented: /* combinedBearerTokenAuthScheme.js */
const Boom = require('boom');
const headerPattern = /^Bearer (.+)/;
const testAuth = (request, strategy) => new Promise((resolve, reject) => {
request.server.auth.test(strategy, request, (error, credentials) => {
if (error === null) {
resolve(credentials);
} else {
reject(error);
}
});
});
/*
* Derive auth strategy to be used from the bearer token.
*/
const strategyForToken = (token) => {
let strategy = false;
if (conditionToDetectFirstStrategy) {
strategy = 'firstStrategyHere';
} else if (conditionToDetectSecondStrategy) {
strategy = 'secondStrategyHere';
}
return strategy;
};
/*
* Extract bearer token from request and identify which auth strategy it is meant for.
*/
const authenticate = async (request, reply) => {
// Reject missing or invalid header.
const header = request.headers.authorization;
if (!header || !headerPattern.test(header)) {
return reply(Boom.badRequest('Missing or invalid authorization header'), {}, {});
}
const token = header.match(headerPattern)[1];
const strategy = strategyForToken(token);
if (!strategy) {
return reply(Boom.badRequest('Unable to determine auth strategy'), {}, {});
}
try {
const credentials = await testAuth(request, strategy);
// Remember which strategy was successfully tested.
const artifacts = { actualStrategy: strategy };
return reply.continue({ credentials, artifacts });
} catch (error) {
return reply(error, {}, {});
}
};
module.exports = {
scheme: () => ({ authenticate })
}; |
Simple great solution for this behavior -> https://github.com/cjihrig/nuisance |
How did you implemented?
Then, I use it in my route service and gave that error. |
@CelsoEspinoza You'd need to register the |
This thread has been automatically locked due to inactivity. Please open a new issue for related bugs or questions following the new issue template instructions. |
Explanation
I'm trying to implement a route with multiple auth strategy, and not working as expected, and i think this is a bug.
This is how i register strategies
And this is how i configured the route
The results
When i request the endpoint
/midias
with theAuthorization
header with a VALIDtoken-client
, the server returns200 OK
as expected. BUT when i request the same endpoint with a VALIDtoken-adm
, the server returns401 Unauthorized
as not expected.So.. i realized what the auth strategy is validating only the first strategy on array... because when i change the order.. putting the
token-adm
to first on array, the request withtoken-adm
returns200 OK
as expected, andtoken-client
returns401 Unauthorized
as not expected.To confirm if the
token-client
andtoken-adm
is valid, i configured the route with unique strategy at a time, and request the endpoint at a time, and the requests returns200 OK
as expected when token is valid, and401 Unauthorized
as expected when token is invalid.My expectation
My expectation is when i request a endpoint with multiple strategy, the mechanism try to validate the auth at least with one strategy, and not only the first on array.
Context
The text was updated successfully, but these errors were encountered: