oauth2.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. **/
  26. function uri($endpoint) {
  27. return $endpoint.'?'.http_build_query($this->args);
  28. }
  29. /**
  30. * Send request to API/token endpoint
  31. * @return string|FALSE
  32. * @param $uri string
  33. * @param $method string
  34. * @param $token array
  35. **/
  36. function request($uri,$method,$token=NULL) {
  37. $web=\Web::instance();
  38. $options=[
  39. 'method'=>$method,
  40. 'content'=>http_build_query($this->args),
  41. 'header'=>['Accept: application/json']
  42. ];
  43. if ($token)
  44. array_push($options['header'],'Authorization: Bearer '.$token);
  45. elseif ($method=='POST')
  46. array_push($options['header'],'Authorization: Basic '.
  47. base64_encode(
  48. $this->args['client_id'].':'.
  49. $this->args['client_secret']
  50. )
  51. );
  52. $response=$web->request($uri,$options);
  53. if ($response['error'])
  54. user_error($response['error'],E_USER_ERROR);
  55. return $response['body'] &&
  56. preg_grep('/HTTP\/1\.\d 200/',$response['headers'])?
  57. json_decode($response['body'],TRUE):
  58. FALSE;
  59. }
  60. /**
  61. * Parse JSON Web token
  62. * @return array
  63. * @param $token string
  64. **/
  65. function jwt($token) {
  66. return json_decode(
  67. base64_decode(
  68. str_replace(
  69. ['-','_'],
  70. ['+','/'],
  71. explode('.',$token)[1]
  72. )
  73. ),
  74. TRUE
  75. );
  76. }
  77. /**
  78. * Return TRUE if scope/claim exists
  79. * @return bool
  80. * @param $key string
  81. **/
  82. function exists($key) {
  83. return isset($this->args[$key]);
  84. }
  85. /**
  86. * Bind value to scope/claim
  87. * @return string
  88. * @param $key string
  89. * @param $val string
  90. **/
  91. function set($key,$val) {
  92. return $this->args[$key]=$val;
  93. }
  94. /**
  95. * Return value of scope/claim
  96. * @return mixed
  97. * @param $key string
  98. **/
  99. function &get($key) {
  100. if (isset($this->args[$key]))
  101. $val=&$this->args[$key];
  102. else
  103. $val=NULL;
  104. return $val;
  105. }
  106. /**
  107. * Remove scope/claim
  108. * @return NULL
  109. * @param $key string
  110. **/
  111. function clear($key=NULL) {
  112. if ($key)
  113. unset($this->args[$key]);
  114. else
  115. $this->args=[];
  116. }
  117. }