Almost every one of us is using services of Google, Microsoft, Facebook, Apple etc., every day, either directly or indirectly. We see in most of the common websites have a feature to Sign up with Google, Facebook, Apple etc., Such feature is possible mainly because of having oAuth and OpenID Connect.
Before we jump in what oAuth is, it's important to understand what exactly means by authentication and authorization.
Authentication : Process of verifying who a user is. E.g : Logging into your email with username and password
Authorization : Process of verifying what access does the user has. E.g : Verify if the user has access to delete the files shared in Google Drive
Let's assume that you have a personal locker at the house and only you have the keys to open it. Everyone in the house (including you) has a separate key only to open the main door of the house. In this scenario, opening the main door of the house with the keys is like Authentication. Once you get into the house, only you're authorized to open the personal locker and no one else. It's called as authorization.
oAuth 2.0 is an Open Standard Authorization (shortened as oAuth) protocol used for authorization and OpenID connect is an identity layer on top of oAuth for authentication.
It's becoming quite common to see options like below when we sign-up or login to any of the sites which we use day-to-day. When there's such option it means that, behind the scenes, these applications are using oAuth 2.0 and OpenID Connect.
The oAuth authorization URL from Coursera to Google looks something like below,
https://authorization-server.com/auth?response_type=code &client_id=****
&redirect_uri=https://redirected-app.com/callback
&scope=calendarreadandwrite
&state=****
Explanation:
- response_type = code : Application is expected to receive the authorization code once the authorization is successful
- clientid : The client id is an identifier of the calling app (coursera in our case). We can get the client id when we register our app/website with Google to access Google services through oAuth
- redirecturi : One of the URL which the developer has registered while registering the app in Google. Normally authorization server would reject the request if the URL doesn't match.
- scope : It represents the accesses requested by the caller. E.g : read, write
- state : It is used to store request specific data. The authorization server should normally return back the state parameter as such in response.
Once you provide the consent, a temporary grant is given to Coursera for accessing the Google Calendar. The grant is provided my means of authorization code sent from Google to Coursera.
Sample Authorization Code : 4/3gEmN1LypvXC6VVEqGGV_V9zKQyuw8g923UhNF3XnPZvgssIrBz0HHs0yAW3-q-XxLrKQIy7GXwgt4XnE94
Once the caller has the authorization code (coursera in our case), it should call the oAuth server again with the authorization code to exchange it for access and refresh tokens.
Access Token : Used to access the resource server (Google calendar in our case). This is normally sent in http header. For security reasons, the lifetime of access token is very short.
Sample Access Token : 1//04iGQnny2BEr-CgYIRAAGAQSNwF-L9IrPyxZ8840SE0MnwZeOkqXW21NBgT4YTmadS1q8Smqg9SFYmiuBzEDXN9VoDZxU8q3M
Refresh Token : Used to get a new access token from oAuth server when the existing one is expired. Refresh token has a longer life time. This token cannot be used to access the resources in resource server directly (like access token).
Sample Refresh Token : ya29.a0AfH6SMB4i8c43VmjYhdQ3tTksrh3EUVa6GwmQJzzSs0aggvTul0rpVqXS8H-qBPUFEMyE31USSMdtB3KQI6Wek6quNFh_2jV7l1D5l4vlbDyEwxJ-B7GlXMDyTy8BJD92cq9EFWTJRg87w7T0AWU_dcoJC3gGZs
So whenever coursera needs to access the Google Calendar, it should send the access token in header along with the http request to Google Server.
The scenario explaining the expired access token and accessing the resource is like below,
Google also provides an easy platform to understand and play with oAuth 2.0. It helps us to understand how exactly an oAuth authorization works.
Google Playground URL : https://developers.google.com/oauthplayground/