@@ -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}
0 commit comments