Skip to content

Commit

Permalink
fix: authentization bug
Browse files Browse the repository at this point in the history
  • Loading branch information
fiftin committed Mar 13, 2023
1 parent f62cea4 commit 3e4a62b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 22 deletions.
35 changes: 21 additions & 14 deletions api/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"time"
)

func authenticationHandler(w http.ResponseWriter, r *http.Request) {
func authenticationHandler(w http.ResponseWriter, r *http.Request) bool {
var userID int

authHeader := strings.ToLower(r.Header.Get("authorization"))
Expand All @@ -25,7 +25,7 @@ func authenticationHandler(w http.ResponseWriter, r *http.Request) {
}

w.WriteHeader(http.StatusUnauthorized)
return
return false
}

userID = token.UserID
Expand All @@ -34,20 +34,20 @@ func authenticationHandler(w http.ResponseWriter, r *http.Request) {
cookie, err := r.Cookie("semaphore")
if err != nil {
w.WriteHeader(http.StatusUnauthorized)
return
return false
}

value := make(map[string]interface{})
if err = util.Cookie.Decode("semaphore", cookie.Value, &value); err != nil {
w.WriteHeader(http.StatusUnauthorized)
return
return false
}

user, ok := value["user"]
sessionVal, okSession := value["session"]
if !ok || !okSession {
w.WriteHeader(http.StatusUnauthorized)
return
return false
}

userID = user.(int)
Expand All @@ -58,7 +58,7 @@ func authenticationHandler(w http.ResponseWriter, r *http.Request) {

if err != nil {
w.WriteHeader(http.StatusUnauthorized)
return
return false
}

if time.Since(session.LastActive).Hours() > 7*24 {
Expand All @@ -70,13 +70,13 @@ func authenticationHandler(w http.ResponseWriter, r *http.Request) {
}

w.WriteHeader(http.StatusUnauthorized)
return
return false
}

if err := helpers.Store(r).TouchSession(userID, sessionID); err != nil {
log.Error(err)
w.WriteHeader(http.StatusUnauthorized)
return
return false
}
}

Expand All @@ -87,26 +87,29 @@ func authenticationHandler(w http.ResponseWriter, r *http.Request) {
log.Error(err)
}
w.WriteHeader(http.StatusUnauthorized)
return
return false
}

if util.Config.DemoMode {
if !user.Admin && r.Method != "GET" &&
!strings.HasSuffix(r.URL.Path, "/tasks") &&
!strings.HasSuffix(r.URL.Path, "/stop") {
w.WriteHeader(http.StatusUnauthorized)
return
return false
}
}

context.Set(r, "user", &user)
return true
}

// nolint: gocyclo
func authentication(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
authenticationHandler(w, r)
next.ServeHTTP(w, r)
ok := authenticationHandler(w, r)
if ok {
next.ServeHTTP(w, r)
}
})
}

Expand All @@ -115,10 +118,14 @@ func authenticationWithStore(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
store := helpers.Store(r)

var ok bool

db.StoreSession(store, r.URL.String(), func() {
authenticationHandler(w, r)
ok = authenticationHandler(w, r)
})

next.ServeHTTP(w, r)
if ok {
next.ServeHTTP(w, r)
}
})
}
8 changes: 0 additions & 8 deletions api/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,16 +348,8 @@ func servePublic(w http.ResponseWriter, r *http.Request) {
}

func getSystemInfo(w http.ResponseWriter, r *http.Request) {
//updateAvailable, err := util.CheckUpdate()

//if err != nil {
// helpers.WriteError(w, err)
// return
//}

body := map[string]interface{}{
"version": util.Version,
//"update": updateAvailable,
"ansible": util.AnsibleVersion(),
"demo": util.Config.DemoMode,
}
Expand Down

0 comments on commit 3e4a62b

Please sign in to comment.