54
Mastering OAuth 2.0 Ben Ramsey True North PHP 5 Nov 2016

Mastering OAuth 2 - Ben Ramsey · 2017. 4. 3. · Ramsey, Ben. “Mastering OAuth 2.0.” True North PHP. Microsoft Canada, Mississauga. 5 Nov. 2016. Conference presentation. This

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Mastering OAuth 2 - Ben Ramsey · 2017. 4. 3. · Ramsey, Ben. “Mastering OAuth 2.0.” True North PHP. Microsoft Canada, Mississauga. 5 Nov. 2016. Conference presentation. This

Mastering OAuth 2.0

Ben RamseyTrue North PHP5 Nov 2016

Page 2: Mastering OAuth 2 - Ben Ramsey · 2017. 4. 3. · Ramsey, Ben. “Mastering OAuth 2.0.” True North PHP. Microsoft Canada, Mississauga. 5 Nov. 2016. Conference presentation. This

OAuth 2.0

Page 3: Mastering OAuth 2 - Ben Ramsey · 2017. 4. 3. · Ramsey, Ben. “Mastering OAuth 2.0.” True North PHP. Microsoft Canada, Mississauga. 5 Nov. 2016. Conference presentation. This
Page 4: Mastering OAuth 2 - Ben Ramsey · 2017. 4. 3. · Ramsey, Ben. “Mastering OAuth 2.0.” True North PHP. Microsoft Canada, Mississauga. 5 Nov. 2016. Conference presentation. This
Page 5: Mastering OAuth 2 - Ben Ramsey · 2017. 4. 3. · Ramsey, Ben. “Mastering OAuth 2.0.” True North PHP. Microsoft Canada, Mississauga. 5 Nov. 2016. Conference presentation. This

#1 Click to authorize

Page 6: Mastering OAuth 2 - Ben Ramsey · 2017. 4. 3. · Ramsey, Ben. “Mastering OAuth 2.0.” True North PHP. Microsoft Canada, Mississauga. 5 Nov. 2016. Conference presentation. This

#2 Log in on site and grant permission

Page 7: Mastering OAuth 2 - Ben Ramsey · 2017. 4. 3. · Ramsey, Ben. “Mastering OAuth 2.0.” True North PHP. Microsoft Canada, Mississauga. 5 Nov. 2016. Conference presentation. This

#3 Redirect back with auth code

#4 Exchange codefor access token

Page 8: Mastering OAuth 2 - Ben Ramsey · 2017. 4. 3. · Ramsey, Ben. “Mastering OAuth 2.0.” True North PHP. Microsoft Canada, Mississauga. 5 Nov. 2016. Conference presentation. This

#5 Use access token to get data

Page 9: Mastering OAuth 2 - Ben Ramsey · 2017. 4. 3. · Ramsey, Ben. “Mastering OAuth 2.0.” True North PHP. Microsoft Canada, Mississauga. 5 Nov. 2016. Conference presentation. This

bram.se/tnphp16-oauth2-app

Page 10: Mastering OAuth 2 - Ben Ramsey · 2017. 4. 3. · Ramsey, Ben. “Mastering OAuth 2.0.” True North PHP. Microsoft Canada, Mississauga. 5 Nov. 2016. Conference presentation. This

Preparing for OAuth

Page 11: Mastering OAuth 2 - Ben Ramsey · 2017. 4. 3. · Ramsey, Ben. “Mastering OAuth 2.0.” True North PHP. Microsoft Canada, Mississauga. 5 Nov. 2016. Conference presentation. This

1. Register your application with the service2. Let the service know your domains or

redirect URLs3. Configure your application to use the

client ID and client secret given to you bythe service

! No two OAuth 2.0 providers are alike!

Page 12: Mastering OAuth 2 - Ben Ramsey · 2017. 4. 3. · Ramsey, Ben. “Mastering OAuth 2.0.” True North PHP. Microsoft Canada, Mississauga. 5 Nov. 2016. Conference presentation. This
Page 13: Mastering OAuth 2 - Ben Ramsey · 2017. 4. 3. · Ramsey, Ben. “Mastering OAuth 2.0.” True North PHP. Microsoft Canada, Mississauga. 5 Nov. 2016. Conference presentation. This
Page 14: Mastering OAuth 2 - Ben Ramsey · 2017. 4. 3. · Ramsey, Ben. “Mastering OAuth 2.0.” True North PHP. Microsoft Canada, Mississauga. 5 Nov. 2016. Conference presentation. This
Page 15: Mastering OAuth 2 - Ben Ramsey · 2017. 4. 3. · Ramsey, Ben. “Mastering OAuth 2.0.” True North PHP. Microsoft Canada, Mississauga. 5 Nov. 2016. Conference presentation. This
Page 16: Mastering OAuth 2 - Ben Ramsey · 2017. 4. 3. · Ramsey, Ben. “Mastering OAuth 2.0.” True North PHP. Microsoft Canada, Mississauga. 5 Nov. 2016. Conference presentation. This
Page 17: Mastering OAuth 2 - Ben Ramsey · 2017. 4. 3. · Ramsey, Ben. “Mastering OAuth 2.0.” True North PHP. Microsoft Canada, Mississauga. 5 Nov. 2016. Conference presentation. This

Integrating with the Provider

Page 18: Mastering OAuth 2 - Ben Ramsey · 2017. 4. 3. · Ramsey, Ben. “Mastering OAuth 2.0.” True North PHP. Microsoft Canada, Mississauga. 5 Nov. 2016. Conference presentation. This

composer require league/oauth2-instagram

Page 19: Mastering OAuth 2 - Ben Ramsey · 2017. 4. 3. · Ramsey, Ben. “Mastering OAuth 2.0.” True North PHP. Microsoft Canada, Mississauga. 5 Nov. 2016. Conference presentation. This

use League\OAuth2\Client\Provider\Instagram;

$provider = new Instagram([ 'clientId' => 'CLIENT_ID', 'clientSecret' => 'CLIENT_SECRET', 'redirectUri' => 'https://example.com/redirect', ]);

Page 20: Mastering OAuth 2 - Ben Ramsey · 2017. 4. 3. · Ramsey, Ben. “Mastering OAuth 2.0.” True North PHP. Microsoft Canada, Mississauga. 5 Nov. 2016. Conference presentation. This

Authorization Request1. Generate authorization URL2. Store state to session3. Prompt user to authorize or redirect them

Page 21: Mastering OAuth 2 - Ben Ramsey · 2017. 4. 3. · Ramsey, Ben. “Mastering OAuth 2.0.” True North PHP. Microsoft Canada, Mississauga. 5 Nov. 2016. Conference presentation. This

$authUrl = $provider->getAuthorizationUrl();

$request->session()->put( 'instagramState', $provider->getState() );

return redirect()->away($authUrl);

Page 22: Mastering OAuth 2 - Ben Ramsey · 2017. 4. 3. · Ramsey, Ben. “Mastering OAuth 2.0.” True North PHP. Microsoft Canada, Mississauga. 5 Nov. 2016. Conference presentation. This

Redirection Endpoint1. Receive authorization code2. Check state3. Exchange code for an access token

Page 23: Mastering OAuth 2 - Ben Ramsey · 2017. 4. 3. · Ramsey, Ben. “Mastering OAuth 2.0.” True North PHP. Microsoft Canada, Mississauga. 5 Nov. 2016. Conference presentation. This

$state = $request->session()->get('instagramState');

if ($request->state !== $state) { abort(400, 'Invalid state'); }

if (!$request->has('code')) { abort(400, 'Authorization code not available'); }

$token = $provider->getAccessToken( 'authorization_code', [ 'code' => $request->code, ] );

$request->session()->put('instagramToken', $token);

return redirect()->action('HomeController@index');

Page 24: Mastering OAuth 2 - Ben Ramsey · 2017. 4. 3. · Ramsey, Ben. “Mastering OAuth 2.0.” True North PHP. Microsoft Canada, Mississauga. 5 Nov. 2016. Conference presentation. This

Expiring & Refreshing Tokens1. Check for expiration & refresh token2. Request access token using refresh token

Page 25: Mastering OAuth 2 - Ben Ramsey · 2017. 4. 3. · Ramsey, Ben. “Mastering OAuth 2.0.” True North PHP. Microsoft Canada, Mississauga. 5 Nov. 2016. Conference presentation. This

if ($token->hasExpired() && $token->getRefreshToken()) { $newToken = $provider->getAccessToken('refresh_token', [ 'refresh_token' => $token->getRefreshToken(), ); }

$request->session()->put('accessToken', $token);

! Instagram does not support refresh tokens

Page 26: Mastering OAuth 2 - Ben Ramsey · 2017. 4. 3. · Ramsey, Ben. “Mastering OAuth 2.0.” True North PHP. Microsoft Canada, Mississauga. 5 Nov. 2016. Conference presentation. This

Using Access Tokens1. getAuthenticatedRequest() returns a PSR-7

RequestInterface object2. Use your favorite HTTP request library to

make a request

Page 27: Mastering OAuth 2 - Ben Ramsey · 2017. 4. 3. · Ramsey, Ben. “Mastering OAuth 2.0.” True North PHP. Microsoft Canada, Mississauga. 5 Nov. 2016. Conference presentation. This

$feedRequest = $provider->getAuthenticatedRequest( 'GET', 'https://api.instagram.com/v1/users/self/media/recent', $instagramToken );

$client = new \GuzzleHttp\Client(); $feedResponse = $client->send($feedRequest);

$instagramFeed = json_decode( $feedResponse->getBody()->getContents() );

Page 28: Mastering OAuth 2 - Ben Ramsey · 2017. 4. 3. · Ramsey, Ben. “Mastering OAuth 2.0.” True North PHP. Microsoft Canada, Mississauga. 5 Nov. 2016. Conference presentation. This

A Brief History of Web Authorization

Page 29: Mastering OAuth 2 - Ben Ramsey · 2017. 4. 3. · Ramsey, Ben. “Mastering OAuth 2.0.” True North PHP. Microsoft Canada, Mississauga. 5 Nov. 2016. Conference presentation. This

What is OAuth 2.0?

Page 30: Mastering OAuth 2 - Ben Ramsey · 2017. 4. 3. · Ramsey, Ben. “Mastering OAuth 2.0.” True North PHP. Microsoft Canada, Mississauga. 5 Nov. 2016. Conference presentation. This

“However, as a rich and highly extensible framework with many optional components, on its own, this specification is likely to produce a wide range of non-interoperable implementations.”

RFC 6749, Section 1.8

Page 31: Mastering OAuth 2 - Ben Ramsey · 2017. 4. 3. · Ramsey, Ben. “Mastering OAuth 2.0.” True North PHP. Microsoft Canada, Mississauga. 5 Nov. 2016. Conference presentation. This

1. Resource owner2. Resource server3. Client4. Authorization server

Page 32: Mastering OAuth 2 - Ben Ramsey · 2017. 4. 3. · Ramsey, Ben. “Mastering OAuth 2.0.” True North PHP. Microsoft Canada, Mississauga. 5 Nov. 2016. Conference presentation. This

composer require league/oauth2-client

Page 33: Mastering OAuth 2 - Ben Ramsey · 2017. 4. 3. · Ramsey, Ben. “Mastering OAuth 2.0.” True North PHP. Microsoft Canada, Mississauga. 5 Nov. 2016. Conference presentation. This

use League\OAuth2\Client\Provider\GenericProvider;

$provider = new GenericProvider([ 'clientId' => 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX', 'clientSecret' => 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX', 'redirectUri' => 'https://you.example.com/redirect-url', 'urlAuthorize' => 'https://them.example.net/authorize', 'urlAccessToken' => 'https://them.example.net/token', 'urlResourceOwnerDetails' => 'https://them.example.net/api/me' ]);

Page 34: Mastering OAuth 2 - Ben Ramsey · 2017. 4. 3. · Ramsey, Ben. “Mastering OAuth 2.0.” True North PHP. Microsoft Canada, Mississauga. 5 Nov. 2016. Conference presentation. This

Authorization Code1. Commonly referred to as three-legged2. Used in our Instagram example3. Very common grant type

Page 35: Mastering OAuth 2 - Ben Ramsey · 2017. 4. 3. · Ramsey, Ben. “Mastering OAuth 2.0.” True North PHP. Microsoft Canada, Mississauga. 5 Nov. 2016. Conference presentation. This

Resource Owner

Client

Auth Server

Resource Server

Page 36: Mastering OAuth 2 - Ben Ramsey · 2017. 4. 3. · Ramsey, Ben. “Mastering OAuth 2.0.” True North PHP. Microsoft Canada, Mississauga. 5 Nov. 2016. Conference presentation. This

Step 1

Page 37: Mastering OAuth 2 - Ben Ramsey · 2017. 4. 3. · Ramsey, Ben. “Mastering OAuth 2.0.” True North PHP. Microsoft Canada, Mississauga. 5 Nov. 2016. Conference presentation. This

Step 2

Page 38: Mastering OAuth 2 - Ben Ramsey · 2017. 4. 3. · Ramsey, Ben. “Mastering OAuth 2.0.” True North PHP. Microsoft Canada, Mississauga. 5 Nov. 2016. Conference presentation. This

Step 3

Page 39: Mastering OAuth 2 - Ben Ramsey · 2017. 4. 3. · Ramsey, Ben. “Mastering OAuth 2.0.” True North PHP. Microsoft Canada, Mississauga. 5 Nov. 2016. Conference presentation. This

Step 4

Page 40: Mastering OAuth 2 - Ben Ramsey · 2017. 4. 3. · Ramsey, Ben. “Mastering OAuth 2.0.” True North PHP. Microsoft Canada, Mississauga. 5 Nov. 2016. Conference presentation. This

1. Gives username and password to client2. Client exchanges them for access token3. Use with extreme caution

Resource Owner Password Credentials

Page 41: Mastering OAuth 2 - Ben Ramsey · 2017. 4. 3. · Ramsey, Ben. “Mastering OAuth 2.0.” True North PHP. Microsoft Canada, Mississauga. 5 Nov. 2016. Conference presentation. This

Resource Owner

Client

Auth Server Resource Server

Page 42: Mastering OAuth 2 - Ben Ramsey · 2017. 4. 3. · Ramsey, Ben. “Mastering OAuth 2.0.” True North PHP. Microsoft Canada, Mississauga. 5 Nov. 2016. Conference presentation. This

Step 1

Page 43: Mastering OAuth 2 - Ben Ramsey · 2017. 4. 3. · Ramsey, Ben. “Mastering OAuth 2.0.” True North PHP. Microsoft Canada, Mississauga. 5 Nov. 2016. Conference presentation. This

Step 2

Page 44: Mastering OAuth 2 - Ben Ramsey · 2017. 4. 3. · Ramsey, Ben. “Mastering OAuth 2.0.” True North PHP. Microsoft Canada, Mississauga. 5 Nov. 2016. Conference presentation. This

Step 3

Page 45: Mastering OAuth 2 - Ben Ramsey · 2017. 4. 3. · Ramsey, Ben. “Mastering OAuth 2.0.” True North PHP. Microsoft Canada, Mississauga. 5 Nov. 2016. Conference presentation. This

$accessToken = $provider->getAccessToken('password', [ 'username' => 'demouser', 'password' => 'testpass' ]);

Page 46: Mastering OAuth 2 - Ben Ramsey · 2017. 4. 3. · Ramsey, Ben. “Mastering OAuth 2.0.” True North PHP. Microsoft Canada, Mississauga. 5 Nov. 2016. Conference presentation. This

Client Credentials1. Client is the resource owner2. Credentials are stored in the client (usually

safely on the server)

Page 47: Mastering OAuth 2 - Ben Ramsey · 2017. 4. 3. · Ramsey, Ben. “Mastering OAuth 2.0.” True North PHP. Microsoft Canada, Mississauga. 5 Nov. 2016. Conference presentation. This

Client

Auth Server Resource Server

Page 48: Mastering OAuth 2 - Ben Ramsey · 2017. 4. 3. · Ramsey, Ben. “Mastering OAuth 2.0.” True North PHP. Microsoft Canada, Mississauga. 5 Nov. 2016. Conference presentation. This

Step 1

Page 49: Mastering OAuth 2 - Ben Ramsey · 2017. 4. 3. · Ramsey, Ben. “Mastering OAuth 2.0.” True North PHP. Microsoft Canada, Mississauga. 5 Nov. 2016. Conference presentation. This

Step 2

Page 50: Mastering OAuth 2 - Ben Ramsey · 2017. 4. 3. · Ramsey, Ben. “Mastering OAuth 2.0.” True North PHP. Microsoft Canada, Mississauga. 5 Nov. 2016. Conference presentation. This

$accessToken = $provider->getAccessToken( 'client_credentials' );

Page 51: Mastering OAuth 2 - Ben Ramsey · 2017. 4. 3. · Ramsey, Ben. “Mastering OAuth 2.0.” True North PHP. Microsoft Canada, Mississauga. 5 Nov. 2016. Conference presentation. This

Implicit1. Relies on client-side redirection using a

client ID and a known redirection URL2. league/oauth2-client cannot support this

Page 52: Mastering OAuth 2 - Ben Ramsey · 2017. 4. 3. · Ramsey, Ben. “Mastering OAuth 2.0.” True North PHP. Microsoft Canada, Mississauga. 5 Nov. 2016. Conference presentation. This

Toward a More Secure Web

Page 53: Mastering OAuth 2 - Ben Ramsey · 2017. 4. 3. · Ramsey, Ben. “Mastering OAuth 2.0.” True North PHP. Microsoft Canada, Mississauga. 5 Nov. 2016. Conference presentation. This

THANK YOU. ANY QUESTIONS?

If you want to talk more, feel free to contact me.

benramsey.com!

" @ramsey

# github.com/ramsey

$ [email protected]

Mastering OAuth 2.0Copyright © 2016 Ben Ramsey

This work is licensed under Creative Commons Attribution-ShareAlike 4.0 International. For uses not covered under this license, please contact the author.

Ramsey, Ben. “Mastering OAuth 2.0.” True North PHP. Microsoft Canada, Mississauga. 5 Nov. 2016. Conference presentation.

This presentation was created using Keynote. The text is set in Chunk Five, Helvetica Neue, and Marker Felt. The source code is set in Menlo. The iconography is provided by Font Awesome.

Unless otherwise noted, all photographs are used by permission under a Creative Commons license. Please refer to the Photo Credits slide for more information.

% joind.in/talk/7a6db

Page 54: Mastering OAuth 2 - Ben Ramsey · 2017. 4. 3. · Ramsey, Ben. “Mastering OAuth 2.0.” True North PHP. Microsoft Canada, Mississauga. 5 Nov. 2016. Conference presentation. This

Photo Credits1. “Untitled” by MICⱵ^ΞL

2. “Master” by Giuditta

3. “Untitled” by MICⱵ^ΞL

4. “Untitled” by MICⱵ^ΞL

5. “Untitled” by MICⱵ^ΞL

6. “master gain” by Chris Blakeley

7. “Mixing board” by Kevin Jaako

1 2

3 4

5 6

7