hwi / HWIOAuthBundle

OAuth client integration for Symfony. Supports both OAuth1.0a and OAuth2.
MIT License
2.27k stars 797 forks source link

i18n problems - No resource owner with name 'check-twitter' or facebook #410

Closed Barno closed 4 years ago

Barno commented 11 years ago

hi, i have a problem with check-facebook and check-twitter

i use also this bundle:JMSI18nRoutingBundle and this https://gist.github.com/danvbe/4476697

my routes are:

it__RG__hwi_oauth_connect                 ANY      ANY    ANY  /it/login/
en__RG__hwi_oauth_connect                 ANY      ANY    ANY  /en/login/
it__RG__hwi_oauth_connect_service         ANY      ANY    ANY  /it/login/service/{service}
en__RG__hwi_oauth_connect_service         ANY      ANY    ANY  /en/login/service/{service}
it__RG__hwi_oauth_connect_registration    ANY      ANY    ANY  /it/login/registration/{key}
en__RG__hwi_oauth_connect_registration    ANY      ANY    ANY  /en/login/registration/{key}
it__RG__hwi_oauth_service_redirect        ANY      ANY    ANY  /it/login/{service}
en__RG__hwi_oauth_service_redirect        ANY      ANY    ANY  /en/login/{service}
it__RG__facebook_login                    ANY      ANY    ANY  /it/login/check-facebook
en__RG__facebook_login                    ANY      ANY    ANY  /en/login/check-facebook
it__RG__twitter_login                     ANY      ANY    ANY  /it/login/check-twitter
en__RG__twitter_login                     ANY      ANY    ANY  /en/login/check-twitter

/app/config/routing.yml

#HWIOAuthBundle routes
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

facebook_login:
    pattern: /login/check-facebook

twitter_login:
    pattern: /login/check-twitter

/app/config/config.yml

hwi_oauth:
    connect:
        account_connector: my_user_provider
    firewall_name: main
    fosub:
        username_iterations: 30
        properties:
            # these properties will be used/redefined later in the custom FOSUBUserProvider service.
            facebook: facebook_id

            twitter: twitter_id
    resource_owners:
        twitter:
            type:                twitter
            client_id:           %twitter.app_id%
            client_secret:       %twitter.app_secret%
        facebook:
            type:                facebook
            client_id:           %facebook.app_id%
            client_secret:       %facebook.app_secret%
            scope:               "email,user_birthday,xmpp_login"
            infos_url:           "https://graph.facebook.com/me?fields=id,name,email,gender,first_name,last_name,locale,birthday,username,picture.type(square)" #da Graph API EXPLORER
            paths:
                email:          email
                profilepicture: picture.data.url
            options:
                display: popup #dialog is optimized for popup window

/app/config/security.yml

   firewalls:
        secured_area:
            pattern:    ^/admin
            provider: in_memory
            stateless: true            
            anonymous: ~
            http_basic:
                realm: "Area demo protetta"      
        main:
            remember_me:
                key: 9798788412338
                lifetime: 31536000 # 365 days in seconds
                path: /
                domain: ~ # Defaults to the current dofailure_path from $_SERVER
                always_remember_me: true
                remember_me_parameter: _remember_me
            pattern: ^/
            form_login:
                provider: fos_userbundle
                csrf_provider: form.csrf_provider
                failure_path: fos_user_security_ripeti_login                 
                check_path: fos_user_security_check
                success_handler: my_user_provider        
            oauth:
                remember_me: true
                resource_owners:
                    facebook:      facebook_login
                    twitter:       twitter_login
                login_path:        fos_user_security_ripeti_login
                failure_path:      fos_user_security_ripeti_login

                success_handler: my_user_provider        

                oauth_user_provider:
                    #this is my custom user provider, created from FOSUBUserProvider - will manage the 
                    #automatic user registration on your site, with data from the provider (facebook. google, etc.)
                    service: my_user_provider

                path: fos_user_security_logout
                target: /
                invalidate_session: false # Provissorio per il 5.11
            anonymous:    true
        login:
            pattern:  ^/login$
            security: false

            remember_me:
                key: "%secret%"
                lifetime: 31536000 # 365 days in seconds
                path: /
                domain: ~ # Defaults to the current domain from $_SERVER

when i try to login with facebook or twitter i have this error:

No resource owner with name 'check-twitter'.

if i do this:

oauth:
                resource_owners:
                    facebook:        "/%locale%/login/check-facebook"
                    twitter:            "/%locale%/login/check-twitter"

works. if I am visiting my application in English (/en) redirects back with my /it (im italian)

/app/config/parameters.yml

parameters:
     ...
    locale: it
Barno commented 11 years ago

I found one solution, is not the Best Solution but works

in my homepage

HomepageController

public function indexAction()
{
    $request = $this->get('request');
    /**
     * Store it into session the user language
     */
    $sessionId  = $this->get("session");
    if($sessionId->get("lingua")==""){
        $this->get('session')->set('lingua', $request->getLocale());
    }
    return array();
}

in the other bundle of FOSUserBundle that have been overwritten

UserBundle/Controller/RegistrationController.php (for example)

public function customAction() {
    /**
     * Store it into session the user language
     */
    $sessionId  = $this->container->get("session");
    if($sessionId->get("lingua")==""){
        $this->container->get("session")->set('lingua',  $this->container->get('request')->getLocale());
    }

    return array(

    );
}

Then i create a service like: http://symfony.com/doc/current/cookbook/session/locale_sticky_session.html

services.xml

services:
    acme_locale.locale_listener:
        class: Acme\LocaleBundle\EventListener\LocaleListener
        arguments: ["%kernel.default_locale%",@session]
        tags:
            - { name: kernel.event_subscriber }

LocaleListener

namespace Acme\LocaleBundle\EventListener;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Session;
class SnLocaleListener implements EventSubscriberInterface
{
    private $defaultLocale;
    private $session;
    public function __construct($defaultLocale = 'it', $session)
    {
        $this->defaultLocale = $defaultLocale;
        $this->session = $session;
    }
    public function onKernelRequest(GetResponseEvent $event)
    {
        $request = $event->getRequest();
        if (!$request->hasPreviousSession()) {
            return;
        }
        //incapsula la lingua in sessione se esiste
        $locale_session = $this->session->get('lingua');
        if ($locale_session == "") {
            // prova a vedere se il locale sia stato impostato come parametro _locale di una rotta
            if ($locale = $request->attributes->get('_locale')) {
                $request->getSession()->set('_locale', $locale);
            } else {
                // se non trova un locale esplicito in questa richiesta, usa quello della sessione
                $request->setLocale($request->getSession()->get('_locale', $this->defaultLocale));
            }
        } else {
            $request->setLocale($request->getSession()->get('_locale', $locale_session));
            $request->getSession()->set('_locale', $locale_session);
            $request->attributes->set('_locale', $locale_session);
        }
    }
    public static function getSubscribedEvents()
    {
        return array(
            // deve essere registrato prima dell'ascoltatore predefinito di locale
            KernelEvents::REQUEST => array(array('onKernelRequest', 17)),
        );
    }
}

/app/config/security.yml

..
oauth:
                remember_me: true
                resource_owners:
                    facebook:           "/%locale%/login/check-facebook"
                    twitter:            "/%locale%/login/check-twitter"
                login_path:        fos_user_security_ripeti_login
                failure_path:      fos_user_security_ripeti_login
..

sorry for my english is not perfect .. This solution for me works, It will not be the best

stof commented 11 years ago

/%locale%/login/check-facebook is equivalent to writing /it/login/check-facebook, because %locale% will be replaced by the DIC parameter locale. If you want a more dynamic path (using the request locale), you need to use a route name in the config, not a path.

Barno commented 11 years ago

thanks for reply, if i use

 twitter_login
it__RG__twitter_login                     ANY      ANY    ANY  /it/login/check-twitter
en__RG__twitter_login                     ANY      ANY    ANY  /en/login/check-twitter

i have this error

No resource owner with name 'check-twitter'

after the redirect from twitter

MarkVasile commented 10 years ago

When using i18n, you can avoid getting the "no resource owner" error by disabling i18n on hwi routing:

#HWIOAuthBundle routes
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
facebook_login:
    pattern: /login/check-facebook
    options: { i18n: false }
google_login:
    pattern: /login/check-google
    options: { i18n: false }
twitter_login:
    pattern: /login/check-twitter
    options: { i18n: false }
XWB commented 4 years ago

^^