forked from pydio/cells
-
Notifications
You must be signed in to change notification settings - Fork 0
/
registry.go
196 lines (152 loc) · 4.08 KB
/
registry.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package auth
import (
"context"
"regexp"
"strconv"
"sync"
"github.com/jmoiron/sqlx"
"github.com/ory/hydra/client"
"github.com/ory/hydra/consent"
"github.com/ory/hydra/driver"
"github.com/ory/hydra/jwk"
"github.com/ory/hydra/oauth2"
"github.com/ory/hydra/x"
"github.com/ory/x/sqlcon"
"github.com/pkg/errors"
"go.uber.org/zap"
"github.com/pydio/cells/common"
"github.com/pydio/cells/common/log"
"github.com/pydio/cells/common/sql"
)
var (
reg driver.Registry
once = &sync.Once{}
syncLock = &sync.Mutex{}
onRegistryInits []func()
)
func InitRegistry(dao sql.DAO) {
once.Do(func() {
db := sqlx.NewDb(dao.DB(), dao.Driver())
reg = driver.NewRegistrySQL().WithConfig(conf)
r := reg.(*driver.RegistrySQL).WithDB(db)
r.Init()
sql.LockMigratePackage()
defer func() {
sql.UnlockMigratePackage()
}()
if _, err := r.ClientManager().(*client.SQLManager).CreateSchemas(dao.Driver()); err != nil {
log.Warn("Failed to create client schemas", zap.Error(err))
return
}
km := r.KeyManager().(*jwk.SQLManager)
if _, err := km.CreateSchemas(dao.Driver()); err != nil {
log.Warn("Failed to create key schemas", zap.Error(err))
return
} else {
if err := jwk.EnsureAsymmetricKeypairExists(context.Background(), r, new(jwk.RS256Generator), x.OpenIDConnectKeyName); err != nil {
log.Info("Could not ensure key exists, deleting...", zap.String("key", x.OpenIDConnectKeyName))
km.DeleteKeySet(context.Background(), x.OpenIDConnectKeyName)
}
if err := jwk.EnsureAsymmetricKeypairExists(context.Background(), r, new(jwk.RS256Generator), x.OAuth2JWTKeyName); err != nil {
log.Info("Could not ensure key exists, deleting...", zap.String("key", x.OAuth2JWTKeyName))
km.DeleteKeySet(context.Background(), x.OAuth2JWTKeyName)
}
}
if _, err := r.ConsentManager().(*consent.SQLManager).CreateSchemas(dao.Driver()); err != nil {
log.Warn("Failed to create consent schemas", zap.Error(err))
return
}
store := oauth2.NewFositeSQLStore(db, r, conf)
if _, err := store.CreateSchemas(dao.Driver()); err != nil {
log.Warn("Failed to create fosite sql store schemas", zap.Error(err))
}
RegisterOryProvider(r.OAuth2Provider())
})
if err := syncClients(context.Background(), reg.ClientManager(), conf.Clients()); err != nil {
log.Warn("Failed to sync clients", zap.Error(err))
return
}
for _, onRegistryInit := range onRegistryInits {
onRegistryInit()
}
}
func OnRegistryInit(f func()) {
onRegistryInits = append(onRegistryInits, f)
}
func GetRegistry() driver.Registry {
return reg
}
func GetRegistrySQL() *driver.RegistrySQL {
return reg.(*driver.RegistrySQL)
}
func syncClients(ctx context.Context, s client.Storage, c common.Scanner) error {
var clients []*client.Client
if c == nil {
return nil
}
syncLock.Lock()
defer syncLock.Unlock()
if err := c.Scan(&clients); err != nil {
return err
}
n, err := s.CountClients(ctx)
if err != nil {
return err
}
old, err := s.GetClients(ctx, n, 0)
if err != nil {
return err
}
for _, cli := range clients {
_, err := s.GetClient(ctx, cli.GetID())
var redirectURIs []string
for _, r := range cli.RedirectURIs {
redirectURIs = append(redirectURIs, rangeFromStr(r)...)
}
cli.RedirectURIs = redirectURIs
if errors.Cause(err) == sqlcon.ErrNoRows {
// Let's create it
if err := s.CreateClient(ctx, cli); err != nil {
return err
}
} else {
if err := s.UpdateClient(ctx, cli); err != nil {
return err
}
}
delete(old, cli.GetID())
}
for _, cli := range old {
if err := s.DeleteClient(ctx, cli.GetID()); err != nil {
return err
}
}
return nil
}
func rangeFromStr(s string) []string {
var res []string
re := regexp.MustCompile(`\[([0-9]+)-([0-9]+)\]`)
r := re.FindStringSubmatch(s)
if len(r) < 3 {
return []string{s}
}
min, err := strconv.Atoi(r[1])
if err != nil {
return []string{s}
}
max, err := strconv.Atoi(r[2])
if err != nil {
return []string{s}
}
if min > max {
return []string{s}
}
for {
if min > max {
break
}
res = append(res, re.ReplaceAllString(s, strconv.Itoa(min)))
min = min + 1
}
return res
}