Skip to content

Commit

Permalink
Merge pull request #90 from objmj-itminds/feature/openid-signin
Browse files Browse the repository at this point in the history
Sign in with Slack support via Open ID api methods
  • Loading branch information
soxtoby authored Sep 23, 2021
2 parents db51f53 + 7fd3334 commit ce9024f
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 0 deletions.
1 change: 1 addition & 0 deletions SlackNet.Tests/ApiLintTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ public ISlackApiClient WithAccessToken(string accessToken)
public IMigrationApi Migration { get; }
public IOAuthApi OAuth { get; }
public IOAuthV2Api OAuthV2 { get; }
public IOpenIdApi OpenIdApi { get; }
public IPinsApi Pins { get; }
public IReactionsApi Reactions { get; }
public IRemindersApi Reminders { get; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -969,6 +969,7 @@ protected class TestApiClient : ISlackApiClient
public IMigrationApi Migration { get; }
public IOAuthApi OAuth { get; }
public IOAuthV2Api OAuthV2 { get; }
public IOpenIdApi OpenIdApi { get; }
public IPinsApi Pins { get; }
public IReactionsApi Reactions { get; }
public IRemindersApi Reminders { get; }
Expand Down
2 changes: 2 additions & 0 deletions SlackNet/SlackApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public interface ISlackApiClient
IMigrationApi Migration { get; }
IOAuthApi OAuth { get; }
IOAuthV2Api OAuthV2 { get; }
IOpenIdApi OpenIdApi { get; }
IPinsApi Pins { get; }
IReactionsApi Reactions { get; }
IRemindersApi Reminders { get; }
Expand Down Expand Up @@ -161,6 +162,7 @@ public SlackApiClient(IHttp http, ISlackUrlBuilder urlBuilder, SlackJsonSettings
public IMigrationApi Migration => new MigrationApi(this);
public IOAuthApi OAuth => new OAuthApi(this);
public IOAuthV2Api OAuthV2 => new OAuthV2Api(this);
public IOpenIdApi OpenIdApi => new OpenIdApi(this);
public IPinsApi Pins => new PinsApi(this);
public IReactionsApi Reactions => new ReactionsApi(this);
public IRemindersApi Reminders => new RemindersApi(this);
Expand Down
92 changes: 92 additions & 0 deletions SlackNet/WebApi/OpenIdApi.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
using System.Threading;
using System.Threading.Tasks;
using Args = System.Collections.Generic.Dictionary<string, object>;

namespace SlackNet.WebApi
{
public interface IOpenIdApi
{
/// <summary>
/// Exchanges a temporary OAuth verifier code for an access token for Sign in with Slack.
/// See https://api.slack.com/methods/openid.connect.token for more information.
/// </summary>
/// <param name="clientId">Issued when you created your application.</param>
/// <param name="clientSecret">Issued when you created your application.</param>
/// <param name="code">The code param returned via the OAuth callback.</param>
/// <param name="grantType">The grant_type param as described in the OAuth spec.</param>
/// <param name="redirectUrl">This must match the originally submitted URI (if one was sent).</param>
/// <param name="refreshToken">The refresh_token param as described in the OAuth spec.</param>
/// <param name="cancellationToken"></param>
Task<OpenIdTokenResponse> Token(
string clientId,
string clientSecret,
#nullable enable
string? code,
string? grantType,
string? redirectUrl,
string? refreshToken,
#nullable disable
CancellationToken? cancellationToken
);

/// <summary>
/// Get the identity of a user who has authorized Sign in with Slack.
/// See https://api.slack.com/methods/openid.connect.userInfo for more information.
/// Note: that this method uses an Authentication token passed in the header.
/// Use <c>SlackApiClient.WithAccessToken</c> to specify a token.
/// </summary>
/// <param name="token">Authentication token bearing required scopes. Tokens should be passed as an HTTP Authorization header or alternatively, as a POST parameter.</param>
/// <param name="cancellationToken"></param>
Task<OpenIdUserInfoResponse> UserInfo(CancellationToken? cancellationToken);

}

public class OpenIdApi : IOpenIdApi
{
private readonly ISlackApiClient _client;
public OpenIdApi(ISlackApiClient client) => _client = client;

/// <summary>
/// Exchanges a temporary OAuth verifier code for an access token for Sign in with Slack.
/// See https://api.slack.com/methods/openid.connect.token for more information.
/// </summary>
/// <param name="clientId">Issued when you created your application.</param>
/// <param name="clientSecret">Issued when you created your application.</param>
/// <param name="code">The code param returned via the OAuth callback.</param>
/// <param name="grantType">The grant_type param as described in the OAuth spec.</param>
/// <param name="redirectUrl">This must match the originally submitted URI (if one was sent).</param>
/// <param name="refreshToken">The refresh_token param as described in the OAuth spec.</param>
/// <param name="cancellationToken"></param>
public Task<OpenIdTokenResponse> Token(
string clientId,
string clientSecret,
#nullable enable
string? code,
string? grantType,
string? redirectUrl,
string? refreshToken,
#nullable disable
CancellationToken? cancellationToken = null
) => _client.Post<OpenIdTokenResponse>("openid.connect.token", new Args
{
{ "client_id", clientId },
{ "client_secret", clientSecret },
{ "code", code },
{ "grant_type", grantType },
{ "redirect_uri", redirectUrl },
{ "refresh_token", refreshToken }
}, cancellationToken);


/// <summary>
/// Get the identity of a user who has authorized Sign in with Slack.
/// See https://api.slack.com/methods/openid.connect.userInfo for more information.
/// Note: that this method uses an Authentication token passed in the header.
/// Use <c>SlackApiClient.WithAccessToken</c> to specify a token.
/// </summary>
/// <param name="token">Authentication token bearing required scopes. Tokens should be passed as an HTTP Authorization header or alternatively, as a POST parameter.</param>
/// <param name="cancellationToken"></param>
public Task<OpenIdUserInfoResponse> UserInfo(CancellationToken? cancellationToken = null) =>
_client.Post<OpenIdUserInfoResponse>("openid.connect.userInfo", new Args { }, cancellationToken);
}
}
10 changes: 10 additions & 0 deletions SlackNet/WebApi/Responses/OpenIdTokenResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace SlackNet.WebApi
{
public class OpenIdTokenResponse
{
public bool Ok { get; set; }
public string AccessToken { get; set; }
public string TokenType { get; set; }
public string IdToken { get; set; }
}
}
60 changes: 60 additions & 0 deletions SlackNet/WebApi/Responses/OpenIdUserInfoResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using Newtonsoft.Json;

namespace SlackNet.WebApi
{
public class OpenIdUserInfoResponse
{
string Ok { get; set; }
string Sub { get; set; }

[JsonProperty("https://slack.com/user_id")]
string UserId { get; set; }

[JsonProperty("https://slack.com/team_id")]
string TeamId { get; set; }

string Email { get; set; }
string EmailVerified { get; set; }
string DateEmailVerified { get; set; }
string Name { get; set; }
string Picture { get; set; }
string GivenName { get; set; }
string FamilyName { get; set; }
string Locale { get; set; }

[JsonProperty("https://slack.com/team_name")]
string TeamName { get; set; }
[JsonProperty("https://slack.com/team_domain")]
string TeamDomain { get; set; }

[JsonProperty("https://slack.com/user_image_24")]
string UserImage24 { get; set; }
[JsonProperty("https://slack.com/user_image_32")]
string UserImage32 { get; set; }
[JsonProperty("https://slack.com/user_image_48")]
string UserImage48 { get; set; }
[JsonProperty("https://slack.com/user_image_72")]
string UserImage72 { get; set; }
[JsonProperty("https://slack.com/user_image_192")]
string UserImage192 { get; set; }
[JsonProperty("https://slack.com/user_image_512")]
string UserImage512 { get; set; }

[JsonProperty("https://slack.com/team_image_34")]
string TeamImage34 { get; set; }
[JsonProperty("https://slack.com/team_image_44")]
string TeamImage44 { get; set; }
[JsonProperty("https://slack.com/team_image_68")]
string TeamImage68 { get; set; }
[JsonProperty("https://slack.com/team_image_88")]
string TeamImage88 { get; set; }
[JsonProperty("https://slack.com/team_image_102")]
string TeamImage102 { get; set; }
[JsonProperty("https://slack.com/team_image_132")]
string TeamImage132 { get; set; }
[JsonProperty("https://slack.com/team_image_230")]
string TeamImage230 { get; set; }
[JsonProperty("https://slack.com/team_image_default")]
string TeamImageDefault { get; set; }
}
}

0 comments on commit ce9024f

Please sign in to comment.