| | |
| | | |
| | | function addOAuthActions():void |
| | | { |
| | | if ($this->isSetUp()) { |
| | | $actions = [ |
| | | [ |
| | | 'name' => 'Connect to Service', |
| | |
| | | ], |
| | | [ |
| | | 'name' => 'Disconnect from Service', |
| | | 'label' => 'Disconnect', |
| | | 'icon' => 'plugs-disconnected', |
| | | 'action' => [$this, 'handleOAuthDisconnect'] |
| | | ], |
| | |
| | | $this->addAction($a['name'], $a['action'], $a['slug']??null, $a['icon']??null, $a['label']??null); |
| | | } |
| | | } |
| | | } |
| | | public function hasOAuthCredentials(?int $userID = null):bool |
| | | { |
| | | if ($userID !== $this->userID) { |
| | |
| | | |
| | | public function revokeOAuth():bool |
| | | { |
| | | if (empty($this->oauth['revoke'])) { |
| | | if (empty($this->oauth->getRevoke())) { |
| | | error_log('No revoke url set for '.$this->service_name); |
| | | return false; |
| | | } |
| | |
| | | if (empty($credentials['access_token'])) { |
| | | error_log('Could not send revoke request, no access_token for '.$this->service_name); |
| | | } else { |
| | | $response = $this->postOAuthRequest($this->oauth['revoke'], [ |
| | | $response = $this->postOAuthRequest($this->oauth->getRevoke(), [ |
| | | 'body' => [ |
| | | 'token' => $credentials['access_token'] |
| | | ] |
| | |
| | | $credentials = $this->loadCredentials(); |
| | | $hasCredentials = $this->hasValidOAuth(); |
| | | $returnURL = $this->getReturnURL(); |
| | | |
| | | if ($hasCredentials) { |
| | | if (!$this->isSetUp()) { |
| | | return ''; |
| | | } |
| | | if (!$hasCredentials) { |
| | | return sprintf( |
| | | '<div class="oauth-connect"> |
| | | <h3>Connect to your %s account</h3> |
| | | <a href="%s" class="btn jvb-oauth-connect" data-service="%s">%s Authorize Connection</a>', |
| | | <a href="%s" class="btn jvb-oauth-connect" data-service="%s">%s Authorize Connection</a> |
| | | </div>', |
| | | $this->title, |
| | | $this->getOAuthURL($returnURL), |
| | | esc_attr($this->service_name), |
| | | $this->outputIcon($this->icon) |
| | | ); |
| | | } |
| | | } else { |
| | | $form = sprintf( |
| | | '<details open> |
| | | <summary>Connected Account |
| | |
| | | if (method_exists($this, 'renderOAuthConnectedOptions')) { |
| | | $form .= $this->renderOAuthConnectedOptions(); |
| | | } |
| | | $form .= '</details'; |
| | | $form .= '</details>'; |
| | | return $form; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * @return string Outputs additional information after the basic OAuth connection |
| | |
| | | { |
| | | |
| | | $credentials = $this->loadCredentials(); |
| | | if (empty($this->oauth['authorize'])) { |
| | | if (empty($this->oauth->getAuthorize())) { |
| | | $this->logError('getOAuthUrl', 'OAuth authorize URL not configured'); |
| | | return ''; |
| | | } |
| | |
| | | $params = $this->addOAuthParams($params); |
| | | } |
| | | |
| | | return $this->oauth['authorize'] . '?' . http_build_query($params); |
| | | return $this->oauth->getAuthorize() . '?' . http_build_query($params); |
| | | } |
| | | |
| | | protected function getRedirectUri():string |