diff --git a/html/inc/token.inc b/html/inc/token.inc new file mode 100644 index 0000000000..bfb6d5d4fb --- /dev/null +++ b/html/inc/token.inc @@ -0,0 +1,45 @@ +. + +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; +} + +?> diff --git a/html/ops/test_token.php b/html/ops/test_token.php index a9bb123754..ab71136df8 100755 --- a/html/ops/test_token.php +++ b/html/ops/test_token.php @@ -1,11 +1,12 @@ #! /usr/bin/env php 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"; +} ?>