Skip to content
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

Add new AuthError class for raising authentication related issues when public/private keys are empty. Closes #158 #161

Merged
merged 4 commits into from
Apr 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
### Changed
* File attribute `datetime_stored` is deprecated and will warn on usage from `File` object properties.

## Unreleased

### Fixed

* Throw `AuthError` if current public key or secret key config are empty when accessing any of the APIs.

## 4.4.0 — 2024-03-09

### Breaking
Expand Down
1 change: 1 addition & 0 deletions lib/uploadcare.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
require 'exception/throttle_error'
require 'exception/request_error'
require 'exception/retry_error'
require 'exception/auth_error'

# Entities
require 'entity/entity'
Expand Down
8 changes: 8 additions & 0 deletions lib/uploadcare/exception/auth_error.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

module Uploadcare
module Exception
# Invalid Auth configuration error
class AuthError < StandardError; end
end
end
12 changes: 12 additions & 0 deletions lib/uploadcare/param/authentication_header.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module Param
class AuthenticationHeader
# @see https://uploadcare.com/docs/api_reference/rest/requests_auth/#auth-uploadcare
def self.call(options = {})
validate_auth_config
case Uploadcare.config.auth_type
when 'Uploadcare'
SecureAuthHeader.call(options)
Expand All @@ -20,6 +21,17 @@ def self.call(options = {})
raise ArgumentError, "Unknown auth_scheme: '#{Uploadcare.config.auth_type}'"
end
end

def self.validate_auth_config
raise AuthError, 'Public Key is blank.' if is_blank?(Uploadcare.config.public_key)
raise AuthError, 'Secret Key is blank.' if is_blank?(Uploadcare.config.secret_key)
end

# rubocop:disable Naming/PredicateName
def self.is_blank?(value)
value.nil? || value.empty?
end
# rubocop:enable Naming/PredicateName
end
end
end
24 changes: 24 additions & 0 deletions spec/uploadcare/client/file_client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,30 @@ module Client
end
end

it 'show raise argument error if public_key is blank' do
Uploadcare.config.public_key = ''
VCR.use_cassette('rest_file_info') do
uuid = '2e17f5d1-d423-4de6-8ee5-6773cc4a7fa6'
expect { subject.info(uuid) }.to raise_error(AuthError, 'Public Key is blank.')
end
end

it 'show raise argument error if secret_key is blank' do
Uploadcare.config.secret_key = ''
VCR.use_cassette('rest_file_info') do
uuid = '2e17f5d1-d423-4de6-8ee5-6773cc4a7fa6'
expect { subject.info(uuid) }.to raise_error(AuthError, 'Secret Key is blank.')
end
end

it 'show raise argument error if secret_key is nil' do
Uploadcare.config.secret_key = nil
VCR.use_cassette('rest_file_info') do
uuid = '2e17f5d1-d423-4de6-8ee5-6773cc4a7fa6'
expect { subject.info(uuid) }.to raise_error(AuthError, 'Secret Key is blank.')
end
end

it 'supports extra params like include' do
VCR.use_cassette('rest_file_info') do
uuid = '640fe4b7-7352-42ca-8d87-0e4387957157'
Expand Down
10 changes: 10 additions & 0 deletions spec/uploadcare/param/authentication_header_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,15 @@ module Uploadcare
Uploadcare.config.auth_type = 'Uploadcare'
expect(subject.call).to eq('SecureAuth called')
end

it 'raise argument error if public_key is blank' do
Uploadcare.config.public_key = ''
expect { subject.call }.to raise_error(AuthError, 'Public Key is blank.')
end

it 'raise argument error if secret_key is blank' do
Uploadcare.config.secret_key = ''
expect { subject.call }.to raise_error(AuthError, 'Secret Key is blank.')
end
end
end
Loading