/
session_get.go
50 lines (39 loc) · 1.06 KB
/
session_get.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package handler
import (
"encoding/json"
"net/http"
"github.com/goydb/goydb/pkg/model"
)
const sessionName = "AuthSession"
type SessionGet struct {
Base
}
func (s *SessionGet) ServeHTTP(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
session, via := Authenticator{Base: s.Base}.Auth(r)
if session == nil {
WriteError(w, http.StatusBadRequest, "session is invalid")
return
}
resp := &SessionResponse{
Ok: true,
SessionInfo: SessionInfo{
AuthenticationHandlers: []string{"cookie", "default"},
},
}
if session.Authenticated() {
resp.SessionUserCtx = *session
resp.SessionInfo.Authenticated = via
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(resp) // nolint: errcheck
}
type SessionResponse struct {
Ok bool `json:"ok"`
SessionUserCtx model.Session `json:"userCtx"`
SessionInfo SessionInfo `json:"info"`
}
type SessionInfo struct {
AuthenticationHandlers []string `json:"authentication_handlers"`
Authenticated string `json:"authenticated,omitempty"`
}