| 1: | <?php |
| 2: | |
| 3: | |
| 4: | |
| 5: | |
| 6: | |
| 7: | |
| 8: | |
| 9: | |
| 10: | |
| 11: | |
| 12: | class Hm_Oauth2 { |
| 13: | |
| 14: | private $client_id; |
| 15: | private $client_secret; |
| 16: | private $redirect_uri; |
| 17: | private $api; |
| 18: | |
| 19: | |
| 20: | |
| 21: | |
| 22: | |
| 23: | |
| 24: | |
| 25: | public function __construct($id, $secret, $uri) { |
| 26: | $this->client_id = $id; |
| 27: | $this->client_secret = $secret; |
| 28: | $this->redirect_uri = $uri; |
| 29: | $this->api = new Hm_API_Curl(); |
| 30: | } |
| 31: | |
| 32: | |
| 33: | |
| 34: | |
| 35: | |
| 36: | |
| 37: | |
| 38: | |
| 39: | |
| 40: | public function request_authorization_url($url, $scope, $state, $login_hint = false) { |
| 41: | $res = sprintf('%s?response_type=code&scope=%s&state=%s&'. |
| 42: | 'approval_prompt=force&access_type=offline&client_id=%s&redirect_uri=%s', |
| 43: | $url, $scope, $state, $this->client_id, $this->redirect_uri); |
| 44: | if ($login_hint !== false) { |
| 45: | $res .= '&login_hint='.$login_hint; |
| 46: | } |
| 47: | return $res; |
| 48: | } |
| 49: | |
| 50: | |
| 51: | |
| 52: | |
| 53: | |
| 54: | |
| 55: | |
| 56: | |
| 57: | public function request_token($url, $authorization_code, $headers = []) { |
| 58: | return $this->api->command($url, $headers, array('code' => $authorization_code, 'client_id' => $this->client_id, |
| 59: | 'client_secret' => $this->client_secret, 'redirect_uri' => $this->redirect_uri, 'grant_type' => 'authorization_code')); |
| 60: | } |
| 61: | |
| 62: | |
| 63: | |
| 64: | |
| 65: | |
| 66: | |
| 67: | |
| 68: | public function refresh_token($url, $refresh_token) { |
| 69: | return $this->api->command($url, [], array('client_id' => $this->client_id, 'client_secret' => $this->client_secret, |
| 70: | 'refresh_token' => $refresh_token, 'grant_type' => 'refresh_token')); |
| 71: | } |
| 72: | } |
| 73: | |