-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsession.go
More file actions
134 lines (120 loc) · 3.48 KB
/
Copy pathsession.go
File metadata and controls
134 lines (120 loc) · 3.48 KB
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
//contains all functions and structs for session handling
package main
import (
"fmt"
"net/http"
"time"
)
func getSessionID(w http.ResponseWriter, r *http.Request) string {
Info.Println("getSessionID()")
var userID string
if userID = getUserID(r); userID == "" {
http.Redirect(w, r, "/login", 303)
return ""
}
if imp := getImpersonate(r); imp != "" {
userID = imp
}
return userID
}
func setSession(userID string, w http.ResponseWriter) {
value := map[string]string{
"userID": userID,
}
placeCookie(value, w)
}
func setImpersonateSession(userID, impersonateID string, w http.ResponseWriter) {
Info.Printf("setImpersonateSession(%s,%s)\n", userID, impersonateID)
value := map[string]string{
"userID": userID,
"impersonateID": impersonateID,
}
placeCookie(value, w)
}
func setDispalyInterval(from, to time.Time, w http.ResponseWriter, request *http.Request) {
Info.Println("setDisplayInterval()")
cookie, err := request.Cookie("user-session")
if err != nil {
fmt.Println(err)
return
}
cookieValue := make(map[string]string)
err = cookieHandler.Decode("user-session", cookie.Value, &cookieValue)
if err != nil {
fmt.Println(err)
return
}
cookieValue["from"] = from.String()
cookieValue["to"] = to.String()
placeCookie(cookieValue, w)
}
func getDisplayInterval(request *http.Request) (from, to time.Time, err error) {
Info.Println("getDisplayInterval()")
cookie, err := request.Cookie("user-session")
if err != nil {
fmt.Println(err)
return time.Time{}, time.Time{}, err
}
cookieValue := make(map[string]string)
err = cookieHandler.Decode("user-session", cookie.Value, &cookieValue)
if err != nil {
fmt.Println(err)
return time.Time{}, time.Time{}, err
}
from, err = time.Parse("2006-01-02 15:04:05.999999999 -0700 MST", cookieValue["from"])
if err != nil {
return time.Time{}, time.Time{}, err
}
to, err = time.Parse("2006-01-02 15:04:05.999999999 -0700 MST", cookieValue["to"])
if err != nil {
return time.Time{}, time.Time{}, err
}
return from, to, nil
}
func placeCookie(value map[string]string, w http.ResponseWriter) {
if encoded, err := cookieHandler.Encode("user-session", value); err == nil {
cookie := &http.Cookie{
Name: "user-session",
Value: encoded,
Path: "/",
}
http.SetCookie(w, cookie)
}
}
func unimpersonateMe(w http.ResponseWriter, r *http.Request) {
Info.Println("unimpersonate()")
if cookie, err := r.Cookie("user-session"); err == nil {
cookieValue := make(map[string]string)
if err = cookieHandler.Decode("user-session", cookie.Value, &cookieValue); err == nil {
cookieValue["impersonateID"] = ""
if encoded, err := cookieHandler.Encode("user-session", cookieValue); err == nil {
cookie := &http.Cookie{
Name: "user-session",
Value: encoded,
Path: "/",
}
http.SetCookie(w, cookie)
}
}
}
}
func getUserID(request *http.Request) (userID string) {
Info.Println("getUserID()")
if cookie, err := request.Cookie("user-session"); err == nil {
cookieValue := make(map[string]string)
if err = cookieHandler.Decode("user-session", cookie.Value, &cookieValue); err == nil {
userID = cookieValue["userID"]
}
}
return userID
}
func getImpersonate(request *http.Request) (userID string) {
Info.Println("getImpersonate()")
if cookie, err := request.Cookie("user-session"); err == nil {
cookieValue := make(map[string]string)
if err = cookieHandler.Decode("user-session", cookie.Value, &cookieValue); err == nil {
userID = cookieValue["impersonateID"]
}
}
return userID
}