PostgreSQL Storage for OAuth 2.0
$ go get -u -v github.com/vgarvardt/go-oauth2-pg
The store accepts an adapter interface that interacts with the DB. Adapter and implementations are extracted to separate package github.com/vgarvardt/go-pg-adapter
for easier maintenance.
package main
import (
"os"
"time"
"github.com/jackc/pgx"
pg "github.com/vgarvardt/go-oauth2-pg"
"github.com/vgarvardt/go-pg-adapter/pgxadapter"
"gopkg.in/oauth2.v3/manage"
)
func main() {
pgxConnConfig, _ := pgx.ParseURI(os.Getenv("DB_URI"))
pgxConn, _ := pgx.Connect(pgxConnConfig)
manager := manage.NewDefaultManager()
// use PostgreSQL token store with pgx.Connection adapter
adapter := pgxadapter.NewConn(pgxConn)
tokenStore, _ := pg.NewTokenStore(adapter, pg.WithTokenStoreGCInterval(time.Minute))
defer tokenStore.Close()
clientStore, _ := pg.NewClientStore(adapter)
manager.MapTokenStorage(tokenStore)
manager.MapClientStorage(clientStore)
// ...
}
You will need running PostgreSQL instance. E.g. the one running in docker and exposing a port to a host system
docker run --rm -p 5432:5432 -it -e POSTGRES_PASSWORD=oauth2 -e POSTGRES_USER=oauth2 -e POSTGRES_DB=oauth2 postgres:10
Now you can run tests using the running PostgreSQL instance using PG_URI
environment variable
PG_URI=postgres://oauth2:oauth2@localhost:5432/oauth2?sslmode=disable go test -cover ./...
Copyright (c) 2019 Vladimir Garvardt