-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.go
54 lines (42 loc) · 1022 Bytes
/
options.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
package login
import (
"net/http"
oauth2 "github.com/oxisto/oauth2go"
)
func WithLoginPage(opts ...handlerOption) oauth2.AuthorizationServerOption {
h := NewHandler()
for _, o := range opts {
o(h)
}
return func(srv *oauth2.AuthorizationServer) {
h.srv = srv
mux := srv.Handler.(*http.ServeMux)
mux.Handle("/login", h)
mux.HandleFunc("/authorize", h.handleAuthorize)
}
}
func WithPassswordHasher(pwh PasswordHasher) handlerOption {
return func(h *handler) {
h.pwh = pwh
}
}
func WithBaseURL(url string) handlerOption {
return func(h *handler) {
h.baseURL = url
}
}
func WithUser(name string, password string) handlerOption {
return func(srv *handler) {
hash, err := srv.pwh.GenerateFromPassword([]byte(password))
if err != nil {
srv.log.Printf("Could not generate hash from password: %w. Not adding user", err)
return
}
srv.users = append(srv.users, &User{
Name: name,
PasswordHash: string(hash),
})
password = ""
}
}
type handlerOption func(*handler)