-
Notifications
You must be signed in to change notification settings - Fork 166
Adding token validation #11889
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
Merged
Merged
Adding token validation #11889
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
823153a
changelog: Upcoming Features, Attempts API, Adding signature validation
Sgtpluck cbdcc0c
Remove verification endpoint
Sgtpluck b714dc7
Update token to shared secret
Sgtpluck 5ec84ca
Pre-hash tokens
Sgtpluck 59617a2
Assume shared secret is hashed
Sgtpluck 1061c0e
Move request logic to a class
Sgtpluck File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,82 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module AttemptsApi | ||
| class RequestTokenValidator | ||
| include ActiveModel::Model | ||
|
|
||
| attr_reader :bearer, :issuer, :token | ||
|
|
||
| validates :token, :issuer, :bearer, presence: true | ||
| validates :bearer, comparison: { equal_to: 'Bearer' } | ||
| validate :config_data_exists | ||
| validate :service_provider_exists, if: :config_data_exists? | ||
| validate :valid_request_token?, if: :config_data_exists? | ||
|
|
||
| def initialize(auth_request_header) | ||
| @bearer, @issuer, @token = auth_request_header&.split(' ', 3) | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def config_data_exists | ||
| return if config_data_exists? | ||
|
|
||
| errors.add( | ||
| :issuer, | ||
| :not_authorized, | ||
| message: 'Issuer is not authorized to use Attempts API', | ||
| ) | ||
| end | ||
|
|
||
| def issuer_is_authorized | ||
| errors.add( | ||
| :issuer, | ||
| :not_authorized, | ||
| message: 'Issuer is not authorized to use Attempts API', | ||
| ) | ||
| end | ||
|
|
||
| def service_provider_exists | ||
| return if service_provider.present? | ||
|
|
||
| errors.add( | ||
| :service_provider, | ||
| :not_authorized, | ||
| message: 'ServiceProvider does not exist', | ||
| ) | ||
| end | ||
|
|
||
| def valid_request_token? | ||
| return if config_data[:tokens].any? do |valid_token| | ||
| scrypt_salt = cost + OpenSSL::Digest::SHA256.hexdigest(valid_token[:salt]) | ||
| scrypted = SCrypt::Engine.hash_secret token, scrypt_salt, 32 | ||
| hashed_req_token = SCrypt::Password.new(scrypted).digest | ||
| ActiveSupport::SecurityUtils.secure_compare(valid_token[:value], hashed_req_token) | ||
| end | ||
|
|
||
| errors.add( | ||
| :request_token, | ||
| :not_valid, | ||
| message: 'Request token is not valid', | ||
| ) | ||
| end | ||
|
|
||
| def config_data | ||
| @config_data ||= IdentityConfig.store.allowed_attempts_providers.find do |config| | ||
| config[:issuer] == issuer | ||
| end | ||
| end | ||
|
|
||
| def config_data_exists? | ||
| config_data.present? | ||
| end | ||
|
|
||
| def cost | ||
| IdentityConfig.store.scrypt_cost | ||
| end | ||
|
|
||
| def service_provider | ||
| @service_provider ||= ServiceProvider.find_by(issuer:) | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or 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 hidden or 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 was deleted.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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
95 changes: 95 additions & 0 deletions
95
spec/services/attempts_api/request_token_validator_spec.rb
This file contains hidden or 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,95 @@ | ||
| require 'rails_helper' | ||
|
|
||
| RSpec.describe AttemptsApi::RequestTokenValidator do | ||
| let(:sp) { create(:service_provider) } | ||
| let(:issuer) { sp.issuer } | ||
| let(:token) { 'a-shared-secret' } | ||
| let(:salt) { SecureRandom.hex(32) } | ||
| let(:cost) { IdentityConfig.store.scrypt_cost } | ||
|
|
||
| let(:hashed_token) do | ||
| scrypt_salt = cost + OpenSSL::Digest::SHA256.hexdigest(salt) | ||
| scrypted = SCrypt::Engine.hash_secret token, scrypt_salt, 32 | ||
| SCrypt::Password.new(scrypted).digest | ||
| end | ||
|
|
||
| let(:auth_header) { "Bearer #{issuer} #{token}" } | ||
|
|
||
| before do | ||
| allow(IdentityConfig.store).to receive(:allowed_attempts_providers).and_return( | ||
| [{ | ||
| issuer: sp.issuer, | ||
| tokens: [{ value: hashed_token, salt: }], | ||
| }], | ||
| ) | ||
| end | ||
|
|
||
| subject { AttemptsApi::RequestTokenValidator.new(auth_header) } | ||
|
|
||
| describe 'validations' do | ||
| context 'with a valid auth header' do | ||
| it 'returns true' do | ||
| expect(subject.valid?).to be true | ||
| end | ||
| end | ||
|
|
||
| context 'with no auth header' do | ||
| let(:auth_header) { nil } | ||
|
|
||
| it 'returns false' do | ||
| expect(subject.valid?).to be false | ||
| end | ||
| end | ||
|
|
||
| context 'with an empty string for auth header' do | ||
| let(:auth_header) { '' } | ||
|
|
||
| it 'returns false' do | ||
| expect(subject.valid?).to be false | ||
| end | ||
| end | ||
|
|
||
| context 'without a Bearer token' do | ||
| let(:auth_header) { "#{sp.issuer} #{token}" } | ||
|
|
||
| it 'returns false' do | ||
| expect(subject.valid?).to be false | ||
| end | ||
| end | ||
|
|
||
| context 'without a valid issuer' do | ||
| context 'with an unknown issuer' do | ||
| let(:issuer) { 'unknown-issuer' } | ||
|
|
||
| it 'returns false' do | ||
| expect(subject.valid?).to be false | ||
| end | ||
| end | ||
|
|
||
| context 'with an issuer associated with an unauthorized sp' do | ||
| let(:unauth_sp) { create(:service_provider) } | ||
| let(:issuer) { unauth_sp.issuer } | ||
|
|
||
| it 'returns false' do | ||
| expect(subject.valid?).to be false | ||
| end | ||
| end | ||
| end | ||
|
|
||
| context 'without a token' do | ||
| let(:token) { nil } | ||
|
|
||
| it 'returns false' do | ||
| expect(subject.valid?).to be false | ||
| end | ||
| end | ||
|
|
||
| context 'with an invalid token' do | ||
| let(:auth_header) { "Bearer #{issuer} not-shared-secret" } | ||
|
|
||
| it 'returns false' do | ||
| expect(subject.valid?).to be false | ||
| end | ||
| end | ||
| end | ||
| end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just for my own reference/note-taking, this is based on some of the previous implementation here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, apologies!
the big difference is we are hashing the token before saving it as config, so we don't have it stored in plaintext anywhere.