From 9b5759097699da498b4e2b480ea151d74425e033 Mon Sep 17 00:00:00 2001 From: Danilo Grieco Date: Mon, 22 Apr 2024 16:02:57 +0200 Subject: [PATCH] Use autentication header when strategy is token --- lib/sharepoint/client.rb | 15 +++++++++------ lib/sharepoint/client/token.rb | 3 +-- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/lib/sharepoint/client.rb b/lib/sharepoint/client.rb index 985f2b5..9319386 100644 --- a/lib/sharepoint/client.rb +++ b/lib/sharepoint/client.rb @@ -12,7 +12,7 @@ module Sharepoint class Client FILENAME_INVALID_CHARS = '~"#%&*:<>?/\{|}' - def authenticating + def authenticating_with_token generate_new_token yield end @@ -326,11 +326,11 @@ def create_folder(name, path, site_path = nil) path = path[1..-1] if path[0].eql?('/') url = uri_escape "#{url}GetFolderByServerRelativeUrl('#{path}')/Folders" easy = ethon_easy_json_requester - easy.headers = { + easy.headers = with_bearer_authentication_header({ 'accept' => 'application/json;odata=verbose', 'content-type' => 'application/json;odata=verbose', 'X-RequestDigest' => xrequest_digest(site_path) - } + }) payload = { '__metadata' => { 'type' => 'SP.Folder' @@ -539,6 +539,7 @@ def computed_web_api_url(site) end def ethon_easy_json_requester + easy = ethon_easy_requester easy.headers = with_bearer_authentication_header({ 'accept' => 'application/json;odata=verbose' }) easy @@ -550,9 +551,11 @@ def ethon_easy_options def ethon_easy_requester if token_auth? - easy = Ethon::Easy.new({ followlocation: 1, maxredirs: 5 }.merge(ethon_easy_options)) - easy.headers = with_bearer_authentication_header({}) - easy + authenticating_with_token do + easy = Ethon::Easy.new({ followlocation: 1, maxredirs: 5 }.merge(ethon_easy_options)) + easy.headers = with_bearer_authentication_header({}) + easy + end elsif ntlm_auth? easy = Ethon::Easy.new({ httpauth: :ntlm, followlocation: 1, maxredirs: 5 }.merge(ethon_easy_options)) easy.username = config.username diff --git a/lib/sharepoint/client/token.rb b/lib/sharepoint/client/token.rb index 05c226b..6a0f060 100644 --- a/lib/sharepoint/client/token.rb +++ b/lib/sharepoint/client/token.rb @@ -40,8 +40,7 @@ def expired? (fetched_at + expires_in) < Time.now.utc.to_i end - def - def(_request_new_token) + def request_new_token auth_request = { client_id: config.client_id, client_secret: config.client_secret,