diff --git a/internal/web/server_test.go b/internal/web/server_test.go index 4a87875..74ef8bc 100644 --- a/internal/web/server_test.go +++ b/internal/web/server_test.go @@ -319,6 +319,18 @@ func TestCallbackURLs(t *testing.T) { } } +func TestOAuthErrorIsShownOnce(t *testing.T) { + s, h := newTestServer(t, nil) + _ = s.Secrets.Set(map[string]string{secrets.LinkedInClientID: "cid", secrets.LinkedInClientSecret: "cs"}) + loc, _ := url.Parse(postForm(h, "/auth/linkedin/start", nil).Header().Get("Location")) + state := loc.Query().Get("state") + body := do(h, http.MethodGet, "/auth/linkedin/callback?state="+state+"&error=unauthorized_scope_error&error_description="+ + url.QueryEscape("Scope "openid" is not authorized"), nil, nil).Body.String() + if !strings.Contains(body, "Scope "openid" is not authorized") || strings.Contains(body, """) { + t.Errorf("error page:\n%s", body) + } +} + func TestOAuthCallbackRejectsUnknownState(t *testing.T) { _, h := newTestServer(t, nil) rec := do(h, http.MethodGet, "/auth/x/callback?state=nope&code=c", nil, nil) diff --git a/internal/web/settings.go b/internal/web/settings.go index 1302c90..9162f72 100644 --- a/internal/web/settings.go +++ b/internal/web/settings.go @@ -2,6 +2,7 @@ package web import ( "context" + "html" "net/http" "net/url" "strings" @@ -371,6 +372,7 @@ func (s *Server) oauthStart(w http.ResponseWriter, r *http.Request) { } s.pending[p.State] = p s.mu.Unlock() + s.Log.Info("oauth: sign-in started", "provider", provider, "redirect_uri", p.RedirectURI) http.Redirect(w, r, authURL, http.StatusSeeOther) } @@ -383,15 +385,19 @@ func (s *Server) oauthCallback(w http.ResponseWriter, r *http.Request) { delete(s.pending, q.Get("state")) // a state is good for one callback s.mu.Unlock() if p == nil || p.Provider != provider || time.Since(p.Started) > oauthTTL { + s.Log.Warn("oauth: callback with an unknown or expired state", "provider", provider) redirect(w, r, "/settings/accounts", "oauth-expired") return } fail := func(msg string) { + s.Log.Warn("oauth: sign-in failed", "provider", provider, "reason", msg) s.render(w, r, "message", page{Title: "Sign-in failed", Nav: "settings/accounts", Error: msg, Data: map[string]string{"Back": "/settings/accounts"}}) } if e := q.Get("error"); e != "" { - fail("The provider said: " + e + " " + q.Get("error_description")) + // Some providers (LinkedIn) send the description HTML-escaped; + // unescape it so the page doesn't show ". + fail("The provider said: " + e + " " + html.UnescapeString(q.Get("error_description"))) return } code := q.Get("code")