-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsession.go
148 lines (142 loc) · 4.54 KB
/
session.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
// Copyright 2015 Afshin Darian. All rights reserved.
// Use of this source code is governed by The MIT License
// that can be found in the LICENSE file.
package wares
import (
"fmt"
"net/http"
"github.com/pborman/uuid"
"github.com/ursiform/bear"
"github.com/ursiform/forest"
)
func SessionDel(app *forest.App, manager SessionManager) func(ctx *bear.Context) {
return func(ctx *bear.Context) {
sessionID, ok := ctx.Get(forest.SessionID).(string)
if !ok {
err := fmt.Errorf("SessionDel %s: %v",
forest.SessionID, ctx.Get(forest.SessionID))
ctx.Set(forest.Error, err)
message := safeErrorMessage(app, ctx, app.Error("Generic"))
app.Response(ctx, http.StatusInternalServerError,
forest.Failure, message).Write(nil)
return
}
userID, ok := ctx.Get(forest.SessionUserID).(string)
if !ok {
err := fmt.Errorf("SessionDel %s: %v",
forest.SessionUserID, ctx.Get(forest.SessionUserID))
ctx.Set(forest.Error, err)
message := safeErrorMessage(app, ctx, app.Error("Generic"))
app.Response(ctx, http.StatusInternalServerError,
forest.Failure, message).Write(nil)
return
}
if err := manager.Delete(sessionID, userID); err != nil {
ctx.Set(forest.Error, err)
message := safeErrorMessage(app, ctx, app.Error("Generic"))
app.Response(ctx, http.StatusInternalServerError,
forest.Failure, message).Write(nil)
return
}
ctx.Next()
}
}
func SessionGet(app *forest.App, manager SessionManager) func(ctx *bear.Context) {
return func(ctx *bear.Context) {
cookieName := forest.SessionID
createEmptySession := func(sessionID string) {
path := app.Config.CookiePath
if path == "" {
path = "/"
}
cookieValue := sessionID
duration := app.Duration("Cookie")
// Reset the cookie.
app.SetCookie(ctx, path, cookieName, cookieValue, duration)
manager.CreateEmpty(sessionID, ctx)
ctx.Next()
}
cookie, err := ctx.Request.Cookie(cookieName)
if err != nil || cookie.Value == "" {
createEmptySession(uuid.New())
return
}
sessionID := cookie.Value
userID, userJSON, err := manager.Read(sessionID)
if err != nil || userID == "" || userJSON == "" {
createEmptySession(uuid.New())
return
}
if err := manager.Create(sessionID, userID, userJSON, ctx); err != nil {
println(fmt.Sprintf("error creating session: %s", err))
defer func(sessionID string, userID string) {
if err := manager.Delete(sessionID, userID); err != nil {
println(fmt.Sprintf("error deleting session: %s", err))
}
}(sessionID, userID)
createEmptySession(uuid.New())
return
}
// If SessionRefresh is set to false, the session will not refresh;
// if it's not set or if it's set to true, the session is refreshed.
refresh, ok := ctx.Get(forest.SessionRefresh).(bool)
if !ok || refresh {
path := app.Config.CookiePath
if path == "" {
path = "/"
}
cookieName := forest.SessionID
cookieValue := sessionID
duration := app.Duration("Cookie")
// Refresh the cookie.
app.SetCookie(ctx, path, cookieName, cookieValue, duration)
err := manager.Update(sessionID, userID,
userJSON, app.Duration("Session"))
if err != nil {
println(fmt.Sprintf("error updating session: %s", err))
}
}
ctx.Next()
}
}
func SessionSet(app *forest.App, manager SessionManager) func(ctx *bear.Context) {
return func(ctx *bear.Context) {
userJSON, err := manager.Marshal(ctx)
if err != nil {
ctx.Set(forest.Error, err)
message := safeErrorMessage(app, ctx, app.Error("Generic"))
app.Response(ctx, http.StatusInternalServerError,
forest.Failure, message).Write(nil)
return
}
sessionID, ok := ctx.Get(forest.SessionID).(string)
if !ok {
err := fmt.Errorf("%s: %v",
forest.SessionID, ctx.Get(forest.SessionID))
ctx.Set(forest.Error, err)
message := safeErrorMessage(app, ctx, app.Error("Generic"))
app.Response(ctx, http.StatusInternalServerError,
forest.Failure, message).Write(nil)
return
}
userID, ok := ctx.Get(forest.SessionUserID).(string)
if !ok {
err := fmt.Errorf("%s: %v",
forest.SessionUserID, ctx.Get(forest.SessionUserID))
ctx.Set(forest.Error, err)
message := safeErrorMessage(app, ctx, app.Error("Generic"))
app.Response(ctx, http.StatusInternalServerError,
forest.Failure, message).Write(nil)
return
}
if err := manager.Update(sessionID, userID,
string(userJSON), app.Duration("Session")); err != nil {
ctx.Set(forest.Error, err)
message := safeErrorMessage(app, ctx, app.Error("Generic"))
app.Response(ctx, http.StatusInternalServerError,
forest.Failure, message).Write(nil)
return
}
ctx.Next()
}
}