Download the PHP package innobyte/token-bundle without Composer

On this page you can find all versions of the php package innobyte/token-bundle. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package token-bundle

Purpose

The goal of TokenBundle is to provide a means to validate actions taken by users.

The validation is achieved by using a hash, which could, for example, be embedded in a URL, be it in an email or a link generated on-the-spot inside a page.

Installation

composer.json - install bundle

"require": {
    "innobyte/token-bundle": "~1.1",
},

AppKernel.php - register bundle

Add the following line into $bundles array:

new Innobyte\TokenBundle\InnobyteTokenBundle(),

config.yml - Add bundle mapping

Add the mapping for the bundle under an entity manager (here "local"). Note: "default" is the default name for the Entity Manager, if not specified otherwise.

doctrine:
    ...
    orm:
        ...
        entity_managers:
            local:
                ...
                mappings:
                    ...
                    InnobyteTokenBundle: ~

paramaters.yml - Add the entity manager name

Add the entity manager name (here "local") - the one you put the mapping under in the above step. If none is provided, "default" will be used. Note: this must be specified only if the entity manager name is different than "default". Otherwise, this step can be omitted entirely.

innobyte_token:
    entity_manager: local

Update schema

First, run to see the generated SQL.

Then, run to run the query on the Database.

Usage

Generate token

$token = $this->get('innobyte_token')->generate(
    'scope',
    'owner_type',
    123,                       // owner_id
    1,                         // number of uses - optional
    new \DateTime('+1 day'),   // expiry time - optional
    array('author' => 'admin') // additional data to check against - optional
);

// use hash (embed in a link/email etc.)
$hash = $token->getHash();

Validate token and consume it

try {
    $this->get('innobyte_token')->consume(
        '19debf971fb937853d77fca8fd3bb775',
        'scope',
        'owner_type',
        123            // owner_id
    );
} catch (\Innobyte\TokenBundle\Exception\TokenNotFoundException $e) {
    echo 'cannot find Token';
} catch (\Innobyte\TokenBundle\Exception\TokenInactiveException $e) {
    echo 'handle explicit disabled token here';
} catch (\Innobyte\TokenBundle\Exception\TokenConsumedException $e) {
    echo 'handle over-used token here';
} catch (\Innobyte\TokenBundle\Exception\TokenExpiredException $e) {
    echo 'handle expired token here';
} catch (\Innobyte\TokenBundle\Exception\TokenException $e) {
    // or, alternately, you can catch all token-related exceptions
    echo 'handle all token-related exceptions';
}

echo 'Token is valid. Token is valid. Perform logic here.';

Get the token and manually validate and consume it

$token = $this->get('innobyte_token')->get(
    '5c15e262c692dbaac75451dcb28282ab',
    'scope',
    'owner_type',
    123            // owner_id
);

// if wrong link or disabled/overused token
if (!$this->get('innobyte_token')->isValid($token)) {
    echo 'Handle invalid token here';
}

// or even more explicit
if (!$token instanceof \Innobyte\TokenBundle\Entity\Token) {
    echo 'handle invalid token here';
} else {
    // manually mark the usage
    try {
        $this->get('innobyte_token')->consumeToken($token);
    } catch (\LogicException $e) {
        echo 'cannot consumed Token because it is not managed';
    }

    echo 'Token is valid. Perform logic here.';
}

Invalidate

Similar methods to "consume" are available for invalidation: "invalidate" (disable the token)

try {
    $this->get('innobyte_token')->invalidate(
        '5c15e262c692dbaac75451dcb28282ab',
        'scope',
        'owner_type',
        123            // owner_id
    );
} catch (\Innobyte\TokenBundle\Exception\TokenNotFoundException $e) {
    echo 'Handle Token not found here';
}

if (!$token instanceof \Innobyte\TokenBundle\Entity\Token) {
    echo 'Handle invalid token here';
} else {
    try {
        $this->get('innobyte_token')->invalidateToken($token);
    } catch (\LogicException $e) {
        echo 'Cannot consume Token because it is not managed';
    }
}

Advanced validation

Additional data can be used in validating additional conditions after performing the standard Token validation.

// first, generate the Token
$token = $this->get('innobyte_token')->generate(
    'scope',
    'owner_type',
    123,                       // owner_id
    1,                         // number of uses - optional
    new \DateTime('+1 hour'),   // expiry time - optional
    array('ip' => $request->getClientIp()) // additional data to check against - optional
);

// use hash (embed in a link/email etc.)
$hash = $token->getHash();

// then, validate it
$token = $this->get('innobyte_token')->get(
    $hash,
    'scope',
    'owner_type',
    123            // owner_id
);

// if wrong link or disabled/overused token
if (!$this->get('innobyte_token')->isValid($token)) {
    echo 'Handle invalid token here';
}

// additional validation by IP
$additionalData = $token->getData();
if ($additionalData['ip'] != $request->getClientIp()) {
    echo 'Handle invalid token here';
}

Unit Testing

Do not forget to override database credentials in with the test database ones. Example:

parameters:
    database_host: 127.0.0.1
    database_port: null
    database_name: test_db
    database_user: root
    database_password: 123

To run the tests, execute:

phpunit -c app/ vendor/innobyte/token-bundle/Innobyte/TokenBundle/Tests/

All versions of token-bundle with dependencies

PHP Build Version
Package Version
Requires php Version >=5.5.0
symfony/config Version ~2.3|~3.0|~4.0|~5.0
symfony/dependency-injection Version ~2.3|~3.0|~4.0|~5.0
symfony/http-kernel Version ~2.3|~3.0|~4.0|~5.0
doctrine/doctrine-bundle Version ~1.4 | ~2.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package innobyte/token-bundle contains the following files

Loading the files please wait ....