Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

Reject revoked OAuth tokens in API auth (#1711)

Juan José Matab74014ab4246
diff --git a/app/controllers/api/v1/base_controller.rb b/app/controllers/api/v1/base_controller.rb
index b2e8bc59c1..4db722b52f 100644
--- a/app/controllers/api/v1/base_controller.rb
+++ b/app/controllers/api/v1/base_controller.rb
@@ -62,7 +62,7 @@ class Api::V1::BaseController < ApplicationController
# Check token validity and scope (read_write includes read access)
has_sufficient_scope = access_token&.scopes&.include?("read") || access_token&.scopes&.include?("read_write")
- unless access_token && !access_token.expired? && has_sufficient_scope
+ unless access_token&.accessible? && has_sufficient_scope
render_json({ error: "unauthorized", message: "Access token is invalid, expired, or missing required scope" }, status: :unauthorized)
return false
end
diff --git a/test/controllers/api/v1/base_controller_test.rb b/test/controllers/api/v1/base_controller_test.rb
index af9e7066db..a6dec7ab63 100644
--- a/test/controllers/api/v1/base_controller_test.rb
+++ b/test/controllers/api/v1/base_controller_test.rb
@@ -60,6 +60,23 @@ class Api::V1::BaseControllerTest < ActionDispatch::IntegrationTest
assert_equal @user.email, response_body["user"]
end
+ test "should reject revoked access token" do
+ access_token = Doorkeeper::AccessToken.create!(
+ application: @oauth_app,
+ resource_owner_id: @user.id,
+ scopes: "read"
+ )
+ access_token.revoke
+
+ get "/api/v1/test", params: {}, headers: {
+ "Authorization" => "Bearer #{access_token.token}"
+ }
+
+ assert_response :unauthorized
+ response_body = JSON.parse(response.body)
+ assert_equal "unauthorized", response_body["error"]
+ end
+
test "should reject invalid access token" do
get "/api/v1/test", params: {}, headers: {
"Authorization" => "Bearer invalid_token"