-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathusers.go
182 lines (139 loc) · 3.87 KB
/
users.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
package main
import (
"database/sql"
"github.com/gorilla/mux"
log "github.com/sirupsen/logrus"
"net/http"
"strings"
"github.com/getsentry/sentry-go"
)
const (
ProviderGoogle = "google"
ProviderDiscord = "discord"
)
type User struct {
Id string `json:"id"`
Name string `json:"name"`
Avatar string `json:"avatar"`
Provider string `json:"provider"`
}
func (app *Application) IdentitySelf(w http.ResponseWriter, r *http.Request) {
user, err := app.ResolveUserFromRequest(r)
if err != nil || user == nil {
http.Error(w, "please login first", http.StatusUnauthorized)
return
}
SerializeJson(w, user)
}
func (app *Application) Identity(w http.ResponseWriter, r *http.Request) {
variables := mux.Vars(r)
requestedProvider := strings.ToLower(variables["provider"])
id := strings.ToLower(variables["id"])
if len(strings.Trim(requestedProvider, " ")) == 0 || len(strings.Trim(id, " ")) == 0 {
http.Error(w, "provider or id empty", http.StatusBadRequest)
return
}
var provider string
switch requestedProvider {
case "google":
provider = ProviderGoogle
break
case "discord":
provider = ProviderDiscord
break
default:
http.Error(w, "provider not found", http.StatusBadRequest)
return
}
log.Printf("checking %s provid %s\n", id, requestedProvider)
user, err := GetUser(id, provider, app.db)
if err != nil || user == nil {
log.Printf("err: %s, user %v", err, user)
http.Error(w, "requested user not found", http.StatusNotFound)
return
}
SerializeJson(w, user)
}
func (app *Application) Logout(w http.ResponseWriter, r *http.Request) {
session, err := app.ResolveSessionFromRequest(r)
if err == nil {
_ = DeleteSession(session, app.db)
}
w.Header().Set("Set-Cookie", "pomu=deleted; Path=/; Expires=Thu, 01 Jan 1970 00:00:00 GMT")
http.Redirect(w, r, "/?successLogout", http.StatusTemporaryRedirect)
}
// ValidateOrCreateUser gets the user based on ID and provider and if they do not exist, registers them. Returns redirect URL
func ValidateOrCreateUser(id string, name string, avatarUrl string, provider string, db *sql.DB) (string, error) {
user, err := GetUser(id, provider, db)
if err != nil {
return "", err
}
var redirectUrl string
if user == nil {
redirectUrl = "/?successRegister"
} else {
redirectUrl = "/?success"
}
_, err = CreateOrUpdateUser(id, name, avatarUrl, provider, db)
if err != nil {
return "", err
}
return redirectUrl, nil
}
func GetUser(id string, provider 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 * from users where id = $1 and provider = $2 limit 1")
if err != nil {
sentry.CaptureException(err)
return nil, err
}
var user User
if err = statement.QueryRow(id, provider).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 CreateOrUpdateUser(id string, name string, avatarUrl string, provider 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(`
insert into users (id, name, avatar, provider)
values ($1, $2, $3, $4)
on conflict (id) do update set
name = $2,
avatar = $3
returning *
`)
if err != nil {
sentry.CaptureException(err)
return nil, err
}
var user User
if err = statement.QueryRow(id, name, avatarUrl, provider).Scan(&user.Id, &user.Name, &user.Avatar, &user.Provider); err != nil {
sentry.CaptureException(err)
return nil, err
}
if err = tx.Commit(); err != nil {
sentry.CaptureException(err)
return nil, err
}
return &user, nil
}