-
Notifications
You must be signed in to change notification settings - Fork 1
/
api_auth.go
172 lines (134 loc) · 3.74 KB
/
api_auth.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package auth
import (
"fmt"
"net/http"
"github.com/deze333/vroom/util"
"github.com/gorilla/sessions"
)
//------------------------------------------------------------
// Authentication functions
//------------------------------------------------------------
// Checks if this user has been authenticated.
func IsAuthd(r *http.Request) bool {
sess, err := _cookieStore.Get(r, _cookieSessName)
if err != nil {
return false
}
//fmt.Println("COOKIE SESSION:", _cookieSessName)
//fmt.Println("IS USER LOGGED IN:", sess.Options, sess.Values)
if _, ok := sess.Values["_auth"]; ok {
return true
}
return false
}
// Authenticates new user by creating a new session.
func Auth(w http.ResponseWriter, r *http.Request, vals map[string]string) (err error) {
// Get session which may be a new one
// New session is auto added to the store
sess, err := _cookieStore.Get(r, _cookieSessName)
if err != nil {
return
}
// Set session values
sess.Values["_auth"] = util.NewUUID()
sess.Values["_ip"] = util.GetRequestIP(r)
// Add user values
for k, v := range vals {
sess.Values[k] = v
}
// Create session options
sess.Options = &sessions.Options{}
if remember, ok := vals["remember"]; ok && remember == "true" {
// Never expires, set to 3 years
sess.Options.MaxAge = 3 * 365 * 24 * 60 * 60
} else {
// Default expiry
sess.Options.MaxAge = _cookieMaxAge
}
if _cookiePath != "" {
sess.Options.Path = _cookiePath
}
if _cookieDomain != "" {
sess.Options.Domain = _cookieDomain
}
err = sess.Save(r, w)
fmt.Println(">>> LOGIN:", sess.Options, sess.Values)
/*
// Report user logged in: err, url, params, session, stack
sess, _ = _cookieStore.Get(r, _cookieSessName)
_onPanic(
fmt.Sprintf("User logged in"),
fmt.Sprintf("%v #%v @ %v", sess.Values["initials"], sess.Values["_auth"], sess.Values["_ip"]),
"Session", fmt.Sprint(sess))
*/
return
}
// Retrieves authentication ID from the session.
// Returns empty string if not found.
func GetAuthdId(r *http.Request) string {
sess, _ := _cookieStore.Get(r, _cookieSessName)
if val, ok := sess.Values["_auth"]; ok {
return fmt.Sprint(val)
}
return ""
}
// De-authenticates user.
func DeAuth(w http.ResponseWriter, r *http.Request) (err error) {
// Get session which may be a new one
sess, err := _cookieStore.Get(r, _cookieSessName)
if err != nil {
return
}
//fmt.Println()
//fmt.Println()
//fmt.Println("xxx LOGOUT:", sess.Values)
var authId string
if val, ok := sess.Values["_auth"]; ok {
authId = fmt.Sprint(val)
}
// Remember user, only deauth them
//delete(sess.Values, "_auth")
// Forget user completely
// Set cookie to expire right away
sess.Values = map[interface{}]interface{}{}
sess.Options.MaxAge = -1
err = sess.Save(r, w)
// Broadcast event
broadcastDeAuth(authId)
return
}
// Retrieve existing session.
func GetSessionValues(r *http.Request) (info map[string]interface{}, err error) {
// Get session which may be a new one
sess, err := _cookieStore.Get(r, _cookieSessName)
if err != nil {
return
}
//fmt.Println()
//fmt.Println("--->", r.RequestURI, ", Session :", sess.Values)
// Build user info
info = map[string]interface{}{}
for k, val := range sess.Values {
key := fmt.Sprint(k)
if len(key) > 0 && key[0] == '_' {
continue
}
info[key] = val
}
return
}
// XXX Experimental, not used.
// Adds item to an array of items in session.
func Values_Array_ItemPush(w http.ResponseWriter, r *http.Request, key string, item interface{}) {
sess, _ := _cookieStore.Get(r, _cookieSessName)
if val, ok := sess.Values[key]; ok {
if arr, ok := val.([]interface{}); ok {
sess.Values[key] = append(arr, item)
} else {
sess.Values[key] = []interface{}{item}
}
} else {
sess.Values[key] = []interface{}{item}
}
sess.Save(r, w)
}