-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Deny access to methods other than GET for readonly access tokens
- Loading branch information
Showing
5 changed files
with
135 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
class AccessTokenPolicy | ||
attr_reader :access_token, :request | ||
|
||
def initialize(access_token, request) | ||
@access_token = access_token | ||
@request = request | ||
end | ||
|
||
def request? | ||
access_token.all_permissions? || request.get? | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
require "rails_helper" | ||
|
||
RSpec.describe AccessTokenPolicy do | ||
subject(:policy) { described_class.new(access, request) } | ||
|
||
let(:access) do | ||
build :access_token | ||
end | ||
|
||
let(:request) do | ||
ActionDispatch::Request.empty | ||
end | ||
|
||
describe "#request?" do | ||
before do | ||
request.headers["REQUEST_METHOD"] = request_method | ||
end | ||
|
||
context "when request method is GET" do | ||
let(:request_method) { "GET" } | ||
|
||
it "grants access if access token has all permissions" do | ||
access.permissions = :all | ||
|
||
expect(policy.request?).to be true | ||
end | ||
|
||
it "grants access if access token has readonly permissions" do | ||
access.permissions = :readonly | ||
|
||
expect(policy.request?).to be true | ||
end | ||
end | ||
|
||
(ActionDispatch::Request::HTTP_METHODS - %w[GET]).each do |request_method_| | ||
context "when request method is #{request_method_}" do | ||
let(:request_method) { request_method_ } | ||
|
||
it "grants access if access token has all permissions" do | ||
access.permissions = :all | ||
|
||
expect(policy.request?).to be true | ||
end | ||
|
||
it "denies access if access token has readonly permissions" do | ||
access.permissions = :readonly | ||
|
||
expect(policy.request?).to be false | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters