SlideShare a Scribd company logo
Using OAuth with PHP
Dave Ingram
@dmi
4th November 2010
Using OAuth with PHP
Using OAuth with PHP
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
What is OAuth anyway?
A long time ago, in a website not far away. . .
Using OAuth with PHP
Using OAuth with PHP
Connect!
Connect!
U:KittehLuvr
P:hunter2
Connect!
U:KittehLuvr
P:hunter2
U:KittehLuvr
P:hunter2
Connect!
U:KittehLuvr
P:hunter2
U:KittehLuvr
P:hunter2
Connect!
U:KittehLuvr
P:hunter2
U:KittehLuvr
P:hunter2
U:KittehLuvr
P:hunter2
Connect!
U:KittehLuvr
P:hunter2
U:KittehLuvr
P:hunter2
U:KittehLuvr
P:hunter2
O HAI TWITTER
LOOK AT MAH
KITTEH LOL!
Full access
Full access
Fragile
Full access
Fragile
Revoking is painful
YOU REVEAL YOUR USERNAME
AND PASSWORD
YOUR USERNAME
AND PASSWORD
Using OAuth with PHP
Who uses it?
Using OAuth with PHP
Using OAuth with PHP
Using OAuth with PHP
Using OAuth with PHP
Using OAuth with PHP
Using OAuth with PHP
Using OAuth with PHP
Using OAuth with PHP
Using OAuth with PHP
Using OAuth with PHP
Using OAuth with PHP
Building a Consumer
To sign requests, you need:
Consumer key
Consumer secret
(Unique per application)
+
Access token
Access secret
(Unique per application user)
Step 1: Register with the provider
I would like my OAuth
application to
consume your service
please, Mr. Provider.
Certainly. I just need
to take a few details
from you, and we’ll be
all set.
OK. Here you go.
Consumer key
Consumer secret
Step 2: Write your application
Step 3: ??????
Step 4: Profit!
Step 2: Write your application
Step 3: ??????
Step 4: Profit!
User Consumer Provider
User clicks connect
User Consumer Provider
C C
Ask provider for
request token
User Consumer Provider
C C
R R
Provider returns
request token and
request secret
User Consumer Provider
C C
R R
R
Redirect user to provider
User Consumer Provider
C C
R R
R
R
User logs in/authorises
app
User Consumer Provider
C C
R R
R
R
V
Provider redirects user
back to app with
verifier
User Consumer Provider
C C
R R
R
R
V
V
User’s arrival with
verifier notifies app
User Consumer Provider
C C
R R
R
R
V
V
C C R R V
App then exchanges
request token for
access token
User Consumer Provider
C C
R R
R
R
V
V
C C R R V
A A
Provider returns
access token and
access secret
User Consumer Provider
C C
R R
R
R
V
V
C C R R V
A A
C C A A
App makes request on
user’s behalf
Get request token
// Create OAuth client object
$o = new OAuth(
MY_CONSUMER_KEY,
MY_CONSUMER_SECRET,
OAUTH_SIG_METHOD_HMACSHA1,
);
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_secret'];
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_secret'];
// Send user to provider's site
header('Location: https://api.twitter.com/oauth/authorize'.
'?oauth_token='.$response['oauth_token']);
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']);
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');
Access token
Access secret
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: %sn", print_r($json, true));
What OAuth doesn’t do
No proof of server identity (use TLS)
No proof of server identity (use TLS)
No confidentiality (use TLS/SSL)
No proof of server identity (use TLS)
No confidentiality (use TLS/SSL)
No open-source consumer
Thoughts on being a
Provider
Very easy to be a Consumer
Very easy to be a Consumer
Many design decisions to make as a Provider
Very easy to be a Consumer
Many design decisions to make as a Provider
A fair amount of work, and not always easy to change
your mind
Very easy to be a Consumer
Many design decisions to make as a Provider
A fair amount of work, and not always easy to change
your mind
For example. . .
How large a range of timestamps do you allow?
How large a range of timestamps do you allow?
What permission granularity do you provide?
How large a range of timestamps do you allow?
What permission granularity do you provide?
What format and length are tokens/secrets?
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 particular
consumers? (e.g. Twitter)
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 particular
consumers? (e.g. Twitter)
What about attacks? Phishing, DoS, clickjacking, CSRF
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 particular
consumers? (e.g. Twitter)
What about attacks? Phishing, DoS, clickjacking, CSRF
Beware proxying/caching (use the right headers!)
Links
OAuth Spec: http://oauth.net/
Intro/tutorial: http://hueniverse.com/
PECL extension: http://pecl.php.net/oauth/
Me: http://twitter.com/dmi
http://www.dmi.me.uk/talks/
http://www.dmi.me.uk/code/php/
Slides: http://slideshare.net/ingramd

More Related Content

PDF
Implementing OAuth with PHP
KEY
OAuth using PHP5
PDF
OAuth - Open API Authentication
PDF
OAuth Hacks A gentle introduction to OAuth 2 and Apache Oltu
PPTX
JWT Authentication with AngularJS
PDF
Implementing OAuth
PDF
Building an API Security Ecosystem
ODP
Mohanraj - Securing Your Web Api With OAuth
Implementing OAuth with PHP
OAuth using PHP5
OAuth - Open API Authentication
OAuth Hacks A gentle introduction to OAuth 2 and Apache Oltu
JWT Authentication with AngularJS
Implementing OAuth
Building an API Security Ecosystem
Mohanraj - Securing Your Web Api With OAuth

What's hot (20)

PDF
Demystifying OAuth 2.0
PPTX
REST Service Authetication with TLS & JWTs
PPTX
Elegant Rest Design Webinar
PPTX
Making Sense of API Access Control
PPTX
Token Authentication for Java Applications
PDF
OAuth 2.0
PDF
Stateless authentication for microservices - GR8Conf 2015
PPTX
Securing RESTful APIs using OAuth 2 and OpenID Connect
PPTX
An Introduction to OAuth2
PPTX
Oauth 2.0 security
PPTX
The State of OAuth2
PPTX
Secureyourrestapi 140530183606-phpapp02
PPTX
Build a Node.js Client for Your REST+JSON API
PPTX
OAuth2 + API Security
PPTX
Best Practices in Building an API Security Ecosystem
PPTX
Single-Page-Application & REST security
PPTX
REST API Design for JAX-RS And Jersey
ODP
OAuth2 - Introduction
PDF
Securing REST APIs
PPTX
An Introduction to OAuth 2
Demystifying OAuth 2.0
REST Service Authetication with TLS & JWTs
Elegant Rest Design Webinar
Making Sense of API Access Control
Token Authentication for Java Applications
OAuth 2.0
Stateless authentication for microservices - GR8Conf 2015
Securing RESTful APIs using OAuth 2 and OpenID Connect
An Introduction to OAuth2
Oauth 2.0 security
The State of OAuth2
Secureyourrestapi 140530183606-phpapp02
Build a Node.js Client for Your REST+JSON API
OAuth2 + API Security
Best Practices in Building an API Security Ecosystem
Single-Page-Application & REST security
REST API Design for JAX-RS And Jersey
OAuth2 - Introduction
Securing REST APIs
An Introduction to OAuth 2
Ad

Similar to Using OAuth with PHP (20)

PDF
APIdays Paris 2018 - Learning the OAuth Dance (Without Stepping on Anyone's T...
PDF
Secure Webservices
PDF
OAuth2 & OpenID Connect with Spring Security
PDF
Twitter4R OAuth
PDF
Stateless Microservice Security via JWT and MicroProfile - Mexico
PDF
Stateless Microservice Security via JWT and MicroProfile - ES
PDF
Stateless Microservice Security via JWT and MicroProfile - Guatemala
PDF
The Many Flavors of OAuth - Understand Everything About OAuth2
PPT
UserCentric Identity based Service Invocation
PDF
Deconstructing and Evolving REST security
PDF
The Identity Problem of the Web and how to solve it
PDF
Some OAuth love
PDF
Patterns to Bring Enterprise and Social Identity to the Cloud
PDF
Integrating services with OAuth
PDF
ASFWS 2012 - Contourner les conditions d’utilisation et l’API du service Twit...
PPTX
2023-May.pptx
KEY
Rails 3 and OAuth for Barcamp Tampa
PDF
OAuth and OEmbed
PDF
Oauth Nightmares Abstract OAuth Nightmares
PPTX
Global Azure Bootcamp 2017 - Azure Key Vault
APIdays Paris 2018 - Learning the OAuth Dance (Without Stepping on Anyone's T...
Secure Webservices
OAuth2 & OpenID Connect with Spring Security
Twitter4R OAuth
Stateless Microservice Security via JWT and MicroProfile - Mexico
Stateless Microservice Security via JWT and MicroProfile - ES
Stateless Microservice Security via JWT and MicroProfile - Guatemala
The Many Flavors of OAuth - Understand Everything About OAuth2
UserCentric Identity based Service Invocation
Deconstructing and Evolving REST security
The Identity Problem of the Web and how to solve it
Some OAuth love
Patterns to Bring Enterprise and Social Identity to the Cloud
Integrating services with OAuth
ASFWS 2012 - Contourner les conditions d’utilisation et l’API du service Twit...
2023-May.pptx
Rails 3 and OAuth for Barcamp Tampa
OAuth and OEmbed
Oauth Nightmares Abstract OAuth Nightmares
Global Azure Bootcamp 2017 - Azure Key Vault
Ad

Recently uploaded (20)

PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
gpt5_lecture_notes_comprehensive_20250812015547.pdf
PDF
Assigned Numbers - 2025 - Bluetooth® Document
PDF
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
PDF
Accuracy of neural networks in brain wave diagnosis of schizophrenia
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PPT
Teaching material agriculture food technology
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Unlocking AI with Model Context Protocol (MCP)
PPTX
Tartificialntelligence_presentation.pptx
PPTX
Spectroscopy.pptx food analysis technology
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PPTX
A Presentation on Artificial Intelligence
PPTX
SOPHOS-XG Firewall Administrator PPT.pptx
PDF
Spectral efficient network and resource selection model in 5G networks
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
Reach Out and Touch Someone: Haptics and Empathic Computing
gpt5_lecture_notes_comprehensive_20250812015547.pdf
Assigned Numbers - 2025 - Bluetooth® Document
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
Accuracy of neural networks in brain wave diagnosis of schizophrenia
Encapsulation_ Review paper, used for researhc scholars
MIND Revenue Release Quarter 2 2025 Press Release
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Teaching material agriculture food technology
Network Security Unit 5.pdf for BCA BBA.
The Rise and Fall of 3GPP – Time for a Sabbatical?
Unlocking AI with Model Context Protocol (MCP)
Tartificialntelligence_presentation.pptx
Spectroscopy.pptx food analysis technology
Building Integrated photovoltaic BIPV_UPV.pdf
A Presentation on Artificial Intelligence
SOPHOS-XG Firewall Administrator PPT.pptx
Spectral efficient network and resource selection model in 5G networks
Digital-Transformation-Roadmap-for-Companies.pptx

Using OAuth with PHP

  • 1. Using OAuth with PHP Dave Ingram @dmi 4th November 2010
  • 5. Coming up • What is OAuth? • How do you write a Consumer in PHP? • What doesn’t OAuth do? • Thoughts on being a Provider
  • 6. What is OAuth anyway?
  • 7. A long time ago, in a website not far away. . .
  • 19. YOU REVEAL YOUR USERNAME AND PASSWORD
  • 35. To sign requests, you need: Consumer key Consumer secret (Unique per application) + Access token Access secret (Unique per application user)
  • 36. Step 1: Register with the provider
  • 37. I would like my OAuth application to consume your service please, Mr. Provider.
  • 38. Certainly. I just need to take a few details from you, and we’ll be all set.
  • 41. Step 2: Write your application Step 3: ?????? Step 4: Profit!
  • 42. Step 2: Write your application Step 3: ?????? Step 4: Profit!
  • 43. User Consumer Provider User clicks connect
  • 44. User Consumer Provider C C Ask provider for request token
  • 45. User Consumer Provider C C R R Provider returns request token and request secret
  • 46. User Consumer Provider C C R R R Redirect user to provider
  • 47. User Consumer Provider C C R R R R User logs in/authorises app
  • 48. User Consumer Provider C C R R R R V Provider redirects user back to app with verifier
  • 49. User Consumer Provider C C R R R R V V User’s arrival with verifier notifies app
  • 50. User Consumer Provider C C R R R R V V C C R R V App then exchanges request token for access token
  • 51. User Consumer Provider C C R R R R V V C C R R V A A Provider returns access token and access secret
  • 52. User Consumer Provider C C R R R R V V C C R R V A A C C A A App makes request on user’s behalf
  • 53. Get request token // Create OAuth client object $o = new OAuth( MY_CONSUMER_KEY, MY_CONSUMER_SECRET, OAUTH_SIG_METHOD_HMACSHA1, );
  • 54. 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_secret'];
  • 55. 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_secret']; // Send user to provider's site header('Location: https://api.twitter.com/oauth/authorize'. '?oauth_token='.$response['oauth_token']);
  • 57. 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']);
  • 58. 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');
  • 60. 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: %sn", print_r($json, true));
  • 62. No proof of server identity (use TLS)
  • 63. No proof of server identity (use TLS) No confidentiality (use TLS/SSL)
  • 64. No proof of server identity (use TLS) No confidentiality (use TLS/SSL) No open-source consumer
  • 65. Thoughts on being a Provider
  • 66. Very easy to be a Consumer
  • 67. Very easy to be a Consumer Many design decisions to make as a Provider
  • 68. Very easy to be a Consumer Many design decisions to make as a Provider A fair amount of work, and not always easy to change your mind
  • 69. Very easy to be a Consumer Many design decisions to make as a Provider A fair amount of work, and not always easy to change your mind For example. . .
  • 70. How large a range of timestamps do you allow?
  • 71. How large a range of timestamps do you allow? What permission granularity do you provide?
  • 72. How large a range of timestamps do you allow? What permission granularity do you provide? What format and length are tokens/secrets?
  • 73. 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 particular consumers? (e.g. Twitter)
  • 74. 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 particular consumers? (e.g. Twitter) What about attacks? Phishing, DoS, clickjacking, CSRF
  • 75. 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 particular consumers? (e.g. Twitter) What about attacks? Phishing, DoS, clickjacking, CSRF Beware proxying/caching (use the right headers!)
  • 76. Links OAuth Spec: http://oauth.net/ Intro/tutorial: http://hueniverse.com/ PECL extension: http://pecl.php.net/oauth/ Me: http://twitter.com/dmi http://www.dmi.me.uk/talks/ http://www.dmi.me.uk/code/php/ Slides: http://slideshare.net/ingramd