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

RubyHttpClient: Add authorization by session_id #624

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
41 changes: 27 additions & 14 deletions lib/src/test/resources/example-response-with-unit-type.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand All @@ -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|
Expand Down Expand Up @@ -464,32 +470,39 @@ module Io
end

class AuthScheme

attr_reader :name

def initialize(name)
@name = HttpClient::Preconditions.check_not_blank('name', 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

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
Expand Down Expand Up @@ -566,4 +579,4 @@ module Io
end
end
end
end
end
41 changes: 27 additions & 14 deletions lib/src/test/resources/example-union-types-ruby-client.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand All @@ -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|
Expand Down Expand Up @@ -741,32 +747,39 @@ module Io
end

class AuthScheme

attr_reader :name

def initialize(name)
@name = HttpClient::Preconditions.check_not_blank('name', 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

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
Expand Down Expand Up @@ -846,4 +859,4 @@ module Io
end
end
end
end
end
41 changes: 27 additions & 14 deletions lib/src/test/resources/generators/ruby-built-in-types.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand All @@ -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|
Expand Down Expand Up @@ -669,32 +675,39 @@ module Apibuilder
end

class AuthScheme

attr_reader :name

def initialize(name)
@name = HttpClient::Preconditions.check_not_blank('name', 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

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
Expand Down Expand Up @@ -769,4 +782,4 @@ module Apibuilder
end

end
end
end
41 changes: 27 additions & 14 deletions lib/src/test/resources/ruby-client-generator-gilt-0.0.1-test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand All @@ -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|
Expand Down Expand Up @@ -918,32 +924,39 @@ module Io
end

class AuthScheme

attr_reader :name

def initialize(name)
@name = HttpClient::Preconditions.check_not_blank('name', 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

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
Expand Down Expand Up @@ -1022,4 +1035,4 @@ module Io
end
end
end
end
end
Loading