Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract PersonalAccessTokenValidator out of User
The token validation logic was quite noisy in the User class, so this commit pulls it out into its own class. As part of this we have updated the validation to not return early if validation fails—now all validation errors will be added to the errors object.
- Loading branch information
1 parent
8674b6a
commit 7b95c17
Showing
4 changed files
with
101 additions
and
76 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,51 @@ | ||
# frozen_string_literal: true | ||
class PersonalAccessTokenValidator < ActiveModel::Validator | ||
|
||
ERRORS = { | ||
invalid_token: [:personal_access_token, 'is not a valid token for this github user'], | ||
missing_notifications_scope: [:personal_access_token, 'does not include the notifications scope'], | ||
missing_read_org_scope: [:personal_access_token, 'does not include the read:org scope'], | ||
disallowed_tokens: [:personal_access_token, 'is not allowed in this instance'], | ||
}.freeze | ||
|
||
def validate(record) | ||
return if record.personal_access_token.blank? | ||
validate_tokens_are_enabled(record) | ||
validate_token_credentials(record) | ||
validate_github_id(record) | ||
validate_github_client_notifications_scope(record) | ||
validate_github_client_read_scope(record) | ||
end | ||
|
||
private | ||
|
||
def validate_tokens_are_enabled(record) | ||
unless Octobox.personal_access_tokens_enabled? | ||
record.errors.add(*ERRORS[:disallowed_tokens]) | ||
end | ||
end | ||
|
||
def validate_token_credentials(record) | ||
valid_credentials = Octokit::Client.new.validate_credentials( | ||
access_token: record.effective_access_token) | ||
record.errors.add(*ERRORS[:invalid_token]) unless valid_credentials | ||
end | ||
|
||
def validate_github_id(record) | ||
unless record.github_id == record.github_client.user.id | ||
record.errors.add(*ERRORS[:invalid_token]) | ||
end | ||
end | ||
|
||
def validate_github_client_notifications_scope(record) | ||
unless record.github_client.scopes.include? 'notifications' | ||
record.errors.add(*ERRORS[:missing_notifications_scope]) | ||
end | ||
end | ||
|
||
def validate_github_client_read_scope(record) | ||
if Octobox.restricted_access_enabled? && !record.github_client.scopes.include?('read:org') | ||
record.errors.add(*ERRORS[:missing_read_org_scope]) | ||
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
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,48 @@ | ||
# frozen_string_literal: true | ||
require 'test_helper' | ||
|
||
class PersonalAccessTokenValidatorTest < ActiveSupport::TestCase | ||
setup do | ||
@user = build(:user, personal_access_token: '1234') | ||
stub_user_request(user: @user) | ||
stub_personal_access_tokens_enabled | ||
stub_notifications_request | ||
end | ||
|
||
def assert_error_present(model_object, error) | ||
refute model_object.valid? | ||
assert model_object.errors[error[0]].include? error[1] | ||
end | ||
|
||
test 'it allows a blank personal_access_token' do | ||
@user.personal_access_token = nil | ||
assert @user.valid? | ||
end | ||
|
||
test 'does not allow setting personal_access_token without being enabled' do | ||
stub_personal_access_tokens_enabled(value: false) | ||
assert_error_present(@user, PersonalAccessTokenValidator::ERRORS[:disallowed_tokens]) | ||
end | ||
|
||
test 'does not allow invalid credentials' do | ||
Octokit::Client.any_instance.stubs(:validate_credentials).returns(false) | ||
stub_user_request(user: @user) | ||
assert_error_present(@user, PersonalAccessTokenValidator::ERRORS[:invalid_token]) | ||
end | ||
|
||
test 'does not allow a personal_access_token for another user' do | ||
stub_user_request(body: '{"id": 98}') | ||
assert_error_present(@user, PersonalAccessTokenValidator::ERRORS[:invalid_token]) | ||
end | ||
|
||
test 'does not allow a personal_access_token without the notifications scope' do | ||
stub_user_request(user: @user, oauth_scopes: 'user, repo') | ||
assert_error_present(@user, PersonalAccessTokenValidator::ERRORS[:missing_notifications_scope]) | ||
end | ||
|
||
test 'does not allow a personal_access_token without the read:org scope if restricted_access enabled' do | ||
stub_restricted_access_enabled | ||
stub_user_request(oauth_scopes: 'user, repo') | ||
assert_error_present(@user, PersonalAccessTokenValidator::ERRORS[:missing_read_org_scope]) | ||
end | ||
end |