-
Notifications
You must be signed in to change notification settings - Fork 13
/
authentication.go
127 lines (106 loc) · 3.75 KB
/
authentication.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
package registry
import (
"github.com/coretrix/hitrix/service"
"github.com/coretrix/hitrix/service/component/authentication"
"github.com/coretrix/hitrix/service/component/clock"
"github.com/coretrix/hitrix/service/component/config"
"github.com/coretrix/hitrix/service/component/jwt"
"github.com/coretrix/hitrix/service/component/mail"
"github.com/coretrix/hitrix/service/component/password"
"github.com/coretrix/hitrix/service/component/sms"
"github.com/coretrix/hitrix/service/component/social"
"github.com/sarulabs/di"
)
const (
DefaultOTPTTLInSeconds = 300
DefaultAccessTokenTTLInSeconds = 24 * 60 * 60
DefaultRefreshTokenTTLInSeconds = 365 * 24 * 60 * 60
)
func ServiceProviderAuthentication() *service.DefinitionGlobal {
return &service.DefinitionGlobal{
Name: service.AuthenticationService,
Build: func(ctn di.Container) (interface{}, error) {
configService := ctn.Get(service.ConfigService).(config.IConfig)
if configService == nil {
panic("`config is nil")
}
secret, ok := configService.String("authentication.secret")
if !ok {
panic("secret is missing")
}
accessTokenTTL := DefaultAccessTokenTTLInSeconds
refreshTokenTTL := DefaultRefreshTokenTTLInSeconds
otpTTL := DefaultOTPTTLInSeconds
accessTokenTTLConfig, ok := configService.Int("authentication.access_token_ttl")
if ok && accessTokenTTLConfig > 0 {
accessTokenTTL = accessTokenTTLConfig
}
refreshTokenTTLConfig, ok := configService.Int("authentication.refresh_token_ttl")
if ok && refreshTokenTTLConfig > 0 {
refreshTokenTTL = refreshTokenTTLConfig
}
otpTTLConfig, ok := configService.Int("authentication.otp_ttl")
if ok && refreshTokenTTLConfig > 0 {
otpTTL = otpTTLConfig
}
passwordService := ctn.Get(service.PasswordService).(password.IPassword)
jwtService := ctn.Get(service.JWTService).(*jwt.JWT)
clockService := ctn.Get(service.ClockService).(clock.IClock)
supportOTPConfig, ok := configService.Bool("authentication.support_otp")
var smsService sms.ISender
if ok && supportOTPConfig {
var has bool
smsService, has = ctn.Get(service.SMSService).(sms.ISender)
if !has {
panic("sms service not loaded")
}
}
var mailService *mail.Sender
mailServiceHitrix, err := ctn.SafeGet(service.MailMandrillService)
if err == nil && mailServiceHitrix != nil {
convertedMail := mailServiceHitrix.(mail.Sender)
mailService = &convertedMail
}
var socialServiceMapping = make(map[string]social.IUserData)
supportSocialLoginGoogle, ok := configService.Bool("authentication.support_social_login_google")
if ok && supportSocialLoginGoogle {
googleService, err := ctn.SafeGet(service.GoogleService)
if err != nil {
panic("google service not loaded")
}
socialServiceMapping[authentication.SocialLoginGoogle] = googleService.(social.IUserData)
}
supportSocialLoginFacebook, ok := configService.Bool("authentication.support_social_login_facebook")
if ok && supportSocialLoginFacebook {
googleService, err := ctn.SafeGet(service.FacebookService)
if err != nil {
panic("google service not loaded")
}
socialServiceMapping[authentication.SocialLoginFacebook] = googleService.(social.IUserData)
}
generatorService, has := service.DI().Generator()
if !has {
panic("generator service not loaded")
}
errorLoggerService, has := service.DI().ErrorLogger()
if !has {
panic("error logger service not loaded")
}
return authentication.NewAuthenticationService(
secret,
accessTokenTTL,
refreshTokenTTL,
otpTTL,
smsService,
generatorService,
errorLoggerService,
clockService,
passwordService,
jwtService,
mailService,
socialServiceMapping,
service.DI().UUID(),
), nil
},
}
}