Slide 1

Slide 1 text

Intro to OAuth Matt Frost @shrtwhitebldguy https://joind.in/10630

Slide 2

Slide 2 text

Who Am I? • Senior Engineer - Synacor • Author • OSS Contributor • Mentoring Proponent • Podcast co-host

Slide 3

Slide 3 text

What is OAuth?

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

Tokens

Slide 6

Slide 6 text

Statelessness

Slide 7

Slide 7 text

Applications have tokens too

Slide 8

Slide 8 text

So what you’re saying is…

Slide 9

Slide 9 text

Yep!

Slide 10

Slide 10 text

Tokens can be stolen though

Slide 11

Slide 11 text

This is bad

Slide 12

Slide 12 text

Good news though!

Slide 13

Slide 13 text

There are different versions

Slide 14

Slide 14 text

Technically OAuth 1 is deprecated

Slide 15

Slide 15 text

Just like the mysql extension You’re probably going to run into it at some point anyway….

Slide 16

Slide 16 text

So here’s the plan

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

OAuth 1.0 Client

Slide 19

Slide 19 text

So we need tokens, right?

Slide 20

Slide 20 text

Token Definitions

Slide 21

Slide 21 text

Consumer Tokens

Slide 22

Slide 22 text

Temporary Credentials

Slide 23

Slide 23 text

Access Tokens

Slide 24

Slide 24 text

Token Request Flow

Slide 25

Slide 25 text

Super simple right? https://developer.yahoo.com/oauth/guide/oauth-auth-flow.html

Slide 26

Slide 26 text

Let’s break this down, eh?

Slide 27

Slide 27 text

You need an application

Slide 28

Slide 28 text

Request the temporary tokens

Slide 29

Slide 29 text

If you signed it right…

Slide 30

Slide 30 text

You’ll have temporary credentials

Slide 31

Slide 31 text

You now use these to request Access Tokens

Slide 32

Slide 32 text

If you sign that request right…

Slide 33

Slide 33 text

You’ll have your actual Access Tokens!

Slide 34

Slide 34 text

You can store them in a session or database and use them now!

Slide 35

Slide 35 text

Remember all that signing talk?

Slide 36

Slide 36 text

This is the hardest part…

Slide 37

Slide 37 text

Base String

Slide 38

Slide 38 text

$this->getNonce(),! ! 'oauth_callback' => $this->getCallback(),! ! 'oauth_signature_method' => $this->getSignatureMethod(),! ! 'oauth_timestamp' => time(),! ! 'oauth_consumer_key' => $this->getConsumerKey(),! ! 'oauth_token' => '',! ! 'oauth_version' => '1.0',! ];

Slide 39

Slide 39 text

HTTP Method and URI

Slide 40

Slide 40 text

Let’s see how this actually works

Slide 41

Slide 41 text

$this->getNonce(),! 'oauth_callback' => $this->getCallback(),! 'oauth_signature_method' => $this->getSignatureMethod(),! 'oauth_timestamp' => time(),! ‘oauth_consumer_key' => $this->getConsumerKey(),! 'oauth_token' => ‘',! 'oauth_version' => '1.0',! ];! ! $tempArray = [];! ksort($params);! foreach($params as $key => $value) {! ! $tempArray = $key . '=' . rawurlencode($value);! }! ! $baseString = $httpMethod . '&';! $baseString .= rawurlencode($uri) . '&';! $baseString .= implode('&', $tempArray);

Slide 42

Slide 42 text

Composite Key This is way easier…

Slide 43

Slide 43 text

Cram the 2 secrets together…

Slide 44

Slide 44 text

$consumer_secret = 'VERYSECRETZ';! $access_secret = 'SUCHSECURITY';! ! $composite_key = rawurlencode($consumer_secret) .'&'. rawurlencode($access_secret);

Slide 45

Slide 45 text

Signing with HMAC-SHA1

Slide 46

Slide 46 text

$signature = base64_encode(hash_hmac(! ! 'sha1',! ! $baseString,! ! $compositeKey,! ! true! )); Here’s your signature!

Slide 47

Slide 47 text

There are other signature types but…

Slide 48

Slide 48 text

However…

Slide 49

Slide 49 text

No content

Slide 50

Slide 50 text

Authorization Header

Slide 51

Slide 51 text

$params = [! 'oauth_nonce' => $this->getNonce(),! ! 'oauth_callback' => $this->getCallback(),! ! 'oauth_signature_method' => $this->getSignatureMethod(),! ! 'oauth_timestamp' => time(),! ! 'oauth_consumer_key' => $this->getConsumerKey(),! ! 'oauth_token' => '',! ! 'oauth_version' => '1.0',! ];! ! $params[‘oauth_signature’] = $signature; You probably remember this array?

Slide 52

Slide 52 text

$header = “Authorization: OAuth “;! $tempArray = [];! ! foreach($params as $key => $value) {! $tempArray[] = $key . ‘=“‘. rawurlencode($value);! }! ! $header .= implode(‘,’, $tempArray);! We’ve seen similar code before…

Slide 53

Slide 53 text

Authorization: OAuth oauth_consumer_key="xxxxxxxxx", oauth_nonce="fklj2324kljfksjf234k", oauth_signature="8xJAdrE00wGH21w87P 6N%2F8c0XZfeo%3D", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1399488541", oauth_token="xxxxxxxxx", oauth_version="1.0" This is the final result

Slide 54

Slide 54 text

Whew! That was some work

Slide 55

Slide 55 text

OAuth 1.0 Server

Slide 56

Slide 56 text

Token Generation

Slide 57

Slide 57 text

Recreate the signature with the info you received

Slide 58

Slide 58 text

If they match, they win!

Slide 59

Slide 59 text

If not…

Slide 60

Slide 60 text

No content

Slide 61

Slide 61 text

Access Control…

Slide 62

Slide 62 text

OAuth 2 Client

Slide 63

Slide 63 text

Good news!

Slide 64

Slide 64 text

No signatures

Slide 65

Slide 65 text

Must use SSL/TLS

Slide 66

Slide 66 text

Consumer Credentials

Slide 67

Slide 67 text

Access Token

Slide 68

Slide 68 text

Grants

Slide 69

Slide 69 text

Authorization Code Grant

Slide 70

Slide 70 text

Authorization example - Foursquare

Slide 71

Slide 71 text

http://foursquare.com/oauth2/authenticate? client_id=XXXXXXXXX&response_type=code&redirect_uri=htt p://oauth.dev/examples/Foursquare/callback.php

Slide 72

Slide 72 text

Token Request

Slide 73

Slide 73 text

http://oauth.dev/examples/ Foursquare/callback.php? code=

Slide 74

Slide 74 text

https://foursquare.com/oauth2/access_token? client_id=&client_secret=&code=&callback=http://oauth.dev/examples/ Foursquare/callback.php&grant_type=authorization_code

Slide 75

Slide 75 text

If you can use this, you should

Slide 76

Slide 76 text

Implicit Grant

Slide 77

Slide 77 text

http://foursquare.com/oauth2/authenticate? client_id=XXXXXXXXX&response_type=token&redirect_uri=ht tp://oauth.dev/examples/Foursquare/callback.php

Slide 78

Slide 78 text

Resource Owner Credentials Grant

Slide 79

Slide 79 text

Client Credentials Grant

Slide 80

Slide 80 text

Scopes

Slide 81

Slide 81 text

“Scopes” in OAuth 1

Slide 82

Slide 82 text

Scopes in OAuth 2

Slide 83

Slide 83 text

No content

Slide 84

Slide 84 text

Important Note on Scopes

Slide 85

Slide 85 text

Provides an ACL Framework

Slide 86

Slide 86 text

Refresh Tokens

Slide 87

Slide 87 text

Same Scope

Slide 88

Slide 88 text

OAuth 2 Server

Slide 89

Slide 89 text

Issuing Tokens

Slide 90

Slide 90 text

Should I Support All The Grants?

Slide 91

Slide 91 text

Maybe…

Slide 92

Slide 92 text

Authorization/Implicit Grants

Slide 93

Slide 93 text

Storing Token Info

Slide 94

Slide 94 text

Scopes

Slide 95

Slide 95 text

Reading Tokens

Slide 96

Slide 96 text

Query String, Header, Both?

Slide 97

Slide 97 text

A Caution Against Rolling Your Own

Slide 98

Slide 98 text

RFCs OAuth 1 RFC 5849 - http://tools.ietf.org/html/rfc5849 OAuth 2 RFC 6749 - http://tools.ietf.org/html/rfc6749

Slide 99

Slide 99 text

Questions?

Slide 100

Slide 100 text

Thank you! Matt Frost @shrtwhitebldguy https://joind.in/10630