Skip to content

Feature request - authorization token should be cached in central cache system #16

@stefliekens

Description

@stefliekens

At this moment the authorization token cannot be saved in central cache system like Redis, filesystem, Memcached, DB, .... Currently the token is stored in a variable in the implementation and is forgotten when another process / request is booted.

Below you can see an explicit implementation we have introduced into our project (Laravel, with Redis cache enabled) by overwriting the Twikey\Api\Twikey class.

class TwikeyClient extends Twikey
{
 
  public function refreshTokenIfRequired($force = false): string
    {
        if ($force) {
            return $this->retrieveAuthorizationToken();
        }

        return Cache::remember(
            'twikey_api_authorization_token',
            config('twikey.api.ttl_authorization_token'),
            $this->retrieveAuthorizationToken(...)
        );
    }

    private function retrieveAuthorizationToken(): string
    {
        $response = $this->client->request(
            'POST',
            $this->baseUrl.'/creditor',
            [
                'headers' => [
                    'Accept' => 'application/json',
                    'Content-Type' => 'application/x-www-form-urlencoded',
                    'User-Agent' => $this->userAgent,
                    'Accept-Language' => 'en',
                ],
                'form_params' => [
                    'apiToken' => $this->apiKey,
                ],
            ]
        );

        if ($response->hasHeader('Authorization')) {
            return $response->getHeader('Authorization')[0];
        }

        if ($response->hasHeader('Apierror')) {
            throw new TwikeyException($response->getHeader('Apierror')[0]);
        }

        throw new TwikeyException('General Twikey exception : '.$response->getReasonPhrase());
    }

}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions