Skip to content
Closed
Changes from 1 commit
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
2feeb48
First working version of Meetup provider
dance2die Feb 23, 2018
c3ee580
Removed commented-out code and an unused using reference
dance2die Feb 24, 2018
a3447be
Mapped all user's claim options
dance2die Feb 24, 2018
7037b3c
Added Meetup provider to the solution
dance2die Feb 24, 2018
42e771c
Updated indentation as suggested by "kinosang" on https://github.com/…
dance2die Feb 24, 2018
522fb33
Fixed indentation as suggested by kinosang on #220
dance2die Feb 24, 2018
8648b81
Introduced a ClaimType static constant class "MeetupClaimTypes" as su…
dance2die Feb 24, 2018
f15cb87
Updated Meetup package author & company in the package (I have forgot…
dance2die Feb 24, 2018
1d66b8f
Migrate to the latest KoreBuild version
kevinchalet Feb 28, 2018
cb903be
First working version of Meetup provider
dance2die Feb 23, 2018
eef1c57
Removed commented-out code and an unused using reference
dance2die Feb 24, 2018
e8a00a2
Mapped all user's claim options
dance2die Feb 24, 2018
4d32726
Added Meetup provider to the solution
dance2die Feb 24, 2018
f3a1c26
Updated indentation as suggested by "kinosang" on https://github.com/…
dance2die Feb 24, 2018
8f6c909
Fixed indentation as suggested by kinosang on #220
dance2die Feb 24, 2018
79d08a8
Introduced a ClaimType static constant class "MeetupClaimTypes" as su…
dance2die Feb 24, 2018
7fe7f1b
Updated Meetup package author & company in the package (I have forgot…
dance2die Feb 24, 2018
ddf0259
Took remote solution
dance2die Feb 28, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Removed commented-out code and an unused using reference
  • Loading branch information
dance2die committed Feb 28, 2018
commit eef1c579ad47a3119f2967e05b4b147b0a89bcd6
95 changes: 41 additions & 54 deletions src/AspNet.Security.OAuth.Meetup/MeetupAuthenticationHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
* for more information concerning the license and the contributors participating to this project.
*/

using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Security.Claims;
Expand All @@ -19,57 +18,45 @@

namespace AspNet.Security.OAuth.Meetup
{
public class MeetupAuthenticationHandler : OAuthHandler<MeetupAuthenticationOptions>
{
public MeetupAuthenticationHandler(
[NotNull] IOptionsMonitor<MeetupAuthenticationOptions> options,
[NotNull] ILoggerFactory logger,
[NotNull] UrlEncoder encoder,
[NotNull] ISystemClock clock)
: base(options, logger, encoder, clock)
{
}

protected override async Task<AuthenticationTicket> CreateTicketAsync([NotNull] ClaimsIdentity identity,
[NotNull] AuthenticationProperties properties, [NotNull] OAuthTokenResponse tokens)
{
var request = new HttpRequestMessage(HttpMethod.Get, Options.UserInformationEndpoint);
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", tokens.AccessToken);

var response = await Backchannel.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, Context.RequestAborted);
if (!response.IsSuccessStatusCode)
{
Logger.LogError("An error occurred while retrieving the user profile: the remote server " +
"returned a {Status} response with the following payload: {Headers} {Body}.",
/* Status: */ response.StatusCode,
/* Headers: */ response.Headers.ToString(),
/* Body: */ await response.Content.ReadAsStringAsync());

throw new HttpRequestException("An error occurred while retrieving the user profile.");
}

var payload = JObject.Parse(await response.Content.ReadAsStringAsync());

var principal = new ClaimsPrincipal(identity);
var context = new OAuthCreatingTicketContext(principal, properties, Context, Scheme, Options, Backchannel, tokens, payload);
context.RunClaimActions(payload);

//// When the email address is not public, retrieve it from
//// the emails endpoint if the user:email scope is specified.
//if (!string.IsNullOrEmpty(Options.UserEmailsEndpoint) &&
// !identity.HasClaim(claim => claim.Type == ClaimTypes.Email) && Options.Scope.Contains("user:email"))
//{
// var address = await GetEmailAsync(tokens);
// if (!string.IsNullOrEmpty(address))
// {
// identity.AddClaim(new Claim(ClaimTypes.Email, address, ClaimValueTypes.String, Options.ClaimsIssuer));
// }
//}

await Options.Events.CreatingTicket(context);

return new AuthenticationTicket(context.Principal, context.Properties, Scheme.Name);
}
}
public class MeetupAuthenticationHandler : OAuthHandler<MeetupAuthenticationOptions>
{
public MeetupAuthenticationHandler(
[NotNull] IOptionsMonitor<MeetupAuthenticationOptions> options,
[NotNull] ILoggerFactory logger,
[NotNull] UrlEncoder encoder,
[NotNull] ISystemClock clock)
: base(options, logger, encoder, clock)
{
}

protected override async Task<AuthenticationTicket> CreateTicketAsync([NotNull] ClaimsIdentity identity,
[NotNull] AuthenticationProperties properties, [NotNull] OAuthTokenResponse tokens)
{
var request = new HttpRequestMessage(HttpMethod.Get, Options.UserInformationEndpoint);
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", tokens.AccessToken);

var response = await Backchannel.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, Context.RequestAborted);
if (!response.IsSuccessStatusCode)
{
Logger.LogError("An error occurred while retrieving the user profile: the remote server " +
"returned a {Status} response with the following payload: {Headers} {Body}.",
/* Status: */ response.StatusCode,
/* Headers: */ response.Headers.ToString(),
/* Body: */ await response.Content.ReadAsStringAsync());

throw new HttpRequestException("An error occurred while retrieving the user profile.");
}

var payload = JObject.Parse(await response.Content.ReadAsStringAsync());

var principal = new ClaimsPrincipal(identity);
var context = new OAuthCreatingTicketContext(principal, properties, Context, Scheme, Options, Backchannel, tokens, payload);
context.RunClaimActions(payload);

await Options.Events.CreatingTicket(context);

return new AuthenticationTicket(context.Principal, context.Properties, Scheme.Name);
}
}
}