intro to oauth

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

Upload: mfrost503

Post on 09-Jul-2015

130 views

Category:

Technology


0 download

DESCRIPTION

Introduction to OAuth talk that I gave at True North PHP

TRANSCRIPT

Page 1: Intro to OAuth

Intro to OAuthMatt Frost

@shrtwhitebldguy https://joind.in/12717

Page 2: Intro to OAuth

Who Am I?• Community Member

• Author

• OSS Contributor

• Mentoring Proponent

• Podcast co-host

Page 3: Intro to OAuth

What is OAuth?

Page 4: Intro to OAuth
Page 5: Intro to OAuth

Tokens

Page 6: Intro to OAuth

Statelessness

Page 7: Intro to OAuth

Applications have tokens too

Page 8: Intro to OAuth

So what you’re saying is…

Page 9: Intro to OAuth

Yep!

Page 10: Intro to OAuth

Tokens can be stolen though

Page 11: Intro to OAuth

This is bad

Page 12: Intro to OAuth

Good news though!

Page 13: Intro to OAuth

There are different versions

Page 14: Intro to OAuth

Technically OAuth 1 is deprecated

Page 15: Intro to OAuth

Just like the mysql extension

You’re probably going to run into it at some point anyway….

Page 16: Intro to OAuth

So here’s the plan

Page 17: Intro to OAuth
Page 18: Intro to OAuth

OAuth 1.0Client

Page 19: Intro to OAuth

So we need tokens, right?

Page 20: Intro to OAuth

Token Definitions

Page 21: Intro to OAuth

Consumer Tokens

Page 22: Intro to OAuth

Temporary Credentials

Page 23: Intro to OAuth

Access Tokens

Page 24: Intro to OAuth

Token Request Flow

Page 25: Intro to OAuth

Super simple right?

https://developer.yahoo.com/oauth/guide/oauth-auth-flow.html

Page 26: Intro to OAuth

Let’s break this down, eh?

Page 27: Intro to OAuth
Page 28: Intro to OAuth

You need an application

Page 29: Intro to OAuth
Page 30: Intro to OAuth

Request the temporary tokens

Page 31: Intro to OAuth
Page 32: Intro to OAuth

If you signed it right…

Page 33: Intro to OAuth

You’ll have temporary credentials

Page 34: Intro to OAuth
Page 35: Intro to OAuth

You now use these to request Access Tokens

Page 36: Intro to OAuth
Page 37: Intro to OAuth

If you sign that request right…

Page 38: Intro to OAuth

You’ll have your actual Access Tokens!

Page 39: Intro to OAuth

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

Page 40: Intro to OAuth

Remember all that signing talk?

Page 41: Intro to OAuth

This is the hardest part…

Page 42: Intro to OAuth

Base String

Page 43: Intro to OAuth

<?php!!

$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',!];

Page 44: Intro to OAuth

<?php!!

$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',!! ‘oauth_verifier’ => ‘xxxxxxxxx’!];

If you have an OAuth Verifier

Page 45: Intro to OAuth

HTTP Method and URI

Page 46: Intro to OAuth

Let’s see how this actually works

Page 47: Intro to OAuth

<?php!$httpMethod = 'POST';!$uri = ‘http://api.example.com/request_tokens';!!$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',!];!!$tempArray = [];!ksort($params);!foreach($params as $key => $value) {!! $tempArray[] = $key . '=' . rawurlencode($value);!}!!$baseString = $httpMethod . '&';!$baseString .= rawurlencode($uri) . '&';!$baseString .= implode('&', $tempArray);

Page 48: Intro to OAuth

Composite KeyThis is way easier…

Page 49: Intro to OAuth

Cram the 2 secrets together…

Page 50: Intro to OAuth

$consumer_secret = 'VERYSECRETZ';!$access_secret = 'SUCHSECURITY';!!

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

Page 51: Intro to OAuth

Signing with HMAC-SHA1

Page 52: Intro to OAuth

$signature = base64_encode(hash_hmac(!! 'sha1',!! $baseString,!! $compositeKey,!! true!));

Here’s your signature!

Page 53: Intro to OAuth

There are other signature types but…

Page 54: Intro to OAuth

However…

Page 55: Intro to OAuth
Page 56: Intro to OAuth

Authorization Header

Page 57: Intro to OAuth

$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?

Page 58: Intro to OAuth

$header = “Authorization: OAuth “;!$tempArray = [];!!

foreach($params as $key => $value) {! $tempArray[] = $key . ‘=“‘. rawurlencode($value);!}!!

$header .= implode(‘,’, $tempArray);!

We’ve seen similar code before…

Page 59: Intro to OAuth

Authorization: OAuth oauth_consumer_key="xxxxxxxxx", oauth_nonce="fklj2324kljfksjf234k", oauth_signature="8xJAdrE00wGH21w87P6N%2F8c0XZfeo%3D", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1399488541", oauth_token="xxxxxxxxx", oauth_version="1.0"

This is the final result

Page 60: Intro to OAuth

Whew! That was some work

Page 61: Intro to OAuth

OAuth 2Client

Page 62: Intro to OAuth

Good news!

Page 63: Intro to OAuth

No signatures

Page 64: Intro to OAuth

Must use SSL/TLS

Page 65: Intro to OAuth

Consumer Credentials

Page 66: Intro to OAuth

Access Token

Page 67: Intro to OAuth

Grants

Page 68: Intro to OAuth

Authorization Code Grant

Page 69: Intro to OAuth

Authorization example - Foursquare

Page 70: Intro to OAuth

http://foursquare.com/oauth2/authenticate?client_id=XXXXXXXXX&response_type=code&redirect_uri=htt

p://oauth.dev/examples/Foursquare/callback.php

Page 71: Intro to OAuth

Token Request

Page 72: Intro to OAuth

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

code=<CODE>

Page 73: Intro to OAuth

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

Page 74: Intro to OAuth

If you can use this, you should

Page 75: Intro to OAuth

Implicit Grant

Page 76: Intro to OAuth

http://foursquare.com/oauth2/authenticate?client_id=XXXXXXXXX&response_type=token&redirect_uri=ht

tp://oauth.dev/examples/Foursquare/callback.php

Page 77: Intro to OAuth

Resource Owner Credentials Grant

Page 78: Intro to OAuth

Client Credentials Grant

Page 79: Intro to OAuth

Scopes

Page 80: Intro to OAuth

“Scopes” in OAuth 1

Page 81: Intro to OAuth

Scopes in OAuth 2

Page 82: Intro to OAuth
Page 83: Intro to OAuth

Important Note on Scopes

Page 84: Intro to OAuth

Provides an ACL Framework

Page 85: Intro to OAuth

Refresh Tokens

Page 86: Intro to OAuth

Same Scope

Page 87: Intro to OAuth

What can we do with this?

Page 88: Intro to OAuth

Access data from APIs

Page 89: Intro to OAuth

Move Authentication Elsewhere a.k.a Single Sign On

Page 90: Intro to OAuth

So this works everywhere right?

Page 91: Intro to OAuth

Well…sorta

Page 92: Intro to OAuth

Useful reading OAuth 1 https://tools.ietf.org/html/rfc5849 OAuth 2 https://tools.ietf.org/html/rfc6749

Page 93: Intro to OAuth

Thanks! Questions?

Page 94: Intro to OAuth