-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
109 lines (90 loc) · 2.36 KB
/
handler.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
package auth
import (
"net/http"
"time"
"github.com/pquerna/ffjson/ffjson"
"golang.org/x/net/context"
)
const (
contextKey = "uid"
)
// Handler is a login handler
type Handler struct {
auth *Authenticator
}
// NewHandler creates a new login handler
func NewHandler(auth *Authenticator) *Handler {
return &Handler{
auth: auth,
}
}
// NewHandlerAndAuthenticator creates a new login handler and Authenticator
func NewHandlerAndAuthenticator(method SigningMethod, storage Storage, lifetime time.Duration, refreshLifetime time.Duration) (*Handler, *Authenticator) {
generator := NewTokenGenerator(method)
auth := NewAuthenticator(generator, storage, lifetime, refreshLifetime)
return NewHandler(auth), auth
}
// CtxServeHTTP implements scaffold.Handler
func (h *Handler) CtxServeHTTP(ctx context.Context, w http.ResponseWriter, r *http.Request) {
response := &Response{}
defer func() {
encoder := ffjson.NewEncoder(w)
encoder.Encode(response)
}()
req, err := ParseRequest(r)
if err != nil {
response.Error = err.Error()
return
}
var token, refresh string
if req.Refresh != "" {
var user *User
user, err = h.auth.ValidateRefresh(req.Refresh)
if err != nil {
response.Error = err.Error()
return
}
token, err = h.auth.Generate(user)
if err != nil {
response.Error = err.Error()
return
}
refresh, err = h.auth.GenerateRefresh(user)
if err != nil {
response.Error = err.Error()
return
}
} else {
token, refresh, err = h.auth.Authenticate(req.Username, req.Password)
if err != nil {
response.Error = err.Error()
return
}
}
response.Token = token
response.RefreshToken = refresh
}
// ServeHTTP implements http.Handler
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
h.CtxServeHTTP(nil, w, r)
}
// UserFromRequest parses the UID from the request
func (h *Handler) UserFromRequest(r *http.Request) (*User, error) {
req, err := ParseRequest(r)
if err != nil {
return nil, err
}
user, err := h.auth.ValidateToken(req.Token)
return user, nil
}
// UserFromContext get the uid of the context
func UserFromContext(ctx context.Context) *User {
if user, ok := ctx.Value(contextKey).(*User); ok {
return user
}
return nil
}
// NewUserContext stores a user information in the context
func NewUserContext(ctx context.Context, user *User) context.Context {
return context.WithValue(ctx, contextKey, user)
}