/application/third_party/hybridauth/Hybrid/Endpoint.php

https://bitbucket.org/paulkish/no-cms · PHP · 217 lines · 176 code · 11 blank · 30 comment · 12 complexity · 0192475c404c9d199406dd2b1303bbd0 MD5 · raw file

  1. <?php
  2. /*!
  3. * HybridAuth
  4. * http://hybridauth.sourceforge.net | http://github.com/hybridauth/hybridauth
  5. * (c) 2009-2012, HybridAuth authors | http://hybridauth.sourceforge.net/licenses.html
  6. */
  7. /**
  8. * Hybrid_Endpoint class
  9. *
  10. * Hybrid_Endpoint class provides a simple way to handle the OpenID and OAuth endpoint.
  11. */
  12. class Hybrid_Endpoint {
  13. public static $request = NULL;
  14. public static $initDone = FALSE;
  15. /**
  16. * Process the current request
  17. *
  18. * $request - The current request parameters. Leave as NULL to default to use $_REQUEST.
  19. */
  20. public static function process( $request = NULL )
  21. {
  22. // Setup request variable
  23. Hybrid_Endpoint::$request = $request;
  24. if ( is_null(Hybrid_Endpoint::$request) ){
  25. // Fix a strange behavior when some provider call back ha endpoint
  26. // with /index.php?hauth.done={provider}?{args}...
  27. // >here we need to recreate the $_REQUEST
  28. if ( strrpos( $_SERVER["QUERY_STRING"], '?' ) ) {
  29. $_SERVER["QUERY_STRING"] = str_replace( "?", "&", $_SERVER["QUERY_STRING"] );
  30. parse_str( $_SERVER["QUERY_STRING"], $_REQUEST );
  31. }
  32. Hybrid_Endpoint::$request = $_REQUEST;
  33. }
  34. // If openid_policy requested, we return our policy document
  35. if ( isset( Hybrid_Endpoint::$request["get"] ) && Hybrid_Endpoint::$request["get"] == "openid_policy" ) {
  36. Hybrid_Endpoint::processOpenidPolicy();
  37. }
  38. // If openid_xrds requested, we return our XRDS document
  39. if ( isset( Hybrid_Endpoint::$request["get"] ) && Hybrid_Endpoint::$request["get"] == "openid_xrds" ) {
  40. Hybrid_Endpoint::processOpenidXRDS();
  41. }
  42. // If we get a hauth.start
  43. if ( isset( Hybrid_Endpoint::$request["hauth_start"] ) && Hybrid_Endpoint::$request["hauth_start"] ) {
  44. Hybrid_Endpoint::processAuthStart();
  45. }
  46. // Else if hauth.done
  47. elseif ( isset( Hybrid_Endpoint::$request["hauth_done"] ) && Hybrid_Endpoint::$request["hauth_done"] ) {
  48. Hybrid_Endpoint::processAuthDone();
  49. }
  50. // Else we advertise our XRDS document, something supposed to be done from the Realm URL page
  51. else {
  52. Hybrid_Endpoint::processOpenidRealm();
  53. }
  54. }
  55. /**
  56. * Process OpenID policy request
  57. */
  58. public static function processOpenidPolicy()
  59. {
  60. $output = file_get_contents( dirname(__FILE__) . "/resources/openid_policy.html" );
  61. print $output;
  62. die();
  63. }
  64. /**
  65. * Process OpenID XRDS request
  66. */
  67. public static function processOpenidXRDS()
  68. {
  69. header("Content-Type: application/xrds+xml");
  70. $output = str_replace
  71. (
  72. "{RETURN_TO_URL}",
  73. str_replace(
  74. array("<", ">", "\"", "'", "&"), array("&lt;", "&gt;", "&quot;", "&apos;", "&amp;"),
  75. Hybrid_Auth::getCurrentUrl( false )
  76. ),
  77. file_get_contents( dirname(__FILE__) . "/resources/openid_xrds.xml" )
  78. );
  79. print $output;
  80. die();
  81. }
  82. /**
  83. * Process OpenID realm request
  84. */
  85. public static function processOpenidRealm()
  86. {
  87. $output = str_replace
  88. (
  89. "{X_XRDS_LOCATION}",
  90. htmlentities( Hybrid_Auth::getCurrentUrl( false ), ENT_QUOTES, 'UTF-8' ) . "?get=openid_xrds&v=" . Hybrid_Auth::$version,
  91. file_get_contents( dirname(__FILE__) . "/resources/openid_realm.html" )
  92. );
  93. print $output;
  94. die();
  95. }
  96. /**
  97. * define:endpoint step 3.
  98. */
  99. public static function processAuthStart()
  100. {
  101. Hybrid_Endpoint::authInit();
  102. $provider_id = trim( strip_tags( Hybrid_Endpoint::$request["hauth_start"] ) );
  103. # check if page accessed directly
  104. if( ! Hybrid_Auth::storage()->get( "hauth_session.$provider_id.hauth_endpoint" ) ) {
  105. Hybrid_Logger::error( "Endpoint: hauth_endpoint parameter is not defined on hauth_start, halt login process!" );
  106. header( "HTTP/1.0 404 Not Found" );
  107. die( "You cannot access this page directly." );
  108. }
  109. # define:hybrid.endpoint.php step 2.
  110. $hauth = Hybrid_Auth::setup( $provider_id );
  111. # if REQUESTed hauth_idprovider is wrong, session not created, etc.
  112. if( ! $hauth ) {
  113. Hybrid_Logger::error( "Endpoint: Invalide parameter on hauth_start!" );
  114. header( "HTTP/1.0 404 Not Found" );
  115. die( "Invalide parameter! Please return to the login page and try again." );
  116. }
  117. try {
  118. Hybrid_Logger::info( "Endpoint: call adapter [{$provider_id}] loginBegin()" );
  119. $hauth->adapter->loginBegin();
  120. }
  121. catch ( Exception $e ) {
  122. Hybrid_Logger::error( "Exception:" . $e->getMessage(), $e );
  123. Hybrid_Error::setError( $e->getMessage(), $e->getCode(), $e->getTraceAsString(), $e );
  124. $hauth->returnToCallbackUrl();
  125. }
  126. die();
  127. }
  128. /**
  129. * define:endpoint step 3.1 and 3.2
  130. */
  131. public static function processAuthDone()
  132. {
  133. Hybrid_Endpoint::authInit();
  134. $provider_id = trim( strip_tags( Hybrid_Endpoint::$request["hauth_done"] ) );
  135. $hauth = Hybrid_Auth::setup( $provider_id );
  136. if( ! $hauth ) {
  137. Hybrid_Logger::error( "Endpoint: Invalide parameter on hauth_done!" );
  138. $hauth->adapter->setUserUnconnected();
  139. header("HTTP/1.0 404 Not Found");
  140. die( "Invalide parameter! Please return to the login page and try again." );
  141. }
  142. try {
  143. Hybrid_Logger::info( "Endpoint: call adapter [{$provider_id}] loginFinish() " );
  144. $hauth->adapter->loginFinish();
  145. }
  146. catch( Exception $e ){
  147. Hybrid_Logger::error( "Exception:" . $e->getMessage(), $e );
  148. Hybrid_Error::setError( $e->getMessage(), $e->getCode(), $e->getTraceAsString(), $e );
  149. $hauth->adapter->setUserUnconnected();
  150. }
  151. Hybrid_Logger::info( "Endpoint: job done. retrun to callback url." );
  152. $hauth->returnToCallbackUrl();
  153. die();
  154. }
  155. public static function authInit()
  156. {
  157. if ( ! Hybrid_Endpoint::$initDone) {
  158. Hybrid_Endpoint::$initDone = TRUE;
  159. # Init Hybrid_Auth
  160. try {
  161. require_once realpath( dirname( __FILE__ ) ) . "/Storage.php";
  162. $storage = new Hybrid_Storage();
  163. // Check if Hybrid_Auth session already exist
  164. if ( ! $storage->config( "CONFIG" ) ) {
  165. header( "HTTP/1.0 404 Not Found" );
  166. die( "You cannot access this page directly." );
  167. }
  168. Hybrid_Auth::initialize( $storage->config( "CONFIG" ) );
  169. }
  170. catch ( Exception $e ){
  171. Hybrid_Logger::error( "Endpoint: Error while trying to init Hybrid_Auth" );
  172. header( "HTTP/1.0 404 Not Found" );
  173. die( "Oophs. Error!" );
  174. }
  175. }
  176. }
  177. }