RichardKnop/go-oauth2-server

Store Abstraction + Redis Session Store

Closed this issue · 1 comments

I worked today on abstracting this code base a little bit. I only really made one change: moving the session.NewService out of web/middlewares.go and into cmd/util.go. After that it was super easy to implement any of the many Gorilla store implementations.

Here's the abstracted code. It uses an in-memory service as a default.

Here's an implementation of using a redis "plugin" to store sessions.

I'm not sure if I'm totally happy with how the abstraction turned out. I'll try to think of cleaner ways of doing it. I may merge this into the uuid branch and submit a PR, if you'd like.

Ok, I think I've got the abstraction to a better place, now.

Developers can now write their own "plugins" for any of the services (sessions, web, health, or oauth). Here's one that I wrote that can be used to add redis storage for sessions. It's less than 200 lines of code!

Using plugins is super easy, just:

/// $ go get https://github.com/adam-hanna/redis-sessions

// within github.com/RichardKnop/go-oauth2-server/cmd/run_server.go
import (
    ...
    "github.com/adam-hanna/redis-sessions/redis"
    ...
)

// RunServer runs the app
func RunServer(configBackend string) error {
    ...

    // configure redis for session store
    sessionSecrets := make([][]byte, 1)
    sessionSecrets[0] = []byte(cnf.Session.Secret)
    redisConfig := redis.ConfigType{
        Size:           10,
        Network:        "tcp",
        Address:        ":6379",
        Password:       "",
        SessionSecrets: sessionSecrets,
    }

    // start the services
    services.UseSessionService(redis.NewService(cnf, redisConfig))
    if err := services.InitServices(cnf, db); err != nil {
        return err
    }
    defer services.CloseServices()

    ...
}