-
Notifications
You must be signed in to change notification settings - Fork 13
/
authentication.go
110 lines (92 loc) · 3.21 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
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.Password)
jwtService := ctn.Get(service.JWTService).(*jwt.JWT)
clockService := ctn.Get(service.ClockService).(clock.Clock)
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.MailMandrill)
if err == nil && mailServiceHitrix != nil {
convertedMail := mailServiceHitrix.(mail.Sender)
mailService = &convertedMail
}
supportSocialLoginGoogle, ok := configService.Bool("authentication.support_social_login_google")
var socialServiceMapping = make(map[string]social.IUserData)
if ok && supportSocialLoginGoogle {
googleService, err := ctn.SafeGet(service.GoogleService)
if err != nil {
panic("google service not loaded")
}
socialServiceMapping[authentication.SocialLoginGoogle] = googleService.(social.IUserData)
}
generatorService, has := service.DI().GeneratorService()
if !has {
panic("generator service not loaded")
}
return authentication.NewAuthenticationService(
secret,
accessTokenTTL,
refreshTokenTTL,
otpTTL,
smsService,
generatorService,
clockService,
passwordService,
jwtService,
mailService,
socialServiceMapping,
), nil
},
}
}