forked from TF2Stadium/Helen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
authenticationHelpers.go
88 lines (68 loc) · 1.74 KB
/
authenticationHelpers.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
package controllerhelpers
import (
"errors"
"net/http"
"github.com/TF2Stadium/Helen/config"
"github.com/TF2Stadium/Helen/config/stores"
"github.com/bitly/go-simplejson"
"github.com/gorilla/sessions"
)
func buildFakeSocketRequest(cookiesObj *simplejson.Json) *http.Request {
cookies, err := cookiesObj.Map()
if err != nil {
return &http.Request{}
}
str := ""
first := true
for k, v := range cookies {
vStr, ok := v.(string)
if !ok {
continue
}
if !first {
str += ";"
}
str += k + "=" + vStr
first = false
}
if str == "" {
return &http.Request{}
}
headers := http.Header{}
headers.Add("Cookie", str)
return &http.Request{Header: headers}
}
func AuthenticateSocket(socketid string, r *http.Request) error {
s, _ := GetSessionHTTP(r)
if _, ok := s.Values["id"]; ok {
stores.SocketAuthStore[socketid] = s
return nil
}
return errors.New("Player isn't logged in")
}
func DeauthenticateSocket(socketid string) {
delete(stores.SocketAuthStore, socketid)
}
func IsLoggedInSocket(socketid string) bool {
_, ok := stores.SocketAuthStore[socketid]
return ok
}
func IsLoggedInHTTP(r *http.Request) bool {
session, _ := stores.SessionStore.Get(r, config.Constants.SessionName)
val, ok := session.Values["id"]
return ok && val != ""
}
func GetSessionHTTP(r *http.Request) (*sessions.Session, error) {
return stores.SessionStore.Get(r, config.Constants.SessionName)
}
func GetSessionSocket(socketid string) (*sessions.Session, error) {
session, ok := stores.SocketAuthStore[socketid]
if !ok {
return nil, errors.New("No session associated with the socket")
}
return session, nil
}
func GetSteamId(socketid string) string {
session, _ := GetSessionSocket(socketid)
return session.Values["steam_id"].(string)
}