Mirror of @tangled.org/core. Running on a Raspberry Pi Zero 2 (Please be gentle).
0
fork

Configure Feed

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

appview/oauth: return to original page after login-block

when attempting to do an authorized request while logged-out, the auth
middleware boots the user to the login page. this page now keeps track
of the 'Referer' in the oauth request. once the oauth callback is
complete, the user is sent back to the page they came from.

Signed-off-by: oppiliappan <me@oppi.li>

authored by

oppiliappan and committed by
Tangled
e503066a e807c571

+26 -6
+1
appview/cache/session/store.go
··· 31 31 PkceVerifier string 32 32 DpopAuthserverNonce string 33 33 DpopPrivateJwk string 34 + ReturnUrl string 34 35 } 35 36 36 37 type SessionStore struct {
+10 -2
appview/middleware/middleware.go
··· 5 5 "fmt" 6 6 "log" 7 7 "net/http" 8 + "net/url" 8 9 "slices" 9 10 "strconv" 10 11 "strings" ··· 47 46 func AuthMiddleware(a *oauth.OAuth) middlewareFunc { 48 47 return func(next http.Handler) http.Handler { 49 48 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 49 + returnURL := "/" 50 + if u, err := url.Parse(r.Header.Get("Referer")); err == nil { 51 + returnURL = u.RequestURI() 52 + } 53 + 54 + loginURL := fmt.Sprintf("/login?return_url=%s", url.QueryEscape(returnURL)) 55 + 50 56 redirectFunc := func(w http.ResponseWriter, r *http.Request) { 51 - http.Redirect(w, r, "/login", http.StatusTemporaryRedirect) 57 + http.Redirect(w, r, loginURL, http.StatusTemporaryRedirect) 52 58 } 53 59 if r.Header.Get("HX-Request") == "true" { 54 60 redirectFunc = func(w http.ResponseWriter, _ *http.Request) { 55 - w.Header().Set("HX-Redirect", "/login") 61 + w.Header().Set("HX-Redirect", loginURL) 56 62 w.WriteHeader(http.StatusOK) 57 63 } 58 64 }
+11 -2
appview/oauth/handler/handler.go
··· 109 109 func (o *OAuthHandler) login(w http.ResponseWriter, r *http.Request) { 110 110 switch r.Method { 111 111 case http.MethodGet: 112 - o.pages.Login(w, pages.LoginParams{}) 112 + returnURL := r.URL.Query().Get("return_url") 113 + o.pages.Login(w, pages.LoginParams{ 114 + ReturnUrl: returnURL, 115 + }) 113 116 case http.MethodPost: 114 117 handle := r.FormValue("handle") 115 118 ··· 197 194 DpopAuthserverNonce: parResp.DpopAuthserverNonce, 198 195 DpopPrivateJwk: string(dpopKeyJson), 199 196 State: parResp.State, 197 + ReturnUrl: r.FormValue("return_url"), 200 198 }) 201 199 if err != nil { 202 200 log.Println("failed to save oauth request:", err) ··· 315 311 } 316 312 } 317 313 318 - http.Redirect(w, r, "/", http.StatusFound) 314 + returnUrl := oauthRequest.ReturnUrl 315 + if returnUrl == "" { 316 + returnUrl = "/" 317 + } 318 + 319 + http.Redirect(w, r, returnUrl, http.StatusFound) 319 320 } 320 321 321 322 func (o *OAuthHandler) logout(w http.ResponseWriter, r *http.Request) {
+2 -2
appview/oauth/oauth.go
··· 103 103 if err != nil { 104 104 return nil, false, fmt.Errorf("error parsing expiry time: %w", err) 105 105 } 106 - if expiry.Sub(time.Now()) <= 5*time.Minute { 106 + if time.Until(expiry) <= 5*time.Minute { 107 107 privateJwk, err := helpers.ParseJWKFromBytes([]byte(session.DpopPrivateJwk)) 108 108 if err != nil { 109 109 return nil, false, err ··· 315 315 redirectURIs := makeRedirectURIs(clientURI) 316 316 317 317 if o.config.Core.Dev { 318 - clientURI = fmt.Sprintf("http://127.0.0.1:3000") 318 + clientURI = "http://127.0.0.1:3000" 319 319 redirectURIs = makeRedirectURIs(clientURI) 320 320 321 321 query := url.Values{}
+1
appview/pages/pages.go
··· 261 261 } 262 262 263 263 type LoginParams struct { 264 + ReturnUrl string 264 265 } 265 266 266 267 func (p *Pages) Login(w io.Writer, params LoginParams) error {
+1
appview/pages/templates/user/login.html
··· 41 41 your Tangled (<code>.tngl.sh</code>) or <a href="https://bsky.app">Bluesky</a> (<code>.bsky.social</code>) account. 42 42 </span> 43 43 </div> 44 + <input type="hidden" name="return_url" value="{{ .ReturnUrl }}"> 44 45 45 46 <button 46 47 class="btn w-full my-2 mt-6 text-base "