+41
-34
legit/routes/routes.go
+41
-34
legit/routes/routes.go
···
440
440
http.ServeFile(w, r, f)
441
441
}
442
442
443
-
func resolveIdent(arg string) (*identity.Identity, error) {
443
+
func resolveIdent(ctx context.Context, arg string) (*identity.Identity, error) {
444
444
id, err := syntax.ParseAtIdentifier(arg)
445
445
if err != nil {
446
446
return nil, err
447
447
}
448
448
449
-
ctx := context.Background()
450
449
dir := identity.DefaultDirectory()
451
450
return dir.Lookup(ctx, *id)
452
451
}
453
452
454
453
func (h *Handle) Login(w http.ResponseWriter, r *http.Request) {
455
-
ctx := context.Background()
456
-
username := r.FormValue("username")
457
-
appPassword := r.FormValue("app_password")
454
+
switch r.Method {
455
+
case http.MethodGet:
456
+
if err := h.t.ExecuteTemplate(w, "user/login", nil); err != nil {
457
+
log.Println(err)
458
+
return
459
+
}
460
+
case http.MethodPost:
461
+
ctx := r.Context()
462
+
username := r.FormValue("username")
463
+
appPassword := r.FormValue("app_password")
458
464
459
-
resolved, err := resolveIdent(username)
460
-
if err != nil {
461
-
http.Error(w, "invalid `handle`", http.StatusBadRequest)
462
-
return
463
-
}
465
+
resolved, err := resolveIdent(ctx, username)
466
+
if err != nil {
467
+
http.Error(w, "invalid `handle`", http.StatusBadRequest)
468
+
return
469
+
}
470
+
471
+
pdsUrl := resolved.PDSEndpoint()
472
+
client := xrpc.Client{
473
+
Host: pdsUrl,
474
+
}
464
475
465
-
pdsUrl := resolved.PDSEndpoint()
466
-
client := xrpc.Client{
467
-
Host: pdsUrl,
468
-
}
476
+
atSession, err := comatproto.ServerCreateSession(ctx, &client, &comatproto.ServerCreateSession_Input{
477
+
Identifier: resolved.DID.String(),
478
+
Password: appPassword,
479
+
})
469
480
470
-
atSession, err := comatproto.ServerCreateSession(ctx, &client, &comatproto.ServerCreateSession_Input{
471
-
Identifier: resolved.DID.String(),
472
-
Password: appPassword,
473
-
})
481
+
clientSession, _ := h.s.Get(r, "bild-session")
482
+
clientSession.Values["handle"] = atSession.Handle
483
+
clientSession.Values["did"] = atSession.Did
484
+
clientSession.Values["accessJwt"] = atSession.AccessJwt
485
+
clientSession.Values["refreshJwt"] = atSession.RefreshJwt
486
+
clientSession.Values["expiry"] = time.Now().Add(time.Hour).String()
487
+
clientSession.Values["pds"] = pdsUrl
488
+
clientSession.Values["authenticated"] = true
474
489
475
-
clientSession, _ := h.s.Get(r, "bild-session")
476
-
clientSession.Values["handle"] = atSession.Handle
477
-
clientSession.Values["did"] = atSession.Did
478
-
clientSession.Values["accessJwt"] = atSession.AccessJwt
479
-
clientSession.Values["refreshJwt"] = atSession.RefreshJwt
480
-
clientSession.Values["expiry"] = time.Now().Add(time.Hour).String()
481
-
clientSession.Values["pds"] = pdsUrl
482
-
clientSession.Values["authenticated"] = true
490
+
err = clientSession.Save(r, w)
483
491
484
-
err = clientSession.Save(r, w)
492
+
if err != nil {
493
+
log.Printf("failed to store session for did: %s\n", atSession.Did)
494
+
log.Println(err)
495
+
return
496
+
}
485
497
486
-
if err != nil {
487
-
log.Printf("failed to store session for did: %s\n", atSession.Did)
488
-
log.Println(err)
489
-
return
498
+
log.Printf("successfully saved session for %s (%s)", atSession.Handle, atSession.Did)
499
+
http.Redirect(w, r, "/@"+atSession.Handle, 302)
490
500
}
491
-
492
-
log.Printf("successfully saved session for %s (%s)", atSession.Handle, atSession.Did)
493
-
http.Redirect(w, r, "/", 302)
494
501
}
495
502
496
503
func (h *Handle) Keys(w http.ResponseWriter, r *http.Request) {