Skip to content

Commit

Permalink
Added token generation method, and make it check the database to enfo…
Browse files Browse the repository at this point in the history
…rce unique tokens. Fixes #3295
  • Loading branch information
isaiahdw committed Jan 20, 2011
1 parent 1d70bde commit 9720af8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
3 changes: 2 additions & 1 deletion auth-schema-mysql.sql
Expand Up @@ -32,7 +32,8 @@ CREATE TABLE IF NOT EXISTS `user_tokens` (
`id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`user_id` int(11) UNSIGNED NOT NULL,
`user_agent` varchar(40) NOT NULL,
`token` varchar(32) NOT NULL,
`token` varchar(40) NOT NULL,
`type` varchar(100) NOT NULL,

This comment has been minimized.

Copy link
@biakaveron

biakaveron Mar 29, 2011

Member

Whats the purpose of this column? It seems unused.

`created` int(10) UNSIGNED NOT NULL,
`expires` int(10) UNSIGNED NOT NULL,
PRIMARY KEY (`id`),
Expand Down
21 changes: 18 additions & 3 deletions classes/model/auth/user/token.php
Expand Up @@ -21,9 +21,6 @@ public function __construct($id = NULL)
{
parent::__construct($id);

// Set the now, we use this a lot
$this->_now = time();

if (mt_rand(1, 100) === 1)
{
// Do garbage collection
Expand Down Expand Up @@ -52,4 +49,22 @@ public function delete_expired()
return $this;
}

public function create(Validation $validation = NULL)
{
$this->token = $this->create_token();

return parent::create($validation);
}

protected function create_token()
{
do
{
$token = sha1(uniqid(Text::random('alnum', 32), TRUE));
}
while(ORM::factory('user_token', array('token' => $token))->loaded());

return $token;
}

} // End Auth User Token Model

0 comments on commit 9720af8

Please sign in to comment.