-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathviews.go
153 lines (130 loc) · 3.5 KB
/
views.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
package views
import (
"context"
"encoding/gob"
"encoding/hex"
"log"
"time"
"github.com/go-playground/validator/v10"
"github.com/gorilla/securecookie"
"github.com/gorilla/sessions"
"github.com/patrickmn/go-cache"
"github.com/ystv/web-auth/api"
"github.com/ystv/web-auth/infrastructure/db"
"github.com/ystv/web-auth/infrastructure/mail"
"github.com/ystv/web-auth/officership"
"github.com/ystv/web-auth/permission"
"github.com/ystv/web-auth/role"
"github.com/ystv/web-auth/templates"
"github.com/ystv/web-auth/user"
)
type (
// Config the global web-auth configuration
Config struct {
Version string
Commit string
Debug bool
Address string
DatabaseURL string
BaseDomainName string
DomainName string
LogoutEndpoint string
SessionCookieName string
CDNEndpoint string
Mail SMTPConfig
Security SecurityConfig
}
// SMTPConfig stores the SMTP Mailer configuration
SMTPConfig struct {
Host string
Username string
Password string
Port int
DomainName string
}
// SecurityConfig stores the security configuration
SecurityConfig struct {
EncryptionKey string
AuthenticationKey string
SigningKey string
}
// Views encapsulates our view dependencies
Views struct {
api *api.Store
cache *cache.Cache
conf *Config
cookie *sessions.CookieStore
Mailer *mail.Mailer
officership *officership.Store
permission *permission.Store
role *role.Store
template *templates.Templater
user *user.Store
mailer *mail.MailerInit
validate *validator.Validate
}
TemplateHelper struct {
UserPermissions []permission.Permission
ActivePage string
Assumed bool
}
)
// New initialises connections, templates, and cookies
func New(conf *Config, host string) *Views {
v := &Views{}
// Connecting to stores
dbStore := db.NewStore(conf.DatabaseURL, host)
v.officership = officership.NewOfficershipRepo(dbStore)
v.permission = permission.NewPermissionRepo(dbStore)
v.role = role.NewRoleRepo(dbStore)
v.user = user.NewUserRepo(dbStore, conf.CDNEndpoint)
v.api = api.NewAPIRepo(dbStore)
v.template = templates.NewTemplate(v.permission, v.role, v.user)
// Initialising cache
v.cache = cache.New(1*time.Hour, 1*time.Hour)
// Initialise mailer
v.mailer = mail.NewMailer(mail.Config{
Host: conf.Mail.Host,
Port: conf.Mail.Port,
Username: conf.Mail.Username,
Password: conf.Mail.Password,
DomainName: conf.Mail.DomainName,
})
// Initialising session cookie
authKey, _ := hex.DecodeString(conf.Security.AuthenticationKey)
if len(authKey) == 0 {
authKey = securecookie.GenerateRandomKey(64)
}
encryptionKey, _ := hex.DecodeString(conf.Security.EncryptionKey)
if len(encryptionKey) == 0 {
encryptionKey = securecookie.GenerateRandomKey(32)
}
v.cookie = sessions.NewCookieStore(
authKey,
encryptionKey,
)
sixty := 60
twentyFour := 24
v.cookie.Options = &sessions.Options{
MaxAge: sixty * sixty * twentyFour,
HttpOnly: true,
Domain: "." + conf.BaseDomainName,
Path: "/",
}
// So we can use our struct in the cookie
gob.Register(user.User{})
gob.Register(InternalContext{})
v.conf = conf
// Struct validator
v.validate = validator.New()
go func() {
for {
err := v.api.DeleteOldToken(context.Background())
if err != nil {
log.Printf("failed to delete old token func: %+v", err)
}
time.Sleep(30 * time.Second)
}
}()
return v
}