Skip to content

Commit

Permalink
improve security middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
ad3n committed Feb 10, 2021
1 parent 9be0a76 commit 15d38c2
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 3 deletions.
3 changes: 2 additions & 1 deletion configs/struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ type (
User struct {
Id string
Email string
Role string
Role int
}

Env struct {
Expand Down Expand Up @@ -34,6 +34,7 @@ type (
HeaderUserId string
HeaderUserEmail string
HeaderUserRole string
MaximumRole int
CacheLifetime int
User *User
TemplateLocation string
Expand Down
1 change: 1 addition & 0 deletions dics/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ var Core = []dingo.Def{
env.HeaderUserId = os.Getenv("HEADER_USER_ID")
env.HeaderUserEmail = os.Getenv("HEADER_USER_EMAIL")
env.HeaderUserRole = os.Getenv("HEADER_USER_ROLE")
env.MaximumRole, _ = strconv.Atoi(os.Getenv("MAXIMUM_ROLE"))

env.CacheLifetime, _ = strconv.Atoi(os.Getenv("CACHE_LIFETIME"))

Expand Down
2 changes: 1 addition & 1 deletion middlewares.yaml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
middlewares:
- core:middleware:auth
# - core:middleware:auth
9 changes: 8 additions & 1 deletion middlewares/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package middlewares

import (
"net/http"
"strconv"

configs "github.com/crowdeco/skeleton/configs"
)
Expand All @@ -13,7 +14,13 @@ type Auth struct {
func (a *Auth) Attach(request *http.Request, response http.ResponseWriter) bool {
a.Env.User.Id = request.Header.Get(a.Env.HeaderUserId)
a.Env.User.Email = request.Header.Get(a.Env.HeaderUserEmail)
a.Env.User.Role = request.Header.Get(a.Env.HeaderUserRole)
a.Env.User.Role, _ = strconv.Atoi(request.Header.Get(a.Env.HeaderUserRole))

if a.Env.User.Role == 0 || a.Env.User.Role > a.Env.MaximumRole {
http.Error(response, "Unauthorization", http.StatusUnauthorized)

return true
}

return false
}
Expand Down

0 comments on commit 15d38c2

Please sign in to comment.