2021-01-29 22:22:14 +01:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2023-01-12 23:21:56 +01:00
|
|
|
"errors"
|
2021-01-29 22:22:14 +01:00
|
|
|
"net/http"
|
|
|
|
|
2022-07-14 00:58:22 +02:00
|
|
|
"github.com/demodesk/neko/pkg/auth"
|
|
|
|
"github.com/demodesk/neko/pkg/types"
|
|
|
|
"github.com/demodesk/neko/pkg/utils"
|
2021-01-29 22:22:14 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type SessionLoginPayload struct {
|
2021-03-14 00:32:52 +01:00
|
|
|
Username string `json:"username"`
|
|
|
|
Password string `json:"password"`
|
2021-01-29 22:22:14 +01:00
|
|
|
}
|
|
|
|
|
2021-03-14 00:32:52 +01:00
|
|
|
type SessionDataPayload struct {
|
2021-01-29 22:22:14 +01:00
|
|
|
ID string `json:"id"`
|
2021-04-24 20:53:37 +02:00
|
|
|
Token string `json:"token,omitempty"`
|
2021-01-29 22:22:14 +01:00
|
|
|
Profile types.MemberProfile `json:"profile"`
|
2021-03-13 23:42:16 +01:00
|
|
|
State types.SessionState `json:"state"`
|
2021-01-29 22:22:14 +01:00
|
|
|
}
|
|
|
|
|
2021-09-17 00:58:50 +02:00
|
|
|
func (api *ApiManagerCtx) Login(w http.ResponseWriter, r *http.Request) error {
|
2021-01-29 22:22:14 +01:00
|
|
|
data := &SessionLoginPayload{}
|
2021-09-17 00:58:50 +02:00
|
|
|
if err := utils.HttpJsonRequest(w, r, data); err != nil {
|
|
|
|
return err
|
2021-01-29 22:22:14 +01:00
|
|
|
}
|
|
|
|
|
2021-03-14 19:59:34 +01:00
|
|
|
session, token, err := api.members.Login(data.Username, data.Password)
|
2021-03-13 21:11:48 +01:00
|
|
|
if err != nil {
|
2023-01-12 23:21:56 +01:00
|
|
|
if errors.Is(err, types.ErrSessionAlreadyConnected) {
|
|
|
|
return utils.HttpUnprocessableEntity("session already connected")
|
|
|
|
} else if errors.Is(err, types.ErrMemberDoesNotExist) || errors.Is(err, types.ErrMemberInvalidPassword) {
|
|
|
|
return utils.HttpUnauthorized().WithInternalErr(err)
|
|
|
|
} else {
|
|
|
|
return utils.HttpInternalServerError().WithInternalErr(err)
|
|
|
|
}
|
2021-03-13 21:11:48 +01:00
|
|
|
}
|
2021-01-29 22:22:14 +01:00
|
|
|
|
2021-04-24 20:53:37 +02:00
|
|
|
sessionData := SessionDataPayload{
|
2021-01-29 23:03:35 +01:00
|
|
|
ID: session.ID(),
|
2021-03-14 00:45:51 +01:00
|
|
|
Profile: session.Profile(),
|
|
|
|
State: session.State(),
|
2021-04-24 20:53:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if api.sessions.CookieEnabled() {
|
|
|
|
api.sessions.CookieSetToken(w, token)
|
|
|
|
} else {
|
|
|
|
sessionData.Token = token
|
|
|
|
}
|
|
|
|
|
2021-09-17 00:58:50 +02:00
|
|
|
return utils.HttpSuccess(w, sessionData)
|
2021-01-29 22:22:14 +01:00
|
|
|
}
|
|
|
|
|
2021-09-17 00:58:50 +02:00
|
|
|
func (api *ApiManagerCtx) Logout(w http.ResponseWriter, r *http.Request) error {
|
|
|
|
session, _ := auth.GetSession(r)
|
2021-03-13 21:11:48 +01:00
|
|
|
|
2021-03-14 19:59:34 +01:00
|
|
|
err := api.members.Logout(session.ID())
|
2021-03-13 21:11:48 +01:00
|
|
|
if err != nil {
|
2023-01-12 23:21:56 +01:00
|
|
|
if errors.Is(err, types.ErrSessionNotFound) {
|
|
|
|
return utils.HttpBadRequest("session is not logged in")
|
|
|
|
} else {
|
|
|
|
return utils.HttpInternalServerError().WithInternalErr(err)
|
|
|
|
}
|
2021-03-13 21:11:48 +01:00
|
|
|
}
|
|
|
|
|
2021-04-24 20:53:37 +02:00
|
|
|
if api.sessions.CookieEnabled() {
|
|
|
|
api.sessions.CookieClearToken(w, r)
|
|
|
|
}
|
2021-01-29 22:22:14 +01:00
|
|
|
|
2021-09-17 00:58:50 +02:00
|
|
|
return utils.HttpSuccess(w, true)
|
2021-01-29 22:22:14 +01:00
|
|
|
}
|
|
|
|
|
2021-09-17 00:58:50 +02:00
|
|
|
func (api *ApiManagerCtx) Whoami(w http.ResponseWriter, r *http.Request) error {
|
|
|
|
session, _ := auth.GetSession(r)
|
2021-01-29 22:22:14 +01:00
|
|
|
|
2021-09-17 00:58:50 +02:00
|
|
|
return utils.HttpSuccess(w, SessionDataPayload{
|
2021-01-29 22:22:14 +01:00
|
|
|
ID: session.ID(),
|
2021-03-14 00:45:51 +01:00
|
|
|
Profile: session.Profile(),
|
|
|
|
State: session.State(),
|
2021-01-29 22:22:14 +01:00
|
|
|
})
|
|
|
|
}
|
2023-05-15 00:34:37 +02:00
|
|
|
|
|
|
|
func (api *ApiManagerCtx) Sessions(w http.ResponseWriter, r *http.Request) error {
|
|
|
|
sessions := []SessionDataPayload{}
|
|
|
|
for _, session := range api.sessions.List() {
|
|
|
|
sessions = append(sessions, SessionDataPayload{
|
|
|
|
ID: session.ID(),
|
|
|
|
Profile: session.Profile(),
|
|
|
|
State: session.State(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return utils.HttpSuccess(w, sessions)
|
|
|
|
}
|