Monorepo for Tangled
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

at fix/knot-version-string 158 lines 4.6 kB view raw
1package state 2 3import ( 4 "errors" 5 "fmt" 6 "net/http" 7 "strings" 8 "time" 9 10 comatproto "github.com/bluesky-social/indigo/api/atproto" 11 "github.com/bluesky-social/indigo/xrpc" 12 "tangled.org/core/appview/pages" 13) 14 15func (s *State) Login(w http.ResponseWriter, r *http.Request) { 16 l := s.logger.With("handler", "Login") 17 18 switch r.Method { 19 case http.MethodGet: 20 returnURL := r.URL.Query().Get("return_url") 21 errorCode := r.URL.Query().Get("error") 22 addAccount := r.URL.Query().Get("mode") == "add_account" 23 24 registry := s.oauth.GetAccounts(r) 25 s.pages.Login(w, pages.LoginParams{ 26 ReturnUrl: returnURL, 27 ErrorCode: errorCode, 28 AddAccount: addAccount, 29 Accounts: registry.Accounts, 30 }) 31 case http.MethodPost: 32 handle := r.FormValue("handle") 33 returnURL := r.FormValue("return_url") 34 35 // remove spaces around the handle, handles can't have spaces around them 36 handle = strings.TrimSpace(handle) 37 38 // when users copy their handle from bsky.app, it tends to have these characters around it: 39 // 40 // @nelind.dk: 41 // \u202a ensures that the handle is always rendered left to right and 42 // \u202c reverts that so the rest of the page renders however it should 43 handle = strings.TrimPrefix(handle, "\u202a") 44 handle = strings.TrimSuffix(handle, "\u202c") 45 46 // `@` is harmless 47 handle = strings.TrimPrefix(handle, "@") 48 49 // basic handle validation 50 if !strings.Contains(handle, ".") { 51 l.Error("invalid handle format", "raw", handle) 52 s.pages.Notice( 53 w, 54 "login-msg", 55 fmt.Sprintf("\"%s\" is an invalid handle. Did you mean %s.bsky.social or %s.tngl.sh?", handle, handle, handle), 56 ) 57 return 58 } 59 60 ident, err := s.idResolver.ResolveAtIdentifier(r.Context(), handle) 61 if err != nil { 62 l.Warn("handle resolution failed", "handle", handle, "err", err) 63 s.pages.Notice(w, "login-msg", fmt.Sprintf("Could not resolve handle \"%s\". The account may not exist.", handle)) 64 return 65 } 66 67 pdsEndpoint := ident.PDSEndpoint() 68 if pdsEndpoint == "" { 69 s.pages.Notice(w, "login-msg", fmt.Sprintf("No PDS found for \"%s\".", handle)) 70 return 71 } 72 73 pdsClient := &xrpc.Client{Host: pdsEndpoint, Client: &http.Client{Timeout: 5 * time.Second}} 74 _, err = comatproto.RepoDescribeRepo(r.Context(), pdsClient, ident.DID.String()) 75 if err != nil { 76 var xrpcErr *xrpc.Error 77 var xrpcBody *xrpc.XRPCError 78 isDeactivated := errors.As(err, &xrpcErr) && 79 errors.As(xrpcErr.Wrapped, &xrpcBody) && 80 xrpcBody.ErrStr == "RepoDeactivated" 81 82 if !isDeactivated { 83 l.Warn("describeRepo failed", "handle", handle, "did", ident.DID, "pds", pdsEndpoint, "err", err) 84 s.pages.Notice(w, "login-msg", fmt.Sprintf("Account \"%s\" is no longer available.", handle)) 85 return 86 } 87 } 88 89 if err := s.oauth.SetAuthReturn(w, r, sanitizeReturnURL(returnURL)); err != nil { 90 l.Error("failed to set auth return", "err", err) 91 } 92 93 redirectURL, err := s.oauth.ClientApp.StartAuthFlow(r.Context(), ident.DID.String()) 94 if err != nil { 95 l.Error("failed to start auth", "err", err) 96 s.pages.Notice( 97 w, 98 "login-msg", 99 fmt.Sprintf("Failed to start auth flow: %v", err), 100 ) 101 return 102 } 103 104 s.pages.HxRedirect(w, redirectURL) 105 } 106} 107 108// sanitizeReturnURL ensures the return URL is a relative path on the same 109// origin. Anything else — absolute URLs, protocol-relative URLs — is replaced 110// with "/" to prevent open redirect after OAuth login. 111func sanitizeReturnURL(s string) string { 112 if strings.HasPrefix(s, "/") && !strings.HasPrefix(s, "//") { 113 return s 114 } 115 return "/" 116} 117 118func (s *State) Logout(w http.ResponseWriter, r *http.Request) { 119 l := s.logger.With("handler", "Logout") 120 121 currentUser := s.oauth.GetMultiAccountUser(r) 122 if currentUser == nil { 123 s.pages.HxRedirect(w, "/login") 124 return 125 } 126 127 currentDid := currentUser.Did 128 129 var remainingAccounts []string 130 for _, acc := range currentUser.Accounts { 131 if acc.Did != currentDid { 132 remainingAccounts = append(remainingAccounts, acc.Did) 133 } 134 } 135 136 if err := s.oauth.RemoveAccount(w, r, currentDid); err != nil { 137 l.Error("failed to remove account from registry", "err", err) 138 } 139 140 if err := s.oauth.DeleteSession(w, r); err != nil { 141 l.Error("failed to delete session", "err", err) 142 } 143 144 if len(remainingAccounts) > 0 { 145 nextDid := remainingAccounts[0] 146 if err := s.oauth.SwitchAccount(w, r, nextDid); err != nil { 147 l.Error("failed to switch to next account", "err", err) 148 s.pages.HxRedirect(w, "/login") 149 return 150 } 151 l.Info("switched to next account after logout", "did", nextDid) 152 s.pages.HxRefresh(w) 153 return 154 } 155 156 l.Info("logged out last account") 157 s.pages.HxRedirect(w, "/login") 158}