forked from qvest-digital/loginsrv
/
provider.go
54 lines (43 loc) · 1.31 KB
/
provider.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
package oauth2
import (
"github.com/tarent/loginsrv/model"
)
// Provider is the description of an oauth provider adapter
type Provider struct {
// The name to access the provider in the configuration
Name string
// The oauth authentication url to redirect to
AuthURL string
// The url for token exchange
TokenURL string
// Default Scopes is a space separated list of oauth scopes to use for this provider.
// This list can be overwritten by configuration.
DefaultScopes string
// GetUserInfo is a provider specific Implementation
// for fetching the user information.
// Possible keys in the returned map are:
// username, email, name
GetUserInfo func(token TokenInfo) (u model.UserInfo, rawUserJson string, err error)
}
var provider = map[string]Provider{}
// RegisterProvider an Oauth provider
func RegisterProvider(p Provider) {
provider[p.Name] = p
}
// UnRegisterProvider removes a provider
func UnRegisterProvider(name string) {
delete(provider, name)
}
// GetProvider returns a provider
func GetProvider(providerName string) (Provider, bool) {
p, exist := provider[providerName]
return p, exist
}
// ProviderList returns the names of all registered provider
func ProviderList() []string {
list := make([]string, 0, len(provider))
for k := range provider {
list = append(list, k)
}
return list
}