Skip to content

Commit

Permalink
set the access level for basic and http auth (#951)
Browse files Browse the repository at this point in the history
  • Loading branch information
motatoes authored Dec 20, 2023
1 parent efd7249 commit c83db17
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 6 deletions.
2 changes: 2 additions & 0 deletions backend/middleware/basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func HttpBasicWebAuth() gin.HandlerFunc {
gin.BasicAuth(gin.Accounts{
username: password,
})(c)
c.Set(ACCESS_LEVEL_KEY, models.AdminPolicyType)
setDefaultOrganisationId(c)
c.Next()
}
Expand Down Expand Up @@ -52,6 +53,7 @@ func HttpBasicApiAuth() gin.HandlerFunc {

if token == os.Getenv("BEARER_AUTH_TOKEN") {
setDefaultOrganisationId(c)
c.Set(ACCESS_LEVEL_KEY, models.AdminPolicyType)
c.Next()
}
return
Expand Down
4 changes: 2 additions & 2 deletions backend/middleware/jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func SetContextParameters(c *gin.Context, auth services.Auth, token *jwt.Token)
return nil
}

func WebAuth(auth services.Auth) gin.HandlerFunc {
func JWTWebAuth(auth services.Auth) gin.HandlerFunc {
return func(c *gin.Context) {
var tokenString string
tokenString, err := c.Cookie("token")
Expand Down Expand Up @@ -178,7 +178,7 @@ func SecretCodeAuth() gin.HandlerFunc {
}
}

func BearerTokenAuth(auth services.Auth) gin.HandlerFunc {
func JWTBearerTokenAuth(auth services.Auth) gin.HandlerFunc {
return func(c *gin.Context) {
authHeader := c.Request.Header.Get("Authorization")
if authHeader == "" {
Expand Down
4 changes: 2 additions & 2 deletions backend/middleware/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func GetWebMiddleware() gin.HandlerFunc {
Secret: os.Getenv("AUTH_SECRET"),
ClientId: os.Getenv("FRONTEGG_CLIENT_ID"),
}
return WebAuth(auth)
return JWTWebAuth(auth)
} else if _, ok := os.LookupEnv("HTTP_BASIC_AUTH"); ok {
log.Printf("Using http basic auth middleware for web routes")
return HttpBasicWebAuth()
Expand All @@ -39,7 +39,7 @@ func GetApiMiddleware() gin.HandlerFunc {
Secret: os.Getenv("AUTH_SECRET"),
ClientId: os.Getenv("FRONTEGG_CLIENT_ID"),
}
return BearerTokenAuth(auth)
return JWTBearerTokenAuth(auth)
} else if _, ok := os.LookupEnv("HTTP_BASIC_AUTH"); ok {
log.Printf("Using http basic auth middleware for API routes")
return HttpBasicApiAuth()
Expand Down
6 changes: 5 additions & 1 deletion backend/middleware/noop.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
package middleware

import (
"github.com/diggerhq/digger/backend/models"
"github.com/gin-gonic/gin"
)

func NoopWebAuth() gin.HandlerFunc {
return func(c *gin.Context) {
c.Set(ORGANISATION_ID_KEY, 1)
setDefaultOrganisationId(c)
c.Set(ACCESS_LEVEL_KEY, models.AdminPolicyType)
c.Next()
}
}

func NoopApiAuth() gin.HandlerFunc {
return func(c *gin.Context) {
setDefaultOrganisationId(c)
c.Set(ACCESS_LEVEL_KEY, models.AdminPolicyType)
c.Next()
}
}
2 changes: 1 addition & 1 deletion go.work.sum
Original file line number Diff line number Diff line change
Expand Up @@ -917,7 +917,7 @@ github.com/ugorji/go v0.0.0-20180813092308-00b869d2f4a5 h1:cMjKdf4PxEBN9K5HaD9UM
github.com/ugorji/go v1.1.11 h1:O5AKWOf+CnfWi6L1WtdBtZpA+YNjoQd2YfbtkowsMrs=
github.com/ugorji/go v1.1.11/go.mod h1:kbRrdMyHY64ADdazOwkrQP9btxt35Z26OJueD3Tq0/4=
github.com/ugorji/go v1.2.11 h1:SskMIRQoD2wRlY2Qddg8rjJC+bc0su8i/vmWkYdquMo=
github.com/ugorji/go v1.2.11/go.mod h1:2CGjQPolsidfhgBxo2wBo1Xh48rJWqUepV6q7OMkdWA=
github.com/ugorji/go v1.2.12 h1:oRySHlrVC5izTtkkuUmr0leYV9ixxb2eXLnyUiNctis=
github.com/ugorji/go/codec v1.1.7 h1:2SvQaVZ1ouYrrKKwoSk2pzd4A9evlKJb9oTL+OaLUSs=
github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY=
github.com/ugorji/go/codec v1.1.11 h1:GaQDxjNe1J3vCZvlVaDjUIHIbFuUByFXY7rMqnhB5ck=
Expand Down

0 comments on commit c83db17

Please sign in to comment.