@@ -66,15 +66,16 @@ func (gate *Gate) WithCustomUnauthorizedResponseBody(unauthorizedResponseBody []
6666// If a custom token extractor is not specified, the token will be extracted from the Authorization header.
6767//
6868// For instance, if you're using a session cookie, you can extract the token from the cookie like so:
69- // authorizationService := g8.NewAuthorizationService()
70- // customTokenExtractorFunc := func(request *http.Request) string {
71- // sessionCookie, err := request.Cookie("session")
72- // if err != nil {
73- // return ""
74- // }
75- // return sessionCookie.Value
76- // }
77- // gate := g8.New().WithAuthorizationService(authorizationService).WithCustomTokenExtractor(customTokenExtractorFunc)
69+ //
70+ // authorizationService := g8.NewAuthorizationService()
71+ // customTokenExtractorFunc := func(request *http.Request) string {
72+ // sessionCookie, err := request.Cookie("session")
73+ // if err != nil {
74+ // return ""
75+ // }
76+ // return sessionCookie.Value
77+ // }
78+ // gate := g8.New().WithAuthorizationService(authorizationService).WithCustomTokenExtractor(customTokenExtractorFunc)
7879//
7980// You would normally use this with a client provider that matches whatever need you have.
8081// For example, if you're using a session cookie, your client provider would retrieve the user from the session ID
@@ -90,8 +91,8 @@ func (gate *Gate) WithCustomTokenExtractor(customTokenExtractorFunc func(request
9091// WithRateLimit adds rate limiting to the Gate
9192//
9293// If you just want to use a gate for rate limiting purposes:
93- // gate := g8.New().WithRateLimit(50)
9494//
95+ // gate := g8.New().WithRateLimit(50)
9596func (gate * Gate ) WithRateLimit (maximumRequestsPerSecond int ) * Gate {
9697 gate .rateLimiter = NewRateLimiter (maximumRequestsPerSecond )
9798 return gate
@@ -102,12 +103,13 @@ func (gate *Gate) WithRateLimit(maximumRequestsPerSecond int) *Gate {
102103// or lack thereof.
103104//
104105// Example:
105- // gate := g8.New().WithAuthorizationService(g8.NewAuthorizationService().WithToken("token"))
106- // router := http.NewServeMux()
107- // // Without protection
108- // router.Handle("/handle", yourHandler)
109- // // With protection
110- // router.Handle("/handle", gate.Protect(yourHandler))
106+ //
107+ // gate := g8.New().WithAuthorizationService(g8.NewAuthorizationService().WithToken("token"))
108+ // router := http.NewServeMux()
109+ // // Without protection
110+ // router.Handle("/handle", yourHandler)
111+ // // With protection
112+ // router.Handle("/handle", gate.Protect(yourHandler))
111113//
112114// The token extracted from the request is passed to the handlerFunc request context under the key TokenContextKey
113115func (gate * Gate ) Protect (handler http.Handler ) http.Handler {
@@ -118,12 +120,13 @@ func (gate *Gate) Protect(handler http.Handler) http.Handler {
118120// as well as a slice of permissions that must be met.
119121//
120122// Example:
121- // gate := g8.New().WithAuthorizationService(g8.NewAuthorizationService().WithClient(g8.NewClient("token").WithPermission("admin")))
122- // router := http.NewServeMux()
123- // // Without protection
124- // router.Handle("/handle", yourHandler)
125- // // With protection
126- // router.Handle("/handle", gate.ProtectWithPermissions(yourHandler, []string{"admin"}))
123+ //
124+ // gate := g8.New().WithAuthorizationService(g8.NewAuthorizationService().WithClient(g8.NewClient("token").WithPermission("ADMIN")))
125+ // router := http.NewServeMux()
126+ // // Without protection
127+ // router.Handle("/handle", yourHandler)
128+ // // With protection
129+ // router.Handle("/handle", gate.ProtectWithPermissions(yourHandler, []string{"admin"}))
127130//
128131// The token extracted from the request is passed to the handlerFunc request context under the key TokenContextKey
129132func (gate * Gate ) ProtectWithPermissions (handler http.Handler , permissions []string ) http.Handler {
@@ -147,12 +150,13 @@ func (gate *Gate) ProtectWithPermission(handler http.Handler, permission string)
147150// permissions or lack thereof.
148151//
149152// Example:
150- // gate := g8.New().WithAuthorizationService(g8.NewAuthorizationService().WithToken("token"))
151- // router := http.NewServeMux()
152- // // Without protection
153- // router.HandleFunc("/handle", yourHandlerFunc)
154- // // With protection
155- // router.HandleFunc("/handle", gate.ProtectFunc(yourHandlerFunc))
153+ //
154+ // gate := g8.New().WithAuthorizationService(g8.NewAuthorizationService().WithToken("token"))
155+ // router := http.NewServeMux()
156+ // // Without protection
157+ // router.HandleFunc("/handle", yourHandlerFunc)
158+ // // With protection
159+ // router.HandleFunc("/handle", gate.ProtectFunc(yourHandlerFunc))
156160//
157161// The token extracted from the request is passed to the handlerFunc request context under the key TokenContextKey
158162func (gate * Gate ) ProtectFunc (handlerFunc http.HandlerFunc ) http.HandlerFunc {
@@ -163,12 +167,13 @@ func (gate *Gate) ProtectFunc(handlerFunc http.HandlerFunc) http.HandlerFunc {
163167// token as well as a slice of permissions that must be met.
164168//
165169// Example:
166- // gate := g8.New().WithAuthorizationService(g8.NewAuthorizationService().WithClient(g8.NewClient("token").WithPermission("admin")))
167- // router := http.NewServeMux()
168- // // Without protection
169- // router.HandleFunc("/handle", yourHandlerFunc)
170- // // With protection
171- // router.HandleFunc("/handle", gate.ProtectFuncWithPermissions(yourHandlerFunc, []string{"admin"}))
170+ //
171+ // gate := g8.New().WithAuthorizationService(g8.NewAuthorizationService().WithClient(g8.NewClient("token").WithPermission("admin")))
172+ // router := http.NewServeMux()
173+ // // Without protection
174+ // router.HandleFunc("/handle", yourHandlerFunc)
175+ // // With protection
176+ // router.HandleFunc("/handle", gate.ProtectFuncWithPermissions(yourHandlerFunc, []string{"admin"}))
172177//
173178// The token extracted from the request is passed to the handlerFunc request context under the key TokenContextKey
174179func (gate * Gate ) ProtectFuncWithPermissions (handlerFunc http.HandlerFunc , permissions []string ) http.HandlerFunc {
@@ -215,3 +220,19 @@ func (gate *Gate) ExtractTokenFromRequest(request *http.Request) string {
215220 }
216221 return strings .TrimPrefix (request .Header .Get (AuthorizationHeader ), "Bearer " )
217222}
223+
224+ // PermissionMiddleware is a middleware that behaves like ProtectWithPermission, but it is meant to be used
225+ // as a middleware for libraries that support such a feature.
226+ //
227+ // For instance, if you are using github.com/gorilla/mux, you can use PermissionMiddleware like so:
228+ //
229+ // router := mux.NewRouter()
230+ // router.Use(gate.PermissionMiddleware("admin"))
231+ // router.Handle("/admin/handle", adminHandler)
232+ //
233+ // If you do not want to protect a router with a specific permission, you can use Gate.Protect instead.
234+ func (gate * Gate ) PermissionMiddleware (permissions ... string ) func (http.Handler ) http.Handler {
235+ return func (next http.Handler ) http.Handler {
236+ return gate .ProtectWithPermissions (next , permissions )
237+ }
238+ }
0 commit comments