-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathmiddlewares.go
71 lines (62 loc) · 1.92 KB
/
middlewares.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
package main
import (
"encoding/base64"
"golang.org/x/crypto/bcrypt"
"net/http"
"net/http/httptest"
"strings"
"time"
)
func (a *App) authMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
s := strings.SplitN(r.Header.Get("Authorization"), " ", 2)
if len(s) != 2 {
respondWithError(w, http.StatusUnauthorized, "Invalid/Missing Credentials.")
return
}
b, err := base64.StdEncoding.DecodeString(s[1])
if err != nil {
respondWithError(w, http.StatusUnauthorized, "Invalid/Missing Credentials.")
return
}
pair := strings.SplitN(string(b), ":", 2)
if len(pair) != 2 {
respondWithError(w, http.StatusUnauthorized, "Invalid/Missing Credentials.")
return
}
user := User{Username: pair[0]}
row := a.DB.QueryRow("SELECT id, saltedpassword, salt FROM users WHERE username=?", user.Username)
if err := row.Scan(&user.Id, &user.Saltedpassword, &user.Salt); err != nil {
respondWithError(w, http.StatusUnauthorized, "Invalid/Missing Credentials.")
return
}
if err := bcrypt.CompareHashAndPassword([]byte(user.Saltedpassword), []byte(pair[1]+user.Salt)); err != nil {
respondWithError(w, http.StatusUnauthorized, "Invalid/Missing Credentials.")
return
}
next.ServeHTTP(w, r)
})
}
func (a *App) cacheMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
next.ServeHTTP(w, r)
return
}
content, err := a.Cache.Get(r.RequestURI).Result()
if err != nil {
rr := httptest.NewRecorder()
next.ServeHTTP(rr, r)
content = rr.Body.String()
err = a.Cache.Set(r.RequestURI, content, 10*time.Minute).Err()
if err != nil {
respondWithError(w, http.StatusInternalServerError, err.Error())
}
respondWithString(w, http.StatusOK, content)
return
} else {
respondWithString(w, http.StatusOK, content)
return
}
})
}