-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser_provider_factory.go
64 lines (51 loc) · 1.39 KB
/
user_provider_factory.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
package grok
import (
"os"
"time"
"github.com/patrickmn/go-cache"
"gopkg.in/auth0.v3/management"
)
// UserProviderFactory ...
func UserProviderFactory(settings *Settings) Provider {
switch settings.UserProvider.Kind {
case "fake":
return createMockProvider(settings)
default:
return createAuth0Provider(settings)
}
}
func createAuth0Provider(settings *Settings) Provider {
cache := cache.New(
time.Duration(settings.UserProvider.Auth0.CacheTTL)*time.Minute,
10*time.Second)
clientID := settings.UserProvider.Auth0.ClientID
clientSecret := settings.UserProvider.Auth0.ClientSecret
if settings.UserProvider.Auth0.ClientFrom == "environment" {
clientID = os.Getenv(settings.UserProvider.Auth0.ClientIDEnv)
clientSecret = os.Getenv(settings.UserProvider.Auth0.ClientSecretEnv)
}
management, _ := management.New(
settings.UserProvider.Auth0.Domain,
clientID,
clientSecret,
)
return NewAuth0Provider(cache, management)
}
func createMockProvider(settings *Settings) Provider {
users := []*User{}
for _, user := range settings.UserProvider.Mock {
users = append(users, &User{
ID: user["id"].(string),
Stores: parseStores(user["stores"].([]interface{})),
Email: user["email"].(string),
})
}
return NewMockProvider(users...)
}
func parseStores(stores []interface{}) []string {
ss := []string{}
for _, s := range stores {
ss = append(ss, s.(string))
}
return ss
}