-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
Copy pathhttp_server.go
194 lines (161 loc) · 4.33 KB
/
http_server.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package session
import (
"context"
"net/http"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
"github.com/influxdata/influxdb/v2"
kithttp "github.com/influxdata/influxdb/v2/kit/transport/http"
"go.uber.org/zap"
)
const (
prefixSignIn = "/api/v2/signin"
prefixSignOut = "/api/v2/signout"
)
// SessionHandler represents an HTTP API handler for authorizations.
type SessionHandler struct {
chi.Router
api *kithttp.API
log *zap.Logger
sessionSvc influxdb.SessionService
passSvc influxdb.PasswordsService
userSvc influxdb.UserService
}
// NewSessionHandler returns a new instance of SessionHandler.
func NewSessionHandler(log *zap.Logger, sessionSvc influxdb.SessionService, userSvc influxdb.UserService, passwordsSvc influxdb.PasswordsService) *SessionHandler {
svr := &SessionHandler{
api: kithttp.NewAPI(kithttp.WithLog(log)),
log: log,
passSvc: passwordsSvc,
sessionSvc: sessionSvc,
userSvc: userSvc,
}
return svr
}
type resourceHandler struct {
prefix string
*SessionHandler
}
// Prefix is necessary to mount the router as a resource handler
func (r resourceHandler) Prefix() string { return r.prefix }
// SignInResourceHandler allows us to return 2 different rousource handler
// for the appropriate mounting location
func (h SessionHandler) SignInResourceHandler() *resourceHandler {
h.Router = chi.NewRouter()
h.Router.Use(
middleware.Recoverer,
middleware.RequestID,
middleware.RealIP,
)
h.Router.Post("/", h.handleSignin)
return &resourceHandler{prefix: prefixSignIn, SessionHandler: &h}
}
// SignOutResourceHandler allows us to return 2 different rousource handler
// for the appropriate mounting location
func (h SessionHandler) SignOutResourceHandler() *resourceHandler {
h.Router = chi.NewRouter()
h.Router.Use(
middleware.Recoverer,
middleware.RequestID,
middleware.RealIP,
)
h.Router.Post("/", h.handleSignout)
return &resourceHandler{prefix: prefixSignOut, SessionHandler: &h}
}
// handleSignin is the HTTP handler for the POST /signin route.
func (h *SessionHandler) handleSignin(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
req, decErr := decodeSigninRequest(ctx, r)
if decErr != nil {
h.api.Err(w, r, ErrUnauthorized)
return
}
u, err := h.userSvc.FindUser(ctx, influxdb.UserFilter{
Name: &req.Username,
})
if err != nil {
h.api.Err(w, r, ErrUnauthorized)
return
}
if err := h.passSvc.ComparePassword(ctx, u.ID, req.Password); err != nil {
h.api.Err(w, r, ErrUnauthorized)
return
}
s, e := h.sessionSvc.CreateSession(ctx, req.Username)
if e != nil {
h.api.Err(w, r, ErrUnauthorized)
return
}
encodeCookieSession(w, s)
w.WriteHeader(http.StatusNoContent)
}
type signinRequest struct {
Username string
Password string
}
func decodeSigninRequest(ctx context.Context, r *http.Request) (*signinRequest, *influxdb.Error) {
u, p, ok := r.BasicAuth()
if !ok {
return nil, &influxdb.Error{
Code: influxdb.EInvalid,
Msg: "invalid basic auth",
}
}
return &signinRequest{
Username: u,
Password: p,
}, nil
}
// handleSignout is the HTTP handler for the POST /signout route.
func (h *SessionHandler) handleSignout(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
req, err := decodeSignoutRequest(ctx, r)
if err != nil {
h.api.Err(w, r, ErrUnauthorized)
return
}
if err := h.sessionSvc.ExpireSession(ctx, req.Key); err != nil {
h.api.Err(w, r, ErrUnauthorized)
return
}
w.WriteHeader(http.StatusNoContent)
}
type signoutRequest struct {
Key string
}
func decodeSignoutRequest(ctx context.Context, r *http.Request) (*signoutRequest, error) {
key, err := decodeCookieSession(ctx, r)
if err != nil {
return nil, err
}
return &signoutRequest{
Key: key,
}, nil
}
const cookieSessionName = "session"
func encodeCookieSession(w http.ResponseWriter, s *influxdb.Session) {
c := &http.Cookie{
Name: cookieSessionName,
Value: s.Key,
}
http.SetCookie(w, c)
}
func decodeCookieSession(ctx context.Context, r *http.Request) (string, error) {
c, err := r.Cookie(cookieSessionName)
if err != nil {
return "", &influxdb.Error{
Code: influxdb.EInvalid,
Err: err,
}
}
return c.Value, nil
}
// SetCookieSession adds a cookie for the session to an http request
func SetCookieSession(key string, r *http.Request) {
c := &http.Cookie{
Name: cookieSessionName,
Value: key,
Secure: true,
}
r.AddCookie(c)
}