-
Notifications
You must be signed in to change notification settings - Fork 10
/
client_store.go
104 lines (86 loc) · 2.25 KB
/
client_store.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package pg
import (
"context"
"encoding/json"
"fmt"
"log"
"os"
"github.com/go-oauth2/oauth2/v4"
"github.com/go-oauth2/oauth2/v4/models"
pgAdapter "github.com/vgarvardt/go-pg-adapter"
)
// ClientStore PostgreSQL client store
type ClientStore struct {
adapter pgAdapter.Adapter
tableName string
logger Logger
initTableDisabled bool
}
// ClientStoreItem data item
type ClientStoreItem struct {
ID string `db:"id"`
Secret string `db:"secret"`
Domain string `db:"domain"`
Data []byte `db:"data"`
}
// NewClientStore creates PostgreSQL store instance
func NewClientStore(adapter pgAdapter.Adapter, options ...ClientStoreOption) (*ClientStore, error) {
store := &ClientStore{
adapter: adapter,
tableName: "oauth2_clients",
logger: log.New(os.Stderr, "[OAUTH2-PG-ERROR]", log.LstdFlags),
}
for _, o := range options {
o(store)
}
var err error
if !store.initTableDisabled {
err = store.initTable()
}
if err != nil {
return store, err
}
return store, err
}
func (s *ClientStore) initTable() error {
return s.adapter.Exec(context.Background(), fmt.Sprintf(`
CREATE TABLE IF NOT EXISTS %[1]s (
"id" TEXT NOT NULL,
"secret" TEXT NOT NULL,
"domain" TEXT NOT NULL,
"data" JSONB NOT NULL,
CONSTRAINT %[1]s_pkey PRIMARY KEY (id)
);
`, s.tableName))
}
func (s *ClientStore) toClientInfo(data []byte) (oauth2.ClientInfo, error) {
var cm models.Client
err := json.Unmarshal(data, &cm)
return &cm, err
}
// GetByID retrieves and returns client information by id
func (s *ClientStore) GetByID(ctx context.Context, id string) (oauth2.ClientInfo, error) {
if id == "" {
return nil, nil
}
var item ClientStoreItem
if err := s.adapter.SelectOne(ctx, &item, fmt.Sprintf(`SELECT "id", "secret", "domain", "data" FROM "%s" WHERE "id" = $1`, s.tableName), id); err != nil {
return nil, err
}
return s.toClientInfo(item.Data)
}
// Create creates and stores the new client information
func (s *ClientStore) Create(info oauth2.ClientInfo) error {
data, err := json.Marshal(info)
if err != nil {
return err
}
return s.adapter.Exec(
context.Background(),
fmt.Sprintf(`INSERT INTO %s ("id", "secret", "domain", "data") VALUES ($1, $2, $3, $4)`, s.tableName),
info.GetID(),
info.GetSecret(),
info.GetDomain(),
data,
)
}