-
Notifications
You must be signed in to change notification settings - Fork 35
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
Improve token stability by making the API token a "unique" index. #40
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -152,14 +152,7 @@ public function login(SS_HTTPRequest $request) | |
)); | ||
if ( $member ) | ||
{ | ||
$tokenData = $this->generateToken(); | ||
|
||
$tokenDBColumn = $this->tokenConfig['DBColumn']; | ||
$expireDBColumn = $this->tokenConfig['expireDBColumn']; | ||
|
||
$member->{$tokenDBColumn} = $tokenData['token']; | ||
$member->{$expireDBColumn} = $tokenData['expire']; | ||
$member->write(); | ||
$tokenData = $this->updateToken($member); | ||
$member->login(); | ||
} | ||
} | ||
|
@@ -203,16 +196,7 @@ public function logout(SS_HTTPRequest $request) | |
|
||
if ( $this->tokenConfig['owner'] === 'Member' ) | ||
{ | ||
//generate expired token | ||
$tokenData = $this->generateToken( true ); | ||
|
||
//write | ||
$tokenDBColumn = $this->tokenConfig['DBColumn']; | ||
$expireDBColumn = $this->tokenConfig['expireDBColumn']; | ||
|
||
$member->{$tokenDBColumn} = $tokenData['token']; | ||
$member->{$expireDBColumn} = $tokenData['expire']; | ||
$member->write(); | ||
$this->updateToken($member, true); | ||
} | ||
} | ||
} | ||
|
@@ -291,16 +275,7 @@ public function resetToken($id, $expired = false) | |
|
||
if ( $owner ) | ||
{ | ||
//generate token | ||
$tokenData = $this->generateToken( $expired ); | ||
|
||
//write | ||
$tokenDBColumn = $this->tokenConfig['DBColumn']; | ||
$expireDBColumn = $this->tokenConfig['expireDBColumn']; | ||
|
||
$owner->{$tokenDBColumn} = $tokenData['token']; | ||
$owner->{$expireDBColumn} = $tokenData['expire']; | ||
$owner->write(); | ||
$this->updateToken($owner, $expired); | ||
} | ||
else{ | ||
user_error("API Token owner '$ownerClass' not found with ID = $id", E_USER_WARNING); | ||
|
@@ -344,6 +319,40 @@ private function generateToken($expired = false) | |
); | ||
} | ||
|
||
/** | ||
* Update the token of a token owner | ||
* @param $owner The token owner instance to update | ||
* @param bool $expired Set to true to generate an outdated token | ||
* @return array|null Token data array('token' => HASH, 'expire' => EXPIRY_DATE) | ||
*/ | ||
private function updateToken($owner, $expired = false) | ||
{ | ||
$ownerId = null; | ||
$tokenData = null; | ||
|
||
// DB field names | ||
$tokenDBColumn = $this->tokenConfig['DBColumn']; | ||
$expireDBColumn = $this->tokenConfig['expireDBColumn']; | ||
|
||
do { | ||
try { | ||
//generate token | ||
$tokenData = $this->generateToken( $expired ); | ||
|
||
// ensure we never regenerate the same token! | ||
if($owner->{$tokenDBColumn} != $tokenData['token']){ | ||
$owner->{$tokenDBColumn} = $tokenData['token']; | ||
$owner->{$expireDBColumn} = $tokenData['expire']; | ||
$ownerId = $owner->write(); | ||
} | ||
} catch(Exception $e){ | ||
$ownerId = null; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we check the type of exception? In case the write error is something else than a non unique key? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hm that would be sensible, yes. But does the ORM throw a special exception for non-uniqueness of a key? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thought I would raise the question, but no idea if the ORM actually returns the relevant exception info... Might be good to investigate... |
||
} | ||
} while(!$ownerId); | ||
|
||
return $tokenData; | ||
} | ||
|
||
|
||
/** | ||
* Returns the DataObject related to the token | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we use '!==' ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The point where the token would collide is in the Database. So, if we add the type check, do we improve or worsen the stability of the check?
The only distinction on the DB side would be 'null' != null? Otherwise everything should be treated as string (varchar). I think it's sensible to assume both tokens will become strings, so I'd stick with the
!=
check, since checking for type inequality isn't important to the DB... or am I missing something important here?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tokenData should always be a string, so it should be fine to use !== no?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I guess stricter is better here. So using
!==
is fine.