Giter Site home page Giter Site logo

una's Introduction

una

Simple PHP library for storing and sending OTPs / 2FAs

Creating an OTP token

Use Una\Adapter;

/* Initialise the factory class with some defaults */
$factory = new Una\Factory([
    'storage'    => Adapter\Storage\BegoAdapter::instance([
        'table' => 'My-Table',
        'aws'   => [
            'region' => 'eu-west-1',
            'credentials'   => [
                'key'    => 'mykey',
                'secret' => 'mysecret',
            ]
        ],
    ]),
    'randomiser' => new Adapter\Randomiser\Numeric([
        'length' => 5
    ]),
    'hasher'     => new Adapter\Hash\Password()
]);

/* Create a fresh token */
$token = $factory->create()
    ->ttl(86400)
    ->notify(function($secret) {
        mail();    
    });

echo $token->raw();
echo $token->hashed();

Specifying a token's payload

$token = $factory->create()
    ->payload($userId)
    ->ttl(86400)
    ->notify(function($secret) {
        mail();    
    });

Token with complex payload

$token = $factory->create()
    ->payload([
        'userId' => $userId,
        'time'   => time()
    ])
    ->ttl(86400)
    ->notify(function($secret) {
        mail();    
    });

Specifying a secret

$token = $factory->create(12345)
    ->notify(function($secret) {
        mail();
    });

echo $token->raw(); //12345

echo $token->hashed();

Specifying a custom random generator. Custom generators implement the Una\Adapter\Randomisable interface.

use Una\Adapter\Randomiser\AlphaNumeric;

$token = $factory->create()
    ->randomise(new AlphaNumeric())
    ->notify(function($secret) {
        mail();    
    });

echo $token->raw(); //random alphanumeric value

echo $token->hashed();

Overriding the default ttl

use Una\Adapter\Randomiser\AlphaNumeric;

$token = $factory->create()
    ->ttl(86400) //24 hours
    ->notify(function($secret) {
        mail();    
    });

echo $token->raw(); //random alphanumeric value

echo $token->hashed();

Verifying an OTP token

try {
    $tokens = $factory->fetch($userId, 'My-App');

    /* Verify input against any valid tokens issued */
    $tokens->verify($input);
    
    /* Successful, invalidate all tokens immediately */
    $tokens->invalidate();

    return true;
} catch (Una\Exception\Storage)
    return 'a usefull message';
} catch (Una\Exception\Notfound)
    return 'a usefull message';
} catch (Una\Exception\Mismatch)
    return 'a usefull message';
} catch (Una\Exception\TokenExpired)
    return 'a usefull message';
}

una's People

Contributors

christianjburger avatar

Watchers

 avatar James Cloos 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.