Slide 1

Slide 1 text

Mastering OAuth 2.0 Ben Ramsey
 True North PHP 5 Nov 2016

Slide 2

Slide 2 text

OAuth 2.0

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

#1 Click to authorize

Slide 6

Slide 6 text

#2 Log in on site and grant permission

Slide 7

Slide 7 text

#3 Redirect back with auth code #4 Exchange code
 for access token

Slide 8

Slide 8 text

#5 Use access token to get data

Slide 9

Slide 9 text

bram.se/tnphp16-oauth2-app

Slide 10

Slide 10 text

Preparing for OAuth

Slide 11

Slide 11 text

1. Register your application with the service 2. Let the service know your domains or
 redirect URLs 3. Configure your application to use the
 client ID and client secret given to you by
 the service ! No two OAuth 2.0 providers are alike!

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

Integrating with the Provider

Slide 18

Slide 18 text

composer require league/oauth2-instagram

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

Authorization Request 1. Generate authorization URL 2. Store state to session 3. Prompt user to authorize or redirect them

Slide 21

Slide 21 text

$authUrl = $provider->getAuthorizationUrl(); $request->session()->put( 'instagramState', $provider->getState() ); return redirect()->away($authUrl);

Slide 22

Slide 22 text

Redirection Endpoint 1. Receive authorization code 2. Check state 3. Exchange code for an access token

Slide 23

Slide 23 text

$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');

Slide 24

Slide 24 text

Expiring & Refreshing Tokens 1. Check for expiration & refresh token 2. Request access token using refresh token

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

Using Access Tokens 1. getAuthenticatedRequest() returns a PSR-7 RequestInterface object 2. Use your favorite HTTP request library to make a request

Slide 27

Slide 27 text

$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() );

Slide 28

Slide 28 text

A Brief History of Web Authorization

Slide 29

Slide 29 text

What is OAuth 2.0?

Slide 30

Slide 30 text

“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

Slide 31

Slide 31 text

1. Resource owner 2. Resource server 3. Client 4. Authorization server

Slide 32

Slide 32 text

composer require league/oauth2-client

Slide 33

Slide 33 text

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' ]);

Slide 34

Slide 34 text

Authorization Code 1. Commonly referred to as three-legged 2. Used in our Instagram example 3. Very common grant type

Slide 35

Slide 35 text

Resource Owner Client Auth Server Resource Server

Slide 36

Slide 36 text

Step 1

Slide 37

Slide 37 text

Step 2

Slide 38

Slide 38 text

Step 3

Slide 39

Slide 39 text

Step 4

Slide 40

Slide 40 text

1. Gives username and password to client 2. Client exchanges them for access token 3. Use with extreme caution Resource Owner Password Credentials

Slide 41

Slide 41 text

Resource Owner Client Auth Server Resource Server

Slide 42

Slide 42 text

Step 1

Slide 43

Slide 43 text

Step 2

Slide 44

Slide 44 text

Step 3

Slide 45

Slide 45 text

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

Slide 46

Slide 46 text

Client Credentials 1. Client is the resource owner 2. Credentials are stored in the client (usually safely on the server)

Slide 47

Slide 47 text

Client Auth Server Resource Server

Slide 48

Slide 48 text

Step 1

Slide 49

Slide 49 text

Step 2

Slide 50

Slide 50 text

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

Slide 51

Slide 51 text

Implicit 1. Relies on client-side redirection using a client ID and a known redirection URL 2. league/oauth2-client cannot support this

Slide 52

Slide 52 text

Toward a More Secure Web

Slide 53

Slide 53 text

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.0 Copyright © 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

Slide 54

Slide 54 text

Photo Credits 1. “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