otpauth

otpauth is One Time Password Authentication, which is usually called as two steps verification. You may have heard it from Google, Dropbox and etc.

Wheel Status Latest Version Travis CI Status Coverage Status App Veyor CI Status

Installation

Installing otpauth is simple with pip:

$ pip install otpauth

or, with easy_install:

$ easy_install otpauth

Usage

Generate and validate an otp code is very simple:

>>> from otpauth import OtpAuth
>>> auth = OtpAuth('secret')  # a secret string
>>> auth.hotp()  # generate a count based code, default count is 4
330810
>>> auth.valid_hotp(330810)
4
>>> auth.hotp(2)  # generate a count based code, count is 2
720111
>>> auth.valid_hotp(720111)
2
>>> auth.totp()  # generate a time based code
828657
>>> auth.valid_totp(828657)
True

Authenticator

You can create a QR code for Google Authenticator to scan:

>>> from otpauth import OtpAuth
>>> auth = OtpAuth('secret')  # a secret string
>>> s = auth.to_uri('totp', 'Example:foo@bar.baz', 'Foo')
>>> import qrcode
>>> img = qrcode.make(s)

Developer Guide

Here is the API reference for otpauth.

class otpauth.OtpAuth(secret)

One Time Password Authentication.

Parameters:secret – A secret token for the authentication.
hotp(counter=4)

Generate a HOTP code.

Parameters:counter – HOTP is a counter based algorithm.
to_google(type, label, issuer, counter=None)

Generate the otpauth protocal string for Google Authenticator.

Deprecated since version 0.2.0: Use to_uri() instead.

to_uri(type, label, issuer, counter=None)

Generate the otpauth protocal string.

Parameters:
  • type – Algorithm type, hotp or totp.
  • label – Label of the identifier.
  • issuer – The company, the organization or something else.
  • counter – Counter of the HOTP algorithm.
totp(period=30, timestamp=None)

Generate a TOTP code.

A TOTP code is an extension of HOTP algorithm.

Parameters:
  • period – A period that a TOTP code is valid in seconds
  • timestamp – Create TOTP at this given timestamp
valid_hotp(code, last=0, trials=100)

Valid a HOTP code.

Parameters:
  • code – A number that is less than 6 characters.
  • last – Guess HOTP code from last + 1 range.
  • trials – Guest HOTP code end at last + trials + 1.
valid_totp(code, period=30, timestamp=None)

Valid a TOTP code.

Parameters:
  • code – A number that is less than 6 characters.
  • period – A period that a TOTP code is valid in seconds
  • timestamp – Validate TOTP at this given timestamp
otpauth.generate_hotp(secret, counter=4)

Generate a HOTP code.

Parameters:
  • secret – A secret token for the authentication.
  • counter – HOTP is a counter based algorithm.
otpauth.generate_totp(secret, period=30, timestamp=None)

Generate a TOTP code.

A TOTP code is an extension of HOTP algorithm.

Parameters:
  • secret – A secret token for the authentication.
  • period – A period that a TOTP code is valid in seconds
  • timestamp – Current time stamp.

Changelog

Here is the full history of otpauth.

Version 1.0

Released on Jan 25, 2015

Nothing new. It is stable now.

Version 0.3.0

Released on Dec 18, 2014

  • Make generate_hotp and generate_totp functions.
  • Add timestamp parameters for generate_totp. #3

Version 0.2.0

Released on Nov 14, 2013

  • Change API name to_google to to_uri.

Version 0.1.2

Released on Aug 16, 2013

  • Raise ValueError instead of TypeError when parameters are wrong.
  • Add documentation.

I believe this library is stable now. Someday it will turn into 1.0.0.

Version 0.1.1

Released on Jul 4, 2013

  • Remove === for Google Authenticator. #1

Version 0.1.0

First preview release.

Fork me on GitHub