Giter VIP home page Giter VIP logo

rotp's Introduction

The Ruby One Time Password Library

Build Status Gem Version License

A ruby library for generating one time passwords (HOTP & TOTP) according to RFC 4226 and RFC 6238.

ROTP is compatible with the Google Authenticator available for Android and iPhone.

Many websites use this for multi-factor authentication, such as GMail, Facebook, Amazon EC2, WordPress, and Salesforce. You can find the whole list here.

Dependencies

  • OpenSSL
  • Ruby 1.9.3 or higher

Installation

gem install rotp

Library Usage

Time based OTP's

totp = ROTP::TOTP.new("base32secret3232")
totp.now # => "492039"

# OTP verified for current time
totp.verify("492039") # => true
sleep 30
totp.verify("492039") # => false

Optionally, you can provide an issuer which will be used as a title in Google Authenticator.

totp = ROTP::TOTP.new("base32secret3232", issuer: "My Service")
totp.provisioning_uri("[email protected]")

Counter based OTP's

hotp = ROTP::HOTP.new("base32secretkey3232")
hotp.at(0) # => "260182"
hotp.at(1) # => "055283"
hotp.at(1401) # => "316439"

# OTP verified with a counter
hotp.verify("316439", 1401) # => true
hotp.verify("316439", 1402) # => false

Verifying a Time based OTP with drift

Some users devices may be slightly behind or ahead of the actual time. ROTP allows users to verify an OTP code with an specific amount of 'drift'

totp = ROTP::TOTP.new("base32secret3232")
totp.now # => "492039"

# OTP verified for current time with 120 seconds allowed drift
totp.verify_with_drift("492039", 60, Time.now - 30) # => true
totp.verify_with_drift("492039", 60, Time.now - 90) # => false

Preventing reuse of Time based OTP's

In order to prevent reuse of time based tokens within the interval window (default 30 seconds) it is necessary to store the last time an OTP was used. The following is an example of this in action:

User.find(someUserID)
totp = ROTP::TOTP.new(user.otp_secret)
totp.now # => "492039"

user.last_otp_at # => 1472145530

# Verify the OTP
verified_at_timestamp = totp.verify_with_drift_and_prior("492039", 0, user.last_otp_at) #=> 1472145760
# Store this on the user's account
user.update(last_otp_at: verified_at_timestamp)
verified_at_timestamp = totp.verify_with_drift_and_prior("492039", 0, user.last_otp_at) #=> false

Generating a Base32 Secret key

ROTP::Base32.random_base32  # returns a 16 character base32 secret. Compatible with Google Authenticator

Note: The Base32 format conforms to RFC 4648 Base32

Google Authenticator Compatible URI's

Provisioning URI's generated by ROTP are compatible with the Google Authenticator App to be scanned with the in-built QR Code scanner.

totp.provisioning_uri("[email protected]") # => 'otpauth://totp/issuer:[email protected]?secret=JBSWY3DPEHPK3PXP'
hotp.provisioning_uri("[email protected]", 0) # => 'otpauth://hotp/issuer:[email protected]?secret=JBSWY3DPEHPK3PXP&counter=0'

This can then be rendered as a QR Code which can then be scanned and added to the users list of OTP credentials.

Working example

Scan the following barcode with your phone, using Google Authenticator

QR Code for OTP

Now run the following and compare the output

require 'rubygems'
require 'rotp'
totp = ROTP::TOTP.new("JBSWY3DPEHPK3PXP")
p "Current OTP: #{totp.now}"

Testing

bundle install
bundle exec rspec

Executable Usage

Once the rotp rubygem is installed on your system, you should be able to run the rotp executable (if not, you might find trouble-shooting help at this stackoverflow question).

# Try this to get an overview of the commands
rotp --help

# Examples
rotp --secret p4ssword                       # Generates a time-based one-time password
rotp --hmac --secret p4ssword --counter 42   # Generates a counter-based one-time password

Contributors

Have a look at the contributors graph on Github.

License

MIT Copyright (C) 2011 by Mark Percival, see LICENSE for details.

Other implementations

A list can be found at Wikipedia.

rotp's People

Contributors

mdp avatar halo avatar dvrensk avatar asmod4n avatar sbc100 avatar isabanin avatar ipoval avatar shaiguitar avatar andrehjr avatar douwem avatar jeremyevans avatar johnnyshields avatar asio avatar mkdynamic avatar

Watchers

mntz avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.