Skip to content

Commit

Permalink
Add "internal" keys - these do not show up on the API but are valid f…
Browse files Browse the repository at this point in the history
…or authenticating the request. #35
  • Loading branch information
ShaneMcC committed Nov 11, 2018
1 parent c456696 commit 5f45229
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
10 changes: 10 additions & 0 deletions admin/init_functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,16 @@ public function run($pdo) {
$dataChanges[22] = new DBChange(<<<MYSQLQUERY
ALTER TABLE `twofactorkeys` ADD COLUMN `expires` int(11) NOT NULL DEFAULT '0' AFTER `lastused`;
MYSQLQUERY
);

// ------------------------------------------------------------------------
// 2FA Key "internal" keys.
// ------------------------------------------------------------------------
$dataChanges[23] = new DBChange(<<<MYSQLQUERY
ALTER TABLE `twofactorkeys`
ADD COLUMN `internal` ENUM('false', 'true') NOT NULL DEFAULT 'false' AFTER `onetime`,
ADD COLUMN `internaldata` TEXT AFTER `internal`;
MYSQLQUERY
);

return $dataChanges;
Expand Down
18 changes: 18 additions & 0 deletions classes/twofactorkey.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ class TwoFactorKey extends DBObject {
'active' => false,
'type' => 'rfc6238',
'onetime' => false,
'internal' => false,
'internaldata' => NULL,
];
protected static $_key = 'id';
protected static $_table = 'twofactorkeys';
Expand Down Expand Up @@ -74,6 +76,14 @@ public function setOneTime($value) {
return $this->setData('onetime', parseBool($value) ? 'true' : 'false');
}

public function setInternal($value) {
return $this->setData('internal', parseBool($value) ? 'true' : 'false');
}

public function setInternalData($value) {
return $this->setData('internaldata', $value);
}

public function getID() {
return $this->getData('id');
}
Expand Down Expand Up @@ -114,6 +124,14 @@ public function isOneTime() {
return parseBool($this->getData('onetime'));
}

public function isInternal() {
return parseBool($this->getData('internal'));
}

public function getInternalData() {
return parseBool($this->getData('internaldata'));
}

/**
* Keys are usable if:
* - They are active
Expand Down
7 changes: 7 additions & 0 deletions web/1.0/methods/useradmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -435,9 +435,13 @@ protected function get2FAKeys($user) {

$result = [];
foreach ($keys as $k => $v) {
if ($v->isInternal()) { continue; }

$result[$k] = $v->toArray();
unset($result[$k]['id']);
unset($result[$k]['user_id']);
unset($result[$k]['internal']);
unset($result[$k]['internalmeta']);
if ($v->isActive()) {
unset($result[$k]['key']);
}
Expand Down Expand Up @@ -473,9 +477,12 @@ protected function get2FADevices($user) {
protected function get2FAKey($user, $key) {
$k = $key->toArray();
unset($k['user_id']);
unset($k['internal']);
unset($k['internaldata']);
if ($key->isActive()) {
unset($k['key']);
}
$k['usable'] = $key->isUsableKey();

$this->getContextKey('response')->data($k);

Expand Down

0 comments on commit 5f45229

Please sign in to comment.