feat: restore CHFS JWT session compatibility
This commit is contained in:
@@ -69,20 +69,16 @@ func (s *Server) Handler() http.Handler {
|
||||
return s.ipFilter(mux)
|
||||
}
|
||||
func account(r *http.Request, s *Server) *auth.Account {
|
||||
if c, e := r.Cookie("chfs_token"); e == nil {
|
||||
s.SessionMu.RLock()
|
||||
a := s.Sessions[c.Value]
|
||||
exp := s.SessionExpiry[c.Value]
|
||||
s.SessionMu.RUnlock()
|
||||
if a != nil && (exp.IsZero() || time.Now().Before(exp)) {
|
||||
return a
|
||||
}
|
||||
if a != nil {
|
||||
s.SessionMu.Lock()
|
||||
delete(s.Sessions, c.Value)
|
||||
delete(s.SessionExpiry, c.Value)
|
||||
s.SessionMu.Unlock()
|
||||
}
|
||||
c, err := r.Cookie("JWT")
|
||||
if err != nil {
|
||||
return s.Auth.Guest
|
||||
}
|
||||
username, err := auth.GetAccFromToken(c.Value)
|
||||
if err != nil {
|
||||
return s.Auth.Guest
|
||||
}
|
||||
if a, ok := s.Auth.Accounts[username]; ok {
|
||||
return a
|
||||
}
|
||||
return s.Auth.Guest
|
||||
}
|
||||
@@ -256,15 +252,20 @@ func (s *Server) exist(w http.ResponseWriter, r *http.Request) {
|
||||
writeJSON(w, map[string]any{"exist": e == nil})
|
||||
}
|
||||
func (s *Server) session(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method == "DELETE" {
|
||||
if c, e := r.Cookie("chfs_token"); e == nil {
|
||||
s.SessionMu.Lock()
|
||||
delete(s.Sessions, c.Value)
|
||||
delete(s.SessionExpiry, c.Value)
|
||||
s.SessionMu.Unlock()
|
||||
if r.Method == http.MethodDelete {
|
||||
jwtValue, _ := r.Cookie("JWT")
|
||||
userValue, _ := r.Cookie("user")
|
||||
jwt := ""
|
||||
user := ""
|
||||
if jwtValue != nil {
|
||||
jwt = jwtValue.Value
|
||||
}
|
||||
http.SetCookie(w, &http.Cookie{Name: "chfs_token", Value: "", Path: "/", MaxAge: -1, HttpOnly: true})
|
||||
writeJSON(w, map[string]any{"ok": true})
|
||||
if userValue != nil {
|
||||
user = userValue.Value
|
||||
}
|
||||
http.SetCookie(w, &http.Cookie{Name: "JWT", Value: jwt, Path: "/", MaxAge: -1})
|
||||
http.SetCookie(w, &http.Cookie{Name: "user", Value: user, Path: "/", MaxAge: -1})
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
return
|
||||
}
|
||||
if r.Method != http.MethodPost {
|
||||
@@ -281,25 +282,15 @@ func (s *Server) session(w http.ResponseWriter, r *http.Request) {
|
||||
writeJSONError(w, http.StatusNotFound, "user or password is wrong")
|
||||
return
|
||||
}
|
||||
token := auth.NewToken()
|
||||
exp := time.Time{}
|
||||
maxAge := 0
|
||||
if s.SessionTimeout > 0 {
|
||||
exp = time.Now().Add(time.Duration(s.SessionTimeout) * time.Minute)
|
||||
maxAge = s.SessionTimeout * 60
|
||||
token, err := auth.NewToken(a.Username)
|
||||
if err != nil {
|
||||
writeJSONError(w, http.StatusInternalServerError, "failed to create token")
|
||||
return
|
||||
}
|
||||
s.SessionMu.Lock()
|
||||
if s.Sessions == nil {
|
||||
s.Sessions = make(map[string]*auth.Account)
|
||||
}
|
||||
if s.SessionExpiry == nil {
|
||||
s.SessionExpiry = make(map[string]time.Time)
|
||||
}
|
||||
s.Sessions[token] = a
|
||||
s.SessionExpiry[token] = exp
|
||||
s.SessionMu.Unlock()
|
||||
http.SetCookie(w, &http.Cookie{Name: "chfs_token", Value: token, Path: "/", MaxAge: maxAge, HttpOnly: true, SameSite: http.SameSiteLaxMode})
|
||||
writeJSON(w, map[string]any{"ok": true, "user": a.Username})
|
||||
expires := time.Now().Add(365 * 24 * time.Hour)
|
||||
http.SetCookie(w, &http.Cookie{Name: "JWT", Value: token, Path: "/", Expires: expires, HttpOnly: true})
|
||||
http.SetCookie(w, &http.Cookie{Name: "user", Value: a.Username, Path: "/", Expires: expires})
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
}
|
||||
|
||||
func (s *Server) newtext(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
Reference in New Issue
Block a user