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
Showing
25 changed files
with
1,588 additions
and
10 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
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,58 @@ | ||
using System.Collections.Generic; | ||
using AuthServer.Infrastructure.Constants; | ||
using IdentityServer4; | ||
using IdentityServer4.Models; | ||
|
||
namespace AuthServer.Api | ||
{ | ||
public class Config | ||
{ | ||
public static IEnumerable<IdentityResource> GetIdentityResources() | ||
{ | ||
return new List<IdentityResource> | ||
{ | ||
new IdentityResources.OpenId(), | ||
new IdentityResources.Email(), | ||
new IdentityResources.Profile(), | ||
}; | ||
} | ||
|
||
public static IEnumerable<ApiResource> GetApiResources() | ||
{ | ||
return new List<ApiResource> | ||
{ | ||
new ApiResource(Roles.Consumer, "Aycoutay API") | ||
}; | ||
} | ||
|
||
public static IEnumerable<Client> GetClients() | ||
{ | ||
// client credentials client | ||
return new List<Client> | ||
{ | ||
// resource owner password grant client | ||
new Client | ||
{ | ||
ClientId = "ro.aycoutay-mobile", | ||
AllowedGrantTypes = GrantTypes.ResourceOwnerPassword, | ||
|
||
ClientSecrets = | ||
{ | ||
new Secret("secret".Sha256()) | ||
}, | ||
AllowedScopes = { | ||
IdentityServerConstants.StandardScopes.OpenId, | ||
IdentityServerConstants.StandardScopes.Profile, | ||
IdentityServerConstants.StandardScopes.Email, | ||
IdentityServerConstants.StandardScopes.Address, | ||
Roles.Consumer | ||
}, | ||
AllowOfflineAccess = true, | ||
// Access token life time is 90 days | ||
AccessTokenLifetime = 7776000, | ||
AbsoluteRefreshTokenLifetime = 10368000 // 120 days | ||
} | ||
}; | ||
} | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
src/AuthServer/AuthServer.Api/Controllers/AccountsController.cs
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,44 @@ | ||
using AuthServer.Infrastructure.Data.Identity; | ||
using Microsoft.AspNetCore.Identity; | ||
using Microsoft.AspNetCore.Mvc; | ||
using AuthServer.Api.Models; | ||
using AuthServer.Infrastructure.Constants; | ||
using System.Threading.Tasks; | ||
|
||
namespace AuthServer.Api.Controllers | ||
{ | ||
[Route("api/[controller]")] | ||
[ApiController] | ||
public class AccountsController : ControllerBase | ||
{ | ||
private readonly UserManager<AppUser> _userManager; | ||
|
||
public AccountsController(UserManager<AppUser> userManager) | ||
{ | ||
_userManager = userManager; | ||
} | ||
|
||
[HttpPost] | ||
public async Task<IActionResult> Register([FromBody]RegisterRequestViewModel model) | ||
{ | ||
if (!ModelState.IsValid) | ||
{ | ||
return BadRequest(ModelState); | ||
} | ||
|
||
var user = new AppUser { UserName = model.Email, Name = model.Name, Email = model.Email }; | ||
|
||
var result = await _userManager.CreateAsync(user, model.Password); | ||
|
||
if (!result.Succeeded) return BadRequest(result.Errors); | ||
|
||
await _userManager.AddToRoleAsync(user, Roles.Consumer); | ||
await _userManager.AddClaimAsync(user, new System.Security.Claims.Claim("userName", user.UserName)); | ||
await _userManager.AddClaimAsync(user, new System.Security.Claims.Claim("name", user.Name)); | ||
await _userManager.AddClaimAsync(user, new System.Security.Claims.Claim("email", user.Email)); | ||
await _userManager.AddClaimAsync(user, new System.Security.Claims.Claim("role", Roles.Consumer)); | ||
|
||
return Ok(new RegisterResponseViewModel(user)); | ||
} | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/AuthServer/AuthServer.Api/Models/RegisterRequestViewModel.cs
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,29 @@ | ||
using System.ComponentModel.DataAnnotations; | ||
|
||
|
||
namespace AuthServer.Api.Models | ||
{ | ||
public class RegisterRequestViewModel | ||
{ | ||
[Required] | ||
[StringLength(50, ErrorMessage = "The {0} must be at least {2} and at max {1} characters long.", MinimumLength = 2)] | ||
[Display(Name = "Name")] | ||
public string Name { get; set; } | ||
|
||
[Required] | ||
[EmailAddress] | ||
[Display(Name = "Email")] | ||
public string Email { get; set; } | ||
|
||
[Required] | ||
[StringLength(100, ErrorMessage = "The {0} must be at least {2} and at max {1} characters long.", MinimumLength = 6)] | ||
[DataType(DataType.Password)] | ||
[Display(Name = "Password")] | ||
public string Password { get; set; } | ||
|
||
[DataType(DataType.Password)] | ||
[Display(Name = "Confirm password")] | ||
[Compare("Password", ErrorMessage = "The password and confirmation password do not match.")] | ||
public string ConfirmPassword { get; set; } | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/AuthServer/AuthServer.Api/Models/RegisterResponseViewModel.cs
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,18 @@ | ||
using AuthServer.Infrastructure.Data.Identity; | ||
|
||
namespace AuthServer.Api.Models | ||
{ | ||
public class RegisterResponseViewModel | ||
{ | ||
public string Id { get; set; } | ||
public string Name { get; set; } | ||
public string Email { get; set; } | ||
|
||
public RegisterResponseViewModel(AppUser user) | ||
{ | ||
Id = user.Id; | ||
Name = user.Name; | ||
Email = user.Email; | ||
} | ||
} | ||
} |
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
Oops, something went wrong.