OpenIDStrategy.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. /**
  3. * OpenID strategy for Opauth
  4. *
  5. * Implemented with Mewp's LightOpenID Library,
  6. * included at Vendor/lightopenid
  7. * (https://gitorious.org/lightopenid/lightopenid)
  8. *
  9. * More information on Opauth: http://opauth.org
  10. *
  11. * @copyright Copyright © 2012 U-Zyn Chua (http://uzyn.com)
  12. * @link http://opauth.org
  13. * @package Opauth.OpenIDStrategy
  14. * @license MIT License
  15. */
  16. /**
  17. * OpenID strategy for Opauth
  18. *
  19. * @package Opauth.OpenIDStrategy
  20. */
  21. class OpenIDStrategy extends OpauthStrategy{
  22. /**
  23. * Compulsory config keys, listed as unassociative arrays
  24. */
  25. public $expects = array();
  26. /**
  27. * Optional config keys, without predefining any default values.
  28. */
  29. public $optionals = array();
  30. /**
  31. * Optional config keys with respective default values, listed as associative arrays
  32. * eg. array('scope' => 'email');
  33. */
  34. public $defaults = array(
  35. // Refer to http://openid.net/specs/openid-attribute-properties-list-1_0-01.html if
  36. // you wish to overwrite these
  37. 'required' => array(
  38. 'contact/email',
  39. 'namePerson',
  40. 'namePerson/first',
  41. 'namePerson/last',
  42. 'namePerson/friendly'
  43. ),
  44. 'optional' => array(
  45. 'contact/phone',
  46. 'contact/web',
  47. 'media/image'
  48. ),
  49. 'identifier_form' => 'identifier_request.html'
  50. );
  51. public function __construct($strategy, $env){
  52. parent::__construct($strategy, $env);
  53. $parsed = parse_url($this->env['host']);
  54. require dirname(__FILE__).'/Vendor/lightopenid/openid.php';
  55. $this->openid = new LightOpenID($parsed['host']);
  56. $this->openid->required = $this->strategy['required'];
  57. $this->openid->optional = $this->strategy['optional'];
  58. }
  59. /**
  60. * Ask for OpenID identifer
  61. */
  62. public function request(){
  63. if (!$this->openid->mode){
  64. if (empty($_POST['openid_url'])){
  65. $this->render($this->strategy['identifier_form']);
  66. }
  67. else{
  68. $this->openid->identity = $_POST['openid_url'];
  69. try{
  70. $this->redirect($this->openid->authUrl());
  71. } catch (Exception $e){
  72. $error = array(
  73. 'provider' => 'OpenID',
  74. 'code' => 'bad_identifier',
  75. 'message' => $e->getMessage()
  76. );
  77. $this->errorCallback($error);
  78. }
  79. }
  80. }
  81. elseif ($this->openid->mode == 'cancel'){
  82. $error = array(
  83. 'provider' => 'OpenID',
  84. 'code' => 'cancel_authentication',
  85. 'message' => 'User has canceled authentication'
  86. );
  87. $this->errorCallback($error);
  88. }
  89. elseif (!$this->openid->validate()){
  90. $error = array(
  91. 'provider' => 'OpenID',
  92. 'code' => 'not_logged_in',
  93. 'message' => 'User has not logged in'
  94. );
  95. $this->errorCallback($error);
  96. }
  97. else{
  98. $attributes = $this->openid->getAttributes();
  99. $this->auth = array(
  100. 'provider' => 'OpenID',
  101. 'uid' => $this->openid->identity,
  102. 'info' => array(),
  103. 'credentials' => array(),
  104. 'raw' => $this->openid->getAttributes()
  105. );
  106. if (!empty($attributes['contact/email'])) $this->auth['info']['email'] = $attributes['contact/email'];
  107. if (!empty($attributes['namePerson'])) $this->auth['info']['name'] = $attributes['namePerson'];
  108. if (!empty($attributes['fullname'])) $this->auth['info']['name'] = $attributes['fullname'];
  109. if (!empty($attributes['namePerson/first'])) $this->auth['info']['first_name'] = $attributes['namePerson/first'];
  110. if (!empty($attributes['namePerson/last'])) $this->auth['info']['last_name'] = $attributes['namePerson/last'];
  111. if (!empty($attributes['namePerson/friendly'])) $this->auth['info']['nickname'] = $attributes['namePerson/friendly'];
  112. if (!empty($attributes['contact/phone'])) $this->auth['info']['phone'] = $attributes['contact/phone'];
  113. if (!empty($attributes['contact/web'])) $this->auth['info']['urls']['website'] = $attributes['contact/web'];
  114. if (!empty($attributes['media/image'])) $this->auth['info']['image'] = $attributes['media/image'];
  115. $this->callback();
  116. }
  117. }
  118. /**
  119. * Render a view
  120. */
  121. protected function render($view, $exit = true){
  122. require($view);
  123. if ($exit) exit();
  124. }
  125. }