-
Notifications
You must be signed in to change notification settings - Fork 13
/
otp.go
158 lines (130 loc) · 5.04 KB
/
otp.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package registry
import (
"errors"
"fmt"
"strings"
"github.com/latolukasz/beeorm"
"github.com/sarulabs/di"
"github.com/coretrix/hitrix/pkg/entity"
"github.com/coretrix/hitrix/service"
"github.com/coretrix/hitrix/service/component/config"
"github.com/coretrix/hitrix/service/component/generator"
"github.com/coretrix/hitrix/service/component/otp"
)
func ServiceProviderOTP(forceProviders ...string) *service.DefinitionGlobal {
return &service.DefinitionGlobal{
Name: service.OTPService,
Build: func(ctn di.Container) (interface{}, error) {
configService := ctn.Get(service.ConfigService).(config.IConfig)
generatorService := ctn.Get(service.GeneratorService).(generator.IGenerator)
providers := make([]otp.IOTPSMSGateway, 0)
if len(forceProviders) > 0 {
for _, forceProvider := range forceProviders {
builderFunc, ok := smsOTPProviderBuilderFactory[forceProvider]
if !ok {
return nil, fmt.Errorf("unknown provider: %v", forceProvider)
}
provider, err := builderFunc(configService, generatorService, nil)
if err != nil {
return nil, err
}
providers = append(providers, provider)
}
} else {
ormService := ctn.Get(service.ORMEngineGlobalService).(*beeorm.Engine)
q := &beeorm.RedisSearchQuery{}
q.FilterString("Key", "otp_sms_provider")
settingsEntity := &entity.SettingsEntity{}
if has := ormService.RedisSearchOne(settingsEntity, q); !has {
return nil, errors.New("otp_sms_provider not found in settings")
}
providersWithPhonePrefixes := strings.Split(settingsEntity.Value, ";")
for _, providerWithPhonePrefixes := range providersWithPhonePrefixes {
providerNameWithPhonePrefixes := strings.Split(providerWithPhonePrefixes, ":")
providerName := providerNameWithPhonePrefixes[0]
builderFunc, ok := smsOTPProviderBuilderFactory[providerName]
if !ok {
return nil, fmt.Errorf("unknown otp_sms_provider: %v", providerName)
}
var phonePrefixes []string
if len(providerNameWithPhonePrefixes) > 1 {
phonePrefixes = make([]string, 0)
phonePrefixesSplit := strings.Split(providerNameWithPhonePrefixes[1], ",")
if len(phonePrefixesSplit) != 0 {
phonePrefixes = phonePrefixesSplit
}
}
provider, err := builderFunc(configService, generatorService, phonePrefixes)
if err != nil {
return nil, err
}
providers = append(providers, provider)
}
}
if len(providers) == 0 {
return nil, errors.New("must provide otp_sms_provider in settings or at least 1 forceProviders")
}
retry, ok := configService.Bool("sms.retry")
if !ok {
return nil, errors.New("missing sms.retry")
}
return otp.NewOTP(retry, providers...), nil
},
}
}
var smsOTPProviderBuilderFactory = map[string]func(configService config.IConfig, generatorService generator.IGenerator, phonePrefixes []string) (otp.IOTPSMSGateway, error){
otp.SMSOTPProviderTwilio: twilioSMSOTPProviderBuilder,
otp.SMSOTPProviderSinch: sinchSMSOTPProviderBuilder,
otp.SMSOTPProviderMada: madaSMSOTPProviderBuilder,
}
func twilioSMSOTPProviderBuilder(configService config.IConfig, _ generator.IGenerator, _ []string) (otp.IOTPSMSGateway, error) {
sid, ok := configService.String("sms.twilio.sid")
if !ok {
return nil, errors.New("missing sms.twilio.sid")
}
token, ok := configService.String("sms.twilio.token")
if !ok {
return nil, errors.New("missing sms.twilio.token")
}
verifySID, _ := configService.String("sms.twilio.verify_sid")
return otp.NewTwilioSMSOTPProvider(sid, token, verifySID), nil
}
func sinchSMSOTPProviderBuilder(configService config.IConfig, _ generator.IGenerator, _ []string) (otp.IOTPSMSGateway, error) {
appID, ok := configService.String("sms.sinch.app_id")
if !ok {
return nil, errors.New("missing sms.sinch.app_id")
}
appSecret, ok := configService.String("sms.sinch.app_secret")
if !ok {
return nil, errors.New("missing sms.sinch.app_secret")
}
verificationURL, ok := configService.String("sms.sinch.verification_url")
if !ok {
return nil, errors.New("missing sms.sinch.verification_url")
}
return otp.NewSinchSMSOTPProvider(appID, appSecret, verificationURL), nil
}
func madaSMSOTPProviderBuilder(configService config.IConfig, generatorService generator.IGenerator, phonePrefixes []string) (otp.IOTPSMSGateway, error) {
username, ok := configService.String("sms.mada.username")
if !ok {
return nil, errors.New("missing sms.mada.username")
}
password, ok := configService.String("sms.mada.password")
if !ok {
return nil, errors.New("missing sms.mada.password")
}
url, ok := configService.String("sms.mada.url")
if !ok {
return nil, errors.New("missing sms.mada.url")
}
sourceName, ok := configService.String("sms.mada.source_name")
if !ok {
return nil, errors.New("missing sms.mada.source_name")
}
var otpLength int
otpLengthConfig, ok := configService.Int("authentication.otp_length")
if ok && otpLengthConfig > 0 {
otpLength = otpLengthConfig
}
return otp.NewMadaSMSOTPProvider(username, password, url, sourceName, otpLength, phonePrefixes, generatorService), nil
}