tuiter 2006
1package main
2
3import (
4 "encoding/json"
5 "net/http"
6
7 "github.com/bluesky-social/indigo/atproto/syntax"
8)
9
10func handleLogin(w http.ResponseWriter, r *http.Request) {
11 ctx := r.Context()
12 identifier := r.FormValue("identifier")
13 if identifier == "" {
14 http.Error(w, "identifier is required", http.StatusBadRequest)
15 return
16 }
17 redirectURL, err := oauthApp.StartAuthFlow(ctx, identifier)
18 if err != nil {
19 http.Error(w, err.Error(), http.StatusInternalServerError)
20 return
21 }
22 http.Redirect(w, r, redirectURL, http.StatusFound)
23}
24
25func handleLogout(w http.ResponseWriter, r *http.Request) {
26 session, _ := store.Get(r, sessionName)
27
28 didStr, ok := session.Values["did"].(string)
29 if ok {
30 did, err := syntax.ParseDID(didStr)
31 if err == nil {
32 sessionID, ok := session.Values["session_id"].(string)
33 if ok {
34 oauthApp.Store.DeleteSession(r.Context(), did, sessionID)
35 }
36 }
37 }
38
39 session.Values["did"] = nil
40 session.Values["session_id"] = nil
41 session.Save(r, w)
42 http.Redirect(w, r, "/signin", http.StatusFound)
43}
44
45func handleOAuthCallback(w http.ResponseWriter, r *http.Request) {
46 ctx := r.Context()
47
48 sessData, err := oauthApp.ProcessCallback(ctx, r.URL.Query())
49 if err != nil {
50 http.Error(w, err.Error(), http.StatusInternalServerError)
51 return
52 }
53
54 session, _ := store.Get(r, sessionName)
55 session.Values["did"] = sessData.AccountDID.String()
56 session.Values["session_id"] = sessData.SessionID
57 err = session.Save(r, w)
58 if err != nil {
59 http.Error(w, err.Error(), http.StatusInternalServerError)
60 return
61 }
62
63 http.Redirect(w, r, "/timeline", http.StatusFound)
64}
65
66func handleClientMetadata(w http.ResponseWriter, r *http.Request) {
67 doc := oauthApp.Config.ClientMetadata()
68 w.Header().Set("Content-Type", "application/json")
69 if err := json.NewEncoder(w).Encode(doc); err != nil {
70 http.Error(w, err.Error(), http.StatusInternalServerError)
71 return
72 }
73}