Skip to content

Commit

Permalink
Cleanup of authentication strategies
Browse files Browse the repository at this point in the history
  • Loading branch information
gridanjbf authored and lleirborras committed Apr 22, 2024
1 parent 3c07636 commit a4a75b5
Showing 1 changed file with 30 additions and 21 deletions.
51 changes: 30 additions & 21 deletions lib/sharepoint/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module Sharepoint
class Client
FILENAME_INVALID_CHARS = '~"#%&*:<>?/\{|}'

attr_accessor :token
attr_accessor :token

def authenticating(&block)
get_token
Expand Down Expand Up @@ -375,7 +375,7 @@ def upload(filename, content, path, site_path = nil)
path = path[1..-1] if path[0].eql?('/')
url = uri_escape "#{url}GetFolderByServerRelativeUrl('#{path}')/Files/Add(url='#{sanitized_filename}',overwrite=true)"
easy = ethon_easy_json_requester
easy.headers = with_authentication_header({ 'accept' => 'application/json;odata=verbose',
easy.headers = with_bearer_authentication_header({ 'accept' => 'application/json;odata=verbose',
'X-RequestDigest' => xrequest_digest(site_path) })
easy.http_request(url, :post, { body: content })
easy.perform
Expand Down Expand Up @@ -404,7 +404,7 @@ def update_metadata(filename, metadata, path, site_path = nil)
prepared_metadata = prepare_metadata(metadata, __metadata['type'])

easy = ethon_easy_json_requester
easy.headers = with_authentication_header({ 'accept' => 'application/json;odata=verbose',
easy.headers = with_bearer_authentication_header({ 'accept' => 'application/json;odata=verbose',
'content-type' => 'application/json;odata=verbose',
'X-RequestDigest' => xrequest_digest(site_path),
'X-Http-Method' => 'PATCH',
Expand Down Expand Up @@ -498,11 +498,21 @@ def process_url(url, fields)
end
end

def with_authentication_header(h)
h.merge(auth_header)
def token_auth?
config.authentication == 'token'
end

def auth_header
def ntlm_auth?
config.authentication == 'ntlm'
end

def with_bearer_authentication_header(h)
return h if ntlm_auth?

h.merge(bearer_auth_header)
end

def bearer_auth_header
{"Authorization" => bearer_auth }
end

Expand Down Expand Up @@ -532,7 +542,7 @@ def computed_web_api_url(site)

def ethon_easy_json_requester
easy = ethon_easy_requester
easy.headers = with_authentication_header({ 'accept'=> 'application/json;odata=verbose'})
easy.headers = with_bearer_authentication_header({ 'accept'=> 'application/json;odata=verbose'})
easy
end

Expand All @@ -541,16 +551,15 @@ def ethon_easy_options
end

def ethon_easy_requester
case config.authentication
when "token"
easy = Ethon::Easy.new({ followlocation: 1, maxredirs: 5 }.merge(ethon_easy_options))
easy.headers = auth_header
easy
when "ntlm"
easy = Ethon::Easy.new({ httpauth: :ntlm, followlocation: 1, maxredirs: 5 }.merge(ethon_easy_options))
easy.username = config.username
easy.password = config.password
easy
if token_auth?
easy = Ethon::Easy.new({ followlocation: 1, maxredirs: 5 }.merge(ethon_easy_options))
easy.headers = with_bearer_authentication_header({})
easy
elsif ntlm_auth?
easy = Ethon::Easy.new({ httpauth: :ntlm, followlocation: 1, maxredirs: 5 }.merge(ethon_easy_options))
easy.username = config.username
easy.password = config.password
easy
end
end

Expand Down Expand Up @@ -626,11 +635,11 @@ def validate_token_config
def validate_ntlm_config
valid_config_options( %i(username password) )
end

def valid_config_options(options = [])
options.map do |opt|
c = config.send(opt)

next if c.present? && string_not_blank?(c)
opt
end.compact
Expand All @@ -651,7 +660,7 @@ def validate_config!

raise Errors::InvalidNTLMConfigError.new(invalid_ntlm_opts) unless invalid_ntlm_opts.empty?
end

raise Errors::UriConfigurationError.new unless valid_uri?(config.uri)
raise Errors::EthonOptionsConfigurationError.new unless ethon_easy_options.is_a?(Hash)
end
Expand Down Expand Up @@ -825,7 +834,7 @@ def update_object_metadata(metadata, new_metadata, site_path = '')
prepared_metadata = prepare_metadata(new_metadata, metadata['type'])

easy = ethon_easy_json_requester
easy.headers = with_authentication_header({ 'accept' => 'application/json;odata=verbose',
easy.headers = with_bearer_authentication_header({ 'accept' => 'application/json;odata=verbose',
'content-type' => 'application/json;odata=verbose',
'X-RequestDigest' => xrequest_digest(site_path),
'X-Http-Method' => 'PATCH',
Expand Down

0 comments on commit a4a75b5

Please sign in to comment.