Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OP#40534 delete anything to do with API tokens #46

Merged
merged 1 commit into from
Feb 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions lib/Controller/ConfigController.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ public function setConfig(array $values): DataResponse {
$this->config->deleteUserValue($this->userId, Application::APP_ID, 'user_name');
$this->config->deleteUserValue($this->userId, Application::APP_ID, 'refresh_token');
$this->config->deleteUserValue($this->userId, Application::APP_ID, 'last_notification_check');
$this->config->deleteUserValue($this->userId, Application::APP_ID, 'token_type');
$result = [
'user_name' => '',
];
Expand Down Expand Up @@ -139,7 +138,6 @@ public function oauthRedirect(string $code = '', string $state = ''): RedirectRe
if (isset($result['access_token']) && isset($result['refresh_token'])) {
$accessToken = $result['access_token'];
$this->config->setUserValue($this->userId, Application::APP_ID, 'token', $accessToken);
$this->config->setUserValue($this->userId, Application::APP_ID, 'token_type', 'oauth');
$refreshToken = $result['refresh_token'];
$this->config->setUserValue($this->userId, Application::APP_ID, 'refresh_token', $refreshToken);
// get user info
Expand Down Expand Up @@ -174,7 +172,6 @@ public function oauthRedirect(string $code = '', string $state = ''): RedirectRe
* @return array{error?: string, user_name?: string, errorMesssage?: string}
*/
private function storeUserInfo(string $accessToken): array {
$tokenType = $this->config->getUserValue($this->userId, Application::APP_ID, 'token_type');
$refreshToken = $this->config->getUserValue($this->userId, Application::APP_ID, 'refresh_token');
$clientID = $this->config->getAppValue(Application::APP_ID, 'client_id');
$clientSecret = $this->config->getAppValue(Application::APP_ID, 'client_secret');
Expand All @@ -184,7 +181,7 @@ private function storeUserInfo(string $accessToken): array {
return ['error' => 'OpenProject URL is invalid'];
}

$info = $this->openprojectAPIService->request($openprojectUrl, $accessToken, $tokenType, $refreshToken, $clientID, $clientSecret, $this->userId, 'users/me');
$info = $this->openprojectAPIService->request($openprojectUrl, $accessToken, $refreshToken, $clientID, $clientSecret, $this->userId, 'users/me');
if (isset($info['lastName'], $info['firstName'], $info['id'])) {
$fullName = $info['firstName'] . ' ' . $info['lastName'];
$this->config->setUserValue($this->userId, Application::APP_ID, 'user_id', $info['id']);
Expand Down
15 changes: 5 additions & 10 deletions lib/Controller/OpenProjectAPIController.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,6 @@ class OpenProjectAPIController extends Controller {
* @var string
*/
private $accessToken;
/**
* @var string
*/
private $tokenType;
/**
* @var string
*/
Expand All @@ -65,7 +61,6 @@ public function __construct(string $appName,
$this->openprojectAPIService = $openprojectAPIService;
$this->userId = $userId;
$this->accessToken = $config->getUserValue($userId, Application::APP_ID, 'token');
$this->tokenType = $config->getUserValue($userId, Application::APP_ID, 'token_type');
$this->refreshToken = $config->getUserValue($userId, Application::APP_ID, 'refresh_token');
$this->clientID = $config->getAppValue(Application::APP_ID, 'client_id');
$this->clientSecret = $config->getAppValue(Application::APP_ID, 'client_secret');
Expand Down Expand Up @@ -96,7 +91,7 @@ public function getOpenProjectUrl(): DataResponse {
*/
public function getOpenProjectAvatar(string $userId = '', string $userName = '') {
$result = $this->openprojectAPIService->getOpenProjectAvatar(
$this->openprojectUrl, $this->accessToken, $this->tokenType, $this->refreshToken,
$this->openprojectUrl, $this->accessToken, $this->refreshToken,
$this->clientID, $this->clientSecret, $userId, $userName
);
$response = new DataDownloadResponse(
Expand All @@ -118,7 +113,7 @@ public function getNotifications(?string $since = null): DataResponse {
return new DataResponse('', 400);
}
$result = $this->openprojectAPIService->getNotifications(
$this->openprojectUrl, $this->accessToken, $this->tokenType, $this->refreshToken, $this->clientID, $this->clientSecret, $this->userId, $since, 7
$this->openprojectUrl, $this->accessToken, $this->refreshToken, $this->clientID, $this->clientSecret, $this->userId, $since, 7
);
if (!isset($result['error'])) {
$response = new DataResponse($result);
Expand All @@ -141,7 +136,7 @@ public function getSearchedWorkPackages(?string $searchQuery = null): DataRespon
return new DataResponse('', 400);
}
$result = $this->openprojectAPIService->searchWorkPackage(
$this->openprojectUrl, $this->accessToken, 'oauth', $this->refreshToken, $this->clientID, $this->clientSecret, $this->userId, $searchQuery
$this->openprojectUrl, $this->accessToken, $this->refreshToken, $this->clientID, $this->clientSecret, $this->userId, $searchQuery
);
if (!isset($result['error'])) {
$response = new DataResponse($result);
Expand All @@ -165,7 +160,7 @@ public function getOpenProjectWorkPackageStatus(string $id): DataResponse {
return new DataResponse('', 400);
}
$result = $this->openprojectAPIService->getOpenProjectWorkPackageStatus(
$this->openprojectUrl, $this->accessToken, 'oauth', $this->refreshToken, $this->clientID, $this->clientSecret, $this->userId, $id
$this->openprojectUrl, $this->accessToken, $this->refreshToken, $this->clientID, $this->clientSecret, $this->userId, $id
);
if (!isset($result['error'])) {
$response = new DataResponse($result);
Expand All @@ -189,7 +184,7 @@ public function getOpenProjectWorkPackageType(string $id): DataResponse {
return new DataResponse('', 400);
}
$result = $this->openprojectAPIService->getOpenProjectWorkPackageType(
$this->openprojectUrl, $this->accessToken, 'oauth', $this->refreshToken, $this->clientID, $this->clientSecret, $this->userId, $id
$this->openprojectUrl, $this->accessToken, $this->refreshToken, $this->clientID, $this->clientSecret, $this->userId, $id
);
if (!isset($result['error'])) {
$response = new DataResponse($result);
Expand Down
3 changes: 1 addition & 2 deletions lib/Search/OpenProjectSearchProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ public function search(IUser $user, ISearchQuery $query): SearchResult {

$openprojectUrl = $this->config->getAppValue(Application::APP_ID, 'oauth_instance_url');
$accessToken = $this->config->getUserValue($user->getUID(), Application::APP_ID, 'token');
$tokenType = $this->config->getUserValue($user->getUID(), Application::APP_ID, 'token_type');
$refreshToken = $this->config->getUserValue($user->getUID(), Application::APP_ID, 'refresh_token');
$clientID = $this->config->getAppValue(Application::APP_ID, 'client_id');
$clientSecret = $this->config->getAppValue(Application::APP_ID, 'client_secret');
Expand All @@ -136,7 +135,7 @@ public function search(IUser $user, ISearchQuery $query): SearchResult {
return SearchResult::paginated($this->getName(), [], 0);
}

$searchResults = $this->service->searchWorkPackage($openprojectUrl, $accessToken, $tokenType, $refreshToken, $clientID, $clientSecret, $user->getUID(), $term);
$searchResults = $this->service->searchWorkPackage($openprojectUrl, $accessToken, $refreshToken, $clientID, $clientSecret, $user->getUID(), $term);
$searchResults = array_slice($searchResults, $offset, $limit);

if (isset($searchResults['error'])) {
Expand Down
43 changes: 14 additions & 29 deletions lib/Service/OpenProjectAPIService.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ private function checkNotificationsForUser(string $userId): void {
$accessToken = $this->config->getUserValue($userId, Application::APP_ID, 'token');
$notificationEnabled = ($this->config->getUserValue($userId, Application::APP_ID, 'notification_enabled', '0') === '1');
if ($accessToken && $notificationEnabled) {
$tokenType = $this->config->getUserValue($userId, Application::APP_ID, 'token_type');
$refreshToken = $this->config->getUserValue($userId, Application::APP_ID, 'refresh_token');
$clientID = $this->config->getAppValue(Application::APP_ID, 'client_id');
$clientSecret = $this->config->getAppValue(Application::APP_ID, 'client_secret');
Expand All @@ -118,7 +117,7 @@ private function checkNotificationsForUser(string $userId): void {
if ($myOPUserId !== '') {
$myOPUserId = (int) $myOPUserId;
$notifications = $this->getNotifications(
$openprojectUrl, $accessToken, $tokenType, $refreshToken, $clientID, $clientSecret, $userId, $lastNotificationCheck
$openprojectUrl, $accessToken, $refreshToken, $clientID, $clientSecret, $userId, $lastNotificationCheck
);
if (!isset($notifications['error']) && count($notifications) > 0) {
$newLastNotificationCheck = $notifications[0]['updatedAt'];
Expand Down Expand Up @@ -190,7 +189,6 @@ public function now(): string {
/**
* @param string $url
* @param string $accessToken
* @param string $authType
* @param string $refreshToken
* @param string $clientID
* @param string $clientSecret
Expand All @@ -199,7 +197,7 @@ public function now(): string {
* @param ?int $limit
* @return array<mixed>
*/
public function getNotifications(string $url, string $accessToken, string $authType,
public function getNotifications(string $url, string $accessToken,
string $refreshToken, string $clientID, string $clientSecret, string $userId,
?string $since = null, ?int $limit = null): array {
if ($since) {
Expand All @@ -218,7 +216,7 @@ public function getNotifications(string $url, string $accessToken, string $authT
// 'limit' => $limit,
];
$result = $this->request(
$url, $accessToken, $authType, $refreshToken, $clientID, $clientSecret, $userId, 'work_packages', $params
$url, $accessToken, $refreshToken, $clientID, $clientSecret, $userId, 'work_packages', $params
);
if (isset($result['error'])) {
return $result;
Expand All @@ -236,7 +234,6 @@ public function getNotifications(string $url, string $accessToken, string $authT
/**
* @param string $url
* @param string $accessToken
* @param string $authType
* @param string $refreshToken
* @param string $clientID
* @param string $clientSecret
Expand All @@ -247,7 +244,7 @@ public function getNotifications(string $url, string $accessToken, string $authT
* @return array<string>
* @throws \Safe\Exceptions\JsonException
*/
public function searchWorkPackage(string $url, string $accessToken, string $authType,
public function searchWorkPackage(string $url, string $accessToken,
string $refreshToken, string $clientID, string $clientSecret, string $userId,
string $query, int $offset = 0, int $limit = 5): array {
$resultsById = [];
Expand All @@ -263,7 +260,7 @@ public function searchWorkPackage(string $url, string $accessToken, string $auth
// 'limit' => $limit,
];
$searchDescResult = $this->request(
$url, $accessToken, $authType, $refreshToken, $clientID, $clientSecret, $userId, 'work_packages', $params
$url, $accessToken, $refreshToken, $clientID, $clientSecret, $userId, 'work_packages', $params
);

if (isset($searchDescResult['_embedded'], $searchDescResult['_embedded']['elements'])) {
Expand All @@ -282,7 +279,7 @@ public function searchWorkPackage(string $url, string $accessToken, string $auth
// 'limit' => $limit,
];
$searchSubjectResult = $this->request(
$url, $accessToken, $authType, $refreshToken, $clientID, $clientSecret, $userId, 'work_packages', $params
$url, $accessToken, $refreshToken, $clientID, $clientSecret, $userId, 'work_packages', $params
);

if (isset($searchSubjectResult['_embedded'], $searchSubjectResult['_embedded']['elements'])) {
Expand All @@ -299,7 +296,6 @@ public function searchWorkPackage(string $url, string $accessToken, string $auth
*
* @param string $url
* @param string $accessToken
* @param string $authType
* @param string $refreshToken
* @param string $clientID
* @param string $clientSecret
Expand All @@ -311,15 +307,12 @@ public function searchWorkPackage(string $url, string $accessToken, string $auth
* @throws \OCP\Lock\LockedException
*/
public function getOpenProjectAvatar(string $url,
string $accessToken, string $authType, string $refreshToken, string $clientID, string $clientSecret,
string $accessToken, string $refreshToken, string $clientID, string $clientSecret,
string $userId, string $userName): array {
$url = $url . '/api/v3/users/' . $userId . '/avatar';
$authHeader = ($authType === 'access')
? 'Basic ' . base64_encode('apikey:' . $accessToken)
: 'Bearer ' . $accessToken;
$options = [
'headers' => [
'Authorization' => $authHeader,
'Authorization' => 'Bearer ' . $accessToken,
'User-Agent' => 'Nextcloud OpenProject integration',
]
];
Expand All @@ -341,7 +334,6 @@ public function getOpenProjectAvatar(string $url,
/**
* @param string $openprojectUrl
* @param string $accessToken
* @param string $authType
* @param string $refreshToken
* @param string $clientID
* @param string $clientSecret
Expand All @@ -352,17 +344,14 @@ public function getOpenProjectAvatar(string $url,
* @return array<mixed>
* @throws \OCP\PreConditionNotMetException
*/
public function request(string $openprojectUrl, string $accessToken, string $authType, string $refreshToken,
public function request(string $openprojectUrl, string $accessToken, string $refreshToken,
string $clientID, string $clientSecret, string $userId,
string $endPoint, array $params = [], string $method = 'GET'): array {
try {
$url = $openprojectUrl . '/api/v3/' . $endPoint;
$authHeader = ($authType === 'access')
? 'Basic ' . base64_encode('apikey:' . $accessToken)
: 'Bearer ' . $accessToken;
$options = [
'headers' => [
'Authorization' => $authHeader,
'Authorization' => 'Bearer ' . $accessToken,
'User-Agent' => 'Nextcloud OpenProject integration',
]
];
Expand Down Expand Up @@ -410,7 +399,7 @@ public function request(string $openprojectUrl, string $accessToken, string $aut
$body = (string) $response->getBody();
// refresh token if it's invalid and we are using oauth
// response can be : 'OAuth2 token is expired!', 'Invalid token!' or 'Not authorized'
if ($response->getStatusCode() === 401 && $authType === 'oauth') {
if ($response->getStatusCode() === 401) {
$this->logger->info('Trying to REFRESH the access token', ['app' => $this->appName]);
// try to refresh the token
$result = $this->requestOAuthAccessToken($openprojectUrl, [
Expand All @@ -424,7 +413,7 @@ public function request(string $openprojectUrl, string $accessToken, string $aut
$this->config->setUserValue($userId, Application::APP_ID, 'token', $accessToken);
// retry the request with new access token
return $this->request(
$openprojectUrl, $accessToken, $authType, $refreshToken, $clientID, $clientSecret, $userId, $endPoint, $params, $method
$openprojectUrl, $accessToken, $refreshToken, $clientID, $clientSecret, $userId, $endPoint, $params, $method
);
}
}
Expand Down Expand Up @@ -505,7 +494,6 @@ public static function validateOpenProjectURL(string $openprojectUrl): bool {
*
* @param string $url
* @param string $accessToken
* @param string $authType
* @param string $refreshToken
* @param string $clientID
* @param string $clientSecret
Expand All @@ -516,14 +504,13 @@ public static function validateOpenProjectURL(string $openprojectUrl): bool {
public function getOpenProjectWorkPackageStatus(
string $url,
string $accessToken,
string $authType,
string $refreshToken,
string $clientID,
string $clientSecret,
string $userId,
string $statusId): array {
$result = $this->request(
$url, $accessToken, $authType, $refreshToken, $clientID, $clientSecret, $userId, 'statuses/' . $statusId);
$url, $accessToken, $refreshToken, $clientID, $clientSecret, $userId, 'statuses/' . $statusId);
if (!isset($result['id'])) {
return ['error' => 'Malformed response'];
}
Expand All @@ -535,7 +522,6 @@ public function getOpenProjectWorkPackageStatus(
*
* @param string $url
* @param string $accessToken
* @param string $authType
* @param string $refreshToken
* @param string $clientID
* @param string $clientSecret
Expand All @@ -546,15 +532,14 @@ public function getOpenProjectWorkPackageStatus(
public function getOpenProjectWorkPackageType(
string $url,
string $accessToken,
string $authType,
string $refreshToken,
string $clientID,
string $clientSecret,
string $userId,
string $typeId
): array {
$result = $this->request(
$url, $accessToken, $authType, $refreshToken, $clientID, $clientSecret, $userId, 'types/' . $typeId);
$url, $accessToken, $refreshToken, $clientID, $clientSecret, $userId, 'types/' . $typeId);
if (!isset($result['id'])) {
return ['error' => 'Malformed response'];
}
Expand Down
3 changes: 0 additions & 3 deletions tests/lib/Controller/ConfigControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,12 @@ public function setUpMocks(): void {
->withConsecutive(
['testUser', 'integration_openproject', 'oauth_state'],
['testUser', 'integration_openproject', 'redirect_uri'],
['testUser', 'integration_openproject', 'token_type'],
['testUser', 'integration_openproject', 'refresh_token'],
)
->willReturnOnConsecutiveCalls(
'randomString',
'http://redirect.back.to.here/some/url',
'oauth',
'oAuthRefreshToken',

);

$this->configController = new ConfigController(
Expand Down
7 changes: 3 additions & 4 deletions tests/lib/Controller/OpenProjectAPIControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,8 @@ public function getUserValueMock($token = '123') {
->method('getUserValue')
->withConsecutive(
['test','integration_openproject', 'token'],
['test','integration_openproject', 'token_type'],
['test','integration_openproject', 'refresh_token'],
)->willReturnOnConsecutiveCalls($token, 'oauth', 'refreshToken');
)->willReturnOnConsecutiveCalls($token, 'refreshToken');
}

/**
Expand Down Expand Up @@ -116,7 +115,7 @@ public function testGetOpenProjectAvatar() {
->getMock();
$service->expects($this->once())
->method('getOpenProjectAvatar')
->with($this->anything(),
->with(
$this->anything(),
$this->anything(),
$this->anything(),
Expand Down Expand Up @@ -154,7 +153,7 @@ public function testGetOpenProjectAvatarNoType() {
->getMock();
$service->expects($this->once())
->method('getOpenProjectAvatar')
->with($this->anything(),
->with(
$this->anything(),
$this->anything(),
$this->anything(),
Expand Down
Loading