This is an automated email from the ASF dual-hosted git repository.

hsluoyz pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/casbin-gateway.git

commit ea3d2e1232142c638c11a3afd606f64d767b3d08
Author: Yang Luo <[email protected]>
AuthorDate: Sat Sep 5 19:41:59 2026 +0800

    fix(connections): name the application, not the status code, when a grant 
is refused
---
 object/connection_oauth.go | 24 +++++++++++++++---------
 1 file changed, 15 insertions(+), 9 deletions(-)

diff --git a/object/connection_oauth.go b/object/connection_oauth.go
index 11ba16c..db3f502 100644
--- a/object/connection_oauth.go
+++ b/object/connection_oauth.go
@@ -265,10 +265,6 @@ func exchange(found connector.Connector, credentials 
map[string]string, form url
        if err != nil {
                return nil, err
        }
-       if response.StatusCode >= 400 {
-               return nil, fmt.Errorf("the token endpoint answered %s: %s", 
response.Status, summarize(body))
-       }
-
        var granted struct {
                AccessToken  string `json:"access_token"`
                RefreshToken string `json:"refresh_token"`
@@ -276,12 +272,22 @@ func exchange(found connector.Connector, credentials 
map[string]string, form url
                Error        string `json:"error"`
                ErrorText    string `json:"error_description"`
        }
-       if err := json.Unmarshal(body, &granted); err != nil {
-               return nil, fmt.Errorf("the token endpoint did not answer with 
JSON: %s", summarize(body))
+       readable := json.Unmarshal(body, &granted) == nil
+
+       // The body is read before the status, because the status is the less
+       // informative half of the answer: an error comes back with 200 more 
often
+       // than it should, and GitHub answers an application it does not know 
with
+       // 404 and nothing but {"error":"Not Found"}. Reporting the status first
+       // would send somebody looking at the URL when the client id is the 
problem.
+       if readable && granted.Error != "" {
+               return nil, fmt.Errorf("the token endpoint refused this 
application: %s %s",
+                       granted.Error, granted.ErrorText)
        }
-       // An error here comes back with 200 more often than it should.
-       if granted.Error != "" {
-               return nil, fmt.Errorf("%s: %s", granted.Error, 
granted.ErrorText)
+       if response.StatusCode >= 400 {
+               return nil, fmt.Errorf("the token endpoint answered %s: %s", 
response.Status, summarize(body))
+       }
+       if !readable {
+               return nil, fmt.Errorf("the token endpoint did not answer with 
JSON: %s", summarize(body))
        }
        if granted.AccessToken == "" {
                return nil, fmt.Errorf("the token endpoint returned no access 
token")


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to