Skip to content

Commit 8f6fc57

Browse files
committed
feat: Add ExtractTokenFromRequest method in Gate struct
1 parent f18d51d commit 8f6fc57

File tree

2 files changed

+40
-8
lines changed

2 files changed

+40
-8
lines changed

gate.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -181,12 +181,7 @@ func (gate *Gate) ProtectFuncWithPermissions(handlerFunc http.HandlerFunc, permi
181181
}
182182
}
183183
if gate.authorizationService != nil {
184-
var token string
185-
if gate.customTokenExtractorFunc != nil {
186-
token = gate.customTokenExtractorFunc(request)
187-
} else {
188-
token = extractTokenFromRequest(request)
189-
}
184+
token := gate.ExtractTokenFromRequest(request)
190185
if !gate.authorizationService.IsAuthorized(token, permissions) {
191186
writer.WriteHeader(http.StatusUnauthorized)
192187
_, _ = writer.Write(gate.unauthorizedResponseBody)
@@ -206,7 +201,17 @@ func (gate *Gate) ProtectFuncWithPermission(handlerFunc http.HandlerFunc, permis
206201
return gate.ProtectFuncWithPermissions(handlerFunc, []string{permission})
207202
}
208203

209-
// extractTokenFromRequest extracts the bearer token from the AuthorizationHeader
210-
func extractTokenFromRequest(request *http.Request) string {
204+
// ExtractTokenFromRequest extracts a token from a request.
205+
//
206+
// By default, it extracts the bearer token from the AuthorizationHeader, but if a customTokenExtractorFunc is defined,
207+
// it will use that instead.
208+
//
209+
// Note that this method is internally used by Protect, ProtectWithPermission, ProtectFunc and
210+
// ProtectFuncWithPermissions, but it is exposed in case you need to use it directly.
211+
func (gate *Gate) ExtractTokenFromRequest(request *http.Request) string {
212+
if gate.customTokenExtractorFunc != nil {
213+
// A custom token extractor function is defined, so we'll use it instead of the default token extraction logic
214+
return gate.customTokenExtractorFunc(request)
215+
}
211216
return strings.TrimPrefix(request.Header.Get(AuthorizationHeader), "Bearer ")
212217
}

gate_bench_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,3 +179,30 @@ func BenchmarkGate_ProtectWithClientProviderConcurrently(b *testing.B) {
179179
})
180180
b.ReportAllocs()
181181
}
182+
183+
func BenchmarkGate_ProtectWithValidTokenAndCustomTokenExtractorFuncConcurrently(b *testing.B) {
184+
customTokenExtractorFunc := func(request *http.Request) string {
185+
sessionCookie, err := request.Cookie("session")
186+
if err != nil {
187+
return ""
188+
}
189+
return sessionCookie.Value
190+
}
191+
gate := New().WithAuthorizationService(NewAuthorizationService().WithToken("good-token")).WithCustomTokenExtractor(customTokenExtractorFunc)
192+
request, _ := http.NewRequest("GET", "/handle", http.NoBody)
193+
request.AddCookie(&http.Cookie{Name: "session", Value: "good-token"})
194+
195+
router := http.NewServeMux()
196+
router.Handle("/handle", gate.Protect(handler))
197+
198+
b.RunParallel(func(pb *testing.PB) {
199+
for pb.Next() {
200+
responseRecorder := httptest.NewRecorder()
201+
router.ServeHTTP(responseRecorder, request)
202+
if responseRecorder.Code != http.StatusOK {
203+
b.Fatalf("%s %s should have returned %d, but returned %d instead", request.Method, request.URL, http.StatusOK, responseRecorder.Code)
204+
}
205+
}
206+
})
207+
b.ReportAllocs()
208+
}

0 commit comments

Comments
 (0)