-
Notifications
You must be signed in to change notification settings - Fork 0
/
module.go
88 lines (83 loc) · 2.04 KB
/
module.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
package auth
import (
"fmt"
"github.com/allentom/haruka"
"github.com/allentom/harukap"
"github.com/allentom/harukap/config"
)
type AuthModuleConfig struct {
EnableAnonymous bool
}
type AuthModule struct {
Plugins []harukap.AuthPlugin
AuthMiddleware AuthMiddleware
ConfigProvider *config.Provider
Config AuthModuleConfig
CacheStore *TokenStoreManager
}
func (m *AuthModule) AddCacheStore(convert Serializer) {
m.CacheStore = &TokenStoreManager{
Serializer: convert,
module: m,
}
}
func (m *AuthModule) InitModule() error {
authConfig := AuthModuleConfig{}
configer := m.ConfigProvider.Manager
for key := range configer.GetStringMap("auth") {
configType := configer.GetString(fmt.Sprintf("auth.%s.type", key))
enable := configer.GetBool(fmt.Sprintf("auth.%s.enable", key))
if configType == "anonymous" && enable {
authConfig.EnableAnonymous = true
}
}
m.Config = authConfig
m.AuthMiddleware = AuthMiddleware{
Module: m,
}
if m.CacheStore != nil {
err := m.CacheStore.Init()
if err != nil {
return err
}
}
return nil
}
func (m *AuthModule) GetAuthPluginByName(name string) harukap.AuthPlugin {
for _, plugin := range m.Plugins {
if plugin.TokenTypeName() == name {
return plugin
}
}
return nil
}
func (m *AuthModule) GetAuthConfig() ([]interface{}, error) {
authMaps := make([]interface{}, 0)
configManager := m.ConfigProvider.Manager
for key := range configManager.GetStringMap("auth") {
authType := configManager.GetString(fmt.Sprintf("auth.%s.type", key))
enable := configManager.GetBool(fmt.Sprintf("auth.%s.enable", key))
if !enable {
continue
}
if authType == "anonymous" {
authMaps = append(authMaps, haruka.JSON{
"type": "anonymous",
"name": "Anonymous",
"url": "",
})
} else {
for _, authPlugin := range m.Plugins {
if authPlugin.AuthName() == authType {
authInfo, err := authPlugin.GetAuthInfo()
if err != nil {
return nil, err
}
authMaps = append(authMaps, authInfo)
break
}
}
}
}
return authMaps, nil
}