-
Notifications
You must be signed in to change notification settings - Fork 13
/
authentication.go
130 lines (108 loc) · 4.01 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
128
129
130
package registry
import (
"github.com/sarulabs/di"
"github.com/coretrix/hitrix/service"
"github.com/coretrix/hitrix/service/component/app"
"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/social"
)
const (
DefaultOTPTTLInSeconds = 300
DefaultOTPLength = 5
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) {
appService := ctn.Get(service.AppService).(*app.App)
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
otpLength := DefaultOTPLength
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
}
otpLengthConfig, ok := configService.Int("authentication.otp_length")
if ok && otpLengthConfig > 0 {
otpLength = otpLengthConfig
}
passwordService := ctn.Get(service.PasswordService).(password.IPassword)
jwtService := ctn.Get(service.JWTService).(*jwt.JWT)
clockService := ctn.Get(service.ClockService).(clock.IClock)
var mailService *mail.ISender
mailServiceHitrix, err := ctn.SafeGet(service.MailService)
if err == nil && mailServiceHitrix != nil {
convertedMail := mailServiceHitrix.(mail.ISender)
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)
}
supportSocialLoginApple, ok := configService.Bool("authentication.support_social_login_apple")
if ok && supportSocialLoginApple {
appleService, err := ctn.SafeGet(service.AppleService)
if err != nil {
panic("apple service not loaded")
}
socialServiceMapping[authentication.SocialLoginApple] = appleService.(social.IUserData)
}
if appService.RedisPools == nil || appService.RedisPools.Persistent == "" {
panic("redis persistent needs to be set")
}
return authentication.NewAuthenticationService(
secret,
accessTokenTTL,
refreshTokenTTL,
otpTTL,
otpLength,
appService,
service.DI().Generator(),
service.DI().ErrorLogger(),
clockService,
passwordService,
jwtService,
mailService,
socialServiceMapping,
service.DI().UUID(),
), nil
},
}
}