github.com/mendersoftware/go-lib-micro@v0.0.0-20240304135804-e8e39c59b148/rbac/middleware.go (about) 1 // Copyright 2023 Northern.tech AS 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 package rbac 15 16 import ( 17 "github.com/ant0ine/go-json-rest/rest" 18 "github.com/gin-gonic/gin" 19 ) 20 21 func Middleware() gin.HandlerFunc { 22 return func(c *gin.Context) { 23 if scope := ExtractScopeFromHeader(c.Request); scope != nil { 24 ctx := c.Request.Context() 25 ctx = WithContext(ctx, scope) 26 c.Request = c.Request.WithContext(ctx) 27 } 28 return 29 } 30 } 31 32 type RBACMiddleware struct { 33 } 34 35 func (mw *RBACMiddleware) MiddlewareFunc(h rest.HandlerFunc) rest.HandlerFunc { 36 return func(w rest.ResponseWriter, r *rest.Request) { 37 if scope := ExtractScopeFromHeader(r.Request); scope != nil { 38 ctx := r.Context() 39 ctx = WithContext(ctx, scope) 40 r.Request = r.WithContext(ctx) 41 } 42 43 h(w, r) 44 } 45 }