Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Social #38

Open
wants to merge 38 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
cfc0934
set environment for github oauth
halilkocaoz Jul 15, 2021
3303ff1
Remove: user implemantations. Update: user entity
halilkocaoz Jul 15, 2021
ac7d5c6
Add github auth
halilkocaoz Jul 15, 2021
b23e829
Add: extensions for servicecollection
halilkocaoz Jul 15, 2021
80275ac
Update: returning token via github auth. Add todos
halilkocaoz Jul 15, 2021
23c62c0
Changes: Drop columns from User entity
halilkocaoz Jul 17, 2021
50cf37a
Preparation for google oauth
halilkocaoz Jul 17, 2021
99d29f1
Add new two model: OAuthUser and OAuthType. Change a method name
halilkocaoz Jul 17, 2021
53ba0c9
Seperate Auth metot jobs.
halilkocaoz Jul 17, 2021
258fba8
Implement Google Auth. Add CustomAuth metot for services
halilkocaoz Jul 17, 2021
3e2f614
Fix miss passing oauth type.
halilkocaoz Jul 17, 2021
cd7d9d0
Move oauth methods to different class
halilkocaoz Jul 17, 2021
1b6005c
Remove unused service. Add todos
halilkocaoz Jul 18, 2021
fd45ffc
Add ctor for OAuthUser. Seperate: Get jobs by provider. Update: servi…
halilkocaoz Jul 18, 2021
82aca0e
Update property name: IdentifierProperty to UniqueByProvider
halilkocaoz Jul 18, 2021
de8c497
Refactor: deserializing response and creating OAuthUser.
halilkocaoz Jul 18, 2021
fc2e648
Merge https://github.com/devnotcom/devnot-mentor-back-end into social
halilkocaoz Jul 18, 2021
797d010
Fix swagger error
halilkocaoz Jul 18, 2021
ebc6c3b
refactor: SignInAsync returns. Add OAuth to Mapper. Update TokenInfo …
halilkocaoz Jul 19, 2021
b08366d
Add 'users/me' endpoint to get authenticated user info
halilkocaoz Jul 21, 2021
8b634b3
Add: new columns: emailconfirmed, createdat and changes for those.
halilkocaoz Jul 21, 2021
cbc5249
Update user entity props
halilkocaoz Jul 21, 2021
6fd8981
Refactor: add try-catch blocks to catch unique email and username exc…
halilkocaoz Jul 21, 2021
f25cd4a
refactor: selecting provider id
halilkocaoz Jul 21, 2021
2d2312f
Add UniqueIndexName to provide Unique Index Names from one point
halilkocaoz Jul 23, 2021
a084156
refactoring
yusufyilmazfr Jul 24, 2021
f19e168
refactoring: namespace düzeltildi.
yusufyilmazfr Jul 24, 2021
18273cb
Change column has index filters, Update comments, Set CreatedAt value…
halilkocaoz Jul 24, 2021
15dabd9
Fix not passing filter. Remove unused codes
halilkocaoz Jul 24, 2021
723dc7c
Add: Access to Emails for GitHub
halilkocaoz Jul 25, 2021
9404c1b
Add GetResponseContentAsStringAsync to get responses from one point
halilkocaoz Jul 26, 2021
5dd6496
Move response methods to OAuthServiceResponse, change parameter names
halilkocaoz Jul 26, 2021
4f4f5ae
Fix misspelling, add summaries for response methods
halilkocaoz Jul 26, 2021
53cbbe1
Remove server-side social login challenging
halilkocaoz Aug 6, 2021
3818d90
Refactor for client-side social login
halilkocaoz Aug 6, 2021
ee4c96e
Merge https://github.com/devnotcom/devnot-mentor-back-end into social
halilkocaoz Aug 7, 2021
244c25c
Add forgotten dispose for OAuthResponse
halilkocaoz Aug 7, 2021
b0d7997
Remove social provider sections from appsettings
halilkocaoz Aug 7, 2021
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
Move response methods to OAuthServiceResponse, change parameter names
halilkocaoz committed Jul 26, 2021
commit 5dd6496f7e8e7244763ebd16237c5f8dc2aed2cb
Original file line number Diff line number Diff line change
@@ -71,10 +71,10 @@ public static IServiceCollection AddCustomAuthentication(this IServiceCollection

options.Events = new OAuthEvents
{
OnCreatingTicket = async ctx =>
OnCreatingTicket = async creatinTicketContext =>
{
var oAuthGitHubUser = await OAuthService.GetOAuthGitHubUserAsync(ctx);
await OAuthService.SignInAsync(oAuthGitHubUser, ctx.HttpContext);
var oAuthGitHubUser = await OAuthService.GetOAuthGitHubUserAsync(creatinTicketContext);
await OAuthService.SignInAsync(oAuthGitHubUser, creatinTicketContext.HttpContext);
}
};
})
@@ -86,10 +86,10 @@ public static IServiceCollection AddCustomAuthentication(this IServiceCollection

options.Events = new OAuthEvents
{
OnCreatingTicket = async ctx =>
OnCreatingTicket = async creatinTicketContext =>
{
var oAuthGoogleUser = await OAuthService.GetOAuthGoogleUserAsync(ctx);
await OAuthService.SignInAsync(oAuthGoogleUser, ctx.HttpContext);
var oAuthGoogleUser = await OAuthService.GetOAuthGoogleUserAsync(creatinTicketContext);
await OAuthService.SignInAsync(oAuthGoogleUser, creatinTicketContext.HttpContext);
}
};
});
39 changes: 5 additions & 34 deletions Devnot.Mentor.Api/Utilities/OAuthService.cs
Original file line number Diff line number Diff line change
@@ -1,55 +1,26 @@
using System.Threading.Tasks;
using System.Net.Http;
using System.Net.Http.Headers;
using DevnotMentor.Api.CustomEntities.Auth;
using DevnotMentor.Api.CustomEntities.Auth.Response;
using Newtonsoft.Json;
using Microsoft.AspNetCore.Authentication.OAuth;
using Microsoft.AspNetCore.Http;
using DevnotMentor.Api.Services.Interfaces;
using Microsoft.Extensions.DependencyInjection;
using System.Collections.Generic;

namespace DevnotMentor.Api.Utilities
{
public static class OAuthService
{
private static async Task<string> GetResponseContentAsStringAsync(HttpRequestMessage request, OAuthCreatingTicketContext ctx)
public static async Task<OAuthGitHubUser> GetOAuthGitHubUserAsync(OAuthCreatingTicketContext creatinTicketContext)
{
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", ctx.AccessToken);
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var response = await ctx.Backchannel.SendAsync(request, ctx.HttpContext.RequestAborted);

return await response.Content.ReadAsStringAsync();
}

private static async Task<TOAuthResponse> GetUserPublicInformationsAsync<TOAuthResponse>(OAuthCreatingTicketContext ctx)
{
var publicInfoRequest = new HttpRequestMessage(HttpMethod.Get, ctx.Options.UserInformationEndpoint);
var publicInfoResponse = await GetResponseContentAsStringAsync(publicInfoRequest, ctx);

return JsonConvert.DeserializeObject<TOAuthResponse>(publicInfoResponse);
}

private static async Task<List<OAuthGitHubEmailResponse>> GetGitHubEmailsAsync(OAuthCreatingTicketContext ctx)
{
var emailRequest = new HttpRequestMessage(HttpMethod.Get, "https://api.github.com/user/emails");
var emailResponse = await GetResponseContentAsStringAsync(emailRequest, ctx);

return JsonConvert.DeserializeObject<List<OAuthGitHubEmailResponse>>(emailResponse);
}

public static async Task<OAuthGitHubUser> GetOAuthGitHubUserAsync(OAuthCreatingTicketContext ctx)
{
var authGitHubResponse = await GetUserPublicInformationsAsync<OAuthGitHubResponse>(ctx);
authGitHubResponse.Emails = await GetGitHubEmailsAsync(ctx);
var authGitHubResponse = await OAuthServiceResponse.GetUserPublicInformationsAsync<OAuthGitHubResponse>(creatinTicketContext);
authGitHubResponse.Emails = await OAuthServiceResponse.GetGitHubEmailsAsync(creatinTicketContext);

return authGitHubResponse.MapToOAuthGitHubUser();
}

public static async Task<OAuthGoogleUser> GetOAuthGoogleUserAsync(OAuthCreatingTicketContext ctx)
public static async Task<OAuthGoogleUser> GetOAuthGoogleUserAsync(OAuthCreatingTicketContext creatinTicketContext)
{
var authGoogleResponse = await GetUserPublicInformationsAsync<OAuthGoogleResponse>(ctx);
var authGoogleResponse = await OAuthServiceResponse.GetUserPublicInformationsAsync<OAuthGoogleResponse>(creatinTicketContext);
return authGoogleResponse.MapToOAuthGoogleUser();
}

38 changes: 38 additions & 0 deletions Devnot.Mentor.Api/Utilities/OAuthServiceResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System.Threading.Tasks;
using System.Net.Http;
using System.Net.Http.Headers;
using DevnotMentor.Api.CustomEntities.Auth.Response;
using Newtonsoft.Json;
using Microsoft.AspNetCore.Authentication.OAuth;
using System.Collections.Generic;

namespace DevnotMentor.Api.Utilities
{
public static class OAuthServiceResponse
{
public static async Task<string> GetResponseContentAsStringAsync(HttpRequestMessage requestWithMethodAndURI, OAuthCreatingTicketContext creatinTicketContext)
{
requestWithMethodAndURI.Headers.Authorization = new AuthenticationHeaderValue("Bearer", creatinTicketContext.AccessToken);
requestWithMethodAndURI.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var response = await creatinTicketContext.Backchannel.SendAsync(requestWithMethodAndURI, creatinTicketContext.HttpContext.RequestAborted);

return await response.Content.ReadAsStringAsync();
}

public static async Task<TOAuthResponse> GetUserPublicInformationsAsync<TOAuthResponse>(OAuthCreatingTicketContext creatinTicketContext)
{
var publicInfoRequest = new HttpRequestMessage(HttpMethod.Get, creatinTicketContext.Options.UserInformationEndpoint);
var publicInfoResponse = await GetResponseContentAsStringAsync(publicInfoRequest, creatinTicketContext);

return JsonConvert.DeserializeObject<TOAuthResponse>(publicInfoResponse);
}

public static async Task<List<OAuthGitHubEmailResponse>> GetGitHubEmailsAsync(OAuthCreatingTicketContext creatinTicketContext)
{
var emailRequest = new HttpRequestMessage(HttpMethod.Get, "https://api.github.com/user/emails");
var emailResponse = await GetResponseContentAsStringAsync(emailRequest, creatinTicketContext);

return JsonConvert.DeserializeObject<List<OAuthGitHubEmailResponse>>(emailResponse);
}
}
}