Download - OAuth 2.0 in depth

Transcript
Page 1: OAuth 2.0 in depth

OAuth 2.0 in Depth

By Rohit GhatolDirector @ Synerzip

Passionate about TechNext

Page 2: OAuth 2.0 in depth

Why study about OAuth?

Page 3: OAuth 2.0 in depth

Reference - http://rainbowseo.com/wp-content/uploads/2012/06/smm.png

Do you care about these or Similar Sites?

Page 4: OAuth 2.0 in depth

Browser

Mashups

Facebook

LinkedInFoursquare

Twitter

Http Access

Api Access

Page 5: OAuth 2.0 in depth

7155 APIs listed on http://ProgrammableWeb.com

Page 6: OAuth 2.0 in depth

390 APIs on http://ProgrammableWeb.com support OAuth

Page 7: OAuth 2.0 in depth

Security

Closed ClosedOpen

Authentication Authorization

Page 8: OAuth 2.0 in depth

OAuth In a Nut ShellCan I have your

Debit Card and ATM Pin?

Page 9: OAuth 2.0 in depth

OAuth In a Nut ShellCan I have your

Credit Card?

Page 10: OAuth 2.0 in depth

OAuth Practical Example

Page 11: OAuth 2.0 in depth

Without OAuth

Page 12: OAuth 2.0 in depth

Without OAuth

Page 13: OAuth 2.0 in depth

Without OAuth

Page 14: OAuth 2.0 in depth

Lets Start Again

Page 15: OAuth 2.0 in depth

With OAuth

Page 16: OAuth 2.0 in depth

With OAuth

Page 17: OAuth 2.0 in depth

With OAuth

Page 18: OAuth 2.0 in depth

With OAuth

Page 19: OAuth 2.0 in depth

Lets get Technical

Page 20: OAuth 2.0 in depth

Why OAuth is required?

• What are limitations with Passwords?– Trust – User not trusting– More Access than required– No Support for Granular permissions– Phishing – Helping Phishing activities– Lower Reliability to API interfaces– Unable to revoke access once provided

Page 21: OAuth 2.0 in depth

Why OAuth is required?

• OAuth is required for Delegating Access– To Certain Party– For Certain Resource– For Limited Time– Which can be selectively be revoked

Page 22: OAuth 2.0 in depth

Understand OAuth Roles

Page 23: OAuth 2.0 in depth

Roles

• Resource Owner – e.g Picasa User• Resource Server – e.g Picasa Hosting• Client – e.g Mashup built for Picasa• Authorization Server – e.g Google Auth Server

Page 24: OAuth 2.0 in depth

Resource Owner

• An entity capable of granting access to a protected resource.

• When the resource owner is a person, it is referred to as an end-user

Page 25: OAuth 2.0 in depth

Resource Server

• The server hosting the protected resources, capable of accepting and responding to protected resource requests using access tokens.

Page 26: OAuth 2.0 in depth

Client

• An application making protected resource requests on behalf of the resource owner and with its authorization.

Page 27: OAuth 2.0 in depth

Authorization Server

• The server issuing access tokens to the client after successfully authenticating the resource owner and obtaining authorization.

Page 28: OAuth 2.0 in depth

Client

Resource Owner

Authorization Server

Resource Server

Authorization Request

Authorization Grant

Authorization Grant

Access Token

Access Token

Protected Resource

Protocol Flow

Page 29: OAuth 2.0 in depth

Pre Requisite for OAuth

• Register Application with OAuth Provider– Facebook

• https://developers.facebook.com/apps

– Google• https://code.google.com/apis/console/b/0/?pli=1#

access

– Twitter• https://dev.twitter.com/apps/new

– LinkedIn• https://www.linkedin.com/secure/developer?newapp=

Page 30: OAuth 2.0 in depth
Page 31: OAuth 2.0 in depth
Page 32: OAuth 2.0 in depth
Page 33: OAuth 2.0 in depth
Page 34: OAuth 2.0 in depth
Page 35: OAuth 2.0 in depth

Pre Requisite for OAuth

• This will give you two essential things– client_id– client_secret– Redirect_uri

Page 36: OAuth 2.0 in depth

Tools used to try OAuth

Page 37: OAuth 2.0 in depth

Different ways to Do OAuth

Page 38: OAuth 2.0 in depth

Grant Types

• Authorization Code Grant• Implicit Grant• Resource Owner Password Credentials Grant• Client Credentials Grant

Page 39: OAuth 2.0 in depth

Client

Resource Owner

Authorization Server

Resource Server

Authorization Request

Authorization Grant

Authorization Grant

Access Token

Access Token

Protected Resource

Protocol Flow

Page 40: OAuth 2.0 in depth

ClientAuthorization

ServerResource

Server

Access Grant & Client Credentials

Access Token & Refresh Token

Access Token

Protected Resource

Protocol Flow

Access Token

Invalid Token Error

Refresh Token & Client Credentials

Access Token & Optional Refresh Token

Page 41: OAuth 2.0 in depth

Live Example Facebook

Page 42: OAuth 2.0 in depth

Step 1 – Get Authorization Code

Page 43: OAuth 2.0 in depth

Understanding the URL

• client_id – The ID of the Client App• redirect_uri – Where to go back after OAuth• scope – Permissions allowed by User• state – Something to pass back to redirect_uri

Page 44: OAuth 2.0 in depth
Page 45: OAuth 2.0 in depth

State which we sent

Page 46: OAuth 2.0 in depth

Authorization Grant Code which needs to be exchanged for Access

Token

Page 47: OAuth 2.0 in depth

Step 2 – Exchange to get Access Code

Page 48: OAuth 2.0 in depth

Important Note

• This Step is to performed at the Server Side. • Why?• Because you need to use your Client ID and

Client Secret along side the Authorization Code you just received to gain an Access Code

• Access Code is required to gain access to protected resources

Page 49: OAuth 2.0 in depth

Understanding the URL

• client_id – The ID of the Client App• client_secret– The Secret of the Client App• redirect_uri– The Registered redirect_uri• code– Authorization Grant Code

Page 50: OAuth 2.0 in depth

Step 3 – Access Protected Resource

Pass the Access Code to access protected resource1. Recommend – HTTP Headers– So this is not cached by proxies

2. Also position as a Query Parameter

Page 51: OAuth 2.0 in depth

Facebook Protected Resource

Page 52: OAuth 2.0 in depth

Facebook Protected Resource

Page 53: OAuth 2.0 in depth

Live Example Google

Page 54: OAuth 2.0 in depth

Step 1 – Get Authorization Code

Page 55: OAuth 2.0 in depth

Understanding the URL

• client_id – The ID of the Client App• redirect_uri – Where to go back after OAuth• scope – Permissions allowed by User• state – Something to pass back to redirect_uri• response_type = “code” means authorization

code• access_type = “offline” to get access to the

“refresh_token”

Page 56: OAuth 2.0 in depth
Page 57: OAuth 2.0 in depth
Page 58: OAuth 2.0 in depth

State which we sent

Page 59: OAuth 2.0 in depth

Authorization Grant Code which needs to be exchanged for Access

Token

Page 60: OAuth 2.0 in depth

Step 2 – Exchange to get Access Code

Page 61: OAuth 2.0 in depth

Step 3 – Access Protected Resource

Pass the Access Code to access protected resource1. Recommend – HTTP Headers– So this is not cached by proxies

2. Also position as a Query Parameter

Page 62: OAuth 2.0 in depth

Google Protected Resource

Page 63: OAuth 2.0 in depth

What happens when access token expires?

Page 64: OAuth 2.0 in depth

Use Refresh_Token to Issue Access_Token

Page 65: OAuth 2.0 in depth

• Authorization Code Grant• Implicit Grant• Resource Owner Password Credentials Grant• Client Credentials Grant

Grant Types

Page 66: OAuth 2.0 in depth

Client

Resource Owner

Resource Server

Authorization Request

Access Token

Access Token

Protected Resource

Protocol Flow

Page 67: OAuth 2.0 in depth

Live Example Facebook

Page 68: OAuth 2.0 in depth

Step 1 – Get Access Code

Page 69: OAuth 2.0 in depth

Step 1 – Get Access Code

See you directly got the access code

Page 70: OAuth 2.0 in depth

Step 2 – Access Protected Resource

Pass the Access Code to access protected resource1. Recommend – HTTP Headers– So this is not cached by proxies

2. Also position as a Query Parameter

Page 71: OAuth 2.0 in depth

Facebook Protected Resource

Page 72: OAuth 2.0 in depth

Facebook Protected Resource

Page 73: OAuth 2.0 in depth

Live Example Google

Page 74: OAuth 2.0 in depth

Step 1 – Get Access Code

Page 75: OAuth 2.0 in depth

Step 1 – Get Access Code

Page 76: OAuth 2.0 in depth

Step 1 – Get Access Code

See you directly got the access code

Page 77: OAuth 2.0 in depth

Step 2 – Access Protected Resource

Pass the Access Code to access protected resource1. Recommend – HTTP Headers– So this is not cached by proxies

2. Also position as a Query Parameter

Page 78: OAuth 2.0 in depth

Google Protected Resource

Page 80: OAuth 2.0 in depth

• Authorization Code Grant• Implicit Grant• Resource Owner Password Credentials Grant• Client Credentials Grant

Grant Types

Page 81: OAuth 2.0 in depth

Client

Authorization Server

Resource Server

Username/Password

Access Token with Optional Refresh Token

Access Token

Protected Resource

Protocol Flow

Resource Owner

Resource Owner Credentials & Client Credentials

Page 82: OAuth 2.0 in depth

Use Cases

• Strong Trust between Resource Owner and Client e.g Operating System or Privileged App

• Client is not supposed to store the Credentials but only the Access token and Refresh Token if provided

• Example – Salesforce OAuth has provision for this

Page 83: OAuth 2.0 in depth

• Authorization Code Grant• Implicit Grant• Resource Owner Password Credentials Grant• Client Credentials Grant

Grant Types

Page 84: OAuth 2.0 in depth

Client

Authorization Server

Resource Server

Access Token with Optional Refresh Token

Access Token

Protected Resource

Protocol Flow

Client Credentials

Page 85: OAuth 2.0 in depth

Use case

• The Data accessed is not owned by Resource Owner, but by the Client

• Say Skype showing statistics of uptime of its services

Page 86: OAuth 2.0 in depth

Use case

• There is contract already set between the Client and the Authorization Server

• E.g Google Apps Marketspace

• An App installed on Google Apps requires permission to everyone’s calendar in that domain. This permission is provided by the admin and not the end user.

Page 87: OAuth 2.0 in depth

OAuth from Mobile Device

Page 88: OAuth 2.0 in depth

Popular Approaches

• Using User Agent (Stock Browser)• Using Embedded WebView

Page 89: OAuth 2.0 in depth
Page 91: OAuth 2.0 in depth

Request Authorization

When user first needs to access some protected resource, client opens a browser and sends user to the authorization endpoint

Device

NativeApp

Browser

1

Cloud!

1

Authorization Endpoint

Token Endpoint

Uri authzUrl = Uri.parse("https://as.example.com/as/authorization.oauth2?client_id=myapp&response_type=code&scope=update_status");

Intent launchBrowser = new Intent(Intent.ACTION_VIEW, authzUrl);

startActivity(launchBrowser);

NSString* launchUrl = @"https://as.example.com/as/authorization.oauth2?client_id=myapp&response_type=code&scope=update_status";

[[UIApplication sharedApplication] openURL:[NSURL URLWithString: launchUrl]];

https://as.example.com/as/authorization.oauth2?client_id=myapp&response_type=code&scope=update_status

http://

Reference - http://www.slideshare.net/briandavidcampbell/is-that-a-token-in-your-phone-in-your-pocket-or-are-you-just-glad-to-see-me-oauth-20-and-mobile-devices

Page 92: OAuth 2.0 in depth

Authenticate and Approve The AS authenticates the user

Directly Indirectly via Facebook, Twitter, Google, Yahoo, etc.

Device

NativeApp

Browser

Cloud!

2

Authorization Endpoint

Token Endpoint

Reference - http://www.slideshare.net/briandavidcampbell/is-that-a-token-in-your-phone-in-your-pocket-or-are-you-just-glad-to-see-me-oauth-20-and-mobile-devices

Page 93: OAuth 2.0 in depth

Approve

Device

NativeApp

Browser

Cloud!

2

Authorization Endpoint

Token Endpoint

User approves the requested access

Reference - http://www.slideshare.net/briandavidcampbell/is-that-a-token-in-your-phone-in-your-pocket-or-are-you-just-glad-to-see-me-oauth-20-and-mobile-devices

Page 94: OAuth 2.0 in depth

Handle Callback

Server returns control to the app via HTTP redirection and includes an authorization code

HTTP/1.1 302 Found

Location: x-com.mycorp.myapp://oauth.callback?code=SplxlOBeZQQYbYS6WxSbIAhttp://

Device

NativeApp

Browser

Cloud!

3

Authorization Endpoint

Token Endpoint

Reference - http://www.slideshare.net/briandavidcampbell/is-that-a-token-in-your-phone-in-your-pocket-or-are-you-just-glad-to-see-me-oauth-20-and-mobile-devices

Page 95: OAuth 2.0 in depth

<activity android:name=".MyAppCallback” … >

<intent-filter>

<action android:name="android.intent.action.VIEW"/>

<category android:name="android.intent.category.DEFAULT"/>

<category android:name="android.intent.category.BROWSABLE"/>

<data android:scheme="x-com.mycorp.myapp" />

</intent-filter>

</activity>

Handle Callback (cont’d)Registering a custom URI scheme

String authzCode = getIntent().getData().getQueryParameter("code");

Device

NativeApp

Browser

Cloud!

Authorization Endpoint

Token Endpoint

3

In AndroidManifest.xml file:

Reference - http://www.slideshare.net/briandavidcampbell/is-that-a-token-in-your-phone-in-your-pocket-or-are-you-just-glad-to-see-me-oauth-20-and-mobile-devices

Page 96: OAuth 2.0 in depth

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url

{

NSString *queryString = [url query];

NSMutableDictionary *qsParms = [[NSMutableDictionary alloc] init];

for (NSString *param in [queryString componentsSeparatedByString:@"&"]) {

NSArray *elts = [param componentsSeparatedByString:@"="];

if([elts count] < 2) continue;

[qsParms setObject:[elts objectAtIndex:1] forKey:[elts objectAtIndex:0]];

};

NSString *code = [qsParms objectForKey:@"code"];

...

Handle Callback (cont’d)Registering a custom URI scheme

Device

NativeApp

Browser

Cloud!

Authorization Endpoint

Token Endpoint

3

In app info plist file:

Reference - http://www.slideshare.net/briandavidcampbell/is-that-a-token-in-your-phone-in-your-pocket-or-are-you-just-glad-to-see-me-oauth-20-and-mobile-devices

Page 97: OAuth 2.0 in depth

POST /as/token.oauth2 HTTP/1.1

Host: as.example.com

Content-Type: application/x-www-form-urlencoded;charset=UTF-8

client_id=myapp&grant_type=authorization_code&code=SplxlOBeZQQYbYS6WxSbIA

Trade Code for Token(s)

Device

NativeApp

Browser

Cloud!

Authorization Endpoint

Token Endpoint

4

HTTP/1.1 200 OK

Content-Type: application/json;charset=UTF-8

Cache-Control: no-store

Pragma: no-cache

{

"token_type":"Bearer",

"expires_in":3600,

"access_token":"PeRTSD9RQrbiuoaHVPxV41MzW1qS”,

"refresh_token":"uyAVrtyLZ2qPzI8rQ5UUTckCdGaJsz8XE8S58ecnt8”

}

http://

http://

Token Endpoint Request

Token Endpoint Response

Reference - http://www.slideshare.net/briandavidcampbell/is-that-a-token-in-your-phone-in-your-pocket-or-are-you-just-glad-to-see-me-oauth-20-and-mobile-devices

Page 98: OAuth 2.0 in depth

POST /api/update-status HTTP/1.1

Host: rs.example.com

Authorization: Bearer PeRTSD9RQrbiuoaHVPxV41MzW1qS

Content-Type: application/x-www-form-urlencoded;charset=UTF-8

status=Almost%20done.

Using an Access Token

http://

Device

NativeApp

Browser

Cloud!

Authorization Endpoint

Token Endpoint

5

NSString *authzHeader = [NSString stringWithFormat:@"Bearer %@", accessToken];

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];

[request setURL:[NSURL URLWithString:@"https://rs.example.com/api/update-status"]];

[request setValue:authzHeader forHTTPHeaderField:@"Authorization"];

DefaultHttpClient httpClient = new DefaultHttpClient();

HttpPost post = new HttpPost("https://rs.example.com/api/update-status");

post.setHeader("Authorization", "Bearer " + accessToken);

Once an access token is obtained, it can be used to authenticate/authorize calls to the protected resources at the RS by including it in HTTP Authorization header

Reference - http://www.slideshare.net/briandavidcampbell/is-that-a-token-in-your-phone-in-your-pocket-or-are-you-just-glad-to-see-me-oauth-20-and-mobile-devices

Page 99: OAuth 2.0 in depth

Pros and Cons

• Pros– User may be already

logged in most cases– User will trust as he/she

sees https and domain name

• Cons– Complicated Custom URI

schema

Page 100: OAuth 2.0 in depth

Popular Approaches

• Using User Agent (Stock Browser)• Using Embedded WebView

Page 101: OAuth 2.0 in depth
Page 102: OAuth 2.0 in depth

Pros and Cons

• Pros– Easier to monitor pages

and extract authorization or access codes

• Cons– May not appeal since

neither https or domain name is visible

– WebView has separate cookie and history leading to client entering credentials each time

Page 103: OAuth 2.0 in depth

Open ID Story

Page 104: OAuth 2.0 in depth

Example of OpenID

Page 105: OAuth 2.0 in depth

What are you doing?

Proving you own http://rohitghatol.myopenid.com

Page 106: OAuth 2.0 in depth

Another Example

Page 107: OAuth 2.0 in depth

Wait!!

It’s http://openid.rohitghatol.com your own page, how does open id work then

Page 108: OAuth 2.0 in depth

Discover who is the Identity provider

Page 109: OAuth 2.0 in depth

Behind the Scene

Reference - http://openid.net/pres/protocolflow-1.1.png

Page 110: OAuth 2.0 in depth

Difference between OpenID and OAuth

Open ID• Know who is coming to your

site .

• Delegated Authentication

OAuth• Give rights to certain APIs

.

• Delegated Authorization

Page 111: OAuth 2.0 in depth

Next Steps : OpenID Connect

Page 112: OAuth 2.0 in depth

Disclaimer

• My Understanding on OpenID Connect is limited.

• Me talking about OpenID Connect is an attempt to understand Why it is born and what is the purpose of it

Page 113: OAuth 2.0 in depth

OpenID Connect

Page 114: OAuth 2.0 in depth

OpenID Connect

Page 115: OAuth 2.0 in depth

OpenID Connect

• Why it came into Picture?– Both OpenID and OAuth rely on redirection to

allow client to grant permissions– Protocol Flow is similar – redirection & verification– Passing Permissions to Gain Authentication

(Identity Information) is same as Passing Permission to Gain authority to some APIs

Page 116: OAuth 2.0 in depth

Let’s see a Dummy Flow

Page 117: OAuth 2.0 in depth

Step 1 – Get Access_Code and ID_Token

Page 118: OAuth 2.0 in depth

Build the OAuth URL for id_token & access_token (implicit grant flow)

Page 119: OAuth 2.0 in depth

Dummy Response

https://oauth2demo.appspot.com/oauthcallback# access_token=ya29.AHES6ZSzX & token_type=Bearer& expires_in=3600& id_token =eyJhbGciOiJSUzI1NiJ9.eyJpc3MiOiJhY2NvdW50cy5nb29nbGUuY29tIiwiY…

Page 120: OAuth 2.0 in depth

ID_Token

• Is a Json Web Token• It can be decrypted to get details• Or • taken to the Check ID Endpoint to get the

decryption done

Page 121: OAuth 2.0 in depth

Step 2 – Verify the Identity by going to Check ID End Point

Page 122: OAuth 2.0 in depth

Verify with Check ID End Point

{ "iss" : "https://accounts.example.com", "user_id" : "113487456102835830811", "aud" : "753560681145-2ik2j3snsvbs80ijdi8.apps.googleusercontent.com", "exp" : 1311281970, “nounce”:12345677}

Page 123: OAuth 2.0 in depth

What does the response mean?

• “user_id”: Authenticated User• “aud”: client_id so we know its meant for it• “exp”: Expiry• “nounce”: what we sent to prevent replay

attacks

Page 124: OAuth 2.0 in depth

Step 3 – Get User Info from UserInfo EndPoint

Page 125: OAuth 2.0 in depth

UserInfo EndPoint is a special resource accessed by providing access_token


Top Related