-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
…ttings Add admin interface to enforce 2FA
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* @copyright 2018 Christoph Wurst <christoph@winzerhof-wurst.at> | ||
* | ||
* @author 2018 Christoph Wurst <christoph@winzerhof-wurst.at> | ||
* | ||
* @license GNU AGPL version 3 or any later version | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
namespace OC\Settings\Controller; | ||
|
||
use OC\Authentication\TwoFactorAuth\MandatoryTwoFactor; | ||
use OCP\AppFramework\Controller; | ||
use OCP\AppFramework\Http\JSONResponse; | ||
use OCP\AppFramework\Http\Response; | ||
use OCP\IRequest; | ||
use OCP\JSON; | ||
|
||
class TwoFactorSettingsController extends Controller { | ||
|
||
/** @var MandatoryTwoFactor */ | ||
private $mandatoryTwoFactor; | ||
|
||
public function __construct(string $appName, | ||
IRequest $request, | ||
MandatoryTwoFactor $mandatoryTwoFactor) { | ||
parent::__construct($appName, $request); | ||
|
||
$this->mandatoryTwoFactor = $mandatoryTwoFactor; | ||
} | ||
|
||
public function index(): Response { | ||
return new JSONResponse([ | ||
'enabled' => $this->mandatoryTwoFactor->isEnforced(), | ||
]); | ||
} | ||
|
||
public function update(bool $enabled): Response { | ||
$this->mandatoryTwoFactor->setEnforced($enabled); | ||
|
||
return new JSONResponse([ | ||
'enabled' => $enabled | ||
]); | ||
} | ||
|
||
} |
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
This file was deleted.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
This file was deleted.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
This file was deleted.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<template> | ||
<div> | ||
<p> | ||
{{ t('settings', 'Two-factor authentication can be enforced for all users. If they do not have a two-factor provider configured, they will be unable to log into the system.') }} | ||
</p> | ||
<p v-if="loading"> | ||
<span class="icon-loading-small two-factor-loading"></span> | ||
<span>{{ t('settings', 'Enforce two-factor authentication') }}</span> | ||
</p> | ||
<p v-else> | ||
<input type="checkbox" | ||
id="two-factor-enforced" | ||
class="checkbox" | ||
v-model="enabled" | ||
v-on:change="onEnforcedChanged"> | ||
<label for="two-factor-enforced">{{ t('settings', 'Enforce two-factor authentication') }}</label> | ||
</p> | ||
</div> | ||
</template> | ||
|
||
<script> | ||
import Axios from 'nextcloud-axios' | ||
export default { | ||
name: "AdminTwoFactor", | ||
data () { | ||
return { | ||
enabled: false, | ||
loading: false | ||
} | ||
}, | ||
mounted () { | ||
this.loading = true | ||
Axios.get(OC.generateUrl('/settings/api/admin/twofactorauth')) | ||
.then(resp => resp.data) | ||
.then(state => { | ||
this.enabled = state.enabled | ||
this.loading = false | ||
console.info('loaded') | ||
}) | ||
.catch(err => { | ||
console.error(error) | ||
this.loading = false | ||
throw err | ||
}) | ||
}, | ||
methods: { | ||
onEnforcedChanged () { | ||
this.loading = true | ||
const data = { | ||
enabled: this.enabled | ||
} | ||
Axios.put(OC.generateUrl('/settings/api/admin/twofactorauth'), data) | ||
.then(resp => resp.data) | ||
.then(state => { | ||
this.enabled = state.enabled | ||
this.loading = false | ||
}) | ||
.catch(err => { | ||
console.error(error) | ||
this.loading = false | ||
throw err | ||
}) | ||
} | ||
} | ||
} | ||
</script> | ||
|
||
<style> | ||
.two-factor-loading { | ||
display: inline-block; | ||
vertical-align: sub; | ||
margin-left: -2px; | ||
margin-right: 1px; | ||
} | ||
</style> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import Vue from 'vue' | ||
|
||
import AdminTwoFactor from './components/AdminTwoFactor' | ||
|
||
Vue.prototype.t = t; | ||
|
||
new Vue({ | ||
el: '#two-factor-auth-settings', | ||
template: '<AdminTwoFactor/>', | ||
components: { | ||
AdminTwoFactor | ||
} | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?php | ||
/** | ||
* @copyright 2018 Christoph Wurst <christoph@winzerhof-wurst.at> | ||
* | ||
* @author 2018 Christoph Wurst <christoph@winzerhof-wurst.at> | ||
* | ||
* @license GNU AGPL version 3 or any later version | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
namespace Tests\Settings\Controller; | ||
|
||
use OC\Authentication\TwoFactorAuth\MandatoryTwoFactor; | ||
use OC\Settings\Controller\TwoFactorSettingsController; | ||
use OCP\AppFramework\Http\JSONResponse; | ||
use OCP\IRequest; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
use Test\TestCase; | ||
|
||
class TwoFactorSettingsControllerTest extends TestCase { | ||
|
||
/** @var IRequest|MockObject */ | ||
private $request; | ||
|
||
/** @var MandatoryTwoFactor|MockObject */ | ||
private $mandatoryTwoFactor; | ||
|
||
/** @var TwoFactorSettingsController */ | ||
private $controller; | ||
|
||
protected function setUp() { | ||
parent::setUp(); | ||
|
||
$this->request = $this->createMock(IRequest::class); | ||
$this->mandatoryTwoFactor = $this->createMock(MandatoryTwoFactor::class); | ||
|
||
$this->controller = new TwoFactorSettingsController( | ||
'settings', | ||
$this->request, | ||
$this->mandatoryTwoFactor | ||
); | ||
} | ||
|
||
public function testIndex() { | ||
$this->mandatoryTwoFactor->expects($this->once()) | ||
->method('isEnforced') | ||
->willReturn(true); | ||
$expected = new JSONResponse([ | ||
'enabled' => true, | ||
]); | ||
|
||
$resp = $this->controller->index(); | ||
|
||
$this->assertEquals($expected, $resp); | ||
} | ||
|
||
public function testUpdate() { | ||
$this->mandatoryTwoFactor->expects($this->once()) | ||
->method('setEnforced') | ||
->with(true); | ||
$expected = new JSONResponse([ | ||
'enabled' => true, | ||
]); | ||
|
||
$resp = $this->controller->update(true); | ||
|
||
$this->assertEquals($expected, $resp); | ||
} | ||
|
||
} |