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
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion Devnot.Mentor.Api/ActionFilters/TokenAuthentication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public override void OnActionExecuting(ActionExecutingContext context)

context.HttpContext.User = claimsPrincipal;
}
catch (Exception ex)
catch (Exception)
{
context.Result = new UnauthorizedObjectResult(new ErrorApiResponse(ResultMessage.InvalidToken));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@
"UpdatePasswordWebPageUrl": "http://subdomain.devnot.com/sifreyi-yenile",
"SecurityKeyExpiryFromHours": 24
}
}
}
37 changes: 37 additions & 0 deletions Devnot.Mentor.Api/Controllers/AuthController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System.Threading.Tasks;
using DevnotMentor.Api.Services.Interfaces;
using DevnotMentor.Api.Utilities.OAuth;
using Microsoft.AspNetCore.Mvc;

namespace DevnotMentor.Api.Controllers
{
public class AuthController : BaseController
{
private readonly IUserService _userService;

public AuthController(IUserService userService)
{
_userService = userService;
}

[Route("/auth/github")]
[HttpPost]
public async Task<IActionResult> GitHubAsync([FromBody] string accesToken)
{
var oAuthGitHubUser = await OAuthService.GetOAuthGitHubUserAsync(accesToken);
var githubSignInResponse = await _userService.SignInAsync(oAuthGitHubUser);

return githubSignInResponse.Success ? Success(githubSignInResponse) : BadRequest();
}

[Route("/auth/google")]
[HttpPost]
public async Task<IActionResult> GoogleAsync([FromBody] string accesToken)
{
var oAuthGoogleUser = await OAuthService.GetOAuthGoogleUserAsync(accesToken);
var googleSignInResponse = await _userService.SignInAsync(oAuthGoogleUser);

return googleSignInResponse.Success ? Success(googleSignInResponse) : BadRequest();
}
}
}
62 changes: 5 additions & 57 deletions Devnot.Mentor.Api/Controllers/UserController.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
using System.Threading.Tasks;
using DevnotMentor.Api.ActionFilters;
using DevnotMentor.Api.CustomEntities.Request.UserRequest;
using DevnotMentor.Api.Helpers.Extensions;
using DevnotMentor.Api.Services.Interfaces;
using Microsoft.AspNetCore.Mvc;

namespace DevnotMentor.Api.Controllers
{
[ApiController]
[ServiceFilter(typeof(TokenAuthentication)), ApiController, Route("users"),]
public class UserController : BaseController
{
private readonly IUserService userService;
Expand All @@ -17,62 +16,11 @@ public UserController(IUserService userService)
this.userService = userService;
}

[HttpPost]
[Route("/users/login")]
public async Task<IActionResult> Login(UserLoginRequest request)
[HttpGet("me")]
public async Task<IActionResult> GetMe()
{
var result = await userService.LoginAsync(request);

return result.Success ? Success(result) : BadRequest(result);
}

[HttpPost]
[Route("/users/register")]
public async Task<IActionResult> Register([FromForm] RegisterUserRequest request)
{
var result = await userService.RegisterAsync(request);

return result.Success ? Success(result) : BadRequest(result);
}

[HttpPost]
[Route("/users/change-password")]
[ServiceFilter(typeof(TokenAuthentication))]
public async Task<IActionResult> ChangePassword([FromBody] UpdatePasswordRequest request)
{
request.UserId = User.Claims.GetUserId();

var result = await userService.ChangePasswordAsync(request);

return result.Success ? Success(result) : BadRequest(result);
}

[HttpPatch]
[Route("/users")]
[ServiceFilter(typeof(TokenAuthentication))]
public async Task<IActionResult> UpdateUser([FromForm] UpdateUserRequest request)
{
request.UserId = User.Claims.GetUserId();

var result = await userService.UpdateAsync(request);

return result.Success ? Success(result) : BadRequest(result);
}

[Route("/users/{email}/remind-password")]
[HttpGet]
public async Task<IActionResult> RemindPassword([FromRoute] string email)
{
var result = await userService.RemindPasswordAsync(email);

return result.Success ? Success(result) : BadRequest(result);
}

[HttpPost]
[Route("/users/me/remind-password-complete")]
public async Task<IActionResult> RemindPasswordCompleteAsync(CompleteRemindPasswordRequest request)
{
var result = await userService.RemindPasswordCompleteAsync(request);
var authenticatedUserId = User.Claims.GetUserId();
var result = await userService.GetByUserIdAsync(authenticatedUserId);

return result.Success ? Success(result) : BadRequest(result);
}
Expand Down
24 changes: 24 additions & 0 deletions Devnot.Mentor.Api/CustomEntities/Auth/OAuthGitHubUser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using System.Threading.Tasks;
using DevnotMentor.Api.Entities;
using DevnotMentor.Api.Repositories.Interfaces;

namespace DevnotMentor.Api.CustomEntities.Auth
{
public class OAuthGitHubUser : OAuthUser
{
public OAuthGitHubUser() : base(OAuthType.GitHub)
{
}

public override async Task<User> GetUserFromDatabaseAsync(IUserRepository repository)
{
if (repository is null)
{
throw new NullReferenceException($"{repository.GetType().FullName} was null.");
}

return await repository.GetByGitHubIdAsync(base.Id);
}
}
}
24 changes: 24 additions & 0 deletions Devnot.Mentor.Api/CustomEntities/Auth/OAuthGoogleUser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using System.Threading.Tasks;
using DevnotMentor.Api.Entities;
using DevnotMentor.Api.Repositories.Interfaces;

namespace DevnotMentor.Api.CustomEntities.Auth
{
public class OAuthGoogleUser : OAuthUser
{
public OAuthGoogleUser() : base(OAuthType.Google)
{
}

public override async Task<User> GetUserFromDatabaseAsync(IUserRepository repository)
{
if (repository is null)
{
throw new NullReferenceException($"{repository.GetType().FullName} was null.");
}

return await repository.GetByGoogleIdAsync(base.Id);
}
}
}
8 changes: 8 additions & 0 deletions Devnot.Mentor.Api/CustomEntities/Auth/OAuthType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace DevnotMentor.Api.CustomEntities.Auth
{
public enum OAuthType : int
{
Google,
GitHub
}
}
37 changes: 37 additions & 0 deletions Devnot.Mentor.Api/CustomEntities/Auth/OAuthUser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System;
using System.Threading.Tasks;
using DevnotMentor.Api.Entities;
using DevnotMentor.Api.Repositories.Interfaces;

namespace DevnotMentor.Api.CustomEntities.Auth
{
public abstract class OAuthUser
{
protected OAuthUser(OAuthType providerType)
{
OAuthProviderType = providerType;
}

/// <summary>
/// Provider ID
/// </summary>
public string Id { get; set; }
public string FullName { get; set; }
public string UserName { get; set; }
public string Email { get; set; }
public bool EmailConfirmed { get; set; }
public string ProfilePictureUrl { get; set; }
public OAuthType OAuthProviderType { get; private set; }

/// <summary>
/// Get User by Provider
/// </summary>
/// <returns></returns>
public abstract Task<User> GetUserFromDatabaseAsync(IUserRepository repository);

public void SetRandomUsername()
{
this.UserName = Guid.NewGuid().ToString().Split('-')[0];
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace DevnotMentor.Api.CustomEntities.Auth.Response
{
public class OAuthGitHubEmailResponse
{
public string email { get; set; }
public bool primary { get; set; }
public bool verified { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System.Collections.Generic;

namespace DevnotMentor.Api.CustomEntities.Auth.Response
{
public class OAuthGitHubResponse
{
public string id { get; set; }
public string name { get; set; }
public string login { get; set; }
public string avatar_url { get; set; }
public List<OAuthGitHubEmailResponse> Emails { get; set; }

public OAuthGitHubUser MapToOAuthGitHubUser()
{
var primaryEmail = Emails.Find(x => x.primary == true);

return new OAuthGitHubUser()
{
Email = primaryEmail?.email,
Id = id,
FullName = name,
ProfilePictureUrl = avatar_url,
UserName = login,
EmailConfirmed = primaryEmail is null ? false : primaryEmail.verified
};
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;

namespace DevnotMentor.Api.CustomEntities.Auth.Response
{
public class OAuthGoogleResponse
{
public string id { get; set; }
public string name { get; set; }
public string email { get; set; }
public string picture { get; set; }

public OAuthGoogleUser MapToOAuthGoogleUser()
{
return new OAuthGoogleUser()
{
Email = email,
Id = id,
FullName = name,
ProfilePictureUrl = picture,
EmailConfirmed = (!String.IsNullOrEmpty(email)),
UserName = Guid.NewGuid().ToString().Split('-')[0]
};
}
}
}
8 changes: 2 additions & 6 deletions Devnot.Mentor.Api/CustomEntities/Dto/UserDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,8 @@ public UserDto()

public int Id { get; set; }
public string UserName { get; set; }
public string Name { get; set; }
public string SurName { get; set; }
public string ProfileImageUrl { get; set; }
public bool? UserNameConfirmed { get; set; }
public int? UserState { get; set; }
public string ProfileUrl { get; set; }
public string FullName { get; set; }
public string ProfilePictureUrl { get; set; }

public bool IsMentee => Mentee.Any();
public bool IsMentor => Mentor.Any();
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
using System.ComponentModel.DataAnnotations;
using Microsoft.AspNetCore.Http;

namespace DevnotMentor.Api.CustomEntities.Request.UserRequest
{
public class UpdateUserRequest
{
public int UserId { get; set; }

[Required]
public string Name { get; set; }

public string FullName { get; set; }
[Required]
public string SurName { get; set; }

public string ProfileImageUrl { get; set; }

public IFormFile ProfileImage { get; set; }
public string Email { get; set; }
}
}
Loading