-
Notifications
You must be signed in to change notification settings - Fork 74
/
app_settings.go
97 lines (79 loc) · 2.88 KB
/
app_settings.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
89
90
91
92
93
94
95
96
97
package config
import (
"encoding/base64"
"fmt"
"github.com/larksuite/oapi-sdk-go/core/constants"
"os"
)
type AppSettingsOpt func(*AppSettings)
type AppSettings struct {
AppType constants.AppType
AppID string
AppSecret string
VerificationToken string
EncryptKey string
HelpDeskID string
HelpDeskToken string
}
func (s *AppSettings) helpDeskAuthorization() string {
if s.HelpDeskID != "" && s.HelpDeskToken != "" {
helpdeskAuthToken := base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", s.HelpDeskID, s.HelpDeskToken)))
return helpdeskAuthToken
}
return ""
}
func GetISVAppSettingsByEnv() *AppSettings {
return NewISVAppSettingsByOpts(getAppSettingsOptsByEnv()...)
}
func GetInternalAppSettingsByEnv() *AppSettings {
return NewInternalAppSettingsByOpts(getAppSettingsOptsByEnv()...)
}
func NewISVAppSettings(appID, appSecret, verificationToken, encryptKey string) *AppSettings {
return NewISVAppSettingsByOpts(SetAppCredentials(appID, appSecret), SetAppEventKey(verificationToken, encryptKey))
}
func NewInternalAppSettings(appID, appSecret, verificationToken, encryptKey string) *AppSettings {
return NewInternalAppSettingsByOpts(SetAppCredentials(appID, appSecret), SetAppEventKey(verificationToken, encryptKey))
}
func NewISVAppSettingsByOpts(opts ...AppSettingsOpt) *AppSettings {
return newAppSettingsByOpts(constants.AppTypeISV, opts...)
}
func NewInternalAppSettingsByOpts(opts ...AppSettingsOpt) *AppSettings {
return newAppSettingsByOpts(constants.AppTypeInternal, opts...)
}
func newAppSettingsByOpts(appType constants.AppType, optFns ...AppSettingsOpt) *AppSettings {
settings := &AppSettings{AppType: appType}
for _, opt := range optFns {
opt(settings)
}
if settings.AppID == "" || settings.AppSecret == "" {
panic("appID or appSecret is empty")
}
return settings
}
func getAppSettingsOptsByEnv() []AppSettingsOpt {
var opts []AppSettingsOpt
appID, appSecret, verificationToken, encryptKey, helpDeskID, helpDeskToken := os.Getenv("APP_ID"), os.Getenv("APP_SECRET"),
os.Getenv("VERIFICATION_TOKEN"), os.Getenv("ENCRYPT_KEY"), os.Getenv("HELP_DESK_ID"), os.Getenv("HELP_DESK_TOKEN")
opts = append(opts, SetAppCredentials(appID, appSecret))
opts = append(opts, SetAppEventKey(verificationToken, encryptKey))
opts = append(opts, SetHelpDeskCredentials(helpDeskID, helpDeskToken))
return opts
}
func SetAppCredentials(appID, appSecret string) AppSettingsOpt {
return func(settings *AppSettings) {
settings.AppID = appID
settings.AppSecret = appSecret
}
}
func SetAppEventKey(verificationToken, encryptKey string) AppSettingsOpt {
return func(settings *AppSettings) {
settings.VerificationToken = verificationToken
settings.EncryptKey = encryptKey
}
}
func SetHelpDeskCredentials(helpDeskID, helpDeskToken string) AppSettingsOpt {
return func(settings *AppSettings) {
settings.HelpDeskID = helpDeskID
settings.HelpDeskToken = helpDeskToken
}
}