-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
319d415
commit 72686b8
Showing
22 changed files
with
1,365 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
988 changes: 988 additions & 0 deletions
988
DatingApp.API/.vs/DatingApp.API/config/applicationhost.config
Large diffs are not rendered by default.
Oops, something went wrong.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
using System; | ||
using System.IdentityModel.Tokens.Jwt; | ||
using System.Security.Claims; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using DatingApp.API.Data; | ||
using DatingApp.API.Dtos; | ||
using DatingApp.API.Models; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.IdentityModel.Tokens; | ||
|
||
namespace DatingApp.API.Controllers | ||
{ | ||
[Route("api/[controller]")] | ||
[ApiController] | ||
public class AuthController : ControllerBase | ||
{ | ||
private readonly IAuthRepository _repo; | ||
private readonly IConfiguration _config; | ||
public AuthController(IAuthRepository repo, IConfiguration config) | ||
{ | ||
_config = config; | ||
_repo = repo; | ||
|
||
} | ||
|
||
[HttpPost("register")] | ||
//if you remove apicontroller above | ||
// public async Task<IActionResult> Register([FromBody] UserForRegisterDto userForRegisterDto) | ||
public async Task<IActionResult> Register(UserForRegisterDto userForRegisterDto) | ||
{ | ||
|
||
//this is for validation if no apicontroller | ||
// if(!ModelState.IsValid) | ||
// return BadRequest(ModelState); | ||
|
||
userForRegisterDto.Username = userForRegisterDto.Username.ToLower(); | ||
if (await _repo.UserExists(userForRegisterDto.Username)) | ||
return BadRequest("Username already exists"); | ||
|
||
var userToCreate = new User | ||
{ | ||
Username = userForRegisterDto.Username | ||
}; | ||
|
||
var createdUser = await _repo.Register(userToCreate, userForRegisterDto.Password); | ||
|
||
return StatusCode(201); | ||
} | ||
|
||
[HttpPost("login")] | ||
|
||
public async Task<IActionResult> Login(UserForLoginDto userForLoginDto) | ||
{ | ||
var userFromRepo = await _repo.Login(userForLoginDto.Username.ToLower(), userForLoginDto.Password); | ||
|
||
if (userFromRepo == null) | ||
return Unauthorized(); | ||
|
||
var claims = new[] | ||
{ | ||
new Claim(ClaimTypes.NameIdentifier, userFromRepo.Id.ToString()), | ||
new Claim(ClaimTypes.Name, userFromRepo.Username) | ||
}; | ||
|
||
var key = new SymmetricSecurityKey(Encoding.UTF8 | ||
.GetBytes(_config.GetSection("AppSettings:Token").Value)); | ||
|
||
|
||
var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha512Signature); | ||
|
||
var tokenDescriptor = new SecurityTokenDescriptor | ||
{ | ||
Subject = new ClaimsIdentity(claims), | ||
Expires = DateTime.Now.AddDays(1), | ||
SigningCredentials = creds | ||
}; | ||
|
||
var tokenHandler = new JwtSecurityTokenHandler(); | ||
|
||
var token = tokenHandler.CreateToken(tokenDescriptor); | ||
|
||
return Ok(new { | ||
token = tokenHandler.WriteToken(token) | ||
}); | ||
|
||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using DatingApp.API.Models; | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
namespace DatingApp.API.Data | ||
{ | ||
public class AuthRepository : IAuthRepository | ||
{ | ||
private readonly DataContext _context; | ||
|
||
public AuthRepository(DataContext context) | ||
{ | ||
_context = context; | ||
|
||
} | ||
public async Task<User> Login(string username, string password) | ||
{ | ||
var user = await _context.Users.FirstOrDefaultAsync(x => x.Username == username); | ||
|
||
if (user == null) | ||
{ | ||
return null; | ||
} | ||
|
||
if (!VerifyPasswordHash(password, user.PasswordHash, user.PasswordSalt)) | ||
{ | ||
return null; | ||
} | ||
return user; | ||
|
||
} | ||
|
||
private bool VerifyPasswordHash(string password, byte[] passwordHash, byte[] passwordSalt) | ||
{ | ||
using (var hmac = new System.Security.Cryptography.HMACSHA512(passwordSalt)) | ||
{ | ||
var computedHash = hmac.ComputeHash(System.Text.Encoding.UTF8.GetBytes(password)); | ||
// since computed hash is in byte array, then compare all arrays through for loop | ||
for (int i = 0; i < computedHash.Length; i++) | ||
{ | ||
if (computedHash[i] != passwordHash[i]) return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
public async Task<User> Register(User user, string password) | ||
{ | ||
byte[] passwordHash, passwordSalt; | ||
CreatePasswordHash(password, out passwordHash, out passwordSalt); | ||
|
||
user.PasswordHash = passwordHash; | ||
user.PasswordSalt = passwordSalt; | ||
|
||
await _context.Users.AddAsync(user); | ||
await _context.SaveChangesAsync(); | ||
|
||
return user; | ||
} | ||
|
||
private void CreatePasswordHash(string password, out byte[] passwordHash, out byte[] passwordSalt) | ||
{ | ||
using (var hmac = new System.Security.Cryptography.HMACSHA512()) | ||
{ | ||
passwordSalt = hmac.Key; | ||
passwordHash = hmac.ComputeHash(System.Text.Encoding.UTF8.GetBytes(password)); | ||
} | ||
|
||
} | ||
|
||
public async Task<bool> UserExists(string username) | ||
{ | ||
if (await _context.Users.AnyAsync(x => x.Username == username)) | ||
return true; | ||
|
||
|
||
return false; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using System.Threading.Tasks; | ||
using DatingApp.API.Models; | ||
|
||
namespace DatingApp.API.Data | ||
{ | ||
public interface IAuthRepository | ||
{ | ||
Task<User> Register (User user, string password); | ||
Task<User> Login (string Username, string password); | ||
Task<bool> UserExists (string username); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'"> | ||
<DebuggerFlavor>ProjectDebugger</DebuggerFlavor> | ||
</PropertyGroup> | ||
<PropertyGroup> | ||
<ActiveDebugProfile>DatingApp.API</ActiveDebugProfile> | ||
</PropertyGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace DatingApp.API.Dtos | ||
{ | ||
public class UserForLoginDto | ||
{ | ||
public string Username { get; set; } | ||
public string Password { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using System.ComponentModel.DataAnnotations; | ||
|
||
namespace DatingApp.API.Dtos | ||
{ | ||
public class UserForRegisterDto | ||
{ | ||
[Required] | ||
public string Username { get; set; } | ||
|
||
[Required] | ||
[StringLength(8, MinimumLength = 4, ErrorMessage = "You must specify password between 4 and 8 characters")] | ||
public string Password { get; set; } | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
DatingApp.API/Migrations/20200709075644_AddUserEntity.Designer.cs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
using System; | ||
using Microsoft.EntityFrameworkCore.Migrations; | ||
|
||
namespace DatingApp.API.Migrations | ||
{ | ||
public partial class AddUserEntity : Migration | ||
{ | ||
protected override void Up(MigrationBuilder migrationBuilder) | ||
{ | ||
migrationBuilder.CreateTable( | ||
name: "Users", | ||
columns: table => new | ||
{ | ||
Id = table.Column<int>(nullable: false) | ||
.Annotation("Sqlite:Autoincrement", true), | ||
Username = table.Column<string>(nullable: true), | ||
PasswordHash = table.Column<byte[]>(nullable: true), | ||
PasswordSalt = table.Column<byte[]>(nullable: true) | ||
}, | ||
constraints: table => | ||
{ | ||
table.PrimaryKey("PK_Users", x => x.Id); | ||
}); | ||
} | ||
|
||
protected override void Down(MigrationBuilder migrationBuilder) | ||
{ | ||
migrationBuilder.DropTable( | ||
name: "Users"); | ||
} | ||
} | ||
} |
Oops, something went wrong.