+ All Categories
Home > Technology > Using OAuth with PHP

Using OAuth with PHP

Date post: 06-May-2015
Category:
Upload: david-ingram
View: 6,638 times
Download: 2 times
Share this document with a friend
Description:
A talk given at PHP London on 4th November 2010. This provides an introduction to OAuth and a simplistic PHP implementation of a consumer, as well as a few things to think about when creating a provider.
76
Using OAuth with PHP Dave Ingram @dmi 4th November 2010
Transcript
Page 1: Using OAuth with PHP

Using OAuth with PHP

Dave Ingram

@dmi

4th November 2010

Page 2: Using OAuth with PHP
Page 3: Using OAuth with PHP
Page 4: Using OAuth with PHP
Page 5: Using OAuth with PHP

Coming up

• What is OAuth?

• How do you write a Consumer in PHP?

• What doesn’t OAuth do?

• Thoughts on being a Provider

Page 6: Using OAuth with PHP

What is OAuth anyway?

Page 7: Using OAuth with PHP

A long time ago, in a website not far away. . .

Page 8: Using OAuth with PHP
Page 9: Using OAuth with PHP
Page 10: Using OAuth with PHP

Connect!

Page 11: Using OAuth with PHP

Connect!

U:KittehLuvrP:hunter2

Page 12: Using OAuth with PHP

Connect!

U:KittehLuvrP:hunter2

U:KittehLuvrP:hunter2

Page 13: Using OAuth with PHP

Connect!

U:KittehLuvrP:hunter2

U:KittehLuvrP:hunter2

Page 14: Using OAuth with PHP

Connect!

U:KittehLuvrP:hunter2

U:KittehLuvrP:hunter2

U:KittehLuvrP:hunter2

Page 15: Using OAuth with PHP

Connect!

U:KittehLuvrP:hunter2

U:KittehLuvrP:hunter2

U:KittehLuvrP:hunter2

O HAI TWITTERLOOK AT MAHKITTEH LOL!

Page 16: Using OAuth with PHP

Full access

Page 17: Using OAuth with PHP

Full access

Fragile

Page 18: Using OAuth with PHP

Full access

Fragile

Revoking is painful

Page 19: Using OAuth with PHP

YOU REVEAL YOUR USERNAMEAND PASSWORD

Page 20: Using OAuth with PHP

YOUR USERNAMEAND PASSWORD

Page 21: Using OAuth with PHP
Page 22: Using OAuth with PHP

Who uses it?

Page 23: Using OAuth with PHP
Page 24: Using OAuth with PHP
Page 25: Using OAuth with PHP
Page 26: Using OAuth with PHP
Page 27: Using OAuth with PHP
Page 28: Using OAuth with PHP
Page 29: Using OAuth with PHP
Page 30: Using OAuth with PHP
Page 31: Using OAuth with PHP
Page 32: Using OAuth with PHP
Page 33: Using OAuth with PHP
Page 34: Using OAuth with PHP

Building a Consumer

Page 35: Using OAuth with PHP

To sign requests, you need:

Consumer keyConsumer secret

(Unique per application)

+

Access tokenAccess secret

(Unique per application user)

Page 36: Using OAuth with PHP

Step 1: Register with the provider

Page 37: Using OAuth with PHP

I would like my OAuthapplication toconsume your serviceplease, Mr. Provider.

Page 38: Using OAuth with PHP

Certainly. I just needto take a few detailsfrom you, and we’ll beall set.

Page 39: Using OAuth with PHP

OK. Here you go.

Page 40: Using OAuth with PHP

Consumer keyConsumer secret

Page 41: Using OAuth with PHP

Step 2: Write your applicationStep 3: ??????Step 4: Profit!

Page 42: Using OAuth with PHP

Step 2: Write your applicationStep 3: ??????Step 4: Profit!

Page 43: Using OAuth with PHP

User Consumer Provider

User clicks connect

Page 44: Using OAuth with PHP

User Consumer Provider

C C

Ask provider forrequest token

Page 45: Using OAuth with PHP

User Consumer Provider

C C

R R

Provider returnsrequest token and

request secret

Page 46: Using OAuth with PHP

User Consumer Provider

C C

R R

R

Redirect user to provider

Page 47: Using OAuth with PHP

User Consumer Provider

C C

R R

R

R

User logs in/authorisesapp

Page 48: Using OAuth with PHP

User Consumer Provider

C C

R R

R

R

V

Provider redirects userback to app with

verifier

Page 49: Using OAuth with PHP

User Consumer Provider

C C

R R

R

R

V

V

User’s arrival withverifier notifies app

Page 50: Using OAuth with PHP

User Consumer Provider

C C

R R

R

R

V

VC C R R V

App then exchangesrequest token for

access token

Page 51: Using OAuth with PHP

User Consumer Provider

C C

R R

R

R

V

VC C R R V

A AProvider returns

access token andaccess secret

Page 52: Using OAuth with PHP

User Consumer Provider

C C

R R

R

R

V

VC C R R V

A A

C C A A

App makes request onuser’s behalf

Page 53: Using OAuth with PHP

Get request token// Create OAuth client object$o = new OAuth(

MY_CONSUMER_KEY,MY_CONSUMER_SECRET,OAUTH_SIG_METHOD_HMACSHA1,

);

Page 54: Using OAuth with PHP

Get request token// Create OAuth client object$o = new OAuth(

MY_CONSUMER_KEY,MY_CONSUMER_SECRET,OAUTH_SIG_METHOD_HMACSHA1,

);

// Fetch the request token$response = $o->getRequestToken(

'https://api.twitter.com/oauth/request_token');

// Save for later exchange$_SESSION['req_token'] = $response['oauth_token'];$_SESSION['req_secret'] = $response['oauth_token_secr et'];

Page 55: Using OAuth with PHP

Get request token// Create OAuth client object$o = new OAuth(

MY_CONSUMER_KEY,MY_CONSUMER_SECRET,OAUTH_SIG_METHOD_HMACSHA1,

);

// Fetch the request token$response = $o->getRequestToken(

'https://api.twitter.com/oauth/request_token');

// Save for later exchange$_SESSION['req_token'] = $response['oauth_token'];$_SESSION['req_secret'] = $response['oauth_token_secr et'];

// Send user to provider's siteheader('Location: https://api.twitter.com/oauth/authorize' .

'?oauth_token='.$response['oauth_token']);

Page 56: Using OAuth with PHP
Page 57: Using OAuth with PHP

Get access token// Create OAuth client object$o = new OAuth(

MY_CONSUMER_KEY, MY_CONSUMER_SECRET,OAUTH_SIG_METHOD_HMACSHA1

);

// Sign requests with the request token$o->setToken($_SESSION['req_token'], $_SESSION['req_ secret']);

Page 58: Using OAuth with PHP

Get access token// Create OAuth client object$o = new OAuth(

MY_CONSUMER_KEY, MY_CONSUMER_SECRET,OAUTH_SIG_METHOD_HMACSHA1

);

// Sign requests with the request token$o->setToken($_SESSION['req_token'], $_SESSION['req_ secret']);

// Exchange request for access token (verifier is automatic )$response = $o->getAccessToken(

'https://api.twitter.com/oauth/access_token');

// Save access tokens for later use$current_user->saveTwitterTokens(

$response['oauth_token'],$response['oauth_token_secret'],

);

header('Location: /twitter-link-ok');

Page 59: Using OAuth with PHP

Access tokenAccess secret

Page 60: Using OAuth with PHP

Make API requests// Create OAuth client object$o = new OAuth(

MY_CONSUMER_KEY, MY_CONSUMER_SECRET,OAUTH_SIG_METHOD_HMACSHA1

);

// Sign requests with the access token$o->setToken(

$current_user->getTwitterToken(),$current_user->getTwitterSecret()

);

$args = array('status'=>'O HAI TWITTER LOOK AT MAH KITTEH LOL!');

$oauth->fetch('https://api.twitter.com/v1/statuses/update.json',$args,OAUTH_HTTP_METHOD_POST

);

$json = json_decode($oauth->getLastResponse());printf("Result: %s\n", print_r($json, true));

Page 61: Using OAuth with PHP

What OAuth doesn’t do

Page 62: Using OAuth with PHP

No proof of server identity (use TLS)

Page 63: Using OAuth with PHP

No proof of server identity (use TLS)

No confidentiality (use TLS/SSL)

Page 64: Using OAuth with PHP

No proof of server identity (use TLS)

No confidentiality (use TLS/SSL)

No open-source consumer

Page 65: Using OAuth with PHP

Thoughts on being aProvider

Page 66: Using OAuth with PHP

Very easy to be a Consumer

Page 67: Using OAuth with PHP

Very easy to be a Consumer

Many design decisions to make as a Provider

Page 68: Using OAuth with PHP

Very easy to be a Consumer

Many design decisions to make as a Provider

A fair amount of work, and not always easy to changeyour mind

Page 69: Using OAuth with PHP

Very easy to be a Consumer

Many design decisions to make as a Provider

A fair amount of work, and not always easy to changeyour mind

For example. . .

Page 70: Using OAuth with PHP

How large a range of timestamps do you allow?

Page 71: Using OAuth with PHP

How large a range of timestamps do you allow?

What permission granularity do you provide?

Page 72: Using OAuth with PHP

How large a range of timestamps do you allow?

What permission granularity do you provide?

What format and length are tokens/secrets?

Page 73: Using OAuth with PHP

How large a range of timestamps do you allow?

What permission granularity do you provide?

What format and length are tokens/secrets?

Do you identify actions as coming from particularconsumers? (e.g. Twitter)

Page 74: Using OAuth with PHP

How large a range of timestamps do you allow?

What permission granularity do you provide?

What format and length are tokens/secrets?

Do you identify actions as coming from particularconsumers? (e.g. Twitter)

What about attacks? Phishing, DoS, clickjacking, CSRF

Page 75: Using OAuth with PHP

How large a range of timestamps do you allow?

What permission granularity do you provide?

What format and length are tokens/secrets?

Do you identify actions as coming from particularconsumers? (e.g. Twitter)

What about attacks? Phishing, DoS, clickjacking, CSRF

Beware proxying/caching (use the right headers!)

Page 76: Using OAuth with PHP

Links

OAuth Spec: http://oauth.net/

Intro/tutorial: http://hueniverse.com/

PECL extension: http://pecl.php.net/oauth/

Me: http://twitter.com/dmihttp://www.dmi.me.uk/talks/http://www.dmi.me.uk/code/php/

Slides: http://slideshare.net/ingramd


Recommended