Search code examples
phpsymfonyoauthaccess-tokenhwioauthbundle

Symfony HWIOAuthBundle Accesstoken


I need to get access token on social network, example github. The logic is when my authentication user enter in profile he can put button and confirm account with social network and this situation I need access token access token oauth. I have standard registration and authentication and only I need this is get access token and set it in DB.

I install bundle write in kernel

"hwi/oauth-bundle": "0.4.*@dev"
new HWI\Bundle\OAuthBundle\HWIOAuthBundle(),

my config:

hwi_oauth:
connect:
  account_connector: app.provider.user_provider

firewall_name: secured_area

resource_owners:
    github:
        type:                github
        client_id:           "%github_app_id%"
        client_secret:       "%github_app_secret%"
        scope:              "user,public_repo"

and my routing:

hwi_oauth_redirect:
resource: "@HWIOAuthBundle/Resources/config/routing/redirect.xml"
prefix:   /connect

hwi_oauth_login:
resource: "@HWIOAuthBundle/Resources/config/routing/login.xml"
prefix:   /login

github_login:
path: /login/check-github

and routnig in bundle

hwi_oauth_security:
resource: "@HWIOAuthBundle/Resources/config/routing/login.xml"
prefix: /login

hwi_oauth_connect:
resource: "@HWIOAuthBundle/Resources/config/routing/connect.xml"
prefix: /login

hwi_oauth_redirect:
resource: "@HWIOAuthBundle/Resources/config/routing/redirect.xml"
prefix:   /login

and my security:

firewalls:
        secured_area:
        anonymous: ~
        oauth:
            resource_owners:
                github:          "/login/check-github"
            login_path:        /login
            use_forward:       false
            failure_path:      /login

            oauth_user_provider:
                service: app.provider.user_provider

and config for service:

<parameters>
        <parameter key="my_user_provider.class">Artel\ProfileBundle\Providers\UserProvider</parameter>
</parameters>

        <service id="app.provider.user_provider" class="%my_user_provider.class%">

        <argument type="collection">
            <argument key="github">githubId</argument>
        </argument>

        <call method="setGithubProvider">
            <argument type="service" id="geekhub.user.github_provider" />
        </call>

    </service>

class UserProvider implements OAuthAwareUserProviderInterface
{
/**
 * {@inheritDoc}
 */
public function connect(UserInterface $user, UserResponseInterface $response)
{
   //I think this I get access token in request
}

/**
 * {@inheritdoc}
 */
public function loadUserByOAuthUserResponse(UserResponseInterface $response)
{
}

and in template

 <a class="logo" href="{{ path('hwi_oauth_service_redirect', {'service' : 'github'}) }}">Gh</a>

but I have error

Unable to generate a URL for the named route "hwi_oauth_connect_service" as such route does not exist.

stack

in app/cache/dev/appDevUrlGenerator.php at line 259   -
public function generate($name, $parameters = array(), $referenceType = self::ABSOLUTE_PATH)
{
    if (!isset(self::$declaredRoutes[$name])) {
        throw new RouteNotFoundException(sprintf('Unable to generate a URL for the named route "%s" as such route does not exist.', $name));
    }
    list($variables, $defaults, $requirements, $tokens, $hostTokens, $requiredSchemes) = self::$declaredRoutes[$name];

How can I just get a token ?

SOLVED

when I add routkng in bundle routing I solved this problem

hwi_oauth_security:
resource: "@HWIOAuthBundle/Resources/config/routing/login.xml"
prefix: /login

hwi_oauth_connect:
resource: "@HWIOAuthBundle/Resources/config/routing/connect.xml"
prefix: /login

hwi_oauth_redirect:
resource: "@HWIOAuthBundle/Resources/config/routing/redirect.xml"
prefix:   /login

But now I have another when I connect to git hub I have enter in rout in hwi_oauth_connect_service action: connectServiceAction and have standard template, how to reload this controller and action and change template ?? Or how after service app.provider.user_provider make redirect in my routing?


Solution

  • A very good example of HWIOAuthBundle implementation with FOSUserBundle https://gist.github.com/danvbe/4476697

    EDIT

    HWIOAuthBundle is a bundle for OAuth which provides a great documentation for its configuration/usage.

    First, you have to set the firewall on which HWIOAuth will be used. It should be the same as your

    Then, you have to register your Symfony2 application on the social network where you would like to connect your users.
    When it's done, in the bundle configuration, add the social network with the credentials (application id + token) provided by the social network). See configuring resource owners doc.

    After that, to authenticate your users on the social network and get the access token, you have to connect your user provider to the HWIOAuthBundle.

    The first link show how make HWIOAuthBundle working with FOSUserBundle as user provider. You can easily adapt it to your needs by setting your own user provider and skip the step about FOSUB.

    There is a lot of examples such as HWIOAuthBundleByExample which show how use it with a basic user provider.
    Another good : Adding HWIOAuthBundle to your symfony2 project

    This bundle is primarily based on configuration, also I will not rewrite the bundle documentation here.

    EDIT2

    You must have the following in your routing :

    hwi_oauth_login:
        resource: "@HWIOAuthBundle/Resources/config/routing/login.xml"
        prefix:   /login
    hwi_oauth_redirect:
        resource: "@HWIOAuthBundle/Resources/config/routing/redirect.xml"
        prefix:   /connect
    hwi_oauth_connect:
        resource: "@HWIOAuthBundle/Resources/config/routing/connect.xml"
        prefix:   /connect
    # ... Add your other routes here