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

display error if token with same application name exists #27587

Closed
wants to merge 7 commits into from
Closed
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
106 changes: 106 additions & 0 deletions core/Migrations/Version20170406110401.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php
namespace OC\Migrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Types\Type;
use OCP\Migration\ISchemaMigration;

/**
* Auto-generated migration step: Please modify to your needs!
*/
class Version20170406110401 implements ISchemaMigration {

/**
* @param Schema $schema
* @param array $options
*/
public function changeSchema(Schema $schema, array $options) {
$tableName = $options['tablePrefix'] . 'authtoken';

if (!$schema->hasTable($tableName)) {

$table = $schema->createTable($tableName);

$table->addColumn('id', Type::INTEGER, [
'autoincrement' => true,
'unsigned' => true,
'length' => 4
]);

$table->addColumn('uid', Type::STRING, [
'default' => '',
'notnull' => true,
'length' => 64
]);

$table->addColumn('login_name', Type::STRING, [
'default' => '',
'notnull' => true,
'length' => 64
]);

$table->addColumn('password', Type::TEXT, [
'default' => '',
'notnull' => false
]);

$table->addColumn('name', Type::TEXT, [
'default' => '',
'notnull' => true
]);

$table->addColumn('token', Type::STRING, [
'default' => '',
'notnull' => true,
'length' => 200
]);

$table->addColumn('type', Type::SMALLINT, [
'default' => 0,
'notnull' => true,
'unsigned' => true,
'length' => 2
]);

$table->addColumn('last_activity', Type::INTEGER, [
'default' => 0,
'notnull' => true,
'unsigned' => true,
'length' => 4
]);

$table->addColumn('last_check', Type::INTEGER, [
'default' => 0,
'notnull' => true,
'unsigned' => true,
'length' => 4
]);

$table->addIndex(
['token'],
'authtoken_token_index',
[],
[
'unique' => true,
'order' => 'asc'
]
);

$table->addIndex(
['last_activity'],
'authtoken_last_activity_index',
[],
[
'order' => 'asc'
]
);

$table->addUniqueIndex(
['uid', 'name'],
'uid_name_index'
);

$table->setPrimaryKey(['id']);
}
}
}
27 changes: 27 additions & 0 deletions core/Migrations/Version20170406131248.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
namespace OC\Migrations;

use Doctrine\DBAL\Schema\Schema;
use OCP\Migration\ISchemaMigration;

/**
* Auto-generated migration step: Please modify to your needs!
*/
class Version20170406131248 implements ISchemaMigration {

/**
* @param Schema $schema
* @param array $options
*/
public function changeSchema(Schema $schema, array $options) {
$tableName = $options['tablePrefix'] . 'authtoken';
$table = $schema->getTable($tableName);

if (!$table->hasIndex('uid_name_index')) {
$table->addUniqueIndex(
['uid', 'name'],
'uid_name_index'
);
}
}
}
101 changes: 0 additions & 101 deletions db_structure.xml
Original file line number Diff line number Diff line change
Expand Up @@ -797,107 +797,6 @@

</table>

<table>
<name>*dbprefix*authtoken</name>

<declaration>

<field>
<name>id</name>
<type>integer</type>
<default>0</default>
<notnull>true</notnull>
<autoincrement>1</autoincrement>
<unsigned>true</unsigned>
<length>4</length>
</field>

<!-- Foreign Key users::uid -->
<field>
<name>uid</name>
<type>text</type>
<default></default>
<notnull>true</notnull>
<length>64</length>
</field>

<field>
<name>login_name</name>
<type>text</type>
<default></default>
<notnull>true</notnull>
<length>64</length>
</field>

<field>
<name>password</name>
<type>clob</type>
<default></default>
<notnull>false</notnull>
</field>

<field>
<name>name</name>
<type>clob</type>
<default></default>
<notnull>true</notnull>
</field>

<field>
<name>token</name>
<type>text</type>
<default></default>
<notnull>true</notnull>
<length>200</length>
</field>

<field>
<name>type</name>
<type>integer</type>
<default>0</default>
<notnull>true</notnull>
<unsigned>true</unsigned>
<length>2</length>
</field>

<field>
<name>last_activity</name>
<type>integer</type>
<default>0</default>
<notnull>true</notnull>
<unsigned>true</unsigned>
<length>4</length>
</field>

<field>
<name>last_check</name>
<type>integer</type>
<default>0</default>
<notnull>true</notnull>
<unsigned>true</unsigned>
<length>4</length>
</field>

<index>
<name>authtoken_token_index</name>
<unique>true</unique>
<field>
<name>token</name>
<sorting>ascending</sorting>
</field>
</index>

<index>
<name>authtoken_last_activity_index</name>
<field>
<name>last_activity</name>
<sorting>ascending</sorting>
</field>
</index>

</declaration>
</table>

<table>

<!--
Expand Down
28 changes: 20 additions & 8 deletions settings/Controller/AuthSettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

namespace OC\Settings\Controller;

use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
use OC\AppFramework\Http;
use OC\Authentication\Exceptions\InvalidTokenException;
use OC\Authentication\Exceptions\PasswordlessTokenException;
Expand Down Expand Up @@ -130,14 +131,25 @@ public function create($name) {
return $this->getServiceNotAvailableResponse();
}

$token = $this->generateRandomDeviceToken();
$deviceToken = $this->tokenProvider->generateToken($token, $this->uid, $loginName, $password, $name, IToken::PERMANENT_TOKEN);

return [
'token' => $token,
'loginName' => $loginName,
'deviceToken' => $deviceToken
];
try {
$token = $this->generateRandomDeviceToken();
$deviceToken = $this->tokenProvider->generateToken($token, $this->uid, $loginName, $password, $name, IToken::PERMANENT_TOKEN);

return [
'token' => $token,
'loginName' => $loginName,
'deviceToken' => $deviceToken
];
} catch (UniqueConstraintViolationException $exception) {
return new JSONResponse(
[
'error' => [
'message' => 'A token with that name already exists.'
]
],
409
);
}
}

private function getServiceNotAvailableResponse() {
Expand Down
8 changes: 6 additions & 2 deletions settings/js/panels/authtoken_view.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,12 @@
_this._newAppPassword.select();
_this._tokenName.val('');
});
$.when(creatingToken).fail(function() {
OC.Notification.showTemporary(t('core', 'Error while creating device token'));
$.when(creatingToken).fail(function(xhr) {
if (xhr.status === 409) {
OC.Notification.showTemporary(t('core', 'A token for that application already exists. Please choose another application name.'));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@DeepDiver1975 can you get me the translations for that string?
Not sure if i will get translations by putting them into transifex myself (not under the owncloud organisation).

} else {
OC.Notification.showTemporary(t('core', 'Error while creating device token'));
}
});
$.when(creatingToken).always(function() {
_this._toggleAddingToken(false);
Expand Down