Skip to content
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

Session scope does not match one to many auth.scope on route. #1875

Closed
poeticninja opened this issue Aug 21, 2014 · 2 comments
Closed

Session scope does not match one to many auth.scope on route. #1875

poeticninja opened this issue Aug 21, 2014 · 2 comments
Assignees
Labels
Milestone

Comments

@poeticninja
Copy link

@poeticninja poeticninja commented Aug 21, 2014

The session scope will not match the auth.scope if auth scope is an array and the session scope is more than one character.

Here are three test examples.

This works but we are only match one character.

// Success
    it('matches scope array item', function (done) {

        var server = new Hapi.Server();
        server.auth.scheme('custom', internals.implementation);
        server.auth.strategy('default', 'custom', true, { users: { steve: { scope: 'a' } } });
        server.route({
            method: 'GET',
            path: '/',
            config: {
                handler: function (request, reply) { reply(request.auth.credentials.user); },
                auth: {
                    scope: ['a', 'c']
                }
            }
        });

        server.inject({ url: '/', headers: { authorization: 'Custom steve' } }, function (res) {

            expect(res.statusCode).to.equal(200);
            done();
        });
    });

This works with user scope an array match and the match is more than one character.

// Success
    it('matches scope array item', function (done) {

        var server = new Hapi.Server();
        server.auth.scheme('custom', internals.implementation);
        server.auth.strategy('default', 'custom', true, { users: { steve: { scope: ['ab', 'cat'] } } });
        server.route({
            method: 'GET',
            path: '/',
            config: {
                handler: function (request, reply) { reply(request.auth.credentials.user); },
                auth: {
                    scope: ['ab', 'cat']
                }
            }
        });

        server.inject({ url: '/', headers: { authorization: 'Custom steve' } }, function (res) {

            expect(res.statusCode).to.equal(200);
            done();
        });
    });

This fails, the user scope is not an array and the match needs to be more than one character.

// Failed
    it('matches scope array item', function (done) {

        var server = new Hapi.Server();
        server.auth.scheme('custom', internals.implementation);
        server.auth.strategy('default', 'custom', true, { users: { steve: { scope: 'ab' } } });
        server.route({
            method: 'GET',
            path: '/',
            config: {
                handler: function (request, reply) { reply(request.auth.credentials.user); },
                auth: {
                    scope: ['ab', 'cat']
                }
            }
        });

        server.inject({ url: '/', headers: { authorization: 'Custom steve' } }, function (res) {

            expect(res.statusCode).to.equal(200);
            done();
        });
    });
@poeticninja
Copy link
Author

@poeticninja poeticninja commented Aug 21, 2014

Looks like Hapi is using the Hoek intersect method for comparing two arrays. But I do not believe that is the intended goal. It should just do a contains. Possibly like lodash.contains? This would let us know if the item is in the array. Maybe in Hoek we add that method? if we add that method it would make the scope check only one or statement instead of two.

I am talking about lines 265-267 in file auth.js

Loading

@hueniverse hueniverse added the bug label Aug 21, 2014
@hueniverse hueniverse self-assigned this Aug 21, 2014
@hueniverse hueniverse added this to the 6.6.0 milestone Aug 21, 2014
@poeticninja
Copy link
Author

@poeticninja poeticninja commented Aug 21, 2014

Nice. Thanks!

Loading

@lock lock bot locked as resolved and limited conversation to collaborators Jan 12, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants