Skip to content

Commit

Permalink
Remove other OAuth providers
Browse files Browse the repository at this point in the history
  • Loading branch information
Senorsen committed Oct 1, 2017
1 parent 4bf9227 commit 2eaa8b5
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 111 deletions.
118 changes: 10 additions & 108 deletions Wukong/Options/OAuthProviderOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,123 +47,25 @@ public static GoogleOptions GoogleOAuthOptions(Secret secret)
{
Query = null
};
avatar = QueryHelpers.AddQueryString(avatarUriBuilder.ToString(), new Dictionary<string, string> { { "sz", "200" } });

avatar = QueryHelpers.AddQueryString(avatarUriBuilder.ToString(),
new Dictionary<string, string> {{"sz", "200"}});

// TODO(Leeleo3x): Use all custom claim types or extend existing claim types.
context.Identity.AddClaim(new Claim(User.AvatarKey, avatar, ClaimValueTypes.String, context.Options.ClaimsIssuer));
context.Identity.AddClaim(new Claim(User.AvatarKey, avatar, ClaimValueTypes.String,
context.Options.ClaimsIssuer));
}

context.Identity.AddClaim(new Claim(ClaimTypes.Authentication, "true", ClaimValueTypes.Boolean, context.Options.ClaimsIssuer));
context.Identity.AddClaim(new Claim(ClaimTypes.AuthenticationMethod, context.Options.AuthenticationScheme, ClaimValueTypes.String, context.Options.ClaimsIssuer));
context.Identity.AddClaim(new Claim(ClaimTypes.Authentication, "true", ClaimValueTypes.Boolean,
context.Options.ClaimsIssuer));
context.Identity.AddClaim(new Claim(ClaimTypes.AuthenticationMethod,
context.Options.AuthenticationScheme, ClaimValueTypes.String,
context.Options.ClaimsIssuer));

return Task.FromResult(0);
},
},

};
}

public static OAuthOptions GitHubOAuthOptions(Secret secret)
{
return new OAuthOptions
{
AuthenticationScheme = "GitHub",
DisplayName = "GitHub",
ClientId = secret.ClientId,
ClientSecret = secret.ClientSecret,
CallbackPath = "/oauth-redirect/github",
Scope = { "user:email" },
SignInScheme = "Cookies",
AuthorizationEndpoint = "https://github.com/login/oauth/authorize",
TokenEndpoint = "https://github.com/login/oauth/access_token",
UserInformationEndpoint = "https://api.github.com/user",
SaveTokens = true,
Events = new OAuthEvents
{
OnTicketReceived = context =>
{
// Cookie expire
context.Properties.IsPersistent = true;
context.Properties.ExpiresUtc = DateTimeOffset.UtcNow.AddDays(30);

return Task.FromResult(0);
},
OnCreatingTicket = async context =>
{
// Get the GitHub user
var request = new HttpRequestMessage(HttpMethod.Get, context.Options.UserInformationEndpoint);
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", context.AccessToken);
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

var response = await context.Backchannel.SendAsync(request, context.HttpContext.RequestAborted);
response.EnsureSuccessStatusCode();

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

var userId = user.Value<string>("id");
if (!string.IsNullOrEmpty(userId))
{
context.Identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, userId, ClaimValueTypes.String, context.Options.ClaimsIssuer));
}

var userName = user.Value<string>("name");
if (!string.IsNullOrEmpty(userName))
{
context.Identity.AddClaim(new Claim(ClaimTypes.Name, userName, ClaimValueTypes.String, context.Options.ClaimsIssuer));
}

var avatar = user.Value<string>("avatar_url");
if (!string.IsNullOrEmpty(avatar))
{
context.Identity.AddClaim(new Claim(User.AvatarKey, avatar, ClaimValueTypes.String, context.Options.ClaimsIssuer));
}

var url = user.Value<string>("html_url");
if (!string.IsNullOrEmpty(url))
{
context.Identity.AddClaim(new Claim(ClaimTypes.Uri, url, ClaimValueTypes.String, context.Options.ClaimsIssuer));
}

context.Identity.AddClaim(new Claim(ClaimTypes.Authentication, "true", ClaimValueTypes.Boolean, context.Options.ClaimsIssuer));
context.Identity.AddClaim(new Claim(ClaimTypes.AuthenticationMethod, context.Options.AuthenticationScheme, ClaimValueTypes.String, context.Options.ClaimsIssuer));
}
}
};
}

public static MicrosoftAccountOptions MicrosoftOAuthOptions(Secret secret)
{
return new MicrosoftAccountOptions
{
AuthenticationScheme = "Microsoft",
DisplayName = "Microsoft",
ClientId = secret.ClientId,
ClientSecret = secret.ClientSecret,
CallbackPath = "/oauth-redirect/microsoft",
SaveTokens = true,
Events = new OAuthEvents
{
OnTicketReceived = context =>
{
// Cookie expire
context.Properties.IsPersistent = true;
context.Properties.ExpiresUtc = DateTimeOffset.UtcNow.AddDays(30);

return Task.FromResult(0);
},
OnCreatingTicket = context =>
{
var user = context.User;
var userId = user.Value<string>("id");
var avatar = string.Format("https://apis.live.net/v5.0/{0}/picture", userId);
context.Identity.AddClaim(new Claim(User.AvatarKey, avatar, ClaimValueTypes.String, context.Options.ClaimsIssuer));

context.Identity.AddClaim(new Claim(ClaimTypes.Authentication, "true", ClaimValueTypes.Boolean, context.Options.ClaimsIssuer));
context.Identity.AddClaim(new Claim(ClaimTypes.AuthenticationMethod, context.Options.AuthenticationScheme, ClaimValueTypes.String, context.Options.ClaimsIssuer));
return Task.FromResult(0);
}
}
};
}
}
}
4 changes: 1 addition & 3 deletions Wukong/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,7 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerF
string redisConnectionString = RedisConnectionUtil.RedisConnectionDnsLookup(settings.RedisConnectionString);
app.UseCookieAuthentication(
Options.AuthenticationOptions.CookieAuthenticationOptions(redisConnectionString));

app.UseMicrosoftAccountAuthentication(OAuthProviderOptions.MicrosoftOAuthOptions(settings.Authentication.Microsoft));
app.UseOAuthAuthentication(OAuthProviderOptions.GitHubOAuthOptions(settings.Authentication.GitHub));

app.UseGoogleAuthentication(OAuthProviderOptions.GoogleOAuthOptions(settings.Authentication.Google));
app.UseWebSockets();
app.UseMiddleware<UserManagerMiddleware>();
Expand Down

0 comments on commit 2eaa8b5

Please sign in to comment.