/
rt-authentication.go
81 lines (69 loc) · 1.81 KB
/
rt-authentication.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// HipparchiaGoServer
// Copyright: E Gunderson 2022
// License: GNU GENERAL PUBLIC LICENSE 3
// (see LICENSE in the top level directory of the distribution)
package main
import (
"github.com/labstack/echo/v4"
"net/http"
)
// RtAuthLogin - accept and validate login info sent from <form id="hipparchiauserlogin"...>
func RtAuthLogin(c echo.Context) error {
cid := readUUIDCookie(c)
s := SafeSessionRead(cid)
u := c.FormValue("user")
p := c.FormValue("pw")
if UserPassPairs[u] == p {
SafeAuthenticationWrite(cid, true)
s.LoginName = u
} else {
SafeAuthenticationWrite(cid, false)
s.LoginName = "Anonymous"
}
SafeSessionMapInsert(s)
e := c.Redirect(http.StatusFound, "/")
chke(e)
return nil
}
// RtAuthLogout - log this session out
func RtAuthLogout(c echo.Context) error {
u := readUUIDCookie(c)
s := SafeSessionRead(u)
s.LoginName = "Anonymous"
SafeSessionMapInsert(s)
SafeAuthenticationWrite(u, false)
return c.JSONPretty(http.StatusOK, "Anonymous", JSONINDENT)
}
// RtAuthChkuser - report who this session is logged in as
func RtAuthChkuser(c echo.Context) error {
user := readUUIDCookie(c)
s := SafeSessionRead(user)
a := SafeAuthenticationRead(s.ID)
type JSO struct {
ID string `json:"userid"`
Auth bool `json:"authorized"`
}
o := JSO{
ID: s.LoginName,
Auth: a,
}
return c.JSONPretty(http.StatusOK, o, JSONINDENT)
}
// SafeAuthenticationRead - use a lock to safely read from AuthorizedMap
func SafeAuthenticationRead(u string) bool {
MapLocker.RLock()
defer MapLocker.RUnlock()
s, e := AuthorizedMap[u]
if e != true {
AuthorizedMap[u] = false
s = false
}
return s
}
// SafeAuthenticationWrite - use a lock to safely write to AuthorizedMap
func SafeAuthenticationWrite(u string, b bool) {
MapLocker.RLock()
defer MapLocker.RUnlock()
AuthorizedMap[u] = b
return
}