/** * @return bool whether the client could be authenticated against the OAuth2 server */ public function authenticate() { $provider = $this->getProvider(); $client = new HttpClient(); $url = $provider->getTokenUrl(); $data = array('grant_type' => 'client_credentials'); if ($this->scope) { $data['scope'] = $this->scope; } YII_DEBUG && Yii::trace("Requesting access token for client from {$url}", 'oauth2.component.clientidentity'); $response = $client->post($url, $data, array(), $this->username, $this->password); $token = AccessToken::parseResponse($response, $provider, $this); if ($token === null) { YII_DEBUG && Yii::trace("Failed to receive client access token", 'oauth2.component.clientidentity'); return false; } else { YII_DEBUG && Yii::trace("Received access token '{$token->token}' for client", 'oauth2.component.clientidentity'); $this->errorCode = self::ERROR_NONE; $token->type = AccessToken::TYPE_CLIENT; $provider->getStorage()->saveToken($this->username, $token); return true; } }
/** * @return bool whether the user could be authenticated against the OAuth2 server */ public function authenticate() { $provider = $this->getProvider(); $client = new HttpClient(); $url = $provider->getTokenUrl(); $data = array('grant_type' => 'password', 'username' => $this->username, 'password' => $this->password); if ($this->scope) { $data['scope'] = $this->scope; } YII_DEBUG && Yii::trace("Requesting access token for user from {$url}", 'oauth2.component.useridentity'); $response = $client->post($url, $data, array(), $provider->clientId, $provider->clientSecret); $token = AccessToken::parseResponse($response, $provider, $this); if ($token === null) { YII_DEBUG && Yii::trace('Access token request for user failed: ' . $response, 'oauth2.component.useridentity'); return false; } else { YII_DEBUG && Yii::trace(sprintf("Received user access token: %s, scope: '%s', expires: %s", $token->token, $token->scope, date('Y-m-d H:i:s', $token->expires)), 'oauth2.component.useridentity'); $this->errorCode = self::ERROR_NONE; $token->type = AccessToken::TYPE_USER; $provider->getStorage()->saveToken($this->username, $token); return true; } }