-
Notifications
You must be signed in to change notification settings - Fork 2
/
middleware.go
64 lines (53 loc) · 1.36 KB
/
middleware.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
package jwtauth
import (
"context"
"net/http"
"github.com/rabee-inc/go-pkg/errcode"
"github.com/rabee-inc/go-pkg/log"
"github.com/rabee-inc/go-pkg/renderer"
)
// Middleware ... JWT認証のミドルウェア
type Middleware struct {
Svc Service
}
// Handle ... JWT認証をする
func (m *Middleware) Handle(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
// Headerを取得
ah := r.Header.Get("Authorization")
if ah == "" {
err := log.Warningc(ctx, http.StatusForbidden, "no Authorization header")
m.renderError(ctx, w, err)
return
}
// 認証
userID, claims, err := m.Svc.Authentication(ctx, ah)
if err != nil {
log.Warning(ctx, err)
m.renderError(ctx, w, err)
return
}
// 認証結果を設定
ctx = setUserID(ctx, userID)
log.Debugf(ctx, "UserID: %s", userID)
ctx = setClaims(ctx, claims)
log.Debugf(ctx, "Claims: %v", claims)
next.ServeHTTP(w, r.WithContext(ctx))
})
}
func (m *Middleware) renderError(ctx context.Context, w http.ResponseWriter, err error) {
var status int
if cd, ok := errcode.Get(err); ok {
status = cd
} else {
status = http.StatusForbidden
}
renderer.Error(ctx, w, status, err.Error())
}
// NewMiddleware ... Middlewareを作成する
func NewMiddleware(svc Service) *Middleware {
return &Middleware{
Svc: svc,
}
}