Skip to content

Conversation

@sprain
Copy link
Contributor

@sprain sprain commented Feb 4, 2015

This is a draft for #147, feedback is welcome! Tests will be added once the approach is accepted (I am new to all this caching stuff).


everything below is obsolete

I added the functionality to determine whether a request is anonymous based on session, basic authentication or oAuth2.

In my Symfony2 app, to only use oAuth2, I would overwrite getDefaultSubscribers in my own cache class like this:

class AppCache extends EventDispatchingHttpCache
{
    protected function getDefaultSubscribers()
    {
        $options = $this->getOptions();
        $subscribers = array();
        $defaultSubscribersOption = isset($options['fos_default_subscribers']) ? $options['fos_default_subscribers'] : self::SUBSCRIBER_ALL;
        if ($defaultSubscribersOption & self::SUBSCRIBER_USER_CONTEXT) {
            $subscribers[] = new UserContextSubscriber(array('authorization' => array('oauth2')));
        }

        return $subscribers;
    }
}

@ddeboer
Copy link
Member

ddeboer commented Feb 4, 2015

Does it make sense to separate the different authentication mechanisms (OAuth2, HTTP) or should we just check generically on whether an Authorization header is present? The problem I see with differentiating between mechanisms is that we can keep adding them: now e.g. OAuth1 (people still use that) is missing.

@dbu
Copy link
Contributor

dbu commented Feb 4, 2015

i would vote for a simpler approach in the sense of what varnish is doing: just check if a Authorization header exists and if so consider the request not anonymous. no configuration, no options. this is the expected behaviour to me, and if i need to tweak that i would extend the subscriber class to change the behaviour

@dbu
Copy link
Contributor

dbu commented Feb 4, 2015

@sprain and thanks a lot for your contribution! i appreciate that.

@sprain
Copy link
Contributor Author

sprain commented Feb 4, 2015

Simplifying sounds good ;) Will look at it.

@sprain
Copy link
Contributor Author

sprain commented Feb 4, 2015

Ok, I have simplified this. Any feedbacks?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lets do if ($request->server->has('HTTP_AUTHORIZATION') ... rather than loop through all the headers.

looking at the ServerBag::getHeaders method, i have the impression that the safe thing to do would be to check for any of the headers AUTHORIZATION, HTTP_AUTHORIZATION or PHP_AUTH_USER and consider the request not anonymous if any of those headers is present.

@dbu
Copy link
Contributor

dbu commented Feb 5, 2015

can you please add some tests like https://github.com/FriendsOfSymfony/FOSHttpCache/blob/master/tests/Unit/SymfonyCache/UserContextSubscriberTest.php#L120 but with AUTHORIZATION headers instead of session cookie? the anonymous case is already covered, but we should have a test where we do have one of these headers and end up being not anonymous.

@sprain
Copy link
Contributor Author

sprain commented Feb 5, 2015

Updated, please have a look.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is this doing? and why not just pass true literally?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be honest: I have no idea. I just tried to adjust the very complicated existing test from the session approach.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Argument $catch can be removed.

@dbu
Copy link
Contributor

dbu commented Feb 6, 2015

coming good, thanks!
@ddeboer can you also have a look at the test and think whether this is exactly what should happen? and do you agree with removing the complete cookie header if there is no single session cookie?

@sprain sprain changed the title [wip] Added basic auth and oAuth2 in user context subscriber Added basic auth and oAuth2 in user context subscriber Feb 9, 2015
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CS: please take the last ) on the same line as the {, so that we have ) {

@ddeboer ddeboer changed the title Added basic auth and oAuth2 in user context subscriber Added authentication support to user context subscriber Feb 13, 2015
@ddeboer ddeboer changed the title Added authentication support to user context subscriber Add authentication support to user context subscriber Feb 13, 2015
@ddeboer
Copy link
Member

ddeboer commented Feb 13, 2015

@dbu Where is the cookie header removed completely?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need a mock here: just construct a Response object directly with the context header.

@ddeboer
Copy link
Member

ddeboer commented Feb 13, 2015

Added some comments on the test; otherwise it’s valid.

@sprain Can you update the test and fix the coding standard violation mentioned by @dbu so we can merge this?

@dbu
Copy link
Contributor

dbu commented Feb 13, 2015

@ddeboer unless i misread https://github.com/sprain/FOSHttpCache/blob/master/src/SymfonyCache/UserContextSubscriber.php#L226 severely, there never is a cookie header on the hash lookup request, unless we manually set it in that method where i commented (and then removed my comments because i thought it less confusing.)

@sprain
Copy link
Contributor Author

sprain commented Feb 13, 2015

@ddeboer Updated.

@dbu
Copy link
Contributor

dbu commented Feb 13, 2015

@sprain can you please add a cookie header in the test (with a cookie that is not a session cookie), and assert that there is no cookie header in the hash lookup? if the basic auth header is copied along, i fear the cookie header might actually be copied as well...

@sprain
Copy link
Contributor Author

sprain commented Feb 15, 2015

@dbu @ddeboer Done!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assertCount is shorter

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 for assertCount - its also more expressive when failing

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ddeboer done

@ddeboer
Copy link
Member

ddeboer commented Feb 15, 2015

@dbu Looks good to me now. Let’s merge?

@dbu
Copy link
Contributor

dbu commented Feb 16, 2015

looks good! so the cookie is not propagated. good we have a test that proves it.

now the last thing that is missing is the doc. @sprain can you please update https://github.com/FriendsOfSymfony/FOSHttpCache/blob/master/doc/symfony-cache-configuration.rst#user-context accordingly? right now its rather implicit that there is only session with cookies support - we should make it explicit that it can be both basic auth or a session cookie. think you manage that one? otherwise i can have a go at that.

@sprain
Copy link
Contributor Author

sprain commented Feb 16, 2015

@dbu Updated the docs. Please have a look at contents and format. I am not skilled with rst as I much more often use markdown.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/php/PHP/

@dbu
Copy link
Contributor

dbu commented Feb 16, 2015

looks good to me. the spellchecker complains about php - not sure if PHP will be recognized - otherwise we just add PHP to the list of extra words it should know about.

@sprain
Copy link
Contributor Author

sprain commented Feb 16, 2015

@dbu Updated to PHP.

@dbu dbu merged commit c283606 into FriendsOfSymfony:master Feb 16, 2015
@dbu
Copy link
Contributor

dbu commented Feb 16, 2015

i did a tiny cleanup on the unit test in 437e05c (after convincing myself that this really is all correct)

thanks a lot @sprain !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants