refactor common util StringifySSBURI

This commit is contained in:
Andre Staltz 2021-11-12 13:34:35 +02:00 committed by André Staltz
parent 2d722f2098
commit 3c6831d39a
4 changed files with 25 additions and 42 deletions

View File

@ -14,12 +14,12 @@ import (
"net/url"
"github.com/gorilla/mux"
ua "github.com/mileusna/useragent"
"go.mindeco.de/http/render"
"github.com/ssb-ngi-pointer/go-ssb-room/v2/internal/aliases"
"github.com/ssb-ngi-pointer/go-ssb-room/v2/internal/network"
"github.com/ssb-ngi-pointer/go-ssb-room/v2/roomdb"
"github.com/ssb-ngi-pointer/go-ssb-room/v2/web"
)
// aliasHandler implements the public resolve endpoint for HTML and JSON requests.
@ -165,23 +165,11 @@ func (html aliasHTMLResponder) SendConfirmation(alias roomdb.Alias) {
RawQuery: queryParams.Encode(),
}
// Special treatment for Android Chrome for issue #135
// https://github.com/ssb-ngi-pointer/go-ssb-room/issues/135
browser := ua.Parse(html.req.UserAgent())
if browser.IsAndroid() && browser.IsChrome() {
ssbURI = url.URL{
Scheme: "intent",
Opaque: "//experimental",
RawQuery: queryParams.Encode(),
Fragment: "Intent;scheme=ssb;end;",
}
}
err := html.renderer.Render(html.rw, html.req, "alias.tmpl", http.StatusOK, struct {
Alias roomdb.Alias
SSBURI template.URL
}{alias, template.URL(ssbURI.String())})
}{alias, template.URL(web.StringifySSBURI(&ssbURI, html.req.UserAgent()))})
if err != nil {
log.Println("alias-resolve render errr:", err)
}

View File

@ -20,7 +20,6 @@ import (
"github.com/gorilla/mux"
"github.com/gorilla/sessions"
ua "github.com/mileusna/useragent"
"github.com/skip2/go-qrcode"
"go.cryptoscope.co/muxrpc/v2"
"go.mindeco.de/http/render"
@ -373,18 +372,6 @@ func (h WithSSBHandler) serverInitiated(sc string, userAgent string) (templateDa
startAuthURI.Opaque = "experimental"
startAuthURI.RawQuery = queryParams.Encode()
// Special treatment for Android Chrome for issue #135
// https://github.com/ssb-ngi-pointer/go-ssb-room/issues/135
browser := ua.Parse(userAgent)
if browser.IsAndroid() && browser.IsChrome() {
startAuthURI = url.URL{
Scheme: "intent",
Opaque: "//experimental",
RawQuery: queryParams.Encode(),
Fragment: "Intent;scheme=ssb;end;",
}
}
var qrURI string
if !isSolvingRemotely {
urlTo := web.NewURLTo(router.Auth(h.router), h.netInfo)
@ -416,7 +403,7 @@ func (h WithSSBHandler) serverInitiated(sc string, userAgent string) (templateDa
// template.URL signals the template engine that those aren't fishy and from a trusted source
data := templateData{
SSBURI: template.URL(startAuthURI.String()),
SSBURI: template.URL(web.StringifySSBURI(&startAuthURI, userAgent)),
QRCodeURI: template.URL(qrURI),
IsSolvingRemotely: isSolvingRemotely,

View File

@ -15,7 +15,6 @@ import (
"net/url"
"github.com/gorilla/csrf"
ua "github.com/mileusna/useragent"
"github.com/skip2/go-qrcode"
"go.mindeco.de/http/render"
"go.mindeco.de/log/level"
@ -54,19 +53,7 @@ func (h inviteHandler) buildJoinRoomURI(token string, userAgent string) template
RawQuery: queryVals.Encode(),
}
// Special treatment for Android Chrome for issue #135
// https://github.com/ssb-ngi-pointer/go-ssb-room/issues/135
browser := ua.Parse(userAgent)
if browser.IsAndroid() && browser.IsChrome() {
joinRoomURI = url.URL{
Scheme: "intent",
Opaque: "//experimental",
RawQuery: queryVals.Encode(),
Fragment: "Intent;scheme=ssb;end;",
}
}
return template.URL(joinRoomURI.String())
return template.URL(web.StringifySSBURI(&joinRoomURI, userAgent))
}
// switch between JSON and HTML responses

View File

@ -18,6 +18,7 @@ import (
"github.com/dustin/go-humanize"
"github.com/gorilla/mux"
"github.com/gorilla/securecookie"
ua "github.com/mileusna/useragent"
"go.mindeco.de/log/level"
"go.mindeco.de/logging"
@ -194,3 +195,23 @@ func LoadOrCreateCSRFSecret(repo repo.Interface) ([]byte, error) {
return secret, nil
}
// Transforms the SSB URI into a string suitable for an <a href> and
// takes into account quirks from some browsers.
func StringifySSBURI(uri *url.URL, userAgent string) string {
browser := ua.Parse(userAgent)
// Special treatment for Android Chrome for issue #135
// https://github.com/ssb-ngi-pointer/go-ssb-room/issues/135
if browser.IsAndroid() && browser.IsChrome() {
href := url.URL{
Scheme: "intent",
Opaque: fmt.Sprintf("//%v", uri.Opaque),
RawQuery: uri.RawQuery,
Fragment: fmt.Sprintf("Intent;scheme=%v;end;", uri.Scheme),
}
return href.String()
} else {
return uri.String()
}
}