rails/rails — pull request #10137 by guilleiguaran (2013-04-08 07:47:50+00:00)
Title: Add ActiveModel::OneTimePassword

This module encapsulates simple one time password authentication with TOTP for use in two-factor authentication schemes. I've found this very useful in some applications recently

## Usage

### Setting Model
Add otp_secret_key to your model:

rails g migration AddOtpSecretKeyToUsers otp_secret_key:string

```ruby
class User < ActiveRecord::Base
has_one_time_password
end
```

The otp_secret_key is saved automatically when a object is created

### Authenticating using a code

```ruby
user.authenticate_otp('186522') # => true
sleep 30
user.authenticate_otp('186522') # => false
```

### Authenticating using a slightly old code

```ruby
user.authenticate_otp('186522') # => true
sleep 30
user.authenticate_otp('186522', drift: 60) # => true
```

### Getting current code (ex. to send via SMS)

```ruby
user.otp_code # => '186522'
sleep 30
user.otp_code # => '850738'
```

### Getting provision URI (ex. to generate QR codes compatibles with Google Authenticator app)

```ruby
user.provision_uri # => 'otpauth://totp/alice@google.com?secret=JBSWY3DPEHPK3PXP'
```