-
Notifications
You must be signed in to change notification settings - Fork 0
Adds PHP implementation of client-side OTP check #1
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
base: master
Are you sure you want to change the base?
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 |
|---|---|---|
|
|
@@ -85,6 +85,7 @@ private function makePairResponse($result) | |
| return array( | ||
| 'id' => $result['id'], | ||
| 'enabled' => $result['enabled'], | ||
| 'secret' => $result['secret'], | ||
| 'userId' => $result['user']['id'], | ||
| 'userName' => $result['user']['name'] | ||
| ); | ||
|
|
@@ -138,6 +139,49 @@ private function request($method, $endpoint, $parameters = []) | |
| } | ||
| return $decoded; | ||
| } | ||
|
|
||
|
|
||
| /////////////////////////////////////////////////////////////// | ||
| // OTP VALIDATION | ||
| /////////////////////////////////////////////////////////////// | ||
| public static function oath_hotp($seed, $counter, $otpLength) | ||
| { | ||
| $bin_counter = pack('N*', 0) . pack('N*', $counter); // Counter must be 64-bit int | ||
| $hash = hash_hmac ('sha1', $bin_counter, $seed, true); | ||
|
|
||
| return str_pad(self::oathTruncate($hash, $otpLength), $otpLength, '0', STR_PAD_LEFT); | ||
| } | ||
|
|
||
| public static function verifyOtp($utf8seed, $otp, $drift=0, $windowSize = 2, $otpResolutionSeconds = 30, $otpLength = 6) { | ||
|
|
||
| $timeStamp = floor(microtime(true)/$otpResolutionSeconds); | ||
|
|
||
| $binarySeed = utf8_decode($utf8seed); | ||
|
|
||
| for ($i = $drift - $windowSize; $i <= $drift + $windowSize; $i++){ | ||
| $ts = $timeStamp + $i; | ||
| if (self::oath_hotp($binarySeed, $ts, $otpLength) == $otp) | ||
| return array(true, $i); | ||
| } | ||
|
|
||
| return array (false, 0); | ||
|
|
||
| } | ||
|
|
||
| public static function oathTruncate($hash, $otpLength) | ||
|
Member
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. What does this do? I see a lot of bit manipulation.
Member
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. Best explanation is http://en.wikipedia.org/wiki/HMAC-based_One-time_Password_Algorithm. Truncate is: This implementation is just a php port of the truncate algorithm from the oath reference implementation |
||
| { | ||
| $offset = ord($hash[19]) & 0xf; | ||
|
|
||
| return ( | ||
| ((ord($hash[$offset+0]) & 0x7f) << 24 ) | | ||
| ((ord($hash[$offset+1]) & 0xff) << 16 ) | | ||
| ((ord($hash[$offset+2]) & 0xff) << 8 ) | | ||
| (ord($hash[$offset+3]) & 0xff) | ||
| ) % pow(10, $otpLength); | ||
| } | ||
|
|
||
|
|
||
|
|
||
| } | ||
|
|
||
| ?> | ||
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 wording here tripped me up a bit--I wasn't sure what my options were. Perhaps we could reword to something like this:
Press [ENTER] to validate using the Toopher API or enter the OTP generated by the Toopher app: