Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

Retire the replaced refresh token when MCP or the API sees its successor (#5918)

Jared Moodyc8fa146d4725
diff --git a/app/controllers/api/v1/base_controller.rb b/app/controllers/api/v1/base_controller.rb
index 11c824498c..226889df54 100644
--- a/app/controllers/api/v1/base_controller.rb
+++ b/app/controllers/api/v1/base_controller.rb
@@ -65,6 +65,9 @@ class Api::V1::BaseController < ApplicationController
# Manually verify the token (bypassing doorkeeper_authorize! which had scope issues)
token_string = request.authorization&.split(" ")&.last
access_token = Doorkeeper::AccessToken.by_token(token_string)
+ # Presenting a refreshed token proves the client received it, so the
+ # refresh token it replaced is retired, as doorkeeper_authorize! would.
+ access_token&.revoke_previous_refresh_token!
# Check token validity and scope (read_write includes read access)
has_sufficient_scope = access_token&.scopes&.include?("read") || access_token&.scopes&.include?("read_write")
diff --git a/app/controllers/mcp_controller.rb b/app/controllers/mcp_controller.rb
index c3d5ec6210..f7876d7a18 100644
--- a/app/controllers/mcp_controller.rb
+++ b/app/controllers/mcp_controller.rb
@@ -149,6 +149,9 @@ class McpController < ApplicationController
def authenticate_via_doorkeeper(token)
access_token = Doorkeeper::AccessToken.by_token(token)
+ # Presenting a refreshed token proves the client received it, so the
+ # refresh token it replaced is retired, as doorkeeper_authorize! would.
+ access_token&.revoke_previous_refresh_token!
return false unless access_token&.accessible?
return false unless access_token.scopes.include?(REQUIRED_SCOPE)
diff --git a/test/controllers/oauth/tokens_controller_test.rb b/test/controllers/oauth/tokens_controller_test.rb
index 2223eb0140..b65594b104 100644
--- a/test/controllers/oauth/tokens_controller_test.rb
+++ b/test/controllers/oauth/tokens_controller_test.rb
@@ -21,6 +21,27 @@ class Oauth::TokensControllerTest < ActionDispatch::IntegrationTest
assert_not token.reload.revoked?, "the predecessor stays valid until the successor is first used"
end
+ test "using a refreshed token at /mcp retires the token it replaced" do
+ replaced = refresh!(issue_token.plaintext_refresh_token)
+ current = refresh!(replaced["refresh_token"])
+
+ post "/mcp", params: { jsonrpc: "2.0", id: 1, method: "initialize" }.to_json,
+ headers: { "Content-Type" => "application/json", "Authorization" => "Bearer #{current["access_token"]}" }
+ assert_response :success
+
+ assert_retired(replaced)
+ end
+
+ test "using a refreshed token at the API retires the token it replaced" do
+ replaced = refresh!(issue_token.plaintext_refresh_token)
+ current = refresh!(replaced["refresh_token"])
+
+ get "/api/v1/test", headers: { "Authorization" => "Bearer #{current["access_token"]}" }
+ assert_response :success
+
+ assert_retired(replaced)
+ end
+
test "refresh is refused once the user is inactive, even if their tokens were not revoked" do
token = issue_token
@user.update_column(:active, false)
@@ -127,6 +148,21 @@ class Oauth::TokensControllerTest < ActionDispatch::IntegrationTest
}
end
+ def refresh!(refresh_token)
+ post_refresh refresh_token
+ assert_response :success
+ response.parsed_body
+ end
+
+ def assert_retired(token_response)
+ assert Doorkeeper::AccessToken.by_token(token_response["access_token"]).revoked?
+
+ post_refresh token_response["refresh_token"]
+
+ assert_response :bad_request
+ assert_equal "invalid_grant", response.parsed_body["error"]
+ end
+
def post_code(code)
post "/oauth/token", params: {
grant_type: "authorization_code",