Monorepo for Tangled tangled.org

knotserver: redirect handle-path to did-path #705

merged opened by boltless.me targeting master from push-xpolynpvotzt

fixing issue reported on discord: https://discord.com/channels/1361963801993285692/1361966362653757450/1430609124114301060

knotserver: redirect handle-path to did-path

this will allow handle based url when cloning from knot over https
usually appview will resolve this before redirecting to the knot

this implementation just redirects to resolved path instead of passing
the entire identity for performance. As we won't want to resolve same
identity twice when proxied by appview.

Signed-off-by: Seongmin Lee <git@boltless.me>
Labels
bugfix
assignee

None yet.

Participants 2
AT URI
at://did:plc:xasnlahkri4ewmbuzly2rlc5/sh.tangled.repo.pull/3m3sj67z25x22
+25
Diff #2
+25
knotserver/router.go
··· 5 5 "fmt" 6 6 "log/slog" 7 7 "net/http" 8 + "strings" 8 9 9 10 "github.com/go-chi/chi/v5" 10 11 "tangled.org/core/idresolver" ··· 79 80 }) 80 81 81 82 r.Route("/{did}", func(r chi.Router) { 83 + r.Use(h.resolveDidRedirect) 82 84 r.Route("/{name}", func(r chi.Router) { 83 85 // routes for git operations 84 86 r.Get("/info/refs", h.InfoRefs) ··· 115 117 return xrpc.Router() 116 118 } 117 119 120 + func (h *Knot) resolveDidRedirect(next http.Handler) http.Handler { 121 + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 122 + didOrHandle := chi.URLParam(r, "did") 123 + if strings.HasPrefix(didOrHandle, "did:") { 124 + next.ServeHTTP(w, r) 125 + return 126 + } 127 + 128 + trimmed := strings.TrimPrefix(didOrHandle, "@") 129 + id, err := h.resolver.ResolveIdent(r.Context(), trimmed) 130 + if err != nil { 131 + // invalid did or handle 132 + h.l.Error("failed to resolve did/handle", "handle", trimmed, "err", err) 133 + http.Error(w, fmt.Sprintf("failed to resolve did/handle: %s", trimmed), http.StatusInternalServerError) 134 + return 135 + } 136 + 137 + suffix := strings.TrimPrefix(r.URL.Path, "/"+didOrHandle) 138 + newPath := fmt.Sprintf("/%s/%s?%s", id.DID.String(), suffix, r.URL.RawQuery) 139 + http.Redirect(w, r, newPath, http.StatusTemporaryRedirect) 140 + }) 141 + } 142 + 118 143 func (h *Knot) configureOwner() error { 119 144 cfgOwner := h.c.Server.Owner 120 145

History

3 rounds 6 comments
sign up or login to add to the discussion
1 commit
expand
knotserver: redirect handle-path to did-path
3/3 success
expand
expand 3 comments

will give this a test locally; but that being said, the knotserver need not provide the full API layer that the appview provides. but IMO this one is pretty nice to have (maybe we want all the rerouting rules in that case?).

oops wrong PR, can't remove the comment yet.

pull request successfully merged
1 commit
expand
knotserver: redirect handle-path to did-path
3/3 failed
expand
expand 1 comment

ready for review! tested https-clone, ssh-clone, ssh-push all still working fine.

1 commit
expand
knotserver: redirect handle-path to did-path
expand 2 comments

...I hate markdown

ah wait, this doesn't work