Authen-TOTP

 view release on metacpan or  search on metacpan

README.md  view on Meta::CPAN

# NAME

Authen::TOTP - Interface to RFC6238 two factor authentication (2FA)

Version 0.1.1

# SYNOPSIS

    use Authen::TOTP;

# DESCRIPTION

`Authen::TOTP` is a simple interface for creating and verifying RFC6238 OTPs
as used by Google Authenticator, Authy, Duo Mobile etc

It currently passes RFC6238 Test Vectors for SHA1, SHA256, SHA512

# USAGE

    my $gen = new Authen::TOTP(
            secret         =>      "some_random_stuff",
    );

    #will generate a TOTP URI, suitable to use in a QR Code
    my $uri = $gen->generate_otp(user => 'user\@example.com', issuer => "example.com");
    
    print qq{$uri\n};
    #store $gen->secret() or $gen->base32secret() someplace safe!

    #use Imager::QRCode to plot the secret for the user
    use Imager::QRCode;
    my $qrcode = Imager::QRCode->new(
              size          => 4,
              margin        => 3,
              level         => 'L',
              casesensitive => 1,
              lightcolor    => Imager::Color->new(255, 255, 255),
              darkcolor     => Imager::Color->new(0, 0, 0),
          );

    my $img = $qrcode->plot($uri);
    $img->write(file => "totp.png", type => "png");
    #...or you can pass it to google charts and be done with it

    #compare user's OTP with computed one
    if ($gen->validate_otp(otp => <user_input>, secret => <stored_secret>, tolerance => 1)) {
           #2FA success
    }
    else {
           #no match
    }

    # Get generated OTP and validate it
    my $otp=$gen->otp();
    print "Generated OTP is $otp\n";
    my $matches=$gen->validate_otp(otp => $otp);
    print "Self generated OTP is ".($matches?"OK":"NOK")."\n";

# new Authen::TOTP

    my $gen = new Authen::TOTP(
            digits         =>      [6|8],
            period         =>      [30|60],
            algorithm      =>      "SHA1", #SHA256 and SHA512 are equally valid
            secret         =>      "some_random_stuff",
            when           =>      <some_epoch>,
            tolerance      =>      0,
    );

## Parameters/Properties (defaults listed)

- digits

    `6`=> How many digits to produce/compare

- period

    `30`=> OTP is valid for this many seconds

- algorithm

    `SHA1`=> supported values are SHA1, SHA256 and SHA512, although most clients only support SHA1 AFAIK

- secret

    `random_20byte_string`=> Secret used as seed for the OTP

- base32secret

    `base32_encoded_random_12byte_string`=> Alternative way to set secret (base32 encoded)

- when

    `epoch`=> Time used for comparison of OTPs

- tolerance

    `1`=> Due to time sync issues, you may want to tune this and compare
    this many OTPs before and after

## Utility Functions



( run in 0.509 second using v1.01-cache-2.11-cpan-788537b7465 )