-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
145 lines (123 loc) · 2.82 KB
/
client.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package gooauth2gorm
import (
"context"
"encoding/json"
"errors"
"io"
"time"
"github.com/go-oauth2/oauth2/v4"
"github.com/go-oauth2/oauth2/v4/models"
"gorm.io/driver/clickhouse"
"gorm.io/driver/mysql"
"gorm.io/driver/postgres"
"gorm.io/driver/sqlite"
"gorm.io/driver/sqlserver"
"gorm.io/gorm"
)
type Client struct {
ID string `gorm:"primaryKey"`
Secret string
Domain string
Data string
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt gorm.DeletedAt `gorm:"index"`
}
type ClientStore struct {
db *gorm.DB
table string
stdout io.Writer
}
func NewClientStore(cfg *Config, table string) *ClientStore {
if cfg == nil {
panic(errors.New("db config is null"))
}
var d gorm.Dialector
switch cfg.DBType {
case PostgresSQL:
d = postgres.New(postgres.Config{
DSN: cfg.DSN,
})
case MySQL:
d = mysql.New(mysql.Config{
DSN: cfg.DSN,
})
case SQLite:
d = sqlite.Open(cfg.DSN)
case SQLServer:
d = sqlserver.Open(cfg.DSN)
case Clickhouse:
d = clickhouse.Open(cfg.DSN)
}
db, err := gorm.Open(d)
if err != nil {
panic(err)
}
sqlDB, err := db.DB()
if err != nil {
panic(err)
}
sqlDB.SetMaxIdleConns(cfg.MaxIdleConns)
sqlDB.SetMaxOpenConns(cfg.MaxOpenConns)
sqlDB.SetConnMaxLifetime(cfg.ConnMaxLifetime)
return NewClientStoreWithDB(cfg, db, table)
}
func NewClientStoreWithDB(cfg *Config, db *gorm.DB, table string) *ClientStore {
cs := &ClientStore{
db: db,
table: defaultClientTable,
}
if table != "" {
cs.table = table
}
if !db.Migrator().HasTable(cs.table) {
if err := db.Table(cs.table).Migrator().CreateTable(&Client{}); err != nil {
panic(err)
}
}
return cs
}
// GetByID retrieves and returns client information by id
func (cs *ClientStore) GetByID(ctx context.Context, id string) (oauth2.ClientInfo, error) {
if id == "" {
return nil, nil
}
var client *Client
if err := cs.db.WithContext(ctx).Table(cs.table).
Where("id = ?", id).
Find(&client).Error; err != nil {
return nil, err
}
return cs.toClientInfo(client.Data)
}
// Create create oauth2 client
func (cs *ClientStore) Create(ctx context.Context, info oauth2.ClientInfo) error {
data, err := json.Marshal(info)
if err != nil {
return err
}
client := &Client{
ID: info.GetID(),
Secret: info.GetSecret(),
Domain: info.GetDomain(),
Data: string(data),
}
return cs.db.WithContext(ctx).Table(cs.table).Create(client).Error
}
// RemoveByID delete the client by id
func (ts *ClientStore) RemoveByID(ctx context.Context, id string) error {
return ts.db.WithContext(ctx).Table(ts.table).
Where("id = ?", id).
Delete(&Client{}).Error
}
func (cs *ClientStore) toClientInfo(data string) (oauth2.ClientInfo, error) {
if data == "" {
return nil, nil
}
var cm models.Client
err := json.Unmarshal([]byte(data), &cm)
if err != nil {
return nil, err
}
return &cm, err
}