oauth2.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. /*
  3. Copyright (c) 2009-2017 F3::Factory/Bong Cosca, All rights reserved.
  4. This file is part of the Fat-Free Framework (http://fatfreeframework.com).
  5. This is free software: you can redistribute it and/or modify it under the
  6. terms of the GNU General Public License as published by the Free Software
  7. Foundation, either version 3 of the License, or later.
  8. Fat-Free Framework is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License along
  13. with Fat-Free Framework. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. namespace Web;
  16. //! Lightweight OAuth2 client
  17. class OAuth2 extends \Magic {
  18. protected
  19. //! Scopes and claims
  20. $args=[];
  21. /**
  22. * Return OAuth2 authentication URI
  23. * @return string
  24. * @param $endpoint string
  25. * @param $query bool
  26. **/
  27. function uri($endpoint,$query=TRUE) {
  28. return $endpoint.($query?('?'.http_build_query($this->args)):'');
  29. }
  30. /**
  31. * Send request to API/token endpoint
  32. * @return string|FALSE
  33. * @param $uri string
  34. * @param $method string
  35. * @param $token array
  36. **/
  37. function request($uri,$method,$token=NULL) {
  38. $web=\Web::instance();
  39. $options=[
  40. 'method'=>$method,
  41. 'content'=>http_build_query($this->args),
  42. 'header'=>['Accept: application/json']
  43. ];
  44. if ($token)
  45. array_push($options['header'],'Authorization: Bearer '.$token);
  46. elseif ($method=='POST' && isset($this->args['client_id']))
  47. array_push($options['header'],'Authorization: Basic '.
  48. base64_encode(
  49. $this->args['client_id'].':'.
  50. $this->args['client_secret']
  51. )
  52. );
  53. $response=$web->request($uri,$options);
  54. if ($response['error'])
  55. user_error($response['error'],E_USER_ERROR);
  56. if (isset($response['body'])) {
  57. if (preg_grep('/^Content-Type:.*application\/json/i',
  58. $response['headers'])) {
  59. $token=json_decode($response['body'],TRUE);
  60. if (isset($token['error_description']))
  61. user_error($token['error_description'],E_USER_ERROR);
  62. if (isset($token['error']))
  63. user_error($token['error'],E_USER_ERROR);
  64. return $token;
  65. }
  66. else
  67. return $response['body'];
  68. }
  69. return FALSE;
  70. }
  71. /**
  72. * Parse JSON Web token
  73. * @return array
  74. * @param $token string
  75. **/
  76. function jwt($token) {
  77. return json_decode(
  78. base64_decode(
  79. str_replace(['-','_'],['+','/'],explode('.',$token)[1])
  80. ),
  81. TRUE
  82. );
  83. }
  84. /**
  85. * URL-safe base64 encoding
  86. * @return array
  87. * @param $data string
  88. **/
  89. function b64url($data) {
  90. return trim(strtr(base64_encode($data),'+/','-_'),'=');
  91. }
  92. /**
  93. * Return TRUE if scope/claim exists
  94. * @return bool
  95. * @param $key string
  96. **/
  97. function exists($key) {
  98. return isset($this->args[$key]);
  99. }
  100. /**
  101. * Bind value to scope/claim
  102. * @return string
  103. * @param $key string
  104. * @param $val string
  105. **/
  106. function set($key,$val) {
  107. return $this->args[$key]=$val;
  108. }
  109. /**
  110. * Return value of scope/claim
  111. * @return mixed
  112. * @param $key string
  113. **/
  114. function &get($key) {
  115. if (isset($this->args[$key]))
  116. $val=&$this->args[$key];
  117. else
  118. $val=NULL;
  119. return $val;
  120. }
  121. /**
  122. * Remove scope/claim
  123. * @return NULL
  124. * @param $key string
  125. **/
  126. function clear($key=NULL) {
  127. if ($key)
  128. unset($this->args[$key]);
  129. else
  130. $this->args=[];
  131. }
  132. }