-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
sessions.go
183 lines (137 loc) · 4.24 KB
/
sessions.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
173
174
175
176
177
178
179
180
181
182
183
package main
import (
"database/sql"
"github.com/getsentry/sentry-go"
log "github.com/sirupsen/logrus"
"net/http"
"time"
)
type Session struct {
UserId string `json:"userId"`
Provider string `json:"provider"`
Hash string `json:"-"`
Country string `json:"country"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
}
func FindSessionAssociatedUser(userId string, provider string, sessionHash string, db *sql.DB) (*User, error) {
tx, err := db.Begin()
if err != nil {
sentry.CaptureException(err)
return nil, err
}
defer tx.Rollback()
statement, err := tx.Prepare(
`select users.* from sessions
inner join users on sessions.user_id = users.id and sessions.provider = users.provider
where sessions.user_id = $1 and sessions.provider = $2 and sessions.hash = $3
limit 1`)
if err != nil {
sentry.CaptureException(err)
return nil, err
}
var user User
if err = statement.QueryRow(userId, provider, sessionHash).Scan(&user.Id, &user.Name, &user.Avatar, &user.Provider); err != nil {
if err == sql.ErrNoRows {
return nil, nil
} else {
sentry.CaptureException(err)
return &user, err
}
}
if err = tx.Commit(); err != nil {
sentry.CaptureException(err)
return nil, err
}
return &user, nil
}
func FindSession(userId string, provider string, sessionHash string, db *sql.DB) (*Session, error) {
tx, err := db.Begin()
if err != nil {
sentry.CaptureException(err)
return nil, err
}
defer tx.Rollback()
statement, err := tx.Prepare("select * from sessions where user_id = $1 and provider = $2 and hash = $3 limit 1")
if err != nil {
sentry.CaptureException(err)
return nil, err
}
var session Session
if err = statement.QueryRow(userId, provider, sessionHash).Scan(&session.UserId, &session.Provider, &session.Hash, &session.Country, &session.CreatedAt, &session.UpdatedAt); err != nil {
if err == sql.ErrNoRows {
return nil, nil
} else {
sentry.CaptureException(err)
return &session, err
}
}
if err = tx.Commit(); err != nil {
sentry.CaptureException(err)
return nil, err
}
return &session, nil
}
func StartSession(userId string, provider string, country string, db *sql.DB) (*Session, error) {
tx, err := db.Begin()
if err != nil {
sentry.CaptureException(err)
return nil, err
}
defer tx.Rollback()
statement, err := tx.Prepare("insert into sessions (user_id, provider, country) values ($1, $2, $3) returning *")
if err != nil {
sentry.CaptureException(err)
return nil, err
}
var session Session
if err = statement.QueryRow(userId, provider, country).Scan(&session.UserId, &session.Provider, &session.Hash, &session.Country, &session.CreatedAt, &session.UpdatedAt); err != nil {
sentry.CaptureException(err)
return nil, err
}
if err = tx.Commit(); err != nil {
sentry.CaptureException(err)
return nil, err
}
return &session, nil
}
func DeleteSession(session *Session, db *sql.DB) error {
tx, err := db.Begin()
if err != nil {
log.WithFields(log.Fields{"error": err}).Error("failed to begin transaction")
return err
}
defer tx.Rollback()
_, err = tx.Exec("delete from sessions where user_id = $1 and provider = $2 and hash = $3", session.UserId, session.Provider, session.Hash)
if err != nil {
log.WithFields(log.Fields{"error": err}).Error("failed to execute statement")
return err
}
if tx.Commit() != nil {
log.WithFields(log.Fields{"error": err}).Error("failed to commit transaction")
return err
}
return nil
}
func (app *Application) ResolveUserFromRequest(r *http.Request) (*User, error) {
cookie, err := r.Cookie("pomu")
if err != nil {
return nil, err
}
var session *Session
if err = app.secureCookie.Decode("session", cookie.Value, &session); err != nil {
return nil, err
}
return FindSessionAssociatedUser(session.UserId, session.Provider, session.Hash, app.db)
}
func (app *Application) ResolveSessionFromRequest(r *http.Request) (*Session, error) {
cookie, err := r.Cookie("pomu")
if err != nil {
return nil, err
}
var untrustedSession *Session
if err = app.secureCookie.Decode("session", cookie.Value, &untrustedSession); err != nil {
return nil, err
}
return FindSession(untrustedSession.UserId, untrustedSession.Provider, untrustedSession.Hash, app.db)
}