WIP Svelte
This commit is contained in:
parent
38180f8afd
commit
ded0e8e1c8
48 changed files with 3976 additions and 46 deletions
47
auth.go
47
auth.go
|
@ -3,6 +3,7 @@ package main
|
|||
import (
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
|
@ -26,39 +27,35 @@ func init() {
|
|||
}
|
||||
|
||||
func validateAuthToken(u *User, _ httprouter.Params, _ []byte) HTTPResponse {
|
||||
return APIResponse{u}
|
||||
if u == nil {
|
||||
return APIErrorResponse{status: http.StatusUnauthorized, err: fmt.Errorf("Not connected")}
|
||||
} else {
|
||||
return APIResponse{u}
|
||||
}
|
||||
}
|
||||
|
||||
func logout(w http.ResponseWriter, ps httprouter.Params, body []byte) HTTPResponse {
|
||||
http.SetCookie(w, &http.Cookie{
|
||||
Name: "auth",
|
||||
Value: "",
|
||||
Path: baseURL + "/",
|
||||
Expires: time.Unix(0, 0),
|
||||
Secure: true,
|
||||
HttpOnly: true,
|
||||
SameSite: http.SameSiteStrictMode,
|
||||
})
|
||||
|
||||
eraseCookie(w)
|
||||
return APIResponse{true}
|
||||
}
|
||||
|
||||
func completeAuth(w http.ResponseWriter, username string, email string, firstname string, lastname string, groups string, session *Session) (err error) {
|
||||
var usr User
|
||||
func completeAuth(w http.ResponseWriter, username string, email string, firstname string, lastname string, groups string, session *Session) (usr User, err error) {
|
||||
if !userExists(username) {
|
||||
if usr, err = NewUser(username, email, firstname, lastname, groups); err != nil {
|
||||
return err
|
||||
return
|
||||
}
|
||||
} else if usr, err = getUserByLogin(username); err != nil {
|
||||
return err
|
||||
return
|
||||
}
|
||||
|
||||
if len(groups) > 255 {
|
||||
groups = groups[:255]
|
||||
}
|
||||
if usr.Groups != groups {
|
||||
usr.Groups = groups
|
||||
usr.Update()
|
||||
if len(groups) > 0 {
|
||||
if len(groups) > 255 {
|
||||
groups = groups[:255]
|
||||
}
|
||||
if usr.Groups != groups {
|
||||
usr.Groups = groups
|
||||
usr.Update()
|
||||
}
|
||||
}
|
||||
|
||||
if session == nil {
|
||||
|
@ -70,7 +67,7 @@ func completeAuth(w http.ResponseWriter, username string, email string, firstnam
|
|||
}
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
return
|
||||
}
|
||||
|
||||
http.SetCookie(w, &http.Cookie{
|
||||
|
@ -78,12 +75,12 @@ func completeAuth(w http.ResponseWriter, username string, email string, firstnam
|
|||
Value: base64.StdEncoding.EncodeToString(session.Id),
|
||||
Path: baseURL + "/",
|
||||
Expires: time.Now().Add(30 * 24 * time.Hour),
|
||||
Secure: true,
|
||||
HttpOnly: true,
|
||||
SameSite: http.SameSiteStrictMode,
|
||||
//Secure: true,
|
||||
})
|
||||
|
||||
return nil
|
||||
return
|
||||
}
|
||||
|
||||
func dummyAuth(w http.ResponseWriter, _ httprouter.Params, body []byte) (interface{}, error) {
|
||||
|
@ -92,5 +89,5 @@ func dummyAuth(w http.ResponseWriter, _ httprouter.Params, body []byte) (interfa
|
|||
return nil, err
|
||||
}
|
||||
|
||||
return map[string]string{"status": "OK"}, completeAuth(w, lf["login"], lf["email"], lf["firstname"], lf["lastname"], "", nil)
|
||||
return completeAuth(w, lf["username"], lf["email"], lf["firstname"], lf["lastname"], "", nil)
|
||||
}
|
||||
|
|
Reference in a new issue