Setting up HWIOAuth Bundle
1. Add the HWI OAuth bundle to the project
Download the OAuthBundle from the official git repository and copy the contents to ProjectRoot/vendor/bundles/HWI/Bundle/OAuthBundle
2. Enable the bundle in AppKernel.php
1 2 3 4 5 6 7 8 9 10 11 |
|
HWI OAuth bundle needs Sensio Buzz bundle for proper functioning. So, add the buzz bundle to your project if it is not already added. You may download it from here. If it is not added, you may get an error message like this
1
|
|
To avoid this, copy the contents of buzz.zip atached along with this to PROJECT_ROOT/vendor
Also, add the namespace for the OAuth bundle and the Buzz bundle to autoload.php
1 2 3 4 |
|
Register the buzz bundle namespace before HWI bundle’s namespace to avoid this error
1
|
|
3. Import the routing file of HWI bundle to the projects routing.yml
1 2 3 |
|
4. Import the security_factory.xml to security section of security.yml
1 2 3 |
|
5.Add the resource owners in the config.yml. For this, we need to register our app in Google and Facebook and obtain the client_id and the client_secret. In addition to that, we have to provide a redirect URL while configuring the app. The response after authenticating the user by the service providers is sent to this URL.
For Google, this redirect URL is www.yourdomain.com/login/check-google. This address should be provide against the field labelled ‘Redirect URIs’. Please see the screenshot below.
For Facebook, once you register the app, click on ‘Add platform’ button and register your app as a web app. The redirect URL for Facebook is www.yourdomain.com/login/check-facebook. Enter this URL in ‘Site URL’ field and ‘Mobile Site URL ’ field under ‘Website’. Please see the screenshot below.
Then, provide the client_id and client_secret for Facebook and Google in the config.yml file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
6.Create a User Provider Service
The bundle needs a service that is able to load users based on the user response of the oauth endpoint and the service should implement
HWI\Bundle\OAuthBundle\Security\Core\User\OAuthAwareUserProviderInterface.
For this, first create a class caller XYZOAuthUserProvider .
1 2 3 4 5 6 7 8 9 10 11 12 |
|
Then, register it as a service in services.yml file
1 2 3 4 5 |
|
7. Modifications in the security.yml file
1. Under Providers, add the service created in the previous step
1 2 3 4 |
|
2. Under Firewalls/main, add
1 2 3 4 5 6 7 8 9 |
|
8. Import the login check routes corresponding to each resource owner in the routing.yml file
1 2 3 4 5 |
|
Checking whether the OAuth user exists in the local database
By default, a user who logs in through facebook or google is given ROLE_USER, and ROLE_OAUTH_USER. In this case, even though all the details required for a registered user are not available from Google or Facebook, the user still have all the privileges of ROLE_USER. This should not be the case. The user should not be granted all the provileges unless he completes his profile. So, for a user who logs in through Google or Facebook is given only ROLE_OAUTH_USER if he is not present in the database. If he is present in the database, he is granted ROLE_USER when he logs in through Google or Facebook. So, there should be a mechanism to check whether the OAUth user is present in the database or not.
Step 1
Edit vendor/bundles/HWI/Bundle/OAuthBundle/Security/Core/User/OAuthUser.php
and modify the getRoles() function to return only ROLE_OAUTH_USER as role.
Original function:
1 2 3 4 |
|
Modified function:
1 2 3 4 |
|
Step 2
Next, edit the OAuthUserProvider.php file
- Import the namespace of the bundle containing the User entity to OAuthUserProvider.php so that we can get the container inside OAuthUserProvider.php.
1
|
|
- In the original configuration loadUserByOAuthUserResponse() function calls the loadUserByUsername() function with nickname as the arguement. The nickname is obtained from the server response. In our entity user provider, we have defined email as a unique field. So, to check whether user exists in the database, we can use email. For this, we need to obtain the email from the response.
Although UserResponseInterface class has a getEmail() method, this won’t return the email address for all the service providers as the response for each service provider is different
A var_dump() for the response object for Facebook and Google are given below
Facebook:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
|
Google:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
|
From the variable dump, we can see that the email address can be obtained from the response by
1 2 |
|
`
- Edit loadUserByOAuthUserResponse() function as given below
1 2 3 4 5 6 7 8 9 |
|
3. To check whether the user exists in the database, edit loadUserByUsername($username) function as given below
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
4. Then, hide or show options in the twig file according to the role of the user.
1 2 3 4 5 6 7 8 9 |
|