Kernel::System::AccessToken::Storage::DB

NAME

Kernel::System::AccessToken::Storage::DB

DESCRIPTION

Authorization tokens database storage class.

PUBLIC INTERFACE

Create()

Creates/adds a new access-token or key to the database.

    # Add a new token.
    my $AccessToken = $Storage->Create(
        Entity         => 'AccessToken',
        UUID           => '...',
        Token          => '...',
        UserID         => '...',
        UserType       => '...',
        ExpiresTime    => '...',
        CreateTime     => '...',
        LastAccessTime => '...',
    );

    # Add a new key.
    my $Key = $Storage->Create(
        Entity     => 'Key',
        Value      => '...',
        CreateTime => '...',
    );

Returns

    # access-token
    {
        UUID           => '...',
        Token          => '...',
        UserID         => '...',
        UserType       => '...',
        ExpiresTime    => '...',
        CreateTime     => '...',
        LastAccessTime => '...',
    };

    # key
    {
        Value      => '...',
        CreateTime => '...',
    };

    or C<undef> in case any error occurs.

List()

Get a list of access-tokens or keys records.

    my $List = $Storage->List(
        Entity  => '...',
        Filters => {
            UUID             => '...' # optional
            UserID           => '...' # optional
            UserType         => '...' # optional
            ExpiresTime      => '...' # optional
            LastAccessTime   => '...' # optional
            ExpiresTimeLower => '...' # optional
        }
    );

Returns

    C<undef> - in case any error occurs
    []       - in case no data was found for the entity
    [
        {
            UUID           => '...',
            UserID         => '...',
            UserType       => '...',
            CreateTime     => '...',
            ExpiresTime    => '...',
            LastAccessTime => '...',
        },
        ...
    ]

Get()

Get an access-token or key record.

    my $List = $Storage->Get(
        Entity => '...',
        UUID   => '...' # for access-token
        Value  => '...' # for key
    );

Returns

    C<undef> - in case any error occurs
    {}       - in case no data was found for the entity
    {...}    - inc case data was found

Delete()

Delete access tokens from the database.

    # delete all access tokens.
    my $Result = $AccessTokenObject->Delete();

    # delete all access tokens according to the filters passed.
    my $Result = $AccessTokenObject->Delete(
        Filters => {
            ExpiresTimeLower => '...', # optional
            UserID           => '...', # optional
            UUID             => '...', # optional
        },
    );

Returns

    1 - in case of success.
    C<undef> - in case any error occurs.

Update()

Update access tokens in the database.

    # update create-time of all access tokens.
    my $Result = $Storage->Update(Data => {CreateTime => '2018-03-02'});

    # update last-access-time and create-time off all the access tokens for user-id X.
    my $Result = $Storage->Update(
        Filters => {UserID => 'X'},
        Data    => {LastAccessTime => '2018-03-02', CreateTime => '2018-03-01'}
    );

Returns

    1 - in case of success.
    C<undef> - in case any error occurs.

Count()

Return the number of records that exists in the storage that match the passed filters.

    # Get the total of access-tokens.
    my $Count = $Storage->Count( Entity => 'AccessToken', );

    # Get the total of access-tokens for the user-id X.
    my $Count = $Storage->Count( Entity => 'AccessToken', Filters => { UserID => 'X', }, );

Returns

    number - in case of success.
    C<undef> - in case any error occurs.

PRIVATE INTERFACE

_FiltersSQLAndBinds()

Builds the SQL 'WHERE' clause and value binds based on the filters.

    # something like:
    my ($SQLWhere, $Binds) = $Self->_FiltersSQLAndBinds(
        Filters => {
            UserID      => 'X',
            ExpiresTime => '2018-02-18',
        },
    );
    # generates:
    $SQLWhere = (user_id = ?) AND (expires_time = ?)
    $Binds    = ['X', '2018-02-18']

    # it's possible to indicate which operator to use:
    my ($SQLWhere, $Binds) = $Self->_FiltersSQLAndBinds(
        Filters => {
            ExpiresTime => { '>=' => '2018-02-18'},
        },
    );
    # generates:
    $SQLWhere = (expires_time >= ?)
    $Binds    = ['2018-02-18']

Returns

    (string, arrayref)

_GetDBColumnMapForParams()

Get the mapping of params to database columns.

    # mapping for all params
    my $Map = $Self->_GetDBColumnMapForParams();

    # mapping only for the needed params
    my $Map = $Self->_GetDBColumnMapForParams(
        Want => [qw( UserType ExpiresTime )],
    );

Returns

    a map/dictionary with the param and respective column name.

_CacheType()

Returns the cache type.

_CacheTTL()

Returns the cache ttl.

_CacheDelete()

Deletes the entity cache. It detects if the operation was only for a record and delete only the 'Get' cache for that record, otherwise deletes everything.

    $Self->_CacheDelete(
        Entity  => 'AccessToken' or 'Key',
        Filters => {...}, # the filters passed to the update|delete operation
    );
Scroll to Top