-
Notifications
You must be signed in to change notification settings - Fork 0
/
mux.go
104 lines (87 loc) · 2.43 KB
/
mux.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
package extensions
import (
"embed"
"time"
"github.com/deltegui/phx"
"github.com/deltegui/phx/core"
"github.com/deltegui/phx/csrf"
"github.com/deltegui/phx/cypher"
"github.com/deltegui/phx/hash"
"github.com/deltegui/phx/middleware"
"github.com/deltegui/phx/renderer"
"github.com/deltegui/phx/session"
)
func AddCypherWithPassword(r *phx.Router, password string) {
r.Add(func() core.Cypher {
return cypher.NewWithPasswordAsString(password)
})
}
func AddHasher(r *phx.Router) {
r.Add(func() core.Hasher { return hash.BcryptHasher{} })
}
func AddCypher(r *phx.Router) {
r.Add(func() core.Cypher { return cypher.New() })
}
func UseCsrf(r *phx.Router, duration time.Duration) {
r.Add(func(cy core.Cypher) *csrf.Csrf {
return csrf.New(duration, cy)
})
r.Run(func(c *csrf.Csrf) {
r.Use(middleware.Csrf(c))
})
}
func AddSession(r *phx.Router, duration time.Duration) {
r.Add(func(hasher core.Hasher, cy core.Cypher) *session.Manager {
return session.NewManager(
session.NewMemoryStore(),
hasher,
duration,
cy)
})
}
func AddSessionWithStore(r *phx.Router, duration time.Duration, store session.SessionStore) {
r.Add(func(hasher core.Hasher, cy core.Cypher) *session.Manager {
return session.NewManager(
store,
hasher,
duration,
cy)
})
}
type Authorization struct {
redirect string
manager *session.Manager
}
func AddAuthorizationWithRedirect(r *phx.Router, redirect string) Authorization {
auth := Authorization{}
r.Run(func(manager *session.Manager) {
auth.manager = manager
})
auth.redirect = redirect
return auth
}
func AddAuthorization(r *phx.Router) Authorization {
return AddAuthorizationWithRedirect(r, "")
}
func (auth Authorization) Authorize() phx.Middleware {
return middleware.Authorize(auth.manager, auth.redirect)
}
func (auth Authorization) Roles(roles []core.Role) phx.Middleware {
return middleware.AuthorizeRoles(auth.manager, auth.redirect, roles)
}
func (auth Authorization) Admin() phx.Middleware {
return middleware.Admin(auth.manager, auth.redirect)
}
func AddRendering(r *phx.Router, fs embed.FS) *renderer.TemplateRenderer {
rend := renderer.NewTemplateRenderer(fs)
rend.AddDefaultTemplateFunctions()
r.Add(func() phx.Renderer { return rend })
r.Add(func() *renderer.TemplateRenderer { return rend })
return rend
}
func UseCors(r *phx.Router, opt middleware.CorsOptions) {
r.Use(middleware.Cors(opt))
}
func UseCorsDefault(r *phx.Router) {
r.Use(middleware.CorsDefault())
}