-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
469 lines (396 loc) · 12.1 KB
/
main.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"net/url"
"time"
"github.com/google/uuid"
"golang.org/x/crypto/bcrypt"
"golang.org/x/oauth2"
"golang.org/x/oauth2/gitlab"
)
type user struct {
password []byte
Name string
}
var oauth = &oauth2.Config{
ClientID: "dcd95bd060cda29d6aff622f699f1ddacf5b2e59cd7c2d28eae00990ad43dd52",
ClientSecret: "6f98b0cb2dca6dbfd0b4d7ddd82bd41427e517dbc4b15fa1a706d61b951b4745",
Endpoint: gitlab.Endpoint,
RedirectURL: "http://localhost:9090/oauth/gitlab/receive",
Scopes: []string{"openid", "email"},
}
// key is email, value is user
var db = map[string]user{}
// key is sessionid, value is email
var sessions = map[string]string{}
// key is uuid from oauth login, value is expiration time
var oauthExp = map[string]time.Time{}
// key is uid from oauth provider; value is user id, eg, email
var oauthConnections = map[string]string{}
func main() {
http.HandleFunc("/", index)
http.HandleFunc("/register", register)
http.HandleFunc("/login", login)
http.HandleFunc("/oauth/gitlab/login", oGitlabLogin)
// notice this is your "redirect" URL listed above in oauth2.Config
http.HandleFunc("/oauth/gitlab/receive", oGitlabReceive)
http.HandleFunc("/partial-register", partialRegister)
http.HandleFunc("/oauth/gitlab/register", oGitlabRegister)
http.HandleFunc("/logout", logout)
http.ListenAndServe(":9090", nil)
}
func index(w http.ResponseWriter, r *http.Request) {
c, err := r.Cookie("sessionID")
if err != nil {
c = &http.Cookie{
Name: "sessionID",
Value: "",
}
}
sID, err := parseToken(c.Value)
if err != nil {
log.Println("index parseToken", err)
}
var e string
if sID != "" {
e = sessions[sID]
}
var f string
if user, ok := db[e]; ok {
f = user.Name
}
errMsg := r.FormValue("msg")
fmt.Fprintf(w, `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<h1>IF YOU HAVE A SESSION, HERE IS YOUR NAME: %s</h1>
<h1>IF YOU HAVE A SESSION, HERE IS YOUR EMAIL: %s</h1>
<h1>IF THERE IS ANY MESSAGE FOR YOU, HERE IT IS: %s</h1>
<h1>REGISTER</h1>
<form action="/register" method="POST">
<label for="name">Name</label>
<input type="text" name="name" placeholder="Name" id="name">
<input type="email" name="e">
<input type="password" name="p">
<input type="submit">
</form>
<h1>LOG IN</h1>
<form action="/login" method="POST">
<input type="email" name="e">
<input type="password" name="p">
<input type="submit">
</form>
<h1>LOG IN WITH gitlab</h1>
<form action="/oauth/gitlab/login" method="POST">
<input type="submit" value="LOGIN WITH gitlab">
</form>
<h1>LOGOUT</h1>
<form action="/logout" method="POST">
<input type="submit" value="LOGOUT">
</form>
</body>
</html>`, f, e, errMsg)
}
func register(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
msg := url.QueryEscape("your method was not post")
http.Redirect(w, r, "/?msg="+msg, http.StatusSeeOther)
return
}
e := r.FormValue("e")
if e == "" {
msg := url.QueryEscape("your email needs to not be empty")
http.Redirect(w, r, "/?msg="+msg, http.StatusSeeOther)
return
}
p := r.FormValue("p")
if p == "" {
msg := url.QueryEscape("your email password needs to not be empty")
http.Redirect(w, r, "/?msg="+msg, http.StatusSeeOther)
return
}
f := r.FormValue("name")
if f == "" {
msg := url.QueryEscape("your name name needs to not be empty")
http.Redirect(w, r, "/?msg="+msg, http.StatusSeeOther)
return
}
if _, ok := db[e]; !ok {
bsp, err := bcrypt.GenerateFromPassword([]byte(p), bcrypt.DefaultCost)
if err != nil {
msg := "there was an internal server error - evil laugh: hahahahaha"
http.Error(w, msg, http.StatusInternalServerError)
return
}
log.Println("password", p)
log.Println("bcrypted", bsp)
db[e] = user{
password: bsp,
Name: f,
}
} else {
msg := "user already exists"
http.Error(w, msg, http.StatusInternalServerError)
return
}
http.Redirect(w, r, "/", http.StatusSeeOther)
}
func login(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
msg := url.QueryEscape("your method was not post")
http.Redirect(w, r, "/?msg="+msg, http.StatusSeeOther)
return
}
e := r.FormValue("e")
if e == "" {
msg := url.QueryEscape("your email needs to not be empty")
http.Redirect(w, r, "/?msg="+msg, http.StatusSeeOther)
return
}
p := r.FormValue("p")
if p == "" {
msg := url.QueryEscape("your email password needs to not be empty")
http.Redirect(w, r, "/?msg="+msg, http.StatusSeeOther)
return
}
if _, ok := db[e]; !ok {
msg := url.QueryEscape("your email or password didn't match")
http.Redirect(w, r, "/?msg="+msg, http.StatusSeeOther)
return
}
err := bcrypt.CompareHashAndPassword(db[e].password, []byte(p))
if err != nil {
msg := url.QueryEscape("your email or password didn't match")
http.Redirect(w, r, "/?msg="+msg, http.StatusSeeOther)
return
}
err = createSession(e, w)
if err != nil {
log.Println("couldn't createSession in login", err)
msg := url.QueryEscape("our server didn't get enough lunch and is not working 200% right now. Try bak later")
http.Redirect(w, r, "/?msg="+msg, http.StatusSeeOther)
return
}
msg := url.QueryEscape("you logged in " + e)
http.Redirect(w, r, "/?msg="+msg, http.StatusSeeOther)
}
func createSession(e string, w http.ResponseWriter) error {
sUUID := uuid.New().String()
sessions[sUUID] = e
token, err := createToken(sUUID)
if err != nil {
return fmt.Errorf("couldn't createtoken in createSession %w", err)
}
c := http.Cookie{
Name: "sessionID",
Value: token,
Path: "/",
}
http.SetCookie(w, &c)
return nil
}
func logout(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Redirect(w, r, "/", http.StatusSeeOther)
return
}
c, err := r.Cookie("sessionID")
if err != nil {
c = &http.Cookie{
Name: "sessionID",
Value: "",
}
}
sID, err := parseToken(c.Value)
if err != nil {
log.Println("index parseToken", err)
}
delete(sessions, sID)
c.MaxAge = -1
http.SetCookie(w, c)
http.Redirect(w, r, "/", http.StatusSeeOther)
}
func oGitlabLogin(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Redirect(w, r, "/", http.StatusSeeOther)
return
}
id := uuid.New().String()
oauthExp[id] = time.Now().Add(time.Hour)
// here we redirect to gitlab at the AuthURL endpoint
// adds state, scope, clientid
http.Redirect(w, r, oauth.AuthCodeURL(id), http.StatusSeeOther)
}
func oGitlabReceive(w http.ResponseWriter, r *http.Request) {
state := r.FormValue("state")
if state == "" {
msg := url.QueryEscape("state was empty in ogitlabReceive")
http.Redirect(w, r, "/?msg="+msg, http.StatusSeeOther)
return
}
// we got this code from gitlab
code := r.FormValue("code")
if code == "" {
msg := url.QueryEscape("code was empty in ogitlabReceive")
http.Redirect(w, r, "/?msg="+msg, http.StatusSeeOther)
return
}
expT := oauthExp[state]
if time.Now().After(expT) {
msg := url.QueryEscape("oauth took too long time.now.after")
http.Redirect(w, r, "/?msg="+msg, http.StatusSeeOther)
return
}
// exchange our code for a token
// this uses the client secret also
// the TokenURL is called
// we get back a token
t, err := oauth.Exchange(r.Context(), code)
if err != nil {
msg := url.QueryEscape("couldn't do oauth exchange: " + err.Error())
http.Redirect(w, r, "/?msg="+msg, http.StatusSeeOther)
return
}
ts := oauth.TokenSource(r.Context(), t)
c := oauth2.NewClient(r.Context(), ts)
resp, err := c.Get("https://gitlab.com/oauth/userinfo")
if err != nil {
msg := url.QueryEscape("couldn't get at gitlab: " + err.Error())
http.Redirect(w, r, "/?msg="+msg, http.StatusSeeOther)
return
}
defer resp.Body.Close()
// xb, _ := io.ReadAll(resp.Body)
// log.Println(string(xb))
if resp.StatusCode < 200 || resp.StatusCode > 299 {
msg := url.QueryEscape("not a 200 resp code")
http.Redirect(w, r, "/?msg="+msg, http.StatusSeeOther)
return
}
type profileResponse struct {
Email string `json:"email"`
Name string `json:"name"`
UserID string `json:"sub"`
}
var pr profileResponse
err = json.NewDecoder(resp.Body).Decode(&pr)
if err != nil {
msg := url.QueryEscape("not able to decode json response")
http.Redirect(w, r, "/?msg="+msg, http.StatusSeeOther)
return
}
// check to see if they have already registered at our site with this oauth2
// key is uid from oauth provider; value is user id, eg, email
eml, ok := oauthConnections[pr.UserID]
if !ok {
// not regiestered at our site yet with gitlab
// register at our site with gitlab
st, err := createToken(pr.UserID)
if err != nil {
log.Println("couldn't createToken in ogitlabReceive", err)
msg := url.QueryEscape("our server didn't get enough lunch and is not working 200% right now. Try bak later")
http.Redirect(w, r, "/?msg="+msg, http.StatusSeeOther)
return
}
uv := url.Values{}
uv.Add("sst", st)
uv.Add("name", pr.Name)
uv.Add("email", pr.Email)
http.Redirect(w, r, "/partial-register?"+uv.Encode(), http.StatusSeeOther)
return
}
err = createSession(eml, w)
if err != nil {
log.Println("couldn't createSession in ogitlabReceive", err)
msg := url.QueryEscape("our server didn't get enough lunch and is not working 200% right now. Try bak later")
http.Redirect(w, r, "/?msg="+msg, http.StatusSeeOther)
return
}
msg := url.QueryEscape("you logged in " + eml)
http.Redirect(w, r, "/?msg="+msg, http.StatusSeeOther)
}
func partialRegister(w http.ResponseWriter, r *http.Request) {
sst := r.FormValue("sst")
name := r.FormValue("name")
email := r.FormValue("email")
if sst == "" {
log.Println("couldn't get sst in partialRegister")
msg := url.QueryEscape("our server didn't get enough lunch and is not working 200% right now. Try bak later")
http.Redirect(w, r, "/?msg="+msg, http.StatusSeeOther)
return
}
fmt.Fprintf(w, `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<form action="/oauth/gitlab/register" method="POST">
<label for="Name">NAME</label>
<input type="text" name="name" id="Name" value="%s">
<label for="Email">EMAIL</label>
<input type="text" name="email" id="Email" value="%s">
<input type="hidden" value="%s" name="oauthID">
<input type="submit">
</form>
</body>
</html>`, name, email, sst)
}
func oGitlabRegister(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
msg := url.QueryEscape("your method was not post")
http.Redirect(w, r, "/?msg="+msg, http.StatusSeeOther)
return
}
f := r.FormValue("name")
e := r.FormValue("email")
oauthID := r.FormValue("oauthID")
if f == "" {
msg := url.QueryEscape("your name name needs to not be empty")
http.Redirect(w, r, "/?msg="+msg, http.StatusSeeOther)
return
}
if e == "" {
msg := url.QueryEscape("your email needs to not be empty")
http.Redirect(w, r, "/?msg="+msg, http.StatusSeeOther)
return
}
if oauthID == "" {
log.Println("oauthID came through as empty at ogitlabRegister")
msg := url.QueryEscape("your oauthID needs to not be empty")
http.Redirect(w, r, "/?msg="+msg, http.StatusSeeOther)
return
}
gitlabUID, err := parseToken(oauthID)
if err != nil {
log.Println("parseToken at ogitlabRegister didn't parse")
msg := url.QueryEscape("there was an issue. send us money so we can fix it.")
http.Redirect(w, r, "/?msg="+msg, http.StatusSeeOther)
return
}
db[e] = user{
Name: f,
}
// key is uid from oauth provider; value is user id, eg, email
oauthConnections[gitlabUID] = e
err = createSession(e, w)
if err != nil {
log.Println("couldn't CreateSession in ogitlabRegister", err)
msg := url.QueryEscape("our server didn't get enough lunch and is not working 200% right now. Try bak later")
http.Redirect(w, r, "/?msg="+msg, http.StatusSeeOther)
return
}
http.Redirect(w, r, "/", http.StatusSeeOther)
}