Skip to content

Instantly share code, notes, and snippets.

@caseysoftware
Last active October 1, 2015 04:48
Show Gist options
  • Select an option

  • Save caseysoftware/1923300 to your computer and use it in GitHub Desktop.

Select an option

Save caseysoftware/1923300 to your computer and use it in GitHub Desktop.
Code to generate the one-time use password
function user_generate_token($username, $phoneNum, $method){
global $accountsid, $authtoken, $fromNumber;
/**
* Create a store a temporary/one-time password. While session-based is ok for
* this proof of concept, to make this more secure we'd want to use a database,
* potentially a salt for the password, and some sort of timeout to expire the
* password of N minutes/hours.
*/
$password = substr(md5(time().rand(0, 10^10)), 0, 10);
$_SESSION['password'] = $password;
$_SESSION['username'] = $username;
// Prepare the message with the password embedded
$content = ('sms' == $method) ? "Your newly generated password is ".$password :
"https://twimlets.com/message?Message%5B0%5D=Your%20newly%20generated%20password%20is%20%2C%2C" .
urlencode(preg_replace("/(.)/i", "\${1},,", $password)) .
"%20To%20repeat%20that%2C%20your%20password%20is%20%2C%2C" . urlencode(preg_replace("/(.)/i", "\${1},,", $password));
$method = ('sms' == $method) ? 'sms_messages' : 'calls';
$client = new Services_Twilio($accountsid, $authtoken);
// Send the message via SMS or Voice
$item = $client->account->$method->create(
$fromNumber, // The Twilio number we're sending from
$phoneNum, // The user's phone number
$content
);
$message = "A new password has been generated and sent to your phone number.";
return $message;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment