This repository was archived by the owner on Jan 8, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 153
Expand file tree
/
Copy pathrefreshProjectToken.php
More file actions
74 lines (62 loc) · 1.69 KB
/
refreshProjectToken.php
File metadata and controls
74 lines (62 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?php
/**
* Refresh project token
*
* @author Timo Tijhof
* @since 1.0.0
* @package TestSwarm
*/
define( 'SWARM_ENTRY', 'SCRIPT' );
require_once __DIR__ . '/../inc/init.php';
class RefreshProjectTokenScript extends MaintenanceScript {
protected function init() {
$this->setDescription(
'Refresh the authentication token of a project. Invalides the current token and returns an newly generated one.'
);
$this->registerOption( 'id', 'value', 'ID of project.' );
$this->registerOption( 'quick', 'boolean', 'Skip confirmation.' );
}
protected function execute() {
$db = $this->getContext()->getDB();
$id = $this->getOption( 'id' );
// Verify parameters
if ( !$id ) {
$this->error( '--id is required.' );
}
$checkId = $db->getOne(str_queryf(
'SELECT
id
FROM projects
WHERE id = %s;',
$id
));
if ( !$checkId || $checkId !== $id ) {
$this->error( 'Project "' . $id . '" does not exist.' );
}
if ( !$this->getOption( 'quick' ) ) {
$this->timeWarningForScriptWill( 'invalidate the existing token' );
}
// New token
$authToken = LoginAction::generateRandomHash( 40 );
$authTokenHash = sha1( $authToken );
$isUpdated = $db->query(str_queryf(
'UPDATE projects
SET
auth_token = %s
WHERE id = %s
LIMIT 1;',
$authTokenHash,
$id
));
if ( !$isUpdated ) {
$this->error( 'Updating of row into database failed.' );
}
$this->out(
'Authentication token of project "' . $id . '" has been succesfully refreshed!' . PHP_EOL
. 'The following auth token has been generated for this project:' . PHP_EOL
. $authToken . PHP_EOL . PHP_EOL
);
}
}
$script = RefreshProjectTokenScript::newFromContext( $swarmContext );
$script->run();