Skip to content

Commit

Permalink
Merge pull request #1793 from microsoft/osose/rubywriters/features
Browse files Browse the repository at this point in the history
Address miscellaneous issues with Ruby Nethttp and Abstractions
  • Loading branch information
baywet authored Aug 25, 2022
2 parents 7886cb9 + 3826c01 commit 89f709a
Show file tree
Hide file tree
Showing 16 changed files with 42 additions and 25 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Added none output formatter to CLI commons. (Shell)

### Changed
- Fixed a bug to properly add request headers to Nethttp requests in Ruby.
- Fixed a bug to properly reject invalid URLs in Ruby.

- Fix issue with duplicate variable declaration in command handlers (Shell)
- Update namespace qualification algorithm (helps in resolving when a type name appears in multiple namespaces) to use case insensitive string comparison (CSharp).
Expand Down
4 changes: 2 additions & 2 deletions abstractions/ruby/microsoft_kiota_abstractions/Rakefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

require 'bundler/gem_tasks'
require 'rspec/core/rake_task'
require "bundler/gem_tasks"
require "rspec/core/rake_task"

RSpec::Core::RakeTask.new(:spec)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,17 @@ def allowed_hosts=(val)

# checks whether the provided host is valid
def url_host_valid?(url)
return false unless url =~ URI::DEFAULT_PARSER.regexp[:ABS_URI]

return true if @allowed_hosts.empty?

parsed_url = URI(url)

return false if parsed_url.host.nil?

return false unless parsed_url.is_a?(URI::HTTPS)

@allowed_hosts.key? parsed_url.host.downcase

rescue URI::InvalidURIError
false
end

# gets the list of valid hosts
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ def initialize(access_token_provider)
end

AUTHORIZATION_HEADER_KEY = 'Authorization'
def authenticate_request(request, additional_properties)
def authenticate_request(request, additional_properties = {})

raise StandardError, 'Request cannot be null' if request.nil?
return if request.headers.key?(AUTHORIZATION_HEADER_KEY)

token = @access_token_provider.get_authorization_token(request, additional_properties)
token = @access_token_provider.get_authorization_token(request.uri, additional_properties)

request.headers[AUTHORIZATION_HEADER_KEY] = "Bearer #{token}" unless token.nil?
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ def path_parameters
@path_parameters ||= Hash.new
end

def path_parameters=(value)
@path_parameters = value
end

def headers=(value)
@headers = value
end

def set_stream_content(value = $stdin)
@content = value
self.headers[@@content_type_header] = @@binary_content_type
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module MicrosoftKiotaAbstractions
module ParseNodeFactory
def get_parse_node(content_type, content)
def ParseNodeFactory.get_parse_node(content_type, content)
raise NotImplementedError.new
end
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module MicrosoftKiotaAbstractions
VERSION = "0.2.0"
VERSION = "0.3.0"
end
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@
url1 = ahv.url_host_valid?("https://www.google.com")
url2 = ahv.url_host_valid?("htts://google.com")
url3 = ahv.url_host_valid?("cool/groovy/art")
url4 = ahv.url_host_valid?("http://example.com")
url4 = ahv.url_host_valid?("https://example.com")
url5 = ahv.url_host_valid?('https%3A%2F%2Fwww.example.com')
url6 = ahv.url_host_valid?('%3A%2F%2F')
expect(url1).to eq(true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,17 @@ def initialize(token_request_context, allowed_hosts = [], scopes = [])
raise StandardError, 'Parameter token_request_context must be an instance of one of our grant flow context classes.'
end

@cached_token = nil
@cached_token = nil

@host_validator = if allowed_hosts.nil? || allowed_hosts.size.zero?
MicrsoftKiotaAbstractions::AllowedHostsValidator.new(['graph.microsoft.com', 'graph.microsoft.us', 'dod-graph.microsoft.us',
'graph.microsoft.de', 'microsoftgraph.chinacloudapi.cn',
'canary.graph.microsoft.com'])
else
MicrsoftKiotaAbstractions::AllocatedHostsValidator.new(allowed_hosts)
MicrosoftKiotaAbstractions::AllowedHostsValidator.new(allowed_hosts)
end

@token_request_context.initialize_oauth_provider
@token_request_context.initialize_scopes(scopes)

end

# This function obtains the authorization token.
Expand All @@ -55,7 +53,7 @@ def get_authorization_token(uri, additional_properties = {})

raise StandardError, 'Only https is supported' if parsed_url.scheme != 'https'

unless @cached_token.nil?
if @cached_token
token = OAuth2::AccessToken.from_hash(@token_request_context.oauth_provider, @cached_token)
return token.token if !token.nil? && !token.expired?

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module MicrosoftKiotaAuthenticationOauth
VERSION = "0.1.0"
module MicrosoftKiotaAuthenticationOAuth
VERSION = "0.2.0"
end
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ require_relative "lib/microsoft_kiota_authentication_oauth/version"

Gem::Specification.new do |spec|
spec.name = "microsoft_kiota_authentication_oauth"
spec.version = MicrosoftKiotaAuthenticationOauth::VERSION
spec.version = MicrosoftKiotaAuthenticationOAuth::VERSION
spec.authors = 'Microsoft Corporation'
spec.email = '[email protected]'
spec.description = 'Kiota Authentication implementation with oauth2'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class NetHttpRequestAdapter

# TODO: When #478 is implemented then parse_node_factory and serialization_writer_factory should default to nil
def initialize(authentication_provider, parse_node_factory, serialization_writer_factory, client = Net::HTTP)

if !authentication_provider
raise StandardError , 'authentication provider cannot be null'
end
Expand Down Expand Up @@ -41,20 +42,26 @@ def send_async(request_info, type, response_handler)
raise StandardError, 'requestInfo cannot be null'
end

self.authentication_provider.await.authenticate_request(request_info)
request = self.get_request_from_request_info(request_info)
uri = request_info.uri
@authentication_provider.await.authenticate_request(request_info)

http = @client.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
request = self.get_request_from_request_info(request_info)

request.add_field 'Authorization', request_info.headers['Authorization']
request.add_field 'Content-Type', 'application/json'
request.add_field 'Accept', 'application/json'
response = http.request(request)

if response_handler
return response_handler.await.handle_response_async(response);
else
payload = response.body
response_content_type = self.get_response_content_type(response);
if !response_content_type

unless response_content_type
raise StandardError, 'no response content type found for deserialization'
end
root_node = @parse_node_factory.get_parse_node(response_content_type, payload)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module MicrosoftKiotaNethttplibrary
VERSION = '0.1.7'
VERSION = '0.2.0'
end
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
require 'json'
require 'microsoft_kiota_abstractions'


module MicrosoftKiotaSerialization
class JsonParseNodeFactory
include MicrosoftKiotaAbstractions::ParseNodeFactory
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module MicrosoftKiotaSerialization
VERSION = "0.2.0"
VERSION = "0.3.0"
end
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
file = File.open("#{File.dirname(__FILE__)}/sample.json")
data = file.read
file.close
message_response = MicrosoftKiotaSerialization::JsonParseNodeFactory.new().get_parse_node("application/json", data)
message_response = MicrosoftKiotaSerialization::JsonParseNodeFactory.new.get_parse_node("application/json", data)
object_value = message_response.get_object_value(Files::MessagesResponse)

## Object Value tests
Expand Down

0 comments on commit 89f709a

Please sign in to comment.