Skip to content
Snippets Groups Projects
Select Git revision
  • 8afcb347749b77205329b172292a2416468567c1
  • main default protected
  • add_export_route
  • add_route_assignments
  • 4.1.0-dev
  • 4.0.0
  • 3.5.3
  • 3.5.3-dev
  • 3.5.2
  • 3.5.2-dev
  • 3.5.1
  • 3.5.1-dev
  • 3.5.0
  • 3.4.2
  • 3.4.1
  • 3.4.0
  • 3.3.0
  • 3.2.0
  • 3.1.3
  • 3.1.2
  • 3.1.1
  • 3.1.0
  • 3.0.1
  • 3.0.0
24 results

SessionMiddleware.ts

Blame
  • Forked from Dojo Project (HES-SO) / Projects / Backend / DojoBackendAPI
    Source project has a limited visibility.
    authentication.go 1.37 KiB
    package middlewares
    
    import (
    	"appSec/pkg/api/auth"
    	"fmt"
    	"github.com/gin-gonic/gin"
    	"net/http"
    )
    
    var UserPermissions map[string][]string
    
    func InitUserPermissions() {
    	UserPermissions = make(map[string][]string)
    	UserPermissions["foo"] = append(UserPermissions["foo"], "GET")
    	UserPermissions["aristote"] = append(UserPermissions["aristote"], "GET", "PUT", "DELETE", "POST")
    	UserPermissions["viewer"] = append(UserPermissions["viewer"], "GET")
    	UserPermissions["adder"] = append(UserPermissions["adder"], "GET", "POST")
    	UserPermissions["destroyer"] = append(UserPermissions["destroyer"], "GET", "DELETE")
    }
    
    // Authorization Middlware to check that the user has access to the method
    func Authorization(c *gin.Context, user string) {
    	//Check that user is present in the userPermissions map and then get his authorized methods access
    	if permissions, ok := UserPermissions[user]; ok {
    		method := c.Request.Method
    		for _, v := range permissions {
    			if v == method {
    				c.Next()
    			}
    		}
    	}
    	c.AbortWithStatus(http.StatusForbidden)
    }
    
    func BasicAuthorization(c *gin.Context) {
    	//Get username from Basic Auth
    	user := c.MustGet(gin.AuthUserKey).(string)
    	Authorization(c, user)
    }
    
    func JWTAuthorization(c *gin.Context) {
    	if jwt, ok := auth.VerifyJWT(c); ok {
    		user := jwt.Claims["user"].(string)
    		fmt.Println(user)
    		Authorization(c, user)
    	}
    	c.AbortWithStatus(http.StatusUnauthorized)
    }