artdarek / oauth-4-laravel

OAuth Service Provider for Laravel 4
684 stars 216 forks source link

Question regarding storage configuration #40

Closed dlabey closed 9 years ago

dlabey commented 10 years ago

If we have Laravel set to use Redis for sessions and our storage is set to Session, will this use Redis?

maknz commented 10 years ago

No, it uses PHP's native sessions.

dlabey commented 10 years ago

Ok, thanks, do you have an example of how to use Redis? Otherwise I could fork your repo and then submit a pull for Redis if its not implemented yet.

maknz commented 10 years ago

(Not my repo, I'm just a happy user!)

The underlying library actually has support for Redis, although this package doesn't have a way to instantiate it with a connected Predis instance.

I looked into it briefly (when I thought a bug I was having was due to storage - it wasn't) and implemented a LaravelRedisStorage class, which extends from OAuth\Common\Storage\Redis and implements OAuth\Common\Storage\TokenStorageInterface. All you would have to do is override the constructor to pass in a Predis instance (obtained by a \Redis::connection(), easy!) to the parent. I can't fully remember what $key and $stateKey needed to be, I dare say a closer look how the storage is instantiated might help. Note that the LaravelRedisStorage class needs to be in the OAuth\Common\Storage namespace, as that is how the storage provider is configured. It will need autoloaded separately.

dlabey commented 9 years ago

Thank you for the answer, seeing the interface I see how it is done.

millar commented 8 years ago

In case anyone else stumbles across this, here is my solution as suggested by @maknz.

The class I created looked like so:

<?php

namespace OAuth\Common\Storage;

use Predis\Client as Predis;

class LaravelRedisStorage extends Redis implements TokenStorageInterface {

    public function __construct()
    {
        $this->redis = \Redis::connection();
        $this->key = 'oauth_user_tokens';
        $this->stateKey = 'oauth_user_states';
        $this->cachedTokens = array();
        $this->cachedStates = array();
    }

}

I autoloaded this in my composer.json file and set it to be used in app/config/packages/artdarek/oauth-4-laravel/config.php like so:

/**
 * Storage
 */
'storage' => 'LaravelRedisStorage',