-
Notifications
You must be signed in to change notification settings - Fork 1
/
provider.go
59 lines (51 loc) · 1.4 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
55
56
57
58
59
package auth
import oauth "golang.org/x/oauth2"
// Provider is a oauth2 provider, like facebook or google
// Name is the provider name, the package use it as a index.
// Key is the oauth2 key, normally called ACCESS_KEY
// Secret is the oauth2 secret key, normally called ACCESS_SECRET
// RedirectURL is a URL configured in provider
// TokenURL is a URL to get the token on provider
// AuthURL is a URL to auth user on provider
// UserInfoURL is a URL to get User Information on provider
// Scope is whats the scope your app wants
type Provider struct {
Name string
Key string
Secret string
RedirectURL string
TokenURL string
AuthURL string
UserInfoURL string
Scopes []string
}
// Email/Password default provider
var EmailPasswordProvider = Provider{
Name: "emailpassword",
}
// Internal auth config builder
type builderConfig struct {
Auth *oauth.Config
UserInfoURL string
}
func (b *Auth) NewProviders(providers []Provider) {
for _, p := range providers {
b.NewProvider(p)
}
}
func (a *Auth) NewProvider(p Provider) {
config := &oauth.Config{
ClientID: p.Key,
ClientSecret: p.Secret,
RedirectURL: p.RedirectURL,
Scopes: p.Scopes,
Endpoint: oauth.Endpoint{
AuthURL: p.AuthURL,
TokenURL: p.TokenURL,
},
}
provider := new(builderConfig)
provider.Auth = config
provider.UserInfoURL = p.UserInfoURL
a.Providers[p.Name] = provider
}