Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4.3.2
4.3.3
Comment thread
ehimen-io marked this conversation as resolved.
Outdated
2 changes: 0 additions & 2 deletions campact_user_service.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ Gem::Specification.new do |spec|
# Runtime dependencies
spec.add_dependency "faraday", "~> 2.14"
spec.add_dependency "json", "~> 2.1"
spec.add_dependency "rotp", "~> 6"

# Development dependencies
spec.add_development_dependency "byebug"
spec.add_development_dependency "faraday-detailed_logger", "~> 2.1"
Expand Down
32 changes: 10 additions & 22 deletions lib/campact_user_service/client.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
require 'base64'
require 'faraday'
require 'json'
require 'rotp'
require 'campact_user_service/response_error'

module CampactUserService
class Client
TIMEOUT = 60.freeze
OPEN_TIMEOUT = 20.freeze

attr_reader :connection, :host, :port, :topt_authorization
attr_reader :connection, :host, :port, :basic_auth

def initialize(options)
@host = options.fetch(:host)
@port = options[:port]
@topt_authorization = options[:topt_authorization]
@basic_auth = options[:basic_auth]
faraday_options = default_faraday_options.merge(options.delete(:faraday) || {})
adapter = faraday_options.delete(:adapter) || Faraday.default_adapter

Expand Down Expand Up @@ -44,8 +44,8 @@ def request(verb, path, options)
req.body = options[:body]
end

if topt_authorization
req.headers['authorization'] = authorization(topt_authorization)
if basic_auth
req.headers['authorization'] = authorization(basic_auth)
end
end

Expand Down Expand Up @@ -93,23 +93,11 @@ def format_cookies(cookies)
end
end

def authorization(totp_options)
user = totp_options.fetch(:user)
secret = totp_options.fetch(:secret)

token = [user, auth_pass(secret)].join(':')

"Token #{token}"
end

def auth_pass(secret)
totp_secret = ROTP::Base32.encode(secret)

ROTP::TOTP.new(totp_secret, {
digest: 'sha256',
digits: 8,
interval: 30
}).now
def authorization(basic_auth_options)
Comment thread
ehimen-io marked this conversation as resolved.
Outdated
user = basic_auth_options.fetch(:user)
password = basic_auth_options.fetch(:password)
credentials = Base64.strict_encode64("#{user}:#{password}")
"Basic #{credentials}"
end
end
end
12 changes: 3 additions & 9 deletions spec/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,19 +131,13 @@
subject.get_request('/foo/bar', cookies: {'foo' => 'bar', 'xyz' => 'abc'})
end

it 'should set TOTP authorization header' do
it 'should set Basic Auth authorization header' do
allow(response).to receive(:status).and_return(200)
allow(response).to receive(:body).and_return(nil)

totp_secret = ROTP::Base32.encode('shh! a secret!')
expect(headers_builder).to receive(:[]=).with('authorization', 'Basic ' + Base64.strict_encode64('api_user:s3cr3t'))

totp = double
expect(totp).to receive(:now).and_return('totp_token')
expect(ROTP::TOTP).to receive(:new).with(totp_secret, hash_including(digest: 'sha256', digits: 8, interval: 30)).and_return(totp)

expect(headers_builder).to receive(:[]=).with('authorization', 'Token api_user:totp_token')

subject = CampactUserService::Client.new(host: 'demo.campact.de', topt_authorization: {user: 'api_user', secret: 'shh! a secret!'})
subject = CampactUserService::Client.new(host: 'demo.campact.de', basic_auth: {user: 'api_user', password: 's3cr3t'})

subject.get_request('/foo/bar')
end
Expand Down
Loading