Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(oauth2): fix a bug that refresh_token could be shared across instances #11342

Merged
merged 9 commits into from
Aug 10, 2023

Conversation

liverpool8056
Copy link
Contributor

Summary

With global_credential=true, access_token can be shared across services, as well as refresh_token currently. This means that a refresh_token belonging to a service can be used to refresh tokens belonging to another service, which is consider as a bug. In this PR, the scope is taken into account as a new creteria of the request validation. Scopes associated with a token provided in the request will be compared with those configured in the Oauth2 instance hit by that request.

Checklist

Full changelog

  • [Implement ...]

Issue reference

FTI-5173

@liverpool8056 liverpool8056 force-pushed the fix/check_scopes_when_refresh_token_oauth2 branch 2 times, most recently from c649429 to 08c44c1 Compare August 2, 2023 08:21
Copy link
Contributor

@vm-001 vm-001 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It overall LGTM. I have one question:

If two OAuth2 plugin has common scopes but the token_expiration is different.

      admin_api.oauth2_plugins:insert({
        route = { id = route18.id },
        config   = {
          scopes                          = { "common", "foo" },
          global_credentials       = true,
          token_expiration          = 10
        }
      })

      admin_api.oauth2_plugins:insert({
        route = { id = route19.id },
        config   = {
          scopes                          = { "common", "bar" },
          global_credentials       = true,
          token_expiration          = 20
        }
      })

A shorter TTL access_token(with scope common)'s refresh_token can be considered valid to another refresh endpoint to get a longer TTL access_token as it passes the scope validation. Is this expected?

kong/plugins/oauth2/access.lua Outdated Show resolved Hide resolved
@liverpool8056 liverpool8056 force-pushed the fix/check_scopes_when_refresh_token_oauth2 branch from f0d6d9c to 855e650 Compare August 8, 2023 03:38
CHANGELOG.md Outdated Show resolved Hide resolved
kong/plugins/oauth2/access.lua Outdated Show resolved Hide resolved
error = "invalid_scope",
error_description = "scope mismatch"
}, res)
end)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a test case that verifies the subsetting functionality.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addressed

@liverpool8056 liverpool8056 force-pushed the fix/check_scopes_when_refresh_token_oauth2 branch from 7bc45ac to c7bea72 Compare August 8, 2023 13:25
@liverpool8056 liverpool8056 force-pushed the fix/check_scopes_when_refresh_token_oauth2 branch from 1cfcd6f to 1dced32 Compare August 9, 2023 07:16
Copy link
Contributor

@hanshuebner hanshuebner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test file does not conform to the while spacing rules that we follow across the code base. Please adjust the white space of the new tests to conform.

spec/03-plugins/25-oauth2/03-access_spec.lua Outdated Show resolved Hide resolved
method = "POST",
path = "/oauth2/token",
body = {
refresh_token = token.refresh_token,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that this test reuses a token generated by a previous test. Please make each test self contained. The outcome of one test should not influence other tests, as that makes it hard to run and debug tests in isolation. Also, it is not clear from looking at the test how the token has been generated, making it difficult to understand what is being tested.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addressed

@liverpool8056 liverpool8056 force-pushed the fix/check_scopes_when_refresh_token_oauth2 branch from f79b23a to 4e3d389 Compare August 9, 2023 11:35
liverpool8056 and others added 9 commits August 10, 2023 20:23
services when `global_credentials` is set as `true`.

With `global_credential=true`, `access_token` can be shared across
services, as well as `refresh_token` currently. This means that a
`refresh_token` belonging to a service can be used to refresh tokens
belonging to another service, which is consider as a bug.
In this PR, the scope is taken into account as a new creteria of the
request validation. Scopes associated with a token provided in the
request will be compared with those configured in the Oauth2 instance
hit by that request.

FTI-5173
Co-authored-by: Hans Hübner <[email protected]>
@liverpool8056 liverpool8056 force-pushed the fix/check_scopes_when_refresh_token_oauth2 branch from 4e3d389 to 34715ad Compare August 10, 2023 12:23
@hanshuebner hanshuebner merged commit 7821654 into master Aug 10, 2023
21 checks passed
@hanshuebner hanshuebner deleted the fix/check_scopes_when_refresh_token_oauth2 branch August 10, 2023 13:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants