this repo has no description

clean up context

Akshay 9bf23795 3fb5dcad

Changed files
+41 -34
legit
routes
+41 -34
legit/routes/routes.go
··· 440 http.ServeFile(w, r, f) 441 } 442 443 - func resolveIdent(arg string) (*identity.Identity, error) { 444 id, err := syntax.ParseAtIdentifier(arg) 445 if err != nil { 446 return nil, err 447 } 448 449 - ctx := context.Background() 450 dir := identity.DefaultDirectory() 451 return dir.Lookup(ctx, *id) 452 } 453 454 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") 458 459 - resolved, err := resolveIdent(username) 460 - if err != nil { 461 - http.Error(w, "invalid `handle`", http.StatusBadRequest) 462 - return 463 - } 464 465 - pdsUrl := resolved.PDSEndpoint() 466 - client := xrpc.Client{ 467 - Host: pdsUrl, 468 - } 469 470 - atSession, err := comatproto.ServerCreateSession(ctx, &client, &comatproto.ServerCreateSession_Input{ 471 - Identifier: resolved.DID.String(), 472 - Password: appPassword, 473 - }) 474 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 483 484 - err = clientSession.Save(r, w) 485 486 - if err != nil { 487 - log.Printf("failed to store session for did: %s\n", atSession.Did) 488 - log.Println(err) 489 - return 490 } 491 - 492 - log.Printf("successfully saved session for %s (%s)", atSession.Handle, atSession.Did) 493 - http.Redirect(w, r, "/", 302) 494 } 495 496 func (h *Handle) Keys(w http.ResponseWriter, r *http.Request) {
··· 440 http.ServeFile(w, r, f) 441 } 442 443 + func resolveIdent(ctx context.Context, arg string) (*identity.Identity, error) { 444 id, err := syntax.ParseAtIdentifier(arg) 445 if err != nil { 446 return nil, err 447 } 448 449 dir := identity.DefaultDirectory() 450 return dir.Lookup(ctx, *id) 451 } 452 453 func (h *Handle) Login(w http.ResponseWriter, r *http.Request) { 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") 464 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 + } 475 476 + atSession, err := comatproto.ServerCreateSession(ctx, &client, &comatproto.ServerCreateSession_Input{ 477 + Identifier: resolved.DID.String(), 478 + Password: appPassword, 479 + }) 480 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 489 490 + err = clientSession.Save(r, w) 491 492 + if err != nil { 493 + log.Printf("failed to store session for did: %s\n", atSession.Did) 494 + log.Println(err) 495 + return 496 + } 497 498 + log.Printf("successfully saved session for %s (%s)", atSession.Handle, atSession.Did) 499 + http.Redirect(w, r, "/@"+atSession.Handle, 302) 500 } 501 } 502 503 func (h *Handle) Keys(w http.ResponseWriter, r *http.Request) {