-
Notifications
You must be signed in to change notification settings - Fork 8
/
http_mock.go
44 lines (38 loc) · 1.11 KB
/
http_mock.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
package auth
import (
"context"
"net/http"
"github.com/ThreeDotsLabs/wild-workouts-go-ddd-example/internal/common/server/httperr"
"github.com/dgrijalva/jwt-go"
"github.com/dgrijalva/jwt-go/request"
)
// HttpMockMiddleware is used in the local environment (which doesn't depend on Firebase)
func HttpMockMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var claims jwt.MapClaims
token, err := request.ParseFromRequest(
r,
request.AuthorizationHeaderExtractor,
func(token *jwt.Token) (i interface{}, e error) {
return []byte("mock_secret"), nil
},
request.WithClaims(&claims),
)
if err != nil {
httperr.BadRequest("unable-to-get-jwt", err, w, r)
return
}
if !token.Valid {
httperr.BadRequest("invalid-jwt", nil, w, r)
return
}
ctx := context.WithValue(r.Context(), userContextKey, User{
UUID: claims["user_uuid"].(string),
Email: claims["email"].(string),
Role: claims["role"].(string),
DisplayName: claims["name"].(string),
})
r = r.WithContext(ctx)
next.ServeHTTP(w, r)
})
}