Goit

Simple and lightweight Git web server
git clone http://git.omkov.net/Goit
Log | Tree | Refs | README | Download

Goit/src/user/sessions.go (78 lines, 1.8 KiB) -rw-r--r-- file download

c2c5250 Jakob Wakeling 2023-12-12 09:13:37
0
// Copyright (C) 2023, Jakob Wakeling
c2c5250 Jakob Wakeling 2023-12-12 09:13:37
1
// All rights reserved.
c2c5250 Jakob Wakeling 2023-12-12 09:13:37
2
410f65b Jakob Wakeling 2023-10-23 15:14:42
3
package user
410f65b Jakob Wakeling 2023-10-23 15:14:42
4
410f65b Jakob Wakeling 2023-10-23 15:14:42
5
import (
410f65b Jakob Wakeling 2023-10-23 15:14:42
6
	"fmt"
410f65b Jakob Wakeling 2023-10-23 15:14:42
7
	"log"
410f65b Jakob Wakeling 2023-10-23 15:14:42
8
	"net/http"
410f65b Jakob Wakeling 2023-10-23 15:14:42
9
	"strconv"
410f65b Jakob Wakeling 2023-10-23 15:14:42
10
	"time"
410f65b Jakob Wakeling 2023-10-23 15:14:42
11
945fd71 Jakob Wakeling 2023-11-05 15:39:31
12
	"github.com/Jamozed/Goit/src/goit"
410f65b Jakob Wakeling 2023-10-23 15:14:42
13
	"github.com/Jamozed/Goit/src/util"
410f65b Jakob Wakeling 2023-10-23 15:14:42
14
)
410f65b Jakob Wakeling 2023-10-23 15:14:42
15
410f65b Jakob Wakeling 2023-10-23 15:14:42
16
func HandleSessions(w http.ResponseWriter, r *http.Request) {
473d02c Jakob Wakeling 2023-11-28 00:01:06
17
	auth, user, err := goit.Auth(w, r, true)
473d02c Jakob Wakeling 2023-11-28 00:01:06
18
	if err != nil {
473d02c Jakob Wakeling 2023-11-28 00:01:06
19
		log.Println("[admin]", err.Error())
473d02c Jakob Wakeling 2023-11-28 00:01:06
20
		goit.HttpError(w, http.StatusInternalServerError)
473d02c Jakob Wakeling 2023-11-28 00:01:06
21
	}
473d02c Jakob Wakeling 2023-11-28 00:01:06
22
410f65b Jakob Wakeling 2023-10-23 15:14:42
23
	if !auth {
410f65b Jakob Wakeling 2023-10-23 15:14:42
24
		goit.HttpError(w, http.StatusUnauthorized)
410f65b Jakob Wakeling 2023-10-23 15:14:42
25
		return
410f65b Jakob Wakeling 2023-10-23 15:14:42
26
	}
410f65b Jakob Wakeling 2023-10-23 15:14:42
27
410f65b Jakob Wakeling 2023-10-23 15:14:42
28
	_, ss := goit.GetSessionCookie(r)
410f65b Jakob Wakeling 2023-10-23 15:14:42
29
410f65b Jakob Wakeling 2023-10-23 15:14:42
30
	revoke, err := strconv.ParseInt(r.FormValue("revoke"), 10, 64)
410f65b Jakob Wakeling 2023-10-23 15:14:42
31
	if err != nil {
410f65b Jakob Wakeling 2023-10-23 15:14:42
32
		revoke = -1
410f65b Jakob Wakeling 2023-10-23 15:14:42
33
	}
410f65b Jakob Wakeling 2023-10-23 15:14:42
34
410f65b Jakob Wakeling 2023-10-23 15:14:42
35
	type row struct{ Index, Ip, Seen, Expiry, Current string }
410f65b Jakob Wakeling 2023-10-23 15:14:42
36
	var data = struct {
410f65b Jakob Wakeling 2023-10-23 15:14:42
37
		Title    string
410f65b Jakob Wakeling 2023-10-23 15:14:42
38
		Sessions []row
410f65b Jakob Wakeling 2023-10-23 15:14:42
39
	}{Title: "User - Sessions"}
410f65b Jakob Wakeling 2023-10-23 15:14:42
40
410f65b Jakob Wakeling 2023-10-23 15:14:42
41
	goit.SessionsMutex.RLock()
570144e Jakob Wakeling 2023-12-15 23:28:06
42
	util.Debugln("[goit.HandleSessions] SessionsMutex rlock")
5166d87 Jakob Wakeling 2023-10-23 16:00:35
43
473d02c Jakob Wakeling 2023-11-28 00:01:06
44
	if revoke >= 0 && revoke < int64(len(goit.Sessions[user.Id])) {
473d02c Jakob Wakeling 2023-11-28 00:01:06
45
		var token = goit.Sessions[user.Id][revoke].Token
410f65b Jakob Wakeling 2023-10-23 15:14:42
46
		var current = token == ss.Token
410f65b Jakob Wakeling 2023-10-23 15:14:42
47
410f65b Jakob Wakeling 2023-10-23 15:14:42
48
		goit.SessionsMutex.RUnlock()
570144e Jakob Wakeling 2023-12-15 23:28:06
49
		util.Debugln("[goit.HandleSessions] SessionsMutex runlock")
5166d87 Jakob Wakeling 2023-10-23 16:00:35
50
473d02c Jakob Wakeling 2023-11-28 00:01:06
51
		goit.EndSession(user.Id, token)
410f65b Jakob Wakeling 2023-10-23 15:14:42
52
410f65b Jakob Wakeling 2023-10-23 15:14:42
53
		if current {
410f65b Jakob Wakeling 2023-10-23 15:14:42
54
			goit.EndSessionCookie(w)
410f65b Jakob Wakeling 2023-10-23 15:14:42
55
			http.Redirect(w, r, "/", http.StatusFound)
410f65b Jakob Wakeling 2023-10-23 15:14:42
56
			return
410f65b Jakob Wakeling 2023-10-23 15:14:42
57
		}
410f65b Jakob Wakeling 2023-10-23 15:14:42
58
410f65b Jakob Wakeling 2023-10-23 15:14:42
59
		http.Redirect(w, r, "/user/sessions", http.StatusFound)
410f65b Jakob Wakeling 2023-10-23 15:14:42
60
		return
410f65b Jakob Wakeling 2023-10-23 15:14:42
61
	}
410f65b Jakob Wakeling 2023-10-23 15:14:42
62
473d02c Jakob Wakeling 2023-11-28 00:01:06
63
	for i, v := range goit.Sessions[user.Id] {
410f65b Jakob Wakeling 2023-10-23 15:14:42
64
		data.Sessions = append(data.Sessions, row{
410f65b Jakob Wakeling 2023-10-23 15:14:42
65
			Index: fmt.Sprint(i), Ip: v.Ip, Seen: v.Seen.Format(time.DateTime), Expiry: v.Expiry.Format(time.DateTime),
410f65b Jakob Wakeling 2023-10-23 15:14:42
66
			Current: util.If(v.Token == ss.Token, "(current)", ""),
410f65b Jakob Wakeling 2023-10-23 15:14:42
67
		})
410f65b Jakob Wakeling 2023-10-23 15:14:42
68
	}
5166d87 Jakob Wakeling 2023-10-23 16:00:35
69
410f65b Jakob Wakeling 2023-10-23 15:14:42
70
	goit.SessionsMutex.RUnlock()
570144e Jakob Wakeling 2023-12-15 23:28:06
71
	util.Debugln("[goit.HandleSessions] SessionsMutex runlock")
410f65b Jakob Wakeling 2023-10-23 15:14:42
72
410f65b Jakob Wakeling 2023-10-23 15:14:42
73
	if err := goit.Tmpl.ExecuteTemplate(w, "user/sessions", data); err != nil {
410f65b Jakob Wakeling 2023-10-23 15:14:42
74
		log.Println("[/user/login]", err.Error())
410f65b Jakob Wakeling 2023-10-23 15:14:42
75
	}
410f65b Jakob Wakeling 2023-10-23 15:14:42
76
}
77