-
Notifications
You must be signed in to change notification settings - Fork 0
/
token.go
116 lines (93 loc) · 2.59 KB
/
token.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
package service
import (
"database/sql"
"encoding/json"
"errors"
"net/http"
"strconv"
"strings"
"github.com/msgurgel/mrthn/pkg/dal"
"github.com/dgrijalva/jwt-go"
"github.com/gorilla/context"
"github.com/sirupsen/logrus"
)
type parseToken struct {
clientID int
valid bool
}
func generateJWT(clientID int, secret []byte) (string, error) {
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.StandardClaims{
// TODO: Make client ID not an integer
Audience: strconv.Itoa(clientID),
Issuer: "mrthn",
})
// TODO: Add expiration to the token
// Sign the token with the given secret
tokenString, _ := token.SignedString(secret)
return tokenString, nil
}
func validateJWT(db *sql.DB, tokenString string) (parseToken, error) {
token, err := jwt.ParseWithClaims(tokenString, &jwt.StandardClaims{}, func(token *jwt.Token) (interface{}, error) {
if claims, ok := token.Claims.(*jwt.StandardClaims); ok {
clientID, _ := strconv.Atoi(claims.Audience)
return dal.GetClientSecret(db, clientID)
} else {
return nil, errors.New("unable to parse JWT claims")
}
})
if err != nil {
return parseToken{}, err
}
if token.Valid {
claims, _ := token.Claims.(*jwt.StandardClaims)
clientID, _ := strconv.Atoi(claims.Audience)
return parseToken{
clientID: clientID,
valid: true,
}, nil
}
return parseToken{valid: false}, nil
}
func jwtMiddleware(db *sql.DB, log *logrus.Logger, next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var token string
// Get token from the Authorization header
tokens, ok := r.Header["Authorization"]
if !ok {
log.Error("Authorization header was empty")
SendErrorToClient(w, log)
return
}
if ok && len(tokens) >= 1 {
token = tokens[0]
token = strings.TrimPrefix(token, "Bearer ")
}
parseToken, err := validateJWT(db, token)
if err != nil {
log.Error("failed to parse JWT")
SendErrorToClient(w, log)
return
}
if parseToken.valid {
context.Set(r, "client_id", parseToken.clientID)
next.ServeHTTP(w, r)
} else {
log.WithFields(logrus.Fields{
"err": err,
}).Error("JWT was invalid")
SendErrorToClient(w, log)
}
})
}
func SendErrorToClient(w http.ResponseWriter, log *logrus.Logger) {
payload := map[string]string{"error": "Access token is missing or invalid"}
response, _ := json.Marshal(payload)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusUnauthorized)
_, err := w.Write(response)
if err != nil {
log.WithFields(logrus.Fields{
"err": err,
}).Error("failed to send response to client")
}
}