-
Notifications
You must be signed in to change notification settings - Fork 13
/
authentication.go
55 lines (49 loc) · 1.59 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
package registry
import (
"github.com/coretrix/hitrix/service"
"github.com/coretrix/hitrix/service/component/authentication"
"github.com/coretrix/hitrix/service/component/jwt"
"github.com/coretrix/hitrix/service/component/password"
"github.com/latolukasz/orm"
"github.com/sarulabs/di"
)
func ServiceProviderAuthentication() *service.Definition {
return &service.Definition{
Name: service.AuthenticationService,
Global: true,
Build: func(ctn di.Container) (interface{}, error) {
subContainer, err := ctn.SubContainer()
if err != nil {
return nil, err
}
config := service.DI().Config().Get("authentication")
if config == nil {
panic("`authentication` key does not exists in configuration")
}
configMap := config.(map[string]interface{})
if configMap["secret"] == nil {
panic("`authentication` key does not exists in configuration")
}
secret := configMap["secret"].(string)
accessTokenTTL := 24 * 60 * 60
refreshTokenTTL := 365 * 24 * 60 * 60
if configMap["accessTokenTTL"] != nil {
accessTokenTTL = configMap["accessTokenTTL"].(int)
}
if configMap["refreshTokenTTL"] != nil {
refreshTokenTTL = configMap["refreshTokenTTL"].(int)
}
ormService := subContainer.Get(service.ORMEngineRequestService).(*orm.Engine)
passwordService := subContainer.Get(service.PasswordService).(*password.Password)
jwtService := subContainer.Get(service.JWTService).(*jwt.JWT)
return authentication.NewAuthenticationService(
secret,
accessTokenTTL,
refreshTokenTTL,
ormService,
passwordService,
jwtService,
), nil
},
}
}