]>
code.octet-stream.net Git - broadcaster/blob - server/session.go
4b4c44536b6a9e67a622488224d26c2dd49c77b8
11 func generateSession() string {
13 _
, err
:= rand
.Read(b
)
17 return hex
.EncodeToString(b
)
20 func currentUser(w http
.ResponseWriter
, r
*http
.Request
) (User
, error
) {
21 // todo: check if user actually exists and is allowed to log in
22 cookie
, e
:= r
.Cookie("broadcast_session")
27 username
, e
:= db
.GetUserForSession(cookie
.Value
)
31 return User
{username
: username
}, nil
34 func createSessionCookie(w http
.ResponseWriter
) {
35 sess
:= generateSession()
36 log
.Println("Generated a random session", sess
)
37 expiration
:= time
.Now().Add(365 * 24 * time
.Hour
)
38 cookie
:= http
.Cookie
{Name
: "broadcast_session", Value
: sess
, Expires
: expiration
, SameSite
: http
.SameSiteLaxMode
}
39 db
.InsertSession("admin", sess
, expiration
)
40 http
.SetCookie(w
, &cookie
)
43 func clearSessionCookie(w http
.ResponseWriter
) {
45 Name
: "broadcast_session",