-
Notifications
You must be signed in to change notification settings - Fork 99
/
handlers.go
114 lines (95 loc) · 3.22 KB
/
handlers.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
package auth
import (
"crypto/md5"
"fmt"
"html/template"
"mime"
"net/http"
"path"
"path/filepath"
"strings"
"time"
"github.com/qor/auth/claims"
"github.com/qor/responder"
"github.com/qor/session"
)
func respondAfterLogged(claims *claims.Claims, context *Context) {
// login user
context.Auth.Login(context.Writer, context.Request, claims)
responder.With("html", func() {
// write cookie
context.Auth.Redirector.Redirect(context.Writer, context.Request, "login")
}).With([]string{"json"}, func() {
// TODO write json token
}).Respond(context.Request)
}
// DefaultLoginHandler default login behaviour
var DefaultLoginHandler = func(context *Context, authorize func(*Context) (*claims.Claims, error)) {
var (
req = context.Request
w = context.Writer
claims, err = authorize(context)
)
if err == nil && claims != nil {
context.SessionStorer.Flash(w, req, session.Message{Message: "logged"})
respondAfterLogged(claims, context)
return
}
context.SessionStorer.Flash(w, req, session.Message{Message: template.HTML(err.Error()), Type: "error"})
// error handling
responder.With("html", func() {
context.Auth.Config.Render.Execute("auth/login", context, req, w)
}).With([]string{"json"}, func() {
// TODO write json error
}).Respond(context.Request)
}
// DefaultRegisterHandler default register behaviour
var DefaultRegisterHandler = func(context *Context, register func(*Context) (*claims.Claims, error)) {
var (
req = context.Request
w = context.Writer
claims, err = register(context)
)
if err == nil && claims != nil {
respondAfterLogged(claims, context)
return
}
context.SessionStorer.Flash(w, req, session.Message{Message: template.HTML(err.Error()), Type: "error"})
// error handling
responder.With("html", func() {
context.Auth.Config.Render.Execute("auth/register", context, req, w)
}).With([]string{"json"}, func() {
// TODO write json error
}).Respond(context.Request)
}
// DefaultLogoutHandler default logout behaviour
var DefaultLogoutHandler = func(context *Context) {
// Clear auth session
context.SessionStorer.Delete(context.Writer, context.Request)
context.Auth.Redirector.Redirect(context.Writer, context.Request, "logout")
}
var cacheSince = time.Now().Format(http.TimeFormat)
// DefaultAssetHandler render auth asset file
var DefaultAssetHandler = func(context *Context) {
asset := strings.TrimPrefix(context.Request.URL.Path, context.Auth.URLPrefix)
if context.Request.Header.Get("If-Modified-Since") == cacheSince {
context.Writer.WriteHeader(http.StatusNotModified)
return
}
context.Writer.Header().Set("Last-Modified", cacheSince)
if content, err := context.Config.Render.Asset(path.Join("/auth", asset)); err == nil {
etag := fmt.Sprintf("%x", md5.Sum(content))
if context.Request.Header.Get("If-None-Match") == etag {
context.Writer.WriteHeader(http.StatusNotModified)
return
}
if ctype := mime.TypeByExtension(filepath.Ext(asset)); ctype != "" {
context.Writer.Header().Set("Content-Type", ctype)
}
context.Writer.Header().Set("Cache-control", "private, must-revalidate, max-age=300")
context.Writer.Header().Set("ETag", etag)
context.Writer.Write(content)
} else {
http.NotFound(context.Writer, context.Request)
}
}