Code Monkey home page Code Monkey logo

onetimepass's Introduction

Versions

Current development release: onetimepass-master.tar.gz otp-status-dev

Changelog

Version Date Changes
1.0.1 2015-07-31
  • fixed tests and build system,
  • extended test coverage with Py3.5, PyPy and PyPy3,
1.0.0 2015-07-31
  • skipping spaces if they are given in secret,
  • test suite made more reliable,
0.3.0 2014-08-16
  • configurable digest method,
  • configurable token length,
  • configurable TOTP interval length,
0.2.2 2013-07-12
  • license clarification,
  • removal of compiled documentation from the sources,
0.2.1 2013-07-12
  • support for unicode secrets,
  • preliminary support for Travis CI,
0.2.0 2013-04-11
  • added compatibility with Python 3.x,
  • removed compatibility with Python 2.5 and earlier,
0.1.2 2013-01-23
  • added automated case fold to secret,
0.1.1 2013-12-20
  • internal code improvements,
  • documentation,
0.1.0 2011-12-19 (initial public release)

What is OneTimePass

OneTimePass (actually onetimepass) is a module for generating one-time passwords, namely HOTPs (HMAC-based one-time passwords) and TOTPs (time-based one-time passwords). They are used eg. within Google Authenticator application for Android or iPhone.

How to install

To install the library, you can either use pip, or just download it separately. Installing in pip is the simplest. Assuming you are installing it system-wide:

$ sudo pip install onetimepass

(if you are installing it in virtualenv, you do not need "sudo" part).

Alternatively, you can follow the download link above and unpack in some directory on your sys.path, or clone it as Git submodule to your own directory.

How to use OneTimePass

You can use this module in the following way:

  1. Install module (download it into your application's directory or into modules directory)

  2. To get time-based token you invoke it like that:

    import onetimepass as otp
    my_secret = 'MFRGGZDFMZTWQ2LK'
    my_token = otp.get_totp(my_secret)
    

Note

my_secret is case-insensitive, also spaces are ignored. This means you can provide your users with more readable representations of the secrets (eg. mfrg gzdf mztw q2lk instead of MFRGGZDFMZTWQ2LK) and pass them unchanged to library. Same applies to other functions accepting secrets in this library.

  1. To get HMAC-based token you invoke it like that:

    import onetimepass as otp
    my_secret = 'MFRGGZDFMZTWQ2LK'
    my_token = otp.get_hotp(my_secret, intervals_no=3)
    

    where intervals_no is the number of the current trial (if checking on the server, you have to check several values, higher than the last successful one, determined for previous successful authentications).

  2. To check time-based token you invoke it like that:

    import onetimepass as otp
    my_secret = 'MFRGGZDFMZTWQ2LK'
    my_token = 123456 # should be probably from some user's input
    is_valid = otp.valid_totp(token=my_token, secret=my_secret)
    
  3. To check HMAC-based token you invoke it like that:

    import onetimepass as otp
    my_secret = 'MFRGGZDFMZTWQ2LK'
    my_token = 123456 # should be probably from some user's input
    last_used = 5 # store last valid interval somewhere else
    is_valid = otp.valid_hotp(token=my_token, secret=my_secret, last=last_used)
    

    where:

    • last argument (in this case being assigned last_used) is the number of the last successfully checked interval number (as valid_totp() will skip it and start checking from the next interval number)
    • is_valid is being assigned value of False if my_token has not been identified as valid OTP for given secret (my_secret) and checked interval range. If it has been successful, is_valid is assigned a number of the working interval number (it should be saved into the database and supplied to the function as last argument next time the password is being checked, so you cannot use the same token again).

License

License for this library is available in LICENSE.rst file, in the same directory. Online version is available here.

onetimepass's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

onetimepass's Issues

License clearification

Hello!

I notice that there is a incongruence about the license:

setup.py:23: 'License :: OSI Approved :: MIT License',
onetimepass/__init__.py:9: @license: MIT
onetimepass/__init__.py:41: __license__ = 'GNU Lesser General Public License (LGPL)'

I guess that this is just a cut and paste error, but non the less... :-)

Generate token not same as google authenticator

Hi,Recently been working on how to get the token from google authenticator with window or chrome extension, But now it is not working, the website is restrict need google authenticator or microsoft authenticator.
Left console is the python output, but is invalid tokenRight is android emulator token code wichi is working fine.
image

Potential issue validating token against candidate

I was reviewing stackoverflow for how people dealt with leftmost zeroes and I stumbled on this library

There's some issues stemming from the integer conversion where tokens with zeroes are treated as octal, and hence the implementation checks against the wrong token https://stackoverflow.com/questions/39695700/python-flask-app-leading-zeros-in-totp-error-python-2-7

But the subtle issue is that the library doesn't enforce the token length. By casting a string/int to an integer, you discard the leftmost zeroes and hence could allow 1 if the token was actually 000001.
https://github.com/tadeck/onetimepass/blob/master/onetimepass/__init__.py#L216

I suggest adhering to string semantics to avoid accepting potentially invalid input, and adopting a length constant time equality check when testing input against a candidate token here to eliminate timing side channels: https://github.com/tadeck/onetimepass/blob/master/onetimepass/__init__.py#L268

get_totp function problem

Hi, the get_totp() function is working great but i notice that if the result starts with 0 (e.g: 028526), it just prints 28526. Is there any way to fix this?

Can't use leading digit in secret

Maybe I am doing something wrong, but when a secret has a digit in the first position generating a OTP fails. Is this by design or am I doing something wrong?

my_secret = 'aaaaaaaaaaaaaaaa'
my_token = otp.get_totp(my_secret)

my_secret = '1aaaaaaaaaaaaaaa'
my_token = otp.get_totp(my_secret)
Traceback (most recent call last):
File "", line 1, in
File "/var/www/project/lib/python2.7/site-packages/onetimepass/init.py", line 162, in get_totp
token_length=token_length,
File "/var/www/project/lib/python2.7/site-packages/onetimepass/init.py", line 113, in get_hotp
raise TypeError('Incorrect secret')
TypeError: Incorrect secret

Thanks for the help

Add clock and window parameters to totp

Add in two features that exists in the oath library. Specifically the ability to specify a specific clock time other than the current for generate and for validate. And the ability to specify a window of time intervals on each side of the clock that will be considered to be successful in valid_totp.

Invalid length returned

Hello,

I was just testing the OTP generation using get_totp method and
otp.get_totp(my_secret,interval_length = timeout,token_length=length)
had given 6 as the desired length, I tested it for 100 odd iterations and to my surprise, it returned OTPs for length less than 6 sometimes, like 3 out of 100 were of length 4 or 5.

mysecret = base64.b32encode(str(chat_id[0:9])) timeout = 160 length=6

Please tell me how to fix this,

uploading the test results for reference

out.log

pip install fails

The download_url in setup.py does not contain the module name. pip install -vvv onetimepass includes the following message (manually wrapped):

Skipping link https://github.com/tadeck/onetimepass/archive/v0.1.2.tar.gz
(from https://github.com/tadeck/onetimepass); wrong project name (not onetimepass)

and fails with the the following errors:

Could not find any downloads that satisfy the requirement onetimepass
No distributions at all found for onetimepass

The following installation method does work:

pip install https://github.com/tadeck/onetimepass/archive/v0.1.2.tar.gz

Incorrect secret

onetimepass__init__.py", line 100, in get_hotp

Incorrect secret

my_secret = 'Q413I60L68T7A9QF'
my_token = otp.get_totp(my_secret)

Enable casefold by default

There was one report of some service returning lowercase secret, so when provided to the library, there was an issue (base64.b32decode accepts only uppercase letters).

This can be solved by setting casefold argument of base64.b32decode to True.

Matching Authy

Hello,

I am attempting to convert your module so that it can be used as a substitute for Authy. They've told me via Twitter that they use the same algorithm as GAuth except with a 7-digit response, 256-bit keys/seed and a 10 second interval.

I think I've made the right changes in this branch, but there's been zero testing because doing so essentially requires one of the two things I'm trying to avoid: giving Authy my mobile phone number. The other thing I'm trying to avoid is handing over authentication details to a third party.

Anyway, you might still find it useful, so feel free to poke at it. If I can find a way to test it against what Authy is selling to various places (including CloudFlare) then I'll update later.

Regards,
Ben

Some secrets simply crash with 'Incorrect padding'

All secrets generated by fastmail see to make the library crash. I can't find any common pattern for them:

>>> import onetimepass
>>> onetimepass.get_totp('7uzthj2u3te6dopflwqbwa5n6u', as_string=True, token_length=6)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.6/site-packages/onetimepass/__init__.py", line 169, in get_totp
    token_length=token_length,
  File "/usr/lib/python3.6/site-packages/onetimepass/__init__.py", line 113, in get_hotp
    key = base64.b32decode(secret, casefold=casefold)
  File "/usr/lib/python3.6/base64.py", line 205, in b32decode
    raise binascii.Error('Incorrect padding')
binascii.Error: Incorrect padding

The one included in the above example is safe to share since I never added confirmed adding that one to my account. :)

wrong OTP size due to int number being used

Hi.
I have seen tokens shorter than others and I think it happens because you use 'int', hence when the first digit is a zero it is stripped.
For instance, an attempt with a random secret, on that precise moment, gave me this result:

>>> my_secret='5keaubwr6am3xmoogkjqxt56t4puuu52'
>>> otp.get_totp(my_secret)
79770

p.s.: I didn't try to add a leading zero and login, as the secret was random, and I couldn't use this OTP in a real scenario.

Token length sometimes vary

The default token_length is 6, but sometime get_totp returns tokens with lengths of 5. You can see from the last value in the screenshot attached.
Screenshot 2023-09-29 at 10 44 13 AM

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.