-
Notifications
You must be signed in to change notification settings - Fork 1
/
login.go
140 lines (129 loc) · 3.63 KB
/
login.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package webserver
import (
"crypto/rand"
"encoding/base64"
"log"
"net/http"
"net/url"
"strings"
"sync"
"time"
"github.com/rothskeller/wppsvr/config"
)
type login struct {
token string
callsign string
expires time.Time
}
var (
logins []login
loginMutex sync.Mutex
)
// serveLogin responds to POST /login requests.
func (*webserver) serveLogin(w http.ResponseWriter, r *http.Request) {
callsign := r.FormValue("callsign")
password := r.FormValue("password")
if callsign == "" || password == "" || !validLogin(callsign, password) {
http.Error(w, "401 Unauthorized", http.StatusUnauthorized)
return
}
token := randomToken()
callsign = strings.ToUpper(callsign)
loginMutex.Lock()
logins = append(logins, login{token: token, callsign: callsign, expires: time.Now().Add(time.Hour)})
loginMutex.Unlock()
http.SetCookie(w, &http.Cookie{Name: "auth", Value: token, Path: "/", Secure: true})
w.WriteHeader(http.StatusNoContent)
}
// validLogin determines whether a callsign/password combination is valid. It
// does so by attempting to log into https://scc-ares-races.org with it.
func validLogin(callsign, password string) bool {
var client http.Client
client.CheckRedirect = func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse }
response, err := client.PostForm("https://www.scc-ares-races.org/activities/login01.php", url.Values{
"user_id": {callsign},
"password": {password},
"Submit": {"Log In"},
})
if err != nil {
log.Printf("ERROR: checking login of %q: post to scc-ares-races.org: %s", callsign, err)
return false
}
defer response.Body.Close()
if response.StatusCode != http.StatusFound {
log.Printf("ERROR: checking login of %q: scc-ares-races.org did not redirect", callsign)
return false
}
if response.Header.Get("Location") != "events.php" {
log.Printf("LOGIN FAIL: %s", callsign)
return false
}
log.Printf("LOGIN: %s", callsign)
return true
}
// checkLoggedIn verifies that the user is logged in, and returns their call
// sign. If the user is not properly logged in, it emits a redirect to the
// login page and returns an empty string.
func checkLoggedIn(w http.ResponseWriter, r *http.Request) string {
var token string
if c, err := r.Cookie("auth"); err == nil {
token = c.Value
loginMutex.Lock()
defer loginMutex.Unlock()
removeExpiredLogins()
for i, login := range logins {
if login.token == token {
logins[i].expires = time.Now().Add(time.Hour)
return login.callsign
}
}
}
http.Redirect(w, r, "/", http.StatusSeeOther)
return ""
}
// removeExpiredLogins removes all expired logins from the list. It must be
// called with loginMutex held.
func removeExpiredLogins() {
now := time.Now()
for i := 0; i < len(logins); {
if !logins[i].expires.Before(now) {
i++
} else {
if i < len(logins)-1 {
logins[i] = logins[len(logins)-1]
}
logins = logins[:len(logins)-1]
}
}
}
// randomToken returns a random token string.
func randomToken() string {
var (
tokenb [24]byte
err error
)
if _, err = rand.Read(tokenb[:]); err != nil {
panic(err)
}
return base64.URLEncoding.EncodeToString(tokenb[:])
}
// canEditSessions returns whether the viewer (identified by callsign) is
// allowed to edit session definitions.
func canEditSessions(callsign string) bool {
for _, cs := range config.Get().CanEditSessions {
if cs == callsign {
return true
}
}
return false
}
// canViewEveryone returns whether the viewer (identified by callsign) is
// allowed to view other people's messages.
func canViewEveryone(callsign string) bool {
for _, cs := range config.Get().CanViewEveryone {
if cs == callsign {
return true
}
}
return false
}