From 2ffb6b4fefc3982f431f08206a35569815b5f283 Mon Sep 17 00:00:00 2001 From: Aurel Branzeanu Date: Fri, 25 Dec 2020 00:55:12 +0200 Subject: [PATCH 1/2] [TEC-3472] Add client authorization by session_id --- lib/flow_commerce/client.rb | 51 ++++++++++++++----------- lib/flow_commerce/flow_api_v0_client.rb | 40 ++++++++++++------- 2 files changed, 54 insertions(+), 37 deletions(-) diff --git a/lib/flow_commerce/client.rb b/lib/flow_commerce/client.rb index c9da58f..9ab9a6c 100644 --- a/lib/flow_commerce/client.rb +++ b/lib/flow_commerce/client.rb @@ -1,8 +1,7 @@ module FlowCommerce - DEFAULT_TOKEN_FILE_LOCATION = "~/.flow/token" - # Creates a new instance of the flow cient, using standard + # Creates a new instance of the flow client, using standard # conventions to identify the API TOKEN, checking in order: # # 1. an environment variable named FLOW_TOKEN @@ -10,39 +9,45 @@ module FlowCommerce # the path of the file with the token in it # # @param base_url Alternate URL for the API - def FlowCommerce.instance(opts={}) - base_url = opts[:base_url].to_s.strip - token = opts[:token].to_s.strip - http_handler = opts[:http_handler] + def FlowCommerce.instance(opts = {}) + session_id = opts[:session_id].to_s.strip - if token.empty? - token = ENV['FLOW_TOKEN'].to_s.strip + if session_id.length > 0 + auth = Io::Flow::V0::HttpClient::Authorization.session(session_id) + else + token = opts[:token].to_s.strip if token.empty? - file = ENV['FLOW_TOKEN_FILE'].to_s.strip - if file.empty? - file = DEFAULT_TOKEN_FILE_LOCATION - end - path = File.expand_path(file) + token = ENV['FLOW_TOKEN'].to_s.strip - if !File.exists?(path) - raise "File %s does not exist. You can specify environment variable FLOW_TOKEN or FLOW_TOKEN_FILE to explicitly provide the token" % path - end - - token = IO.read(path).strip if token.empty? - raise "File %s did not contain an API Token" % path + file = ENV['FLOW_TOKEN_FILE'].to_s.strip + if file.empty? + file = DEFAULT_TOKEN_FILE_LOCATION + end + path = File.expand_path(file) + + unless File.exists?(path) + raise "File #{path} does not exist. You can specify environment variable FLOW_TOKEN or FLOW_TOKEN_FILE to explicitly provide the token" + end + + token = IO.read(path).strip + if token.empty? + raise "File #{path} did not contain an API Token" + end end end + + auth = Io::Flow::V0::HttpClient::Authorization.basic(token) end - auth = Io::Flow::V0::HttpClient::Authorization.basic(token) + base_url = opts[:base_url].to_s.strip + http_handler = opts[:http_handler] if base_url.empty? - Io::Flow::V0::Client.at_base_url(:authorization => auth, :http_handler => http_handler) + Io::Flow::V0::Client.at_base_url(authorization: auth, http_handler: http_handler) else - Io::Flow::V0::Client.new(base_url, :authorization => auth, :http_handler => http_handler) + Io::Flow::V0::Client.new(base_url, authorization: auth, http_handler: http_handler) end end - end diff --git a/lib/flow_commerce/flow_api_v0_client.rb b/lib/flow_commerce/flow_api_v0_client.rb index 97748c6..be23473 100644 --- a/lib/flow_commerce/flow_api_v0_client.rb +++ b/lib/flow_commerce/flow_api_v0_client.rb @@ -55258,7 +55258,8 @@ def with_auth(auth) Preconditions.assert_class('auth', auth, HttpClient::Authorization) Preconditions.check_state(@auth.nil?, "auth previously set") - if auth.scheme.name == AuthScheme::BASIC.name + case auth.scheme.name + when AuthScheme::BASIC.name, AuthScheme::SESSION.name @auth = auth else raise "Auth Scheme[#{auth.scheme.name}] not supported" @@ -55318,8 +55319,8 @@ def do_request(klass) Preconditions.assert_class('klass', klass, Class) uri = @full_uri.dup - if q = to_query(@params) - uri += "?%s" % q + if (q = to_query(@params)) + uri += "?#{q}" end request = klass.send(:new, uri) @@ -55340,9 +55341,14 @@ def do_request(klass) # DEBUG curl << "-u \"%s:%s\"" % [@auth.username, @auth.password] Preconditions.check_state(!@header_keys_lower_case.include?("authorization"), "Cannot specify both an Authorization header and an auth instance") - user_pass = "%s:%s" % [@auth.username, @auth.password] - encoded = Base64.encode64(user_pass).to_s.split("\n").map(&:strip).join - request.add_field("Authorization", "Basic %s" % encoded) + session_id = @auth.session_id.to_s.strip + if session_id.length > 0 + request.add_field("Authorization", "Session #{session_id}") + else + user_pass = "#{@auth.username}:#{@auth.password}" + encoded = Base64.encode64(user_pass).to_s.split("\n").map(&:strip).join + request.add_field("Authorization", "Basic #{encoded}") + end end @headers.each { |key, value| @@ -55510,7 +55516,6 @@ def Preconditions.assert_hash_of_class(field_name, hash, klass) end class AuthScheme - attr_reader :name def initialize(name) @@ -55518,17 +55523,21 @@ def initialize(name) end BASIC = AuthScheme.new("basic") unless defined?(BASIC) - + SESSION = AuthScheme.new("session") unless defined?(SESSION) end class Authorization + attr_reader :scheme, :username, :password, :session_id - attr_reader :scheme, :username, :password - - def initialize(scheme, username, opts={}) + def initialize(scheme, username = nil, opts={}) @scheme = HttpClient::Preconditions.assert_class('schema', scheme, AuthScheme) - @username = HttpClient::Preconditions.check_not_blank('username', username, "username is required") - @password = HttpClient::Preconditions.assert_class_or_nil('password', opts.delete(:password), String) + if scheme.name == AuthScheme::BASIC.name + @username = HttpClient::Preconditions.check_not_blank('username', username, "username is required") + @password = HttpClient::Preconditions.assert_class_or_nil('password', opts.delete(:password), String) + elsif scheme.name == AuthScheme::SESSION.name + @session_id = HttpClient::Preconditions.assert_class_or_nil('session_id', opts.delete(:session_id), String) + end + HttpClient::Preconditions.assert_empty_opts(opts) end @@ -55536,6 +55545,9 @@ def Authorization.basic(username, password=nil) Authorization.new(AuthScheme::BASIC, username, :password => password) end + def Authorization.session(session_id) + Authorization.new(AuthScheme::SESSION, nil, :session_id => session_id) + end end module Helper @@ -55612,4 +55624,4 @@ def Helper.to_boolean(field_name, value) end end end -end \ No newline at end of file +end From b5b7f888507e89d163db453b9ce411cfed7f1c13 Mon Sep 17 00:00:00 2001 From: Aurel Branzeanu Date: Fri, 25 Dec 2020 01:09:30 +0200 Subject: [PATCH 2/2] Fix bundler to process gem version from version.rb for our Mejuri fork (flowcommerce is relying on git tags only) --- flowcommerce.gemspec | 7 ++++++- lib/flow_commerce/version.rb | 3 +++ 2 files changed, 9 insertions(+), 1 deletion(-) create mode 100644 lib/flow_commerce/version.rb diff --git a/flowcommerce.gemspec b/flowcommerce.gemspec index a8174b6..b315703 100644 --- a/flowcommerce.gemspec +++ b/flowcommerce.gemspec @@ -1,7 +1,12 @@ +$:.push File.expand_path('../lib', __FILE__) + +# Maintain your gem's version: +require 'flow_commerce/version' + Gem::Specification.new do |s| s.name = 'flowcommerce' s.homepage = "https://github.com/flowcommerce/ruby-sdk" - s.version = `sem-info tag latest`.strip + s.version = FlowCommerce::VERSION s.date = Time.now.strftime('%Y-%m-%d') s.summary = "Native ruby client for the Flow REST API." s.description = "Native ruby client for the Flow REST API. Detailed information at https://app.apibuilder.io/flow/api" diff --git a/lib/flow_commerce/version.rb b/lib/flow_commerce/version.rb new file mode 100644 index 0000000..8a0d8c0 --- /dev/null +++ b/lib/flow_commerce/version.rb @@ -0,0 +1,3 @@ +module FlowCommerce + VERSION = '0.2.89' +end