-
Notifications
You must be signed in to change notification settings - Fork 16
/
oauth2.go
100 lines (76 loc) · 2.12 KB
/
oauth2.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
package obligator
import (
"context"
"errors"
"fmt"
"os"
"sync"
"github.com/lestrrat-go/jwx/v2/jwk"
)
type OAuth2MetadataManager struct {
db Database
oidcConfigs map[string]*OAuth2ServerMetadata
jwksRefreshers map[string]*jwk.Cache
mut *sync.Mutex
}
func NewOAuth2MetadataManager(db Database) *OAuth2MetadataManager {
m := &OAuth2MetadataManager{
db: db,
oidcConfigs: make(map[string]*OAuth2ServerMetadata),
mut: &sync.Mutex{},
}
return m
}
func (m *OAuth2MetadataManager) GetMeta(providerId string) (*OAuth2ServerMetadata, error) {
m.mut.Lock()
defer m.mut.Unlock()
if meta, exists := m.oidcConfigs[providerId]; exists {
return &(*meta), nil
}
return nil, errors.New("No such provider")
}
func (m *OAuth2MetadataManager) GetKeyset(providerId string) (jwk.Set, error) {
m.mut.Lock()
defer m.mut.Unlock()
ctx := context.Background()
keyset, err := m.jwksRefreshers[providerId].Get(ctx, m.oidcConfigs[providerId].JwksUri)
if err != nil {
return nil, err
}
return keyset, nil
}
func (m *OAuth2MetadataManager) Update() error {
// TODO: Make this more robust. We're holding the lock until the update
// completes. It will either succeed or we exit
m.mut.Lock()
go func() {
defer m.mut.Unlock()
m.oidcConfigs = make(map[string]*OAuth2ServerMetadata)
m.jwksRefreshers = make(map[string]*jwk.Cache)
ctx := context.Background()
providers, err := m.db.GetOAuth2Providers()
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
for _, oidcProvider := range providers {
if !oidcProvider.OpenIDConnect {
continue
}
var err error
m.oidcConfigs[oidcProvider.ID], err = GetOidcConfiguration(oidcProvider.URI)
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
m.jwksRefreshers[oidcProvider.ID] = jwk.NewCache(ctx)
m.jwksRefreshers[oidcProvider.ID].Register(m.oidcConfigs[oidcProvider.ID].JwksUri)
_, err = m.jwksRefreshers[oidcProvider.ID].Refresh(ctx, m.oidcConfigs[oidcProvider.ID].JwksUri)
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
}
}()
return nil
}