Repository navigation
Expand file tree
/
Copy pathappcontext.go
More file actions
62 lines (52 loc) · 1.55 KB
/
Copy pathappcontext.go
File metadata and controls
62 lines (52 loc) · 1.55 KB
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
package main
import (
"errors"
"fmt"
"net/http"
jwt "github.com/dgrijalva/jwt-go"
"github.com/idrum4316/devpad-server/internal/datastore"
"github.com/idrum4316/devpad-server/internal/search"
)
// AppContext holds the overall application context (config, etc..)
type AppContext struct {
Config *AppConfig
Index *search.Index
Store *datastore.Datastore
}
// NewAppContext returns a pointer to a new AppContext with default values set.
func NewAppContext() (a *AppContext) {
a = &AppContext{
Config: NewAppConfig(),
Index: nil,
Store: nil,
}
return
}
// GetUserIDFromRequest returns the user id from the JWT token in the request
func (a *AppContext) GetUserIDFromRequest(r *http.Request) (id string, err error) {
// Get the token from the HTTP headers. If it doesn't exist, return an
// error.
tokenHeader := r.Header.Get("jwt")
if tokenHeader == "" {
err = errors.New("missing jwt token")
return
}
// Parse the token. Return any errors that are thrown.
token, err := jwt.Parse(tokenHeader, func(token *jwt.Token) (interface{}, error) {
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, fmt.Errorf("Unexpected signing method: %v", token.Header["alg"])
}
return []byte(a.Config.SigningKey), nil
})
if err != nil {
return
}
// If the token is good, return the user id. If it's not, return an error.
if claims, ok := token.Claims.(jwt.MapClaims); ok && token.Valid {
id = claims["userid"].(string)
return
}
// If this code runs, that means a valid claim was not found
err = errors.New("invalid token")
return
}