diff --git a/api/swagger.json b/api/swagger.json index b3cbd12..9b25481 100644 --- a/api/swagger.json +++ b/api/swagger.json @@ -535,13 +535,12 @@ }, "/redirect": { "get": { - "description": "Receives authorization code and state from the OAuth2 provider,\nthen redirects the browser back to the desktop client's local HTTP listener.", + "description": "Receives the authorization response from the OAuth2 provider,\nthen redirects the browser back to the desktop client's local HTTP listener.\nIf the user cancels or denies consent, the provider sends error (e.g. access_denied)\nand state instead of code; these are forwarded to the client so it can fail immediately.", "parameters": [ { - "description": "Authorization code from OAuth2 provider", + "description": "Authorization code from OAuth2 provider (required unless error is present)", "in": "query", "name": "code", - "required": true, "schema": { "type": "string" } @@ -554,6 +553,22 @@ "schema": { "type": "string" } + }, + { + "description": "OAuth2 error code from provider, e.g. access_denied when consent is cancelled", + "in": "query", + "name": "error", + "schema": { + "type": "string" + } + }, + { + "description": "Human-readable error description from provider", + "in": "query", + "name": "error_description", + "schema": { + "type": "string" + } } ], "responses": { diff --git a/internal/app/server_test.go b/internal/app/server_test.go index a2136cc..288e231 100644 --- a/internal/app/server_test.go +++ b/internal/app/server_test.go @@ -217,6 +217,66 @@ func TestMWLegacyHandler_Redirect(t *testing.T) { r.Contains(w.Body.String(), "http://localhost:1234/cb") } +func TestMWLegacyHandler_RedirectConsentCancelled(t *testing.T) { + r := require.New(t) + srv := newTestServer() + + req := httptest.NewRequest(http.MethodGet, + "/oauth/v1/redirect?error=access_denied&error_description=User+cancelled&state=100-http://localhost:1234/cb", nil) + w := httptest.NewRecorder() + srv.MWLegacyHandler().ServeHTTP(w, req) + + r.Equal(http.StatusOK, w.Code) + r.Contains(w.Header().Get("Content-Type"), "text/html") + body := w.Body.String() + r.Contains(body, "http://localhost:1234/cb?error=access_denied") + r.Contains(body, "error_description=User+cancelled") + r.Contains(body, "Sign-in was cancelled or did not complete.") + r.NotContains(body, "Authentication complete.") +} + +func TestMWLegacyHandler_RedirectErrorWithoutState(t *testing.T) { + r := require.New(t) + srv := newTestServer() + + req := httptest.NewRequest(http.MethodGet, "/oauth/v1/redirect?error=access_denied", nil) + w := httptest.NewRecorder() + srv.MWLegacyHandler().ServeHTTP(w, req) + + r.Equal(http.StatusOK, w.Code) + var resp map[string]any + r.NoError(json.Unmarshal(w.Body.Bytes(), &resp)) + r.Equal("core/Fault", resp["mwtype"]) +} + +func TestMWLegacyHandler_RedirectErrorWithInvalidState(t *testing.T) { + r := require.New(t) + srv := newTestServer() + + req := httptest.NewRequest(http.MethodGet, "/oauth/v1/redirect?error=access_denied&state=100-javascript:alert(1)", nil) + w := httptest.NewRecorder() + srv.MWLegacyHandler().ServeHTTP(w, req) + + r.Equal(http.StatusOK, w.Code) + var resp map[string]any + r.NoError(json.Unmarshal(w.Body.Bytes(), &resp)) + r.Equal("core/Fault", resp["mwtype"]) +} + +func TestMWLegacyHandler_RedirectMissingCodeAndError(t *testing.T) { + r := require.New(t) + srv := newTestServer() + + req := httptest.NewRequest(http.MethodGet, "/oauth/v1/redirect?state=100-http://localhost:1234/cb", nil) + w := httptest.NewRecorder() + srv.MWLegacyHandler().ServeHTTP(w, req) + + r.Equal(http.StatusOK, w.Code) + var resp map[string]any + r.NoError(json.Unmarshal(w.Body.Bytes(), &resp)) + r.Equal("core/Fault", resp["mwtype"]) +} + func TestMWLegacyHandler_Token(t *testing.T) { r := require.New(t) srv := newTestServer() diff --git a/internal/catalog.go b/internal/catalog.go index 4e530d8..0fc64e8 100644 --- a/internal/catalog.go +++ b/internal/catalog.go @@ -40,24 +40,30 @@ func init() { var messageKeyToIndex = map[string]int{ "Authentication complete. You may close this tab.": 1, - "Click here if not redirected automatically.": 2, + "Click here if not redirected automatically.": 3, "OAuth2 Redirect": 0, + "Sign-in was cancelled or did not complete. You may close this tab.": 2, } -var en_USIndex = []uint32{ // 4 elements - 0x00000000, 0x00000010, 0x00000041, 0x0000006d, -} // Size: 40 bytes +var en_USIndex = []uint32{ // 5 elements + 0x00000000, 0x00000010, 0x00000041, 0x00000084, + 0x000000b0, +} // Size: 44 bytes -const en_USData string = "" + // Size: 109 bytes +const en_USData string = "" + // Size: 176 bytes "\x02OAuth2 Redirect\x02Authentication complete. You may close this tab." + + "\x02Sign-in was cancelled or did not complete. You may close this tab." + "\x02Click here if not redirected automatically." -var fr_FRIndex = []uint32{ // 4 elements - 0x00000000, 0x00000013, 0x0000004e, 0x00000089, -} // Size: 40 bytes +var fr_FRIndex = []uint32{ // 5 elements + 0x00000000, 0x00000013, 0x0000004e, 0x0000009e, + 0x000000d9, +} // Size: 44 bytes -const fr_FRData string = "" + // Size: 137 bytes +const fr_FRData string = "" + // Size: 217 bytes "\x02Redirection OAuth2\x02Authentification terminée. Vous pouvez fermer " + - "cet onglet.\x02Cliquez ici si vous n'êtes pas redirigé automatiquement." + "cet onglet.\x02La connexion a été annulée ou n'a pas abouti. Vous pouvez" + + " fermer cet onglet.\x02Cliquez ici si vous n'êtes pas redirigé automatiq" + + "uement." - // Total table size 326 bytes (0KiB); checksum: C8AD52D0 + // Total table size 481 bytes (0KiB); checksum: 9CCE937B diff --git a/internal/docs/swagger.json b/internal/docs/swagger.json index b3cbd12..9b25481 100644 --- a/internal/docs/swagger.json +++ b/internal/docs/swagger.json @@ -535,13 +535,12 @@ }, "/redirect": { "get": { - "description": "Receives authorization code and state from the OAuth2 provider,\nthen redirects the browser back to the desktop client's local HTTP listener.", + "description": "Receives the authorization response from the OAuth2 provider,\nthen redirects the browser back to the desktop client's local HTTP listener.\nIf the user cancels or denies consent, the provider sends error (e.g. access_denied)\nand state instead of code; these are forwarded to the client so it can fail immediately.", "parameters": [ { - "description": "Authorization code from OAuth2 provider", + "description": "Authorization code from OAuth2 provider (required unless error is present)", "in": "query", "name": "code", - "required": true, "schema": { "type": "string" } @@ -554,6 +553,22 @@ "schema": { "type": "string" } + }, + { + "description": "OAuth2 error code from provider, e.g. access_denied when consent is cancelled", + "in": "query", + "name": "error", + "schema": { + "type": "string" + } + }, + { + "description": "Human-readable error description from provider", + "in": "query", + "name": "error_description", + "schema": { + "type": "string" + } } ], "responses": { diff --git a/internal/locales/en-US/out.gotext.json b/internal/locales/en-US/out.gotext.json index ecf7a0c..04e07b9 100644 --- a/internal/locales/en-US/out.gotext.json +++ b/internal/locales/en-US/out.gotext.json @@ -15,6 +15,13 @@ "translatorComment": "Copied from source.", "fuzzy": true }, + { + "id": "Sign-in was cancelled or did not complete. You may close this tab.", + "message": "Sign-in was cancelled or did not complete. You may close this tab.", + "translation": "Sign-in was cancelled or did not complete. You may close this tab.", + "translatorComment": "Copied from source.", + "fuzzy": true + }, { "id": "Click here if not redirected automatically.", "message": "Click here if not redirected automatically.", diff --git a/internal/locales/fr-FR/messages.gotext.json b/internal/locales/fr-FR/messages.gotext.json index 95bbdfd..e28d00a 100644 --- a/internal/locales/fr-FR/messages.gotext.json +++ b/internal/locales/fr-FR/messages.gotext.json @@ -15,6 +15,11 @@ "id": "Click here if not redirected automatically.", "message": "Click here if not redirected automatically.", "translation": "Cliquez ici si vous n'êtes pas redirigé automatiquement." + }, + { + "id": "Sign-in was cancelled or did not complete. You may close this tab.", + "message": "Sign-in was cancelled or did not complete. You may close this tab.", + "translation": "La connexion a été annulée ou n'a pas abouti. Vous pouvez fermer cet onglet." } ] } diff --git a/internal/locales/fr-FR/out.gotext.json b/internal/locales/fr-FR/out.gotext.json index 4de7167..8fba1fc 100644 --- a/internal/locales/fr-FR/out.gotext.json +++ b/internal/locales/fr-FR/out.gotext.json @@ -11,6 +11,11 @@ "message": "Authentication complete. You may close this tab.", "translation": "Authentification terminée. Vous pouvez fermer cet onglet." }, + { + "id": "Sign-in was cancelled or did not complete. You may close this tab.", + "message": "Sign-in was cancelled or did not complete. You may close this tab.", + "translation": "La connexion a été annulée ou n'a pas abouti. Vous pouvez fermer cet onglet." + }, { "id": "Click here if not redirected automatically.", "message": "Click here if not redirected automatically.", diff --git a/internal/redirect/redirect.go b/internal/redirect/redirect.go index 12a7092..2416443 100644 --- a/internal/redirect/redirect.go +++ b/internal/redirect/redirect.go @@ -91,30 +91,30 @@ func loadRedirectTemplate() *template.Template { } // @Summary OAuth2 redirect callback -// @Description Receives authorization code and state from the OAuth2 provider, +// @Description Receives the authorization response from the OAuth2 provider, // @Description then redirects the browser back to the desktop client's local HTTP listener. +// @Description If the user cancels or denies consent, the provider sends error (e.g. access_denied) +// @Description and state instead of code; these are forwarded to the client so it can fail immediately. // @Tags oauth // @Produce html -// @Param code query string true "Authorization code from OAuth2 provider" -// @Param state query string true "State containing appID and client redirect URL (format: appID-redirectURL)" -// @Success 200 {string} string "HTML page that redirects the browser" -// @Failure 200 {object} domain.FaultResponse "Fault on missing or invalid parameters" +// @Param code query string false "Authorization code from OAuth2 provider (required unless error is present)" +// @Param state query string true "State containing appID and client redirect URL (format: appID-redirectURL)" +// @Param error query string false "OAuth2 error code from provider, e.g. access_denied when consent is cancelled" +// @Param error_description query string false "Human-readable error description from provider" +// @Success 200 {string} string "HTML page that redirects the browser" +// @Failure 200 {object} domain.FaultResponse "Fault on missing or invalid parameters" // @Router /redirect [get] func (h *handler) handleRedirect(w http.ResponseWriter, r *http.Request) { - code := r.URL.Query().Get("code") - state := r.URL.Query().Get("state") + query := r.URL.Query() + providerErr := query.Get("error") - if state == "" || code == "" { - domain.WriteFault(w, "StatusBadRequest", "state and code query params not found") - return - } - - appID, stateURL, faultMsg := parseRedirectState(state) + appID, stateURL, faultMsg := parseCallback(query.Get("code"), query.Get("state"), providerErr) if faultMsg != "" { domain.WriteFault(w, "StatusBadRequest", faultMsg) return } + // The full query (code or error, plus state) is forwarded to the client listener. newURL := *r.URL newURL.Scheme = stateURL.Scheme newURL.Host = stateURL.Host @@ -128,11 +128,16 @@ func (h *handler) handleRedirect(w http.ResponseWriter, r *http.Request) { title = app.Description } + complete := p.Sprintf("Authentication complete. You may close this tab.") + if providerErr != "" { + complete = p.Sprintf("Sign-in was cancelled or did not complete. You may close this tab.") + } + data := redirectData{ URL: template.URL(newURL.String()), Lang: tag.String(), Title: title, - Complete: p.Sprintf("Authentication complete. You may close this tab."), + Complete: complete, ClickHere: p.Sprintf("Click here if not redirected automatically."), } @@ -143,6 +148,15 @@ func (h *handler) handleRedirect(w http.ResponseWriter, r *http.Request) { loadRedirectTemplate().Execute(w, data) //nolint:errcheck } +// parseCallback requires a valid state so the browser can always be sent back to +// the client, plus either an authorization code or a provider error. +func parseCallback(code, state, providerErr string) (appID string, stateURL *url.URL, faultMsg string) { + if state == "" || (code == "" && providerErr == "") { + return "", nil, "state and code query params not found" + } + return parseRedirectState(state) +} + func parseRedirectState(state string) (appID string, stateURL *url.URL, faultMsg string) { pieces := strings.SplitN(state, "-", 2) if len(pieces) != 2 { diff --git a/matlab/example_usage.m b/matlab/example_usage.m index 04916d7..91829e4 100644 --- a/matlab/example_usage.m +++ b/matlab/example_usage.m @@ -62,6 +62,8 @@ authCode = ""; authState = ""; authScope = ""; + authError = ""; + authErrorDesc = ""; for i = 1:numel(queryParams) if queryParams(i).Name == "code" authCode = string(queryParams(i).Value); @@ -69,6 +71,10 @@ authState = string(queryParams(i).Value); elseif queryParams(i).Name == "scope" authScope = string(queryParams(i).Value); + elseif queryParams(i).Name == "error" + authError = string(queryParams(i).Value); + elseif queryParams(i).Name == "error_description" + authErrorDesc = urldecode(string(queryParams(i).Value)); end end else @@ -76,11 +82,11 @@ pause(0.5); rawRequest = read(server, server.NumBytesAvailable, "string"); - % Send a minimal HTTP response so the browser shows success + % Send a minimal HTTP response so the browser tab can be closed httpResponse = "HTTP/1.1 200 OK" + newline + ... "Content-Type: text/html" + newline + ... "Connection: close" + newline + newline + ... - "