Skip to content

Commit 0a874c5

Browse files
GretaDrullzer
authored andcommitted
Disable app token creation for impersonated people, ref #15539
Signed-off-by: Greta Doci <gretadoci@gmail.com>
1 parent d231fc9 commit 0a874c5

File tree

7 files changed

+81
-2
lines changed

7 files changed

+81
-2
lines changed

apps/files_external/lib/Migration/DummyUserSession.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,24 @@ public function getUser() {
5050
public function isLoggedIn() {
5151
return !is_null($this->user);
5252
}
53+
54+
/**
55+
* get getImpersonatingUserID
56+
*
57+
* @return string|null
58+
* @since 17.0.0
59+
*/
60+
public function getImpersonatingUserID() : ?string {
61+
return null;
62+
}
63+
64+
/**
65+
* set setImpersonatingUserID
66+
*
67+
* @since 17.0.0
68+
*/
69+
public function setImpersonatingUserID(bool $useCurrentUser = true): void {
70+
//no OP
71+
}
72+
5373
}

lib/private/User/Session.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,29 @@ public function getLoginName() {
314314
return null;
315315
}
316316

317+
/**
318+
* @return mixed
319+
*/
320+
public function getImpersonatingUserID(): ?string {
321+
322+
return $this->session->get('oldUserId');
323+
324+
}
325+
326+
public function setImpersonatingUserID(bool $useCurrentUser = true): void {
327+
if ($useCurrentUser === false) {
328+
$this->session->remove('oldUserId');
329+
return;
330+
}
331+
332+
$currentUser = $this->getUser();
333+
334+
if ($currentUser === null) {
335+
throw new \OC\User\NoUserException();
336+
}
337+
$this->session->set('oldUserId', $currentUser->getUID());
338+
339+
}
317340
/**
318341
* set the token id
319342
*

lib/public/IUserSession.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
interface IUserSession {
4343
/**
4444
* Do a user login
45+
*
4546
* @param string $user the username
4647
* @param string $password the password
4748
* @return bool true if successful
@@ -52,6 +53,7 @@ public function login($user, $password);
5253
/**
5354
* Logs the user out including all the session data
5455
* Logout, destroys session
56+
*
5557
* @return void
5658
* @since 6.0.0
5759
*/
@@ -80,4 +82,19 @@ public function getUser();
8082
* @since 8.0.0
8183
*/
8284
public function isLoggedIn();
85+
86+
/**
87+
* get getImpersonatingUserID
88+
*
89+
* @return string|null
90+
* @since 18.0.0
91+
*/
92+
public function getImpersonatingUserID(): ?string;
93+
94+
/**
95+
* set setImpersonatingUserID
96+
*
97+
* @since 18.0.0
98+
*/
99+
public function setImpersonatingUserID(bool $useCurrentUser = true): void;
83100
}

settings/Controller/AuthSettingsController.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
use OCP\ILogger;
4545
use OCP\IRequest;
4646
use OCP\ISession;
47+
use OCP\IUserSession;
4748
use OCP\Security\ISecureRandom;
4849
use OCP\Session\Exceptions\SessionNotAvailableException;
4950

@@ -55,6 +56,9 @@ class AuthSettingsController extends Controller {
5556
/** @var ISession */
5657
private $session;
5758

59+
/** IUserSession */
60+
private $userSession;
61+
5862
/** @var string */
5963
private $uid;
6064

@@ -77,6 +81,7 @@ class AuthSettingsController extends Controller {
7781
* @param ISession $session
7882
* @param ISecureRandom $random
7983
* @param string|null $userId
84+
* @param IUserSession $userSession
8085
* @param IManager $activityManager
8186
* @param RemoteWipe $remoteWipe
8287
* @param ILogger $logger
@@ -87,12 +92,14 @@ public function __construct(string $appName,
8792
ISession $session,
8893
ISecureRandom $random,
8994
?string $userId,
95+
IUserSession $userSession,
9096
IManager $activityManager,
9197
RemoteWipe $remoteWipe,
9298
ILogger $logger) {
9399
parent::__construct($appName, $request);
94100
$this->tokenProvider = $tokenProvider;
95101
$this->uid = $userId;
102+
$this->userSession = $userSession;
96103
$this->session = $session;
97104
$this->random = $random;
98105
$this->activityManager = $activityManager;
@@ -114,6 +121,10 @@ public function create($name) {
114121
} catch (SessionNotAvailableException $ex) {
115122
return $this->getServiceNotAvailableResponse();
116123
}
124+
if ($this->userSession->getImpersonatingUserID() !== null)
125+
{
126+
return $this->getServiceNotAvailableResponse();
127+
}
117128

118129
try {
119130
$sessionToken = $this->tokenProvider->getToken($sessionId);

settings/Settings/Personal/Security.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,18 @@ public function getForm(): TemplateResponse {
8080
$passwordChangeSupported = $user->canChangePassword();
8181
}
8282

83+
$this->initialStateService->provideInitialState(
84+
'settings',
85+
'can_create_app_token',
86+
$this->userSession->getImpersonatingUserID() !== null
87+
);
88+
8389
return new TemplateResponse('settings', 'settings/personal/security', [
8490
'passwordChangeSupported' => $passwordChangeSupported,
8591
'twoFactorProviderData' => $this->getTwoFactorProviderData(),
8692
'themedark' => $this->config->getUserValue($this->uid, 'accessibility', 'theme', false)
8793
]);
94+
8895
}
8996

9097
public function getSection(): string {

settings/src/components/AuthTokenSection.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
@rename="rename"
2929
@delete="deleteToken"
3030
@wipe="wipeToken" />
31-
<AuthTokenSetupDialogue :add="addNewToken" />
31+
<AuthTokenSetupDialogue v-if="canCreateToken" :add="addNewToken" />
3232
</div>
3333
</template>
3434

@@ -63,7 +63,7 @@
6363
props: {
6464
tokens: {
6565
type: Array,
66-
requried: true,
66+
required: true,
6767
},
6868
},
6969
components: {

settings/src/main-personal-security.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,6 @@ const View = Vue.extend(AuthTokenSection);
3535
new View({
3636
propsData: {
3737
tokens: OCP.InitialState.loadState('settings', 'app_tokens'),
38+
canCreateToken: OCP.InitialState.loadState('settings', 'can_create_app_token'),
3839
}
3940
}).$mount('#security-authtokens');

0 commit comments

Comments
 (0)