From 8f00287aa27a120185098f3b03d467ce9856f856 Mon Sep 17 00:00:00 2001 From: tiendc Date: Sat, 5 Dec 2026 14:52:05 +0700 Subject: [PATCH] Let the phones sign in: a code may go to the app's own scheme The desktop app gets its code on a loopback port. The phones cannot listen on one while the browser sheet (iOS) or Custom Tab (Android) is open, so they come back to the app's own scheme instead (RFC 8252, 7.1). The server now takes exactly com.hivepaas.shellwing://server besides a loopback port. Another app that claimed the scheme could catch a code, but not use it without the verifier, which stays on the phone. The approval page shows a phone for ios and android. Co-Authored-By: Claude Opus 5.5 --- docs/openapi/swagger.json | 2 +- hperrors/errors_sync.go | 3 ++- interface/api/handler/sessionhandler/device.go | 2 +- usecase/sessionuc/device.go | 17 ++++++++++++----- usecase/sessionuc/device_test.go | 16 ++++++++++++++-- .../route/connect-device.route.com.tsx | 6 ++++-- 6 files changed, 34 insertions(+), 12 deletions(-) diff --git a/docs/openapi/swagger.json b/docs/openapi/swagger.json index 60d82d0..4548f0b 100644 --- a/docs/openapi/swagger.json +++ b/docs/openapi/swagger.json @@ -541,7 +541,7 @@ }, "/devices/authorize" : { "post" : { - "description" : "The person approves in their browser. The answer is where the browser goes next: the app's loopback\naddress, with a code.", + "description" : "The person approves in their browser. The answer is where the browser goes next: the app's loopback\naddress (the desktop) or its own scheme (the phones), with a code.", "operationId" : "authorizeDevice", "requestBody" : { "content" : { diff --git a/hperrors/errors_sync.go b/hperrors/errors_sync.go index 5705832..190f234 100644 --- a/hperrors/errors_sync.go +++ b/hperrors/errors_sync.go @@ -14,7 +14,8 @@ var ( // ErrSyncSpaceFull refuses a write that would take the space past its limit. ErrSyncSpaceFull = NewErr(ErrConflict, "ERR_SYNC_SPACE_FULL") - // ErrDeviceRedirectInvalid is an address that is no app's loopback listener: a code never goes anywhere else. + // ErrDeviceRedirectInvalid is an address that is neither an app's loopback listener nor the phones' redirect: a + // code never goes anywhere else. ErrDeviceRedirectInvalid = NewErr(ErrArgumentInvalid, "ERR_DEVICE_REDIRECT_INVALID") // ErrDeviceCodeInvalid is a code that is unknown, used already, too old, or not this app's. An app signs in again. ErrDeviceCodeInvalid = NewErr(ErrArgumentInvalid, "ERR_DEVICE_CODE_INVALID") diff --git a/interface/api/handler/sessionhandler/device.go b/interface/api/handler/sessionhandler/device.go index 6523821..fd4731c 100644 --- a/interface/api/handler/sessionhandler/device.go +++ b/interface/api/handler/sessionhandler/device.go @@ -12,7 +12,7 @@ import ( // AuthorizeDevice lets an app on the person's device sign in // @Summary Lets an app on the person's device sign in // @Description The person approves in their browser. The answer is where the browser goes next: the app's loopback -// @Description address, with a code. +// @Description address (the desktop) or its own scheme (the phones), with a code. // @Tags Sessions // @Produce json // @Id authorizeDevice diff --git a/usecase/sessionuc/device.go b/usecase/sessionuc/device.go index 426b5fd..db1b664 100644 --- a/usecase/sessionuc/device.go +++ b/usecase/sessionuc/device.go @@ -29,12 +29,16 @@ import ( const ( deviceSecretLen = 32 deviceCallback = "/callback" + // The phones' redirect: the app's own scheme (RFC 8252, 7.1), which the browser sheet (iOS) or tab (Android) + // comes back to. Exactly this: the app is the only one that asks for it. + deviceAppRedirect = "com.hivepaas.shellwing://server" deviceAuthCodeTTL = 2 * time.Minute // the app is waiting ) // How an app signs in (RFC 8252): it opens the browser at the server, whatever sign-in the server has happens there, -// and the person approves the device. The server answers with a code, which goes to the app's loopback address, and -// the app exchanges it for tokens with the verifier only it has (RFC 7636). +// and the person approves the device. The server answers with a code, which goes to the app's loopback address (the +// desktop) or its own scheme (the phones), and the app exchanges it for tokens with the verifier only it has +// (RFC 7636). Another app that claimed the scheme could catch a code, but not use it without the verifier. // // Its tokens reach sync and nothing else: an admin's device cannot manage the server. @@ -49,10 +53,13 @@ func hashSecret(secret string) string { return hex.EncodeToString(sum[:]) } -// validDeviceRedirect says whether the address is an app's loopback listener, the only place a code may go. Anything -// else would let a web page have the browser hand it the code. Literal addresses, as RFC 8252 asks: a name could -// resolve elsewhere. +// validDeviceRedirect says whether the address is an app's loopback listener or the phones' redirect, the only places +// a code may go. Anything else would let a web page have the browser hand it the code. Literal addresses, as RFC 8252 +// asks: a name could resolve elsewhere. func validDeviceRedirect(raw string) bool { + if raw == deviceAppRedirect { + return true + } parsed, err := url.Parse(raw) if err != nil || parsed.Scheme != "http" || parsed.Path != deviceCallback || parsed.User != nil || parsed.RawQuery != "" || parsed.Fragment != "" { diff --git a/usecase/sessionuc/device_test.go b/usecase/sessionuc/device_test.go index f537a16..0c73de4 100644 --- a/usecase/sessionuc/device_test.go +++ b/usecase/sessionuc/device_test.go @@ -8,9 +8,14 @@ import ( "github.com/stretchr/testify/assert" ) -// A code goes nowhere but an app's own loopback listener, or a web page could have the browser hand it over. +// A code goes nowhere but an app's own loopback listener or the phones' redirect, or a web page could have the browser +// hand it over. func TestValidDeviceRedirect(t *testing.T) { - for _, good := range []string{"http://127.0.0.1:53124/callback", "http://[::1]:53124/callback"} { + for _, good := range []string{ + "http://127.0.0.1:53124/callback", + "http://[::1]:53124/callback", + "com.hivepaas.shellwing://server", + } { assert.True(t, validDeviceRedirect(good), good) } for _, bad := range []string{ @@ -30,6 +35,13 @@ func TestValidDeviceRedirect(t *testing.T) { "http://10.0.0.1:53124/callback", "javascript:alert(1)", "http://127.0.0.1:53124//evil.example.com/callback", + "com.hivepaas.shellwing://server/", + "com.hivepaas.shellwing://server?x=1", + "com.hivepaas.shellwing://auth", + "com.hivepaas.shellwing://evil.example.com", + "com.hivepaas.shellwing.evil://server", + "COM.HIVEPAAS.SHELLWING://server", + "evil://server", } { assert.False(t, validDeviceRedirect(bad), bad) } diff --git a/web/src/application/authentication/routes/connect-device/route/connect-device.route.com.tsx b/web/src/application/authentication/routes/connect-device/route/connect-device.route.com.tsx index 5c4e40f..b07e9d3 100644 --- a/web/src/application/authentication/routes/connect-device/route/connect-device.route.com.tsx +++ b/web/src/application/authentication/routes/connect-device/route/connect-device.route.com.tsx @@ -1,4 +1,4 @@ -import { Laptop, ShieldAlert } from "lucide-react"; +import { Laptop, ShieldAlert, Smartphone } from "lucide-react"; import { useSearchParams } from "react-router"; import { LogoIcon } from "@assets/icons"; @@ -35,6 +35,8 @@ export function ConnectDeviceRoute() { const platform = params.get("platform") ?? ""; const complete = redirectUri !== "" && codeChallenge !== "" && state !== "" && deviceName !== ""; + // The phones come back to the app's own scheme; the desktop to a port of its own. + const DeviceIcon = platform === "ios" || platform === "android" ? Smartphone : Laptop; return ( @@ -50,7 +52,7 @@ export function ConnectDeviceRoute() { {complete ? (
- +
Allow this device to sync?