From 67f95ee57517aa610b7c6b2c9584d02345f47ba0 Mon Sep 17 00:00:00 2001 From: Aurel Branzeanu Date: Sun, 24 Jan 2021 02:05:59 +0200 Subject: [PATCH 1/2] RubyHttpClient: Add authorization by session_id --- .../main/scala/models/RubyHttpClient.scala | 39 ++++++++++++------- 1 file changed, 26 insertions(+), 13 deletions(-) diff --git a/ruby-generator/src/main/scala/models/RubyHttpClient.scala b/ruby-generator/src/main/scala/models/RubyHttpClient.scala index 0cd023f81..15c3bd339 100644 --- a/ruby-generator/src/main/scala/models/RubyHttpClient.scala +++ b/ruby-generator/src/main/scala/models/RubyHttpClient.scala @@ -120,7 +120,8 @@ module HttpClient 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" @@ -180,8 +181,8 @@ module HttpClient 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) @@ -202,9 +203,14 @@ module HttpClient # 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| @@ -372,7 +378,6 @@ module HttpClient end class AuthScheme - attr_reader :name def initialize(name) @@ -380,17 +385,22 @@ module HttpClient 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 @@ -398,6 +408,9 @@ module HttpClient 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 From 927cd66e718f199b78ab64fd80737603456af677 Mon Sep 17 00:00:00 2001 From: Aurel Branzeanu Date: Sun, 24 Jan 2021 03:27:05 +0200 Subject: [PATCH 2/2] Regenerate tests' responses --- .../example-response-with-unit-type.txt | 41 ++++++++++++------- .../example-union-types-ruby-client.txt | 41 ++++++++++++------- .../generators/ruby-built-in-types.txt | 41 ++++++++++++------- .../ruby-client-generator-gilt-0.0.1-test.txt | 41 ++++++++++++------- 4 files changed, 108 insertions(+), 56 deletions(-) diff --git a/lib/src/test/resources/example-response-with-unit-type.txt b/lib/src/test/resources/example-response-with-unit-type.txt index 9cc07f02f..bb3d65ed9 100644 --- a/lib/src/test/resources/example-response-with-unit-type.txt +++ b/lib/src/test/resources/example-response-with-unit-type.txt @@ -212,7 +212,8 @@ module Io 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" @@ -272,8 +273,8 @@ module Io 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) @@ -294,9 +295,14 @@ module Io # 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| @@ -464,7 +470,6 @@ module Io end class AuthScheme - attr_reader :name def initialize(name) @@ -472,17 +477,22 @@ module Io 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 @@ -490,6 +500,9 @@ module Io 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 @@ -566,4 +579,4 @@ module Io end end end -end +end \ No newline at end of file diff --git a/lib/src/test/resources/example-union-types-ruby-client.txt b/lib/src/test/resources/example-union-types-ruby-client.txt index 318d37f11..f93913ec6 100644 --- a/lib/src/test/resources/example-union-types-ruby-client.txt +++ b/lib/src/test/resources/example-union-types-ruby-client.txt @@ -489,7 +489,8 @@ module Io 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" @@ -549,8 +550,8 @@ module Io 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) @@ -571,9 +572,14 @@ module Io # 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| @@ -741,7 +747,6 @@ module Io end class AuthScheme - attr_reader :name def initialize(name) @@ -749,17 +754,22 @@ module Io 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 @@ -767,6 +777,9 @@ module Io 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 @@ -846,4 +859,4 @@ module Io end end end -end +end \ No newline at end of file diff --git a/lib/src/test/resources/generators/ruby-built-in-types.txt b/lib/src/test/resources/generators/ruby-built-in-types.txt index b79872b2f..60522771a 100644 --- a/lib/src/test/resources/generators/ruby-built-in-types.txt +++ b/lib/src/test/resources/generators/ruby-built-in-types.txt @@ -417,7 +417,8 @@ module Apibuilder 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" @@ -477,8 +478,8 @@ module Apibuilder 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) @@ -499,9 +500,14 @@ module Apibuilder # 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| @@ -669,7 +675,6 @@ module Apibuilder end class AuthScheme - attr_reader :name def initialize(name) @@ -677,17 +682,22 @@ module Apibuilder 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 @@ -695,6 +705,9 @@ module Apibuilder 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 @@ -769,4 +782,4 @@ module Apibuilder end end -end +end \ No newline at end of file diff --git a/lib/src/test/resources/ruby-client-generator-gilt-0.0.1-test.txt b/lib/src/test/resources/ruby-client-generator-gilt-0.0.1-test.txt index c9a8dde95..c572be22c 100644 --- a/lib/src/test/resources/ruby-client-generator-gilt-0.0.1-test.txt +++ b/lib/src/test/resources/ruby-client-generator-gilt-0.0.1-test.txt @@ -666,7 +666,8 @@ module Io 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" @@ -726,8 +727,8 @@ module Io 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) @@ -748,9 +749,14 @@ module Io # 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| @@ -918,7 +924,6 @@ module Io end class AuthScheme - attr_reader :name def initialize(name) @@ -926,17 +931,22 @@ module Io 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 @@ -944,6 +954,9 @@ module Io 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 @@ -1022,4 +1035,4 @@ module Io end end end -end +end \ No newline at end of file