-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.go
46 lines (38 loc) · 985 Bytes
/
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
package auth
import (
"context"
"net/http"
"strings"
"github.com/sirupsen/logrus"
)
type localJwtClaims struct{}
type ApiSecurityOptions struct {
IssuerURL string `json:"authority"`
ClientID string `json:"client_id"`
Audience []string `json:"-"`
}
type CustomClaims struct {
PreferredUsername string `json:"preferred_username"`
EmailVerified bool `json:"email_verified"`
}
func (c *CustomClaims) Validate(ctx context.Context) error {
return nil
}
func checkLocalJWT(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
auth := r.Header.Get("Authorization")
if len(auth) > 8 && strings.ToLower(auth[:6]) == "bearer" {
token := auth[7:]
claims, err := ValidateToken(token)
if err == nil {
r = r.WithContext(context.WithValue(r.Context(), localJwtClaims{}, claims))
} else {
logrus.Debug(err.Error())
}
}
next.ServeHTTP(w, r)
})
}
type UserInfo struct {
Username string
}