-
Notifications
You must be signed in to change notification settings - Fork 50
/
provider_list.go
54 lines (44 loc) · 1.53 KB
/
provider_list.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 gomniauth
import (
"github.com/stretchr/gomniauth/common"
)
// SharedProviderList keeps track of the last created ProviderList.
// It is useful for using shortcut methods directly on gomniauth package
// rather than having to refer to the list.
var SharedProviderList common.ProviderList
// ProviderList represents a simple common.ProviderList that holds
// an array of providers, and allows access to them.
type ProviderList struct {
providers []common.Provider
}
// WithProviders generates a new ProviderList which should be
// used to interact with Gomniauth services.
func WithProviders(providers ...common.Provider) *ProviderList {
GetSecurityKey()
list := &ProviderList{providers}
SharedProviderList = list
return list
}
// Add adds a provider to this list.
func (l *ProviderList) Add(provider common.Provider) *ProviderList {
l.providers = append(l.providers, provider)
return l
}
// Provider gets a provider by name, or returns a common.MissingProviderError
// if no provider with that name is registered.
func (l *ProviderList) Provider(name string) (common.Provider, error) {
// panic on nil
if l == nil {
panic(common.PrefixForErrors + "No providers have been initialised. Make sure you have called gomniauth.WithProviders(...).")
}
for _, provider := range l.providers {
if provider.Name() == name {
return provider, nil
}
}
return nil, &common.MissingProviderError{ProviderName: name}
}
// Providers gets all registered Provider objects.
func (l *ProviderList) Providers() []common.Provider {
return l.providers
}