Skip to content

Commit b7b6948

Browse files
authored
Merge pull request #1 from vgarvardt/feature/initial-implementation
Initial implementation
2 parents 1f5d916 + b246b7a commit b7b6948

File tree

13 files changed

+1124
-2
lines changed

13 files changed

+1124
-2
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,5 @@
1010

1111
# Output of the go coverage tool, specifically when used with LiteIDE
1212
*.out
13+
14+
vendor/

.travis.yml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
language: go
2+
sudo: false
3+
4+
go:
5+
- "1.10"
6+
- "1.11"
7+
- "stable"
8+
9+
services:
10+
- postgresql
11+
12+
addons:
13+
postgresql: "9.6"
14+
15+
env:
16+
global:
17+
- PG_URI=postgres://postgres@localhost:5432/oauth2_test?sslmode=disable
18+
19+
before_install:
20+
- psql -c 'create database oauth2_test;' -U postgres
21+
- go get -u github.com/golang/dep/cmd/dep
22+
- dep ensure -v -vendor-only
23+
24+
script:
25+
- go test -coverprofile=coverage.txt -covermode=atomic ./...
26+
27+
after_success:
28+
- bash <(curl -s https://codecov.io/bash)

Gopkg.lock

Lines changed: 122 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Gopkg.toml

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Gopkg.toml example
2+
#
3+
# Refer to https://golang.github.io/dep/docs/Gopkg.toml.html
4+
# for detailed Gopkg.toml documentation.
5+
#
6+
# required = ["github.com/user/thing/cmd/thing"]
7+
# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"]
8+
#
9+
# [[constraint]]
10+
# name = "github.com/user/project"
11+
# version = "1.0.0"
12+
#
13+
# [[constraint]]
14+
# name = "github.com/user/project2"
15+
# branch = "dev"
16+
# source = "github.com/myfork/project2"
17+
#
18+
# [[override]]
19+
# name = "github.com/x/y"
20+
# version = "2.4.0"
21+
#
22+
# [prune]
23+
# non-go = false
24+
# go-tests = true
25+
# unused-packages = true
26+
27+
28+
[[constraint]]
29+
name = "github.com/json-iterator/go"
30+
version = "1.1.5"
31+
32+
[[constraint]]
33+
name = "gopkg.in/oauth2.v3"
34+
version = "3.9.5"
35+
36+
[prune]
37+
go-tests = true
38+
unused-packages = true
39+
40+
[[constraint]]
41+
name = "github.com/jackc/pgx"
42+
version = "3.3.0"
43+
44+
[[constraint]]
45+
name = "github.com/jmoiron/sqlx"
46+
version = "1.2.0"
47+
48+
[[constraint]]
49+
name = "github.com/vgarvardt/pgx-helpers"
50+
version = "0.1.0"
51+
52+
[[constraint]]
53+
name = "github.com/stretchr/testify"
54+
version = "1.3.0"

README.md

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,60 @@
1-
# go-oauth2-pg
2-
PostgreSQL storage for OAuth 2.0
1+
# PostgreSQL Storage for [OAuth 2.0](https://github.com/go-oauth2/oauth2)
2+
3+
## Install
4+
5+
```bash
6+
$ go get -u -v github.com/vgarvardt/go-oauth2-pg
7+
```
8+
9+
## PostgreSQL drivers
10+
11+
The store accepts an adapter interface that interacts with the DB. The package is bundled with the following adapter implementations
12+
13+
- `database/sql` (e.g. [`github.com/lib/pq`](https://github.com/lib/pq)) - `github.com/vgarvardt/go-oauth2-pg/adapter/sql.NewSQL()`
14+
- [`github.com/jmoiron/sqlx.DB`](https://github.com/jmoiron/sqlx) - `github.com/vgarvardt/go-oauth2-pg/adapter/sqlx.NewSQLx()`
15+
- [`github.com/jackc/pgx.Conn`](https://github.com/jackc/pgx) - `github.com/vgarvardt/go-oauth2-pg/adapter/pgx.NewConnAdapter()`
16+
- [`github.com/jackc/pgx.ConnPool`](https://github.com/jackc/pgx) - `github.com/vgarvardt/go-oauth2-pg/adapter/pgx.NewConnPoolAdapter()`
17+
18+
## Usage example
19+
20+
```go
21+
package main
22+
23+
import (
24+
"os"
25+
"time"
26+
27+
"github.com/jackc/pgx"
28+
pg "github.com/vgarvardt/go-oauth2-pg"
29+
pgxAdapter "github.com/vgarvardt/go-oauth2-pg/adapter/pgx"
30+
"gopkg.in/oauth2.v3/manage"
31+
)
32+
33+
func main() {
34+
pgxConnConfig, _ := pgx.ParseURI(os.Getenv("DB_URI"))
35+
pgxConn, _ := pgx.Connect(pgxConnConfig)
36+
37+
manager := manage.NewDefaultManager()
38+
39+
// use PostgreSQL token store with pgx.Connection adapter
40+
store, _ := pg.NewStore(pgxAdapter.NewConnAdapter(pgxConn), pg.WithGCInterval(time.Minute))
41+
defer store.Close()
42+
43+
manager.MapTokenStorage(store)
44+
// ...
45+
}
46+
```
47+
48+
## How to run tests
49+
50+
You will need running PostgreSQL instance. E.g. the one running in docker and exposing a port to a host system
51+
52+
```bash
53+
docker run --rm -p 5432:5432 -it -e POSTGRES_PASSWORD=oauth2 -e POSTGRES_USER=oauth2 -e POSTGRES_DB=oauth2 postgres:10
54+
```
55+
56+
Now you can run tests using the running PostgreSQL instance using `PG_URI` environment variable
57+
58+
```bash
59+
PG_URI=postgres://oauth2:oauth2@localhost:5432/oauth2?sslmode=disable go test -cover ./...
60+
```

adapter/pgx/adapter.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package pgx
2+
3+
import (
4+
"github.com/jackc/pgx"
5+
"github.com/vgarvardt/go-oauth2-pg"
6+
pgxHelpers "github.com/vgarvardt/pgx-helpers"
7+
)
8+
9+
// ConnPoolAdapter is the adapter type for PGx connection pool connection type
10+
type ConnPoolAdapter struct {
11+
conn *pgx.ConnPool
12+
}
13+
14+
// NewConnPoolAdapter instantiates PGx connection pool adapter
15+
func NewConnPoolAdapter(conn *pgx.ConnPool) *ConnPoolAdapter {
16+
return &ConnPoolAdapter{conn}
17+
}
18+
19+
// ConnPoolAdapter is the adapter type for PGx connection connection type
20+
type ConnAdapter struct {
21+
conn *pgx.Conn
22+
}
23+
24+
// NewConnAdapter instantiates PGx connection adapter
25+
func NewConnAdapter(conn *pgx.Conn) *ConnAdapter {
26+
return &ConnAdapter{conn}
27+
}
28+
29+
// Exec runs a query and returns an error if any
30+
func (a *ConnPoolAdapter) Exec(query string, args ...interface{}) error {
31+
_, err := a.conn.Exec(query, args...)
32+
return err
33+
}
34+
35+
// SelectOne runs a select query and scans the object into a struct or returns an error
36+
func (a *ConnPoolAdapter) SelectOne(dst interface{}, query string, args ...interface{}) error {
37+
row := a.conn.QueryRow(query, args...)
38+
if err := pgxHelpers.ScanStruct(row, dst); err != nil {
39+
if err == pgx.ErrNoRows {
40+
return pg.ErrNoRows
41+
}
42+
return err
43+
}
44+
45+
return nil
46+
}
47+
48+
// Exec runs a query and returns an error if any
49+
func (a *ConnAdapter) Exec(query string, args ...interface{}) error {
50+
_, err := a.conn.Exec(query, args...)
51+
return err
52+
}
53+
54+
// SelectOne runs a select query and scans the object into a struct or returns an error
55+
func (a *ConnAdapter) SelectOne(dst interface{}, query string, args ...interface{}) error {
56+
row := a.conn.QueryRow(query, args...)
57+
if err := pgxHelpers.ScanStruct(row, dst); err != nil {
58+
if err == pgx.ErrNoRows {
59+
return pg.ErrNoRows
60+
}
61+
return err
62+
}
63+
64+
return nil
65+
}

0 commit comments

Comments
 (0)