PageRenderTime 54ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

/lib/web/oauth2.php

http://github.com/bcosca/fatfree
PHP | 163 lines | 78 code | 15 blank | 70 comment | 9 complexity | 051bd235e4f7d837291af652178117f6 MD5 | raw file
Possible License(s): GPL-3.0
  1. <?php
  2. /*
  3. Copyright (c) 2009-2019 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. //! Encoding
  22. $enc_type = PHP_QUERY_RFC1738;
  23. /**
  24. * Return OAuth2 authentication URI
  25. * @return string
  26. * @param $endpoint string
  27. * @param $query bool
  28. **/
  29. function uri($endpoint,$query=TRUE) {
  30. return $endpoint.($query?('?'.
  31. http_build_query($this->args,null,'&',$this->enc_type)):'');
  32. }
  33. /**
  34. * Send request to API/token endpoint
  35. * @return string|array|FALSE
  36. * @param $uri string
  37. * @param $method string
  38. * @param $token string
  39. **/
  40. function request($uri,$method,$token=NULL) {
  41. $web=\Web::instance();
  42. $options=[
  43. 'method'=>$method,
  44. 'content'=>http_build_query($this->args,null,'&',$this->enc_type),
  45. 'header'=>['Accept: application/json']
  46. ];
  47. if ($token)
  48. array_push($options['header'],'Authorization: Bearer '.$token);
  49. elseif ($method=='POST' && isset($this->args['client_id']))
  50. array_push($options['header'],'Authorization: Basic '.
  51. base64_encode(
  52. $this->args['client_id'].':'.
  53. $this->args['client_secret']
  54. )
  55. );
  56. $response=$web->request($uri,$options);
  57. if ($response['error'])
  58. user_error($response['error'],E_USER_ERROR);
  59. if (isset($response['body'])) {
  60. if (preg_grep('/^Content-Type:.*application\/json/i',
  61. $response['headers'])) {
  62. $token=json_decode($response['body'],TRUE);
  63. if (isset($token['error_description']))
  64. user_error($token['error_description'],E_USER_ERROR);
  65. if (isset($token['error']))
  66. user_error($token['error'],E_USER_ERROR);
  67. return $token;
  68. }
  69. else
  70. return $response['body'];
  71. }
  72. return FALSE;
  73. }
  74. /**
  75. * Parse JSON Web token
  76. * @return array
  77. * @param $token string
  78. **/
  79. function jwt($token) {
  80. return json_decode(
  81. base64_decode(
  82. str_replace(['-','_'],['+','/'],explode('.',$token)[1])
  83. ),
  84. TRUE
  85. );
  86. }
  87. /**
  88. * change default url encoding type, i.E. PHP_QUERY_RFC3986
  89. * @param $type
  90. */
  91. function setEncoding($type) {
  92. $this->enc_type = $type;
  93. }
  94. /**
  95. * URL-safe base64 encoding
  96. * @return array
  97. * @param $data string
  98. **/
  99. function b64url($data) {
  100. return trim(strtr(base64_encode($data),'+/','-_'),'=');
  101. }
  102. /**
  103. * Return TRUE if scope/claim exists
  104. * @return bool
  105. * @param $key string
  106. **/
  107. function exists($key) {
  108. return isset($this->args[$key]);
  109. }
  110. /**
  111. * Bind value to scope/claim
  112. * @return string
  113. * @param $key string
  114. * @param $val string
  115. **/
  116. function set($key,$val) {
  117. return $this->args[$key]=$val;
  118. }
  119. /**
  120. * Return value of scope/claim
  121. * @return mixed
  122. * @param $key string
  123. **/
  124. function &get($key) {
  125. if (isset($this->args[$key]))
  126. $val=&$this->args[$key];
  127. else
  128. $val=NULL;
  129. return $val;
  130. }
  131. /**
  132. * Remove scope/claim
  133. * @return NULL
  134. * @param $key string
  135. **/
  136. function clear($key=NULL) {
  137. if ($key)
  138. unset($this->args[$key]);
  139. else
  140. $this->args=[];
  141. }
  142. }