web: Add html include file that makes it easy to create and verify

tokens for delete_account.
This commit is contained in:
Kevin Reed 2018-04-18 15:29:06 -05:00
parent 638f8284a7
commit 81b1cf50f2
2 changed files with 67 additions and 1 deletions

45
html/inc/token.inc Normal file
View File

@ -0,0 +1,45 @@
<?php
// This file is part of BOINC.
// https://boinc.berkeley.edu
// Copyright (C) 2018 University of California
//
// BOINC is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License
// as published by the Free Software Foundation,
// either version 3 of the License, or (at your option) any later version.
//
// BOINC 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 Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with BOINC. If not, see <https://www.gnu.org/licenses/>.
require_once("../inc/boinc_db.inc");
require_once("../inc/util.inc");
// Constants for valid token types
define("TOKEN_TYPE_DELETE_ACCOUNT", "D");
// Constants for token durations
define("TOKEN_DURATION_ONE_DAY", "86400");
function create_confirm_delete_account_token($user) {
$token = random_string();
$ret = BoincToken::insert("(token,userid,type,create_time,expire_time) values ('$token', $user->id, '".TOKEN_TYPE_DELETE_ACCOUNT."', unix_timestamp(), unix_timestamp()+".TOKEN_DURATION_ONE_DAY.")");
if ( !$ret ) {
return null;
}
return $token;
}
function is_valid_delete_account_token($userid, $token) {
$boincToken = BoincToken::lookup_valid_token($userid, $token, TOKEN_TYPE_DELETE_ACCOUNT);
if ( $boincToken == null ) {
return false;
}
return true;
}
?>

View File

@ -1,11 +1,12 @@
#! /usr/bin/env php
<?php
require_once("../inc/util.inc");
require_once("../inc/token.inc");
require_once("../inc/db_ops.inc");
$token = random_string();
BoincToken::insert("(token,userid,type,expire_time) values ('$token', 0, 'T', unix_timestamp()+3600)");
BoincToken::insert("(token,userid,type,create_time, expire_time) values ('$token', 0, 'T', unix_timestamp(), unix_timestamp()+3600)");
$boincTokens = BoincToken::enum("userid=0");
foreach($boincTokens as $boincToken) {
@ -24,5 +25,25 @@ echo $boincToken->type . "\n";
echo $boincToken->create_time . "\n";
echo $boincToken->expire_time . "\n";
echo "---------------\n";
$boincToken = BoincToken::lookup_valid_token(0, $token, 'T');
if ( $boincToken != null ) {
echo "Found valid token\n";
}
echo "---------------\n";
$boincToken = BoincToken::lookup_valid_token(0, 'notrealtoken', 'T');
if ( $boincToken == null ) {
echo "Successfully didn't find invalid token\n";
}
echo "---------------\n";
$user = new BoincUser();
$user->id=0;
$token = create_confirm_delete_account_token($user);
if ( is_valid_delete_account_token($user->id, $token) ) {
echo "Successfully created and validated delete account token";
}
?>