SlideShare a Scribd company logo
OAuth
Nurulazrad Murad @azrad

     3rd Nov 2012
OAuth using PHP5
look for “primus core”
topics
topics


what is OAuth?
topics


what is OAuth?
writing a Consumer in PHP
traditionally, this is how we do it
OAuth using PHP5
onn ect!
               c

user: azrad
pass: secret
onn ect!
               c

user: azrad
pass: secret


               user: azrad
               pass: secret
onn ect!
               c

user: azrad
pass: secret


               user: azrad
               pass: secret




user: azrad
OAuth using PHP5
you reveal your username
      and password
OAuth using PHP5
who using it?
who using it?
the love triangle
end user




                              consumer application
service provider
end user




                              consumer application
service provider
OAuth goal...
 oAuth is...
OAuth goal...
         oAuth is...


Authentication
•   must logged-in to access the website/application
OAuth goal...
         oAuth is...


Authentication
•   must logged-in to access the website/application

Token-based authentication
•   logged-in user has unique token per application
OAuth goal...
oAuth goal...
OAuth goal...
        oAuth goal...

be simple
•   standard for website API authentication
•   consistent for developers
•   easy for users to understand *
OAuth goal...
           oAuth goal...

  be simple
   •   standard for website API authentication
   •   consistent for developers
   •   easy for users to understand *




* this is hard
OAuth goal...
oAuth goal...
OAuth goal...
         oAuth goal...


be secure
•   secure for users
•   easy to implement security features for developers
•   balance security with ease of use
OAuth goal...
oAuth goal...
OAuth goal...
         oAuth goal...

be open
•   any website can implement OAuth
•   any developer can user OAuth
•   open source client libraries
•   published technical specifications
OAuth goal...
OAuth goal...

be flexible
•   don’t need username and password
•   authentication method agnostic
•   can use OpenID (or not)
•   whatever works best for the web service
•   developers don’t need to handle auth
what the user end sees?
  example from Primus Core Helang Api
OAuth using PHP5
OAuth using PHP5
how does OAuth works?
register a consumer app
register a consumer app

 provide service provider with data about your
 application (name, url...)
register a consumer app

 provide service provider with data about your
 application (name, url...)
 service provider assigns consumer a
 consumer key and consumer secret
register a consumer app

 provide service provider with data about your
 application (name, url...)
 service provider assigns consumer a
 consumer key and consumer secret
 service provider gives documentation of
 authorization URLs and methods
user   consumer   service provider
user             consumer   service provider

 click connect
user             consumer             service provider

 click connect        request token
user             consumer                         service provider

 click connect        request token



                             request token, request secret
user                               consumer                         service provider

 click connect                          request token



                                               request token, request secret
       redirect user to provider
user                               consumer                         service provider

 click connect                          request token



                                               request token, request secret
       redirect user to provider


 user authorise request token
user                               consumer                           service provider

 click connect                          request token



                                               request token, request secret
       redirect user to provider


 user authorise request token


                                                        redirect with verifier
user                               consumer                           service provider

 click connect                          request token



                                               request token, request secret
       redirect user to provider


 user authorise request token


                                                        redirect with verifier

   notifies app with verifier
user                               consumer                           service provider

 click connect                          request token



                                               request token, request secret
       redirect user to provider


 user authorise request token


                                                        redirect with verifier

   notifies app with verifier
                                       request token → access token
user                               consumer                           service provider

 click connect                          request token



                                               request token, request secret
       redirect user to provider


 user authorise request token


                                                        redirect with verifier

   notifies app with verifier
                                       request token → access token


                                               access token, access secret
user                               consumer                           service provider

 click connect                          request token



                                               request token, request secret
       redirect user to provider


 user authorise request token


                                                        redirect with verifier

   notifies app with verifier
                                       request token → access token


                                               access token, access secret
                                        request on user’s behalf
the codes
https://github.com/myelin/fireeagle-php-lib
request token + secret from FE
request token + secret from FE
 if (@$_GET['f'] == 'start') {
   // get a request token + secret from FE and redirect to the authorization
page
   // START step 1
   $fe = new FireEagle($fe_key, $fe_secret);
   $tok = $fe->getRequestToken($fe_callback);
   if (!isset($tok['oauth_token'])
       || !is_string($tok['oauth_token'])
       || !isset($tok['oauth_token_secret'])
       || !is_string($tok['oauth_token_secret'])) {
     echo "ERROR! FireEagle::getRequestToken() returned an invalid
response. Giving up.";
     exit;
   }
   $_SESSION['auth_state'] = "start";
   $_SESSION['request_token'] = $token = $tok['oauth_token'];
   $_SESSION['request_secret'] = $tok['oauth_token_secret'];
   header("Location: ".$fe->getAuthorizeURL($token));
   // END step 1
OAuth using PHP5
OAuth using PHP5
} else if (@$_GET['f'] == 'callback') {
  // the user has authorized us at FE, so now we can pick up our access token + secret
  // START step 2
  if (@$_SESSION['auth_state'] != "start") {
    echo "Out of sequence.";
    exit;
  }
  if ($_GET['oauth_token'] != $_SESSION['request_token']) {
    echo "Token mismatch.";
    exit;
  }
      if ((FireEagle::$FE_OAUTH_VERSION == OAUTH_VERSION_10A)
          && !isset($_GET['oauth_verifier'])) {
          echo "OAuth protocol error. No verifier in response.";
          exit;
      }

 $fe = new FireEagle($fe_key, $fe_secret, $_SESSION['request_token'], $_SESSION['request_secret']);
 $tok = $fe->getAccessToken($_GET['oauth_verifier']);
 if (!isset($tok['oauth_token']) || !is_string($tok['oauth_token'])
     || !isset($tok['oauth_token_secret']) || !is_string($tok['oauth_token_secret'])) {
   error_log("Bad token from FireEagle::getAccessToken(): ".var_export($tok, TRUE));
   echo "ERROR! FireEagle::getAccessToken() returned an invalid response. Giving up.";
   exit;
 }

 $_SESSION['access_token'] = $tok['oauth_token'];
 $_SESSION['access_secret'] = $tok['oauth_token_secret'];
 $_SESSION['auth_state'] = "done";
 header("Location: ".$_SERVER['SCRIPT_NAME']);
                                                                             get access
 // END step 2
                                                                             token + secret
OAuth using PHP5
// we have our access token + secret, so now we can actually *use* the api
  // START step 3
  $fe = new FireEagle($fe_key, $fe_secret, $_SESSION['access_token'], $_SESSION['access_secret']);

  $loc = $fe->user(); // equivalent to $fe->call("user")
  ?><h2>Where you are<?php if ($loc->user->best_guess) echo ": ".htmlspecialchars($loc->user->best_guess-
>name) ?></h2><?php
  if (empty($loc->user->location_hierarchy)) {
    ?><p>Fire Eagle doesn't know where you are yet.</p><?php // '
  } else {
    foreach ($loc->user->location_hierarchy as $location) {
      switch ($location->geotype) {
      case 'point':
        $locinfo = "[".$location->latitude.", ".$location->longitude."]";
        break;
      case 'box':
        $locinfo = "[[".$location->bbox[0][1].", ".$location->bbox[0][0]."], ["
          .$location->bbox[1][1].", ".$location->bbox[1][0]."]]";
        break;
      default:
        $locinfo = "[unknown]";
        break;
      }
      if ($location->best_guess) $locinfo .= " BEST GUESS";
      print "<h3>".htmlspecialchars($location->level_name).": ".htmlspecialchars($location->name)." $locinfo</h3>";
      print "<ul>";
      // turn location object into array, with sorted keys
      $l = array(); foreach ($location as $k => $v) $l[$k] = $v; ksort($l);
      foreach ($l as $k => $v) {
        print "<li>".htmlspecialchars($k).": <b>".htmlspecialchars(var_export($v, TRUE))."</b></li>";
      }
      print "</ul>";
    }
  }
demo
where is info passed?
where is info passed?


http authorisation header
where is info passed?


http authorisation header
http post request body (form params)
where is info passed?


http authorisation header
http post request body (form params)
url query string parameters
security
security

tokens: aren’t passing username/password
security

tokens: aren’t passing username/password
timestamp and nonce: very unique requests
security

tokens: aren’t passing username/password
timestamp and nonce: very unique requests
signature: encrypted parameters help service
provider recognise consumer
security

tokens: aren’t passing username/password
timestamp and nonce: very unique requests
signature: encrypted parameters help service
provider recognise consumer
signature methods: HMAC-SHA1, RSA-SHA1,
plaintext over a secure channel (SSL)
current status of OAuth
current status of OAuth

 oauth.net
current status of OAuth

 oauth.net
 Auth 1.0 protocol (RFC 5849)
current status of OAuth

 oauth.net
 Auth 1.0 protocol (RFC 5849)
 OAuth 2.0 working draft
current status of OAuth

 oauth.net
 Auth 1.0 protocol (RFC 5849)
 OAuth 2.0 working draft
 several libraries for consumers and service
 providers
links

OAuth spec          http://oauth.net
PECL Extension      http://pecl.php.net/oauth
Fireeagle           http://fireeagle.yahoo.net
FE library (PHP)
 https://github.com/myelin/fireeagle-php-lib
thanks!

twitter: @azrad
tumblr: nurulazrad.tumblr.com
works at: www.primuscore.com
credit

OAuth - Open API Authentication by
leahculver on Dec 01, 2007
Implementing OAuth with PHP by Lorna
Mitchell on May 17, 2011
Using OAuth with PHP by David Ingram on
Nov 04, 2010

More Related Content

PDF
Implementing OAuth with PHP
PDF
Implementing OAuth
PPT
Oauth2.0
PPTX
The State of OAuth2
PPTX
(1) OAuth 2.0 Overview
PDF
OAuth - Open API Authentication
PDF
Using OAuth with PHP
PPTX
OAuth 2.0 - The fundamentals, the good , the bad, technical primer and commo...
Implementing OAuth with PHP
Implementing OAuth
Oauth2.0
The State of OAuth2
(1) OAuth 2.0 Overview
OAuth - Open API Authentication
Using OAuth with PHP
OAuth 2.0 - The fundamentals, the good , the bad, technical primer and commo...

What's hot (20)

ODP
OAuth2 - Introduction
PDF
OAuth for your API - The Big Picture
PPTX
An Introduction to OAuth 2
PDF
OAuth big picture
PPTX
OAuth 2
PDF
OAuth 2.0
PPTX
An Introduction to OAuth2
PPTX
Using ArcGIS with OAuth 2.0 - Esri DevSummit Dubai 2013
PDF
Demystifying OAuth 2.0
PDF
Security for oauth 2.0 - @topavankumarj
PDF
The Current State of OAuth 2
PDF
OAuth2 Authentication
PDF
Stateless Auth using OAuth2 & JWT
PPTX
Securing RESTful APIs using OAuth 2 and OpenID Connect
PDF
OAuth Hacks A gentle introduction to OAuth 2 and Apache Oltu
PDF
Stateless authentication for microservices - GR8Conf 2015
PPTX
OAuth2 + API Security
PDF
Stateless authentication for microservices - Greach 2015
PPTX
OAuth 2 at Webvisions
OAuth2 - Introduction
OAuth for your API - The Big Picture
An Introduction to OAuth 2
OAuth big picture
OAuth 2
OAuth 2.0
An Introduction to OAuth2
Using ArcGIS with OAuth 2.0 - Esri DevSummit Dubai 2013
Demystifying OAuth 2.0
Security for oauth 2.0 - @topavankumarj
The Current State of OAuth 2
OAuth2 Authentication
Stateless Auth using OAuth2 & JWT
Securing RESTful APIs using OAuth 2 and OpenID Connect
OAuth Hacks A gentle introduction to OAuth 2 and Apache Oltu
Stateless authentication for microservices - GR8Conf 2015
OAuth2 + API Security
Stateless authentication for microservices - Greach 2015
OAuth 2 at Webvisions
Ad

Similar to OAuth using PHP5 (20)

PPTX
OAuth 2.0 and Mobile Devices: Is that a token in your phone in your pocket or...
KEY
OAuth: demystified (hopefully)
PDF
Oauth Php App
PDF
Distributed Identities with OpenID
PDF
OAuth: Trust Issues
PDF
Integrating services with OAuth
PDF
Securing APIs with OAuth 2.0
PDF
Distributed Identities with OpenID
PDF
OAuth 1.0
PPTX
Smartphone Native Application OP
PPTX
OpenID Connect Demo at OpenID Tech Night
PPTX
Saas webinar-dec6-01
PDF
Draft Ietf Oauth V2 12
PDF
Full stack security
PDF
When and Why Would I use Oauth2?
PPTX
Authentication Server
PDF
The Identity Problem of the Web and how to solve it
PDF
Oauth2.0
KEY
OpenID vs OAuth - Identity on the Web
PPT
Oauth
OAuth 2.0 and Mobile Devices: Is that a token in your phone in your pocket or...
OAuth: demystified (hopefully)
Oauth Php App
Distributed Identities with OpenID
OAuth: Trust Issues
Integrating services with OAuth
Securing APIs with OAuth 2.0
Distributed Identities with OpenID
OAuth 1.0
Smartphone Native Application OP
OpenID Connect Demo at OpenID Tech Night
Saas webinar-dec6-01
Draft Ietf Oauth V2 12
Full stack security
When and Why Would I use Oauth2?
Authentication Server
The Identity Problem of the Web and how to solve it
Oauth2.0
OpenID vs OAuth - Identity on the Web
Oauth
Ad

Recently uploaded (20)

PPTX
Understanding_Digital_Forensics_Presentation.pptx
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Modernizing your data center with Dell and AMD
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Electronic commerce courselecture one. Pdf
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Approach and Philosophy of On baking technology
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
KodekX | Application Modernization Development
PDF
Encapsulation theory and applications.pdf
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PPTX
A Presentation on Artificial Intelligence
PPT
Teaching material agriculture food technology
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PDF
Chapter 3 Spatial Domain Image Processing.pdf
Understanding_Digital_Forensics_Presentation.pptx
The AUB Centre for AI in Media Proposal.docx
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Modernizing your data center with Dell and AMD
Spectral efficient network and resource selection model in 5G networks
Encapsulation_ Review paper, used for researhc scholars
Electronic commerce courselecture one. Pdf
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Per capita expenditure prediction using model stacking based on satellite ima...
Approach and Philosophy of On baking technology
The Rise and Fall of 3GPP – Time for a Sabbatical?
Reach Out and Touch Someone: Haptics and Empathic Computing
KodekX | Application Modernization Development
Encapsulation theory and applications.pdf
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
A Presentation on Artificial Intelligence
Teaching material agriculture food technology
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
Chapter 3 Spatial Domain Image Processing.pdf

OAuth using PHP5

  • 6. topics what is OAuth? writing a Consumer in PHP
  • 7. traditionally, this is how we do it
  • 9. onn ect! c user: azrad pass: secret
  • 10. onn ect! c user: azrad pass: secret user: azrad pass: secret
  • 11. onn ect! c user: azrad pass: secret user: azrad pass: secret user: azrad
  • 13. you reveal your username and password
  • 18. end user consumer application service provider
  • 19. end user consumer application service provider
  • 21. OAuth goal... oAuth is... Authentication • must logged-in to access the website/application
  • 22. OAuth goal... oAuth is... Authentication • must logged-in to access the website/application Token-based authentication • logged-in user has unique token per application
  • 24. OAuth goal... oAuth goal... be simple • standard for website API authentication • consistent for developers • easy for users to understand *
  • 25. OAuth goal... oAuth goal... be simple • standard for website API authentication • consistent for developers • easy for users to understand * * this is hard
  • 27. OAuth goal... oAuth goal... be secure • secure for users • easy to implement security features for developers • balance security with ease of use
  • 29. OAuth goal... oAuth goal... be open • any website can implement OAuth • any developer can user OAuth • open source client libraries • published technical specifications
  • 31. OAuth goal... be flexible • don’t need username and password • authentication method agnostic • can use OpenID (or not) • whatever works best for the web service • developers don’t need to handle auth
  • 32. what the user end sees? example from Primus Core Helang Api
  • 35. how does OAuth works?
  • 37. register a consumer app provide service provider with data about your application (name, url...)
  • 38. register a consumer app provide service provider with data about your application (name, url...) service provider assigns consumer a consumer key and consumer secret
  • 39. register a consumer app provide service provider with data about your application (name, url...) service provider assigns consumer a consumer key and consumer secret service provider gives documentation of authorization URLs and methods
  • 40. user consumer service provider
  • 41. user consumer service provider click connect
  • 42. user consumer service provider click connect request token
  • 43. user consumer service provider click connect request token request token, request secret
  • 44. user consumer service provider click connect request token request token, request secret redirect user to provider
  • 45. user consumer service provider click connect request token request token, request secret redirect user to provider user authorise request token
  • 46. user consumer service provider click connect request token request token, request secret redirect user to provider user authorise request token redirect with verifier
  • 47. user consumer service provider click connect request token request token, request secret redirect user to provider user authorise request token redirect with verifier notifies app with verifier
  • 48. user consumer service provider click connect request token request token, request secret redirect user to provider user authorise request token redirect with verifier notifies app with verifier request token → access token
  • 49. user consumer service provider click connect request token request token, request secret redirect user to provider user authorise request token redirect with verifier notifies app with verifier request token → access token access token, access secret
  • 50. user consumer service provider click connect request token request token, request secret redirect user to provider user authorise request token redirect with verifier notifies app with verifier request token → access token access token, access secret request on user’s behalf
  • 53. request token + secret from FE
  • 54. request token + secret from FE if (@$_GET['f'] == 'start') { // get a request token + secret from FE and redirect to the authorization page // START step 1 $fe = new FireEagle($fe_key, $fe_secret); $tok = $fe->getRequestToken($fe_callback); if (!isset($tok['oauth_token']) || !is_string($tok['oauth_token']) || !isset($tok['oauth_token_secret']) || !is_string($tok['oauth_token_secret'])) { echo "ERROR! FireEagle::getRequestToken() returned an invalid response. Giving up."; exit; } $_SESSION['auth_state'] = "start"; $_SESSION['request_token'] = $token = $tok['oauth_token']; $_SESSION['request_secret'] = $tok['oauth_token_secret']; header("Location: ".$fe->getAuthorizeURL($token)); // END step 1
  • 57. } else if (@$_GET['f'] == 'callback') { // the user has authorized us at FE, so now we can pick up our access token + secret // START step 2 if (@$_SESSION['auth_state'] != "start") { echo "Out of sequence."; exit; } if ($_GET['oauth_token'] != $_SESSION['request_token']) { echo "Token mismatch."; exit; } if ((FireEagle::$FE_OAUTH_VERSION == OAUTH_VERSION_10A) && !isset($_GET['oauth_verifier'])) { echo "OAuth protocol error. No verifier in response."; exit; } $fe = new FireEagle($fe_key, $fe_secret, $_SESSION['request_token'], $_SESSION['request_secret']); $tok = $fe->getAccessToken($_GET['oauth_verifier']); if (!isset($tok['oauth_token']) || !is_string($tok['oauth_token']) || !isset($tok['oauth_token_secret']) || !is_string($tok['oauth_token_secret'])) { error_log("Bad token from FireEagle::getAccessToken(): ".var_export($tok, TRUE)); echo "ERROR! FireEagle::getAccessToken() returned an invalid response. Giving up."; exit; } $_SESSION['access_token'] = $tok['oauth_token']; $_SESSION['access_secret'] = $tok['oauth_token_secret']; $_SESSION['auth_state'] = "done"; header("Location: ".$_SERVER['SCRIPT_NAME']); get access // END step 2 token + secret
  • 59. // we have our access token + secret, so now we can actually *use* the api // START step 3 $fe = new FireEagle($fe_key, $fe_secret, $_SESSION['access_token'], $_SESSION['access_secret']); $loc = $fe->user(); // equivalent to $fe->call("user") ?><h2>Where you are<?php if ($loc->user->best_guess) echo ": ".htmlspecialchars($loc->user->best_guess- >name) ?></h2><?php if (empty($loc->user->location_hierarchy)) { ?><p>Fire Eagle doesn't know where you are yet.</p><?php // ' } else { foreach ($loc->user->location_hierarchy as $location) { switch ($location->geotype) { case 'point': $locinfo = "[".$location->latitude.", ".$location->longitude."]"; break; case 'box': $locinfo = "[[".$location->bbox[0][1].", ".$location->bbox[0][0]."], [" .$location->bbox[1][1].", ".$location->bbox[1][0]."]]"; break; default: $locinfo = "[unknown]"; break; } if ($location->best_guess) $locinfo .= " BEST GUESS"; print "<h3>".htmlspecialchars($location->level_name).": ".htmlspecialchars($location->name)." $locinfo</h3>"; print "<ul>"; // turn location object into array, with sorted keys $l = array(); foreach ($location as $k => $v) $l[$k] = $v; ksort($l); foreach ($l as $k => $v) { print "<li>".htmlspecialchars($k).": <b>".htmlspecialchars(var_export($v, TRUE))."</b></li>"; } print "</ul>"; } }
  • 60. demo
  • 61. where is info passed?
  • 62. where is info passed? http authorisation header
  • 63. where is info passed? http authorisation header http post request body (form params)
  • 64. where is info passed? http authorisation header http post request body (form params) url query string parameters
  • 67. security tokens: aren’t passing username/password timestamp and nonce: very unique requests
  • 68. security tokens: aren’t passing username/password timestamp and nonce: very unique requests signature: encrypted parameters help service provider recognise consumer
  • 69. security tokens: aren’t passing username/password timestamp and nonce: very unique requests signature: encrypted parameters help service provider recognise consumer signature methods: HMAC-SHA1, RSA-SHA1, plaintext over a secure channel (SSL)
  • 71. current status of OAuth oauth.net
  • 72. current status of OAuth oauth.net Auth 1.0 protocol (RFC 5849)
  • 73. current status of OAuth oauth.net Auth 1.0 protocol (RFC 5849) OAuth 2.0 working draft
  • 74. current status of OAuth oauth.net Auth 1.0 protocol (RFC 5849) OAuth 2.0 working draft several libraries for consumers and service providers
  • 75. links OAuth spec http://oauth.net PECL Extension http://pecl.php.net/oauth Fireeagle http://fireeagle.yahoo.net FE library (PHP) https://github.com/myelin/fireeagle-php-lib
  • 77. credit OAuth - Open API Authentication by leahculver on Dec 01, 2007 Implementing OAuth with PHP by Lorna Mitchell on May 17, 2011 Using OAuth with PHP by David Ingram on Nov 04, 2010

Editor's Notes