From 899dcf961e706b7bd0a18a40f47cfa35a618b3f3 Mon Sep 17 00:00:00 2001 From: Fannur Date: Mon, 1 Mar 2021 18:05:29 +0300 Subject: [PATCH 1/8] fixed auth with token --- SlackAPI/SlackClient.cs | 9 +-------- SlackAPI/SlackClientBase.cs | 26 ++++++++++++++++++++++---- SlackAPI/SlackTaskClient.cs | 6 +----- 3 files changed, 24 insertions(+), 17 deletions(-) diff --git a/SlackAPI/SlackClient.cs b/SlackAPI/SlackClient.cs index 8857b9ef..63ec2445 100644 --- a/SlackAPI/SlackClient.cs +++ b/SlackAPI/SlackClient.cs @@ -108,14 +108,7 @@ protected virtual void Connected(LoginResponse loginDetails) public void APIRequestWithToken(Action callback, params Tuple[] getParameters) where K : Response { - Tuple[] tokenArray = new Tuple[]{ - new Tuple("token", APIToken) - }; - - if (getParameters != null && getParameters.Length > 0) - tokenArray = tokenArray.Concat(getParameters).ToArray(); - - APIRequest(callback, tokenArray, new Tuple[0]); + APIRequest(callback, getParameters, new Tuple[0], APIToken); } public void TestAuth(Action callback) diff --git a/SlackAPI/SlackClientBase.cs b/SlackAPI/SlackClientBase.cs index 5b2f08f4..8581f9df 100644 --- a/SlackAPI/SlackClientBase.cs +++ b/SlackAPI/SlackClientBase.cs @@ -33,7 +33,11 @@ protected SlackClientBase(IWebProxy proxySettings) protected Uri GetSlackUri(string path, Tuple[] getParameters) { - string parameters = getParameters + string parameters = default; + + if (getParameters != null && getParameters.Length > 0) + { + parameters = getParameters .Where(x => x.Item2 != null) .Select(new Func, string>(a => { @@ -54,11 +58,19 @@ protected Uri GetSlackUri(string path, Tuple[] getParameters) return string.Format("{0}&{1}", a, b); }); - Uri requestUri = new Uri(string.Format("{0}?{1}", path, parameters)); + } + + Uri requestUri = default; + + if (!string.IsNullOrEmpty(parameters)) + requestUri = new Uri(string.Format("{0}?{1}", path, parameters)); + else + requestUri = new Uri(path); + return requestUri; } - protected void APIRequest(Action callback, Tuple[] getParameters, Tuple[] postParameters) + protected void APIRequest(Action callback, Tuple[] getParameters, Tuple[] postParameters, string token = "") where K : Response { RequestPath path = RequestPath.GetRequestPath(); @@ -68,12 +80,15 @@ protected void APIRequest(Action callback, Tuple[] getPara Uri requestUri = GetSlackUri(Path.Combine(APIBaseLocation, path.Path), getParameters); HttpWebRequest request = CreateWebRequest(requestUri); + if (!string.IsNullOrEmpty(token)) + request.Headers.Add("Authorization", "Bearer " + token); + //This will handle all of the processing. RequestState state = new RequestState(request, postParameters, callback); state.Begin(); } - public Task APIRequestAsync(Tuple[] getParameters, Tuple[] postParameters) + public Task APIRequestAsync(Tuple[] getParameters, Tuple[] postParameters, string token = "") where K : Response { RequestPath path = RequestPath.GetRequestPath(); @@ -83,6 +98,9 @@ public Task APIRequestAsync(Tuple[] getParameters, Tuple(request, postParameters); return state.Execute(); diff --git a/SlackAPI/SlackTaskClient.cs b/SlackAPI/SlackTaskClient.cs index e87f65fb..fcb87a4f 100644 --- a/SlackAPI/SlackTaskClient.cs +++ b/SlackAPI/SlackTaskClient.cs @@ -91,11 +91,7 @@ public Task APIRequestWithTokenAsync() public Task APIRequestWithTokenAsync(params Tuple[] postParameters) where K : Response { - Tuple[] tokenArray = new Tuple[]{ - new Tuple("token", APIToken) - }; - - return APIRequestAsync(tokenArray, postParameters); + return APIRequestAsync(new Tuple[] { }, postParameters, APIToken); } public Task TestAuthAsync() From b4434483f9e36907eaf2d8b3f4b5a6c5fcf2e056 Mon Sep 17 00:00:00 2001 From: Fannur Date: Mon, 1 Mar 2021 19:28:27 +0300 Subject: [PATCH 2/8] added conversation list test --- SlackAPI.Tests/Conversations.cs | 41 +++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 SlackAPI.Tests/Conversations.cs diff --git a/SlackAPI.Tests/Conversations.cs b/SlackAPI.Tests/Conversations.cs new file mode 100644 index 00000000..3c8b74dc --- /dev/null +++ b/SlackAPI.Tests/Conversations.cs @@ -0,0 +1,41 @@ +using System.Linq; +using SlackAPI.RPCMessages; +using SlackAPI.Tests.Configuration; +using SlackAPI.Tests.Helpers; +using Xunit; + +namespace SlackAPI.Tests +{ + [Collection("Integration tests")] + public class Conversations + { + private readonly IntegrationFixture fixture; + + public Conversations(IntegrationFixture fixture) + { + this.fixture = fixture; + } + [Fact] + public void ConversationList() + { + var client = this.fixture.UserClient; + ConversationsListResponse actual = null; + using (var sync = new InSync(nameof(SlackClient.ChannelLookup))) + { + client.GetConversationsList(response => + { + actual = response; + sync.Proceed(); + }); + } + + Assert.True(actual.ok, "Error while fetching conversation list."); + Assert.True(actual.channels.Any()); + + // check to null + var someChannel = actual.channels.First(); + Assert.NotNull(someChannel.id); + Assert.NotNull(someChannel.name); + } + } +} \ No newline at end of file From ffb101ab0e85bb3ec2fa94773a75e34fdcbd1a59 Mon Sep 17 00:00:00 2001 From: Fannur Date: Mon, 1 Mar 2021 19:49:55 +0300 Subject: [PATCH 3/8] added is_im property --- SlackAPI/Channel.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/SlackAPI/Channel.cs b/SlackAPI/Channel.cs index 7335e8bd..add01e3d 100644 --- a/SlackAPI/Channel.cs +++ b/SlackAPI/Channel.cs @@ -16,6 +16,7 @@ public class Channel : Conversation public bool is_general; public bool is_channel; public bool is_group; + public bool is_im; //Is this deprecated by is_open? public bool IsPrivateGroup { get { return id != null && id[0] == 'G'; } } From bbff3354a74b809e71f497ba4bfb5d6cd17c0404 Mon Sep 17 00:00:00 2001 From: Fannur Date: Mon, 1 Mar 2021 21:51:18 +0300 Subject: [PATCH 4/8] added user property to channel --- SlackAPI/Channel.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/SlackAPI/Channel.cs b/SlackAPI/Channel.cs index add01e3d..c45d1c23 100644 --- a/SlackAPI/Channel.cs +++ b/SlackAPI/Channel.cs @@ -10,6 +10,7 @@ public class Channel : Conversation { public string name; public string creator; + public string user; public bool is_archived; public bool is_member; From 9ba99aebeaa66af353dbe93d92f8cfa087eeb4a6 Mon Sep 17 00:00:00 2001 From: Fannur Date: Mon, 1 Mar 2021 21:51:44 +0300 Subject: [PATCH 5/8] fixed upload file method --- SlackAPI/SlackClient.cs | 3 +-- SlackAPI/SlackClientBase.cs | 14 ++++++++++++-- SlackAPI/SlackTaskClient.cs | 3 +-- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/SlackAPI/SlackClient.cs b/SlackAPI/SlackClient.cs index 63ec2445..fe47bea2 100644 --- a/SlackAPI/SlackClient.cs +++ b/SlackAPI/SlackClient.cs @@ -766,7 +766,6 @@ public void UploadFile(Action callback, byte[] fileData, str Uri target = new Uri(Path.Combine(APIBaseLocation, useAsync ? "files.uploadAsync" : "files.upload")); List parameters = new List(); - parameters.Add(string.Format("token={0}", APIToken)); //File/Content if (!string.IsNullOrEmpty(fileType)) @@ -786,7 +785,7 @@ public void UploadFile(Action callback, byte[] fileData, str using (MultipartFormDataContent form = new MultipartFormDataContent()) { form.Add(new ByteArrayContent(fileData), "file", fileName); - HttpResponseMessage response = PostRequest(string.Format("{0}?{1}", target, string.Join("&", parameters.ToArray())), form); + HttpResponseMessage response = PostRequestAsync(string.Format("{0}?{1}", target, string.Join("&", parameters.ToArray())), form, APIToken).Result; string result = response.Content.ReadAsStringAsync().Result; callback(result.Deserialize()); } diff --git a/SlackAPI/SlackClientBase.cs b/SlackAPI/SlackClientBase.cs index 8581f9df..1e6d357b 100644 --- a/SlackAPI/SlackClientBase.cs +++ b/SlackAPI/SlackClientBase.cs @@ -3,6 +3,7 @@ using System.Linq; using System.Net; using System.Net.Http; +using System.Net.Http.Headers; using System.Threading.Tasks; using Newtonsoft.Json; @@ -129,9 +130,18 @@ protected HttpWebRequest CreateWebRequest(Uri requestUri) return httpWebRequest; } - protected HttpResponseMessage PostRequest(string requestUri, MultipartFormDataContent form) + protected Task PostRequestAsync(string requestUri, MultipartFormDataContent form, string token) { - return httpClient.PostAsync(requestUri, form).Result; + var requestMessage = new HttpRequestMessage + { + Method = HttpMethod.Post, + Content = form, + RequestUri = new Uri(requestUri), + }; + + requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token); + + return httpClient.SendAsync(requestMessage); } public void RegisterConverter(JsonConverter converter) diff --git a/SlackAPI/SlackTaskClient.cs b/SlackAPI/SlackTaskClient.cs index fcb87a4f..5f23204d 100644 --- a/SlackAPI/SlackTaskClient.cs +++ b/SlackAPI/SlackTaskClient.cs @@ -705,7 +705,6 @@ public async Task UploadFileAsync(byte[] fileData, string fi Uri target = new Uri(Path.Combine(APIBaseLocation, useAsync ? "files.uploadAsync" : "files.upload")); List parameters = new List(); - parameters.Add(string.Format("token={0}", APIToken)); //File/Content if (!string.IsNullOrEmpty(fileType)) @@ -725,7 +724,7 @@ public async Task UploadFileAsync(byte[] fileData, string fi using (MultipartFormDataContent form = new MultipartFormDataContent()) { form.Add(new ByteArrayContent(fileData), "file", fileName); - HttpResponseMessage response = PostRequest(string.Format("{0}?{1}", target, string.Join("&", parameters.ToArray())), form); + HttpResponseMessage response = await PostRequestAsync(string.Format("{0}?{1}", target, string.Join("&", parameters.ToArray())), form, APIToken); string result = await response.Content.ReadAsStringAsync().ConfigureAwait(false); return result.Deserialize(); } From 092bbf6a3e13626f69f0e12dfc68c4b85092c055 Mon Sep 17 00:00:00 2001 From: Fannur Date: Tue, 2 Mar 2021 18:15:39 +0300 Subject: [PATCH 6/8] fixed UserUIInteraction test --- SlackAPI.Tests/UserUIInteraction.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SlackAPI.Tests/UserUIInteraction.cs b/SlackAPI.Tests/UserUIInteraction.cs index 60b7faf8..9323fd97 100644 --- a/SlackAPI.Tests/UserUIInteraction.cs +++ b/SlackAPI.Tests/UserUIInteraction.cs @@ -52,7 +52,7 @@ public void TestGetAccessToken() var accessTokenResponse = GetAccessToken(slackClientHelpers, clientId, clientSecret, redirectUrl, code); Assert.True(accessTokenResponse.ok); - Assert.Equal("identify", accessTokenResponse.scope); + Assert.Equal(accessTokenResponse.access_token, this.fixture.Config.UserAuthToken); } } From 00b0d8de273309d7da87282320605aa201be3217 Mon Sep 17 00:00:00 2001 From: Fannur Date: Tue, 9 Mar 2021 15:15:18 +0300 Subject: [PATCH 7/8] fix channel members --- SlackAPI/Channel.cs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/SlackAPI/Channel.cs b/SlackAPI/Channel.cs index 8b3cb381..c45d1c23 100644 --- a/SlackAPI/Channel.cs +++ b/SlackAPI/Channel.cs @@ -26,9 +26,5 @@ public class Channel : Conversation public OwnedStampedMessage purpose; public string[] members; - - //im related properties - public bool is_im; - public string user; } } From bebd5009443f51fa7657d61776e3b5e425b93bca Mon Sep 17 00:00:00 2001 From: Fannur Date: Tue, 9 Mar 2021 15:21:57 +0300 Subject: [PATCH 8/8] fix UserUIIntercation test --- SlackAPI.Tests/UserUIInteraction.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SlackAPI.Tests/UserUIInteraction.cs b/SlackAPI.Tests/UserUIInteraction.cs index 9323fd97..0ece63cf 100644 --- a/SlackAPI.Tests/UserUIInteraction.cs +++ b/SlackAPI.Tests/UserUIInteraction.cs @@ -52,7 +52,7 @@ public void TestGetAccessToken() var accessTokenResponse = GetAccessToken(slackClientHelpers, clientId, clientSecret, redirectUrl, code); Assert.True(accessTokenResponse.ok); - Assert.Equal(accessTokenResponse.access_token, this.fixture.Config.UserAuthToken); + Assert.Contains("identify", accessTokenResponse.scope); } }