Skip to content

Commit

Permalink
Added code expiration time to output
Browse files Browse the repository at this point in the history
  • Loading branch information
charger88 committed May 19, 2017
1 parent b3c4cbe commit 41382f3
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ Add SMS Verification endpoints to your routing file:

For example, if an app wants to send an authorization code to a cell phone 855-123-8765

1. Send a POST `/sms-verification` API to URL https://api.example.com/sms-verification with JSON body `{ "phone_number" : "+18551238765" }`
2. The API returns `{"success":true,"description":"OK"}` if the code is sent
1. Send a POST `/sms-verification` API to URL https://api.example.com/sms-verification with JSON body `{"phone_number" : "+18551238765"}`
2. The API returns `{"success":true,"description":"OK","expires_at": 1495120612}` if the code is sent. `expires_at` is actual time of code expiration.
3. The cell phone will receive a 6-digit code (for example: 782025)
4. In order to verify the code, send a Get /sms-verification API to `https://api.example.com/sms-verification/782025/+18551238765`
5. The API returns:
* On success: `{"success":true,"description":"OK"}`
* On success: `{"success":true,"description":"OK","expires_at": 1495120612}`
* On failure: `{"success":false,"description":"Wrong code"}`

### Exceptions
Expand Down
7 changes: 7 additions & 0 deletions src/CodeProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,11 @@ private function trimPhoneNumber($phoneNumber){
return trim(ltrim($phoneNumber, '+'));
}

/**
* @return int Seconds
*/
public function getLifetime(){
return $this->minutesLifetime * 60;
}

}
8 changes: 8 additions & 0 deletions src/SmsVerification.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ class SmsVerification
public static function sendCode($phoneNumber)
{
$exceptionClass = null;
$expiresAt = null;
try {
static::validatePhoneNumber($phoneNumber);
$now = time();
$code = CodeProcessor::getInstance()->generateCode($phoneNumber);
$translationCode = config('sms-verification.message-translation-code');
$text = $translationCode
Expand All @@ -35,6 +37,9 @@ public static function sendCode($phoneNumber)
}
$success = $sender->send($phoneNumber, $text);
$description = $success ? 'OK' : 'Error';
if ($success){
$expiresAt = $now + CodeProcessor::getInstance()->getLifetime();
}
} catch (\Exception $e) {
$description = $e->getMessage();
if (!($e instanceof ValidationException)) {
Expand All @@ -48,6 +53,9 @@ public static function sendCode($phoneNumber)
if ($exceptionClass){
$response['exception'] = $exceptionClass;
}
if ($expiresAt){
$response['expires_at'] = $expiresAt;
}
return $response;
}

Expand Down

0 comments on commit 41382f3

Please sign in to comment.