autogenerate token.

This commit is contained in:
Miroslav Šedivý 2021-03-13 21:43:36 +01:00
parent a6b66e4d55
commit 93ac4e8332
4 changed files with 13 additions and 8 deletions

View File

@ -65,7 +65,10 @@ func (h *MembersHandler) membersCreate(w http.ResponseWriter, r *http.Request) {
} }
} }
session, err := h.sessions.Create(data.ID, *data.MemberProfile) // TODO: Refactor.
// data.ID
session, err := h.sessions.Create(*data.MemberProfile)
if err != nil { if err != nil {
utils.HttpInternalServerError(w, err) utils.HttpInternalServerError(w, err)
return return

View File

@ -30,10 +30,8 @@ func (api *ApiManagerCtx) Login(w http.ResponseWriter, r *http.Request) {
return return
} }
token := data.Secret
// TODO: Proper login. // TODO: Proper login.
session, err := api.sessions.Create(token, types.MemberProfile{ session, err := api.sessions.Create(types.MemberProfile{
Name: data.ID, Name: data.ID,
IsAdmin: true, IsAdmin: true,
CanLogin: true, CanLogin: true,
@ -55,7 +53,7 @@ func (api *ApiManagerCtx) Login(w http.ResponseWriter, r *http.Request) {
http.SetCookie(w, &http.Cookie{ http.SetCookie(w, &http.Cookie{
Name: "NEKO_SESSION", Name: "NEKO_SESSION",
Value: token, Value: session.ID(),
Expires: CookieExpirationDate, Expires: CookieExpirationDate,
Secure: !UnsecureCookies, Secure: !UnsecureCookies,
SameSite: sameSite, SameSite: sameSite,

View File

@ -35,9 +35,13 @@ type SessionManagerCtx struct {
emmiter events.EventEmmiter emmiter events.EventEmmiter
} }
func (manager *SessionManagerCtx) Create(id string, profile types.MemberProfile) (types.Session, error) { func (manager *SessionManagerCtx) Create(profile types.MemberProfile) (types.Session, error) {
manager.sessionsMu.Lock() id, err := utils.NewUID(32)
if err != nil {
return nil, err
}
manager.sessionsMu.Lock()
_, ok := manager.sessions[id] _, ok := manager.sessions[id]
if ok { if ok {
manager.sessionsMu.Unlock() manager.sessionsMu.Unlock()

View File

@ -59,7 +59,7 @@ type Session interface {
} }
type SessionManager interface { type SessionManager interface {
Create(id string, profile MemberProfile) (Session, error) Create(profile MemberProfile) (Session, error)
Update(id string, profile MemberProfile) error Update(id string, profile MemberProfile) error
Get(id string) (Session, bool) Get(id string) (Session, bool)
Delete(id string) error Delete(id string) error