Monday, May 15, 2017

OAuth 2.0 Learn


Everyone nowadays deal with internet.By going through it you have to create some accounts for some sites to access there resources. Finally you come up with lots of different accounts and passwords.On regarding that point i want to ask question from you...How you memorize these passwords?I know that it is very difficult work for you to do.

No worries,modern websites make use of the OAuth protocol [1] with the concepts of “Identity Federation” and “Delegated Authorization”. For example, the website you need to create an account may provide the facility to login with an existing account in a different Identity Provider such as Facebook, Twitter or LinkedIn so you don’t need to create a new account and also remember your credentials.

what i want to say is Facebook Apps......when you here that what comes to your mind,surely the post which you share couple weeks ago.ah ha what is about?prediction about you new job?or how your weeding dress looks like?mmmmm catch it?In this article you are going learn how to create such kind of app using OAth protocol.

First i need this below diagram to explain how this OAuth protocol happen.


 

 Registering the Client App in Facebook Developer Website


First step is to create an application in the developer account on Facebook. Visit https://developers.facebook.com/ and add a new application.

First got to Login button which in top right corner.


If it ask to provide password supply it register.



Go to "My Apps" on top right corner and select add new app.
This window will pop up. Enter required details and click "create App ID".






Once your app is created, associate “Facebook Login” with it.Under "Add product"




You will see this area. In here you have to provide redirect URL in valid OAuth redirect URLs.
Facebook will send all his responses to this URL.





In "settings" provide a App Domain and a Website URL. To provide a Website you have to click on "+ Platform" and then click on website.


In Dashboard you can see your app's App ID and App secret.



Now let's see how to use these values ( Redirection point URL, App ID, App secret ) to get resources from Facebook.


Obtain Authorization code from Facebook


For this we have to prepare the URL. This URL contains for elements.When we put these elements together all should be encoded using a URL encoding method. Parameter name, value and encoded value is given below.

1. response_type.
Code
Code

2. client_id
1322495584465273
1322495584465273

3. redirect_uri
http://localhost/toko/
http%3A%2F%2Flocalhost%2Ftoko%2F

4. scope
public_profile user_posts user_friends user_photos
public_profile%20user_posts%20user_friends%20user_photos

Ler's combine these values and make the URL.


Once you login, it will show the following popup. We call this as the “User Consent Page” in OAuth terminology. In there, it will show what are the resources from the user account that this external app would be able to access on behalf of you.


Since you are the owner of this App you don't have to worry about privacy. Click on continue.;
This page will appear.



This page appear because for real you don't have a project which supports http://localhost/toko/.
But check the URL. You can see authorization code is sent to you from Facebook. (highlighted)


http://localhost/toko/?code=AQA1Pq4xatyaqrGBiDiQTygen5i7U0wNFGqZVhWhmW0Q1yJH8C_P1muVw8luKm08dLt-ssYoaWWwwyEKXUMLebziVE6IRGP8szjzV7lqEWGilOcq0bsy3tU_SSpWsOQavxDHfV4ahjiR1i8zjvcJHoHpgh5UCZuGnqxkOPu9WYbguI0IeNZzj7SvmYKm9T1Wzu9lyQrsGAGu2LMUgIkr_A8V2_s9q_lPGLU67OKGw5XqAuURP6tqBCjHnoGoe-Kui0fu3TxuP9JZGzjZFAMmUKd-eAEcgDe_xiOPItCBzu9amjTLfR9rsI946TXxrK8fMl1xLDPcpOxW3qpoDI8QzHyV#_=_


Obtain access token

To obtain access token we have to have four parameters.
1. grant_type
Authorization_code

2. client_id
1322495584465273

3. redirect_uri
http://localhost/toko/

4. code
AQA1Pq4xatyaqrGBiDiQTygen5i7U0wNFGqZVhWhmW0Q1yJH8C_P1muVw8luKm08dLt-ssYoaWWwwyEKXUMLebziVE6IRGP8szjzV7lqEWGilOcq0bsy3tU_SSpWsOQavxDHfV4ahjiR1i8zjvcJHoHpgh5UCZuGnqxkOPu9WYbguI0IeNZzj7SvmYKm9T1Wzu9lyQrsGAGu2LMUgIkr_A8V2_s9q_lPGLU67OKGw5XqAuURP6tqBCjHnoGoe-Kui0fu3TxuP9JZGzjZFAMmUKd-eAEcgDe_xiOPItCBzu9amjTLfR9rsI946TXxrK8fMl1xLDPcpOxW3qpoDI8QzHyV#_=_


In the HTTP Headers, I need to add the Authorization header with the App credentials. For that I can prepare the value like this.

App ID = 183994178774345
App Secret = dc321ebea29283cd4092b6b476ccadbd

AppID:AppSecret = 183994178774345:dc321ebea29283cd4092b6b476ccadbd
Base64(AppID:AppSecret) = MTgzOTk0MTc4Nzc0MzQ1OmRjMzIxZWJlYTI5MjgzY2Q0MDkyYjZiNDc2Y2NhZGJk

So, I can add the header as following.

Authorization:MTMyMjQ5NTU4NDQ2NTI3MzpiYmVhNzUxY2EwMWZkNGUwMTQ2NjA3Y2RjOWRlZTI3ZQ==


                        


Now let's see how to implement an app which can retrieve these information and output to the user.
I used php to develop this app.
Github link - https://github.com/pamoda-perera/OAuth

You have to have Facebook SDK v5 for PHP. (You don't have to download this. I have added in it my project. Check folder "facebook" in my project folder)
https://developers.facebook.com/docs/reference/php
index.php






If your app does not have that permission provide this instead of $permissions = ['email']; in both i.php and index.php files

$permissions = array("email","user_friends");
Otherwise you will not get lover's name.



i.php


see finally ruining this on localhost/fb .You can come up with this page







Hope you all are got something from this,make sure to try this.Have a nice day..    :D


Thank you,
Pamoda P. Perera