-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.go
97 lines (79 loc) · 2.71 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
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
package auth
import (
"context"
"net/http"
"strings"
// Packages
"github.com/mutablelogic/go-server"
"github.com/mutablelogic/go-server/pkg/handler/router"
"github.com/mutablelogic/go-server/pkg/httpresponse"
)
///////////////////////////////////////////////////////////////////////////////
// TYPES
// Check interfaces are satisfied
var _ server.Middleware = (*auth)(nil)
///////////////////////////////////////////////////////////////////////////////
// GLOBALS
const (
defaultBearerHeader = "Authorization"
defaultBearerType = "Bearer"
)
///////////////////////////////////////////////////////////////////////////////
// PUBLIC METHODS
func (middleware *auth) Wrap(ctx context.Context, next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var tokenValue string
// If bearer is true, get the token from the Authorization: Bearer header
if middleware.bearer {
tokenValue = strings.ToLower(strings.TrimSpace(getBearer(r)))
}
// Get token from request
if tokenValue == "" {
httpresponse.Error(w, http.StatusUnauthorized)
return
}
// TODO: Hook for getting JWT from request here
// Get token from the jar - check it is found and valid
token := middleware.jar.GetWithValue(tokenValue)
authorized := true
if token.IsZero() {
authorized = false
httpresponse.Error(w, http.StatusUnauthorized, "invalid or missing token")
} else if !token.IsValid() {
authorized = false
httpresponse.Error(w, http.StatusUnauthorized, "invalid or missing token")
} else if token.IsScope(ScopeRoot) {
// Allow - token is a super-user token
} else if allowedScopes := router.Scope(r.Context()); len(allowedScopes) == 0 {
// Allow - no scopes have been defined on this endpoint
} else if !token.IsScope(allowedScopes...) {
// Deny - token does not have the required scopes
authorized = false
httpresponse.Error(w, http.StatusUnauthorized, "required scope: ", strings.Join(allowedScopes, ","))
}
// Return unauthorized if token is not found or not valid
if !authorized {
return
}
// TODO: Hook for setting JWT cookie here
// Create a new context with the token name and scopes
r = r.WithContext(WithToken(r.Context(), token))
// Call next handler in chain
next(w, r)
}
}
///////////////////////////////////////////////////////////////////////////////
// PRIVATE METHODS
// Get bearer token from request, or return empty string
func getBearer(r *http.Request) string {
// Get the bearer token
if value := r.Header.Get(defaultBearerHeader); value == "" {
return ""
} else if parts := strings.SplitN(value, " ", 2); len(parts) != 2 {
return ""
} else if parts[0] != defaultBearerType {
return ""
} else {
return parts[1]
}
}