public async Task <Session> SignUp(string callSign, string firstName, string lastName, string password) { //TODO Validation try { using var dbContextScope = dbContextScopeFactory.Create(); if (await playerRepository.Exists(callSign)) { throw new Exception(); } var(passwordHash, passwordSalt) = Password.CreatePasswordHashWithSalt(password); var player = Player.SignUp(generator.NewReference <Player>(), callSign, firstName, lastName, passwordHash, passwordSalt); await playerRepository.Save(player); await dbContextScope.SaveChangesAsync(); return(GetSession(player)); } catch { throw new InvalidOperationException("Call sign in use."); } }
string GetToken(Player player) { var tokenHandler = new JwtSecurityTokenHandler(); var key = Encoding.ASCII.GetBytes(appSettings.Secret); var tokenDescriptor = new SecurityTokenDescriptor { Subject = new ClaimsIdentity(GetClaims(player)), Expires = DateTime.UtcNow.AddDays(2), SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature) }; var token = tokenHandler.CreateToken(tokenDescriptor); return(tokenHandler.WriteToken(token)); }
static IEnumerable <Claim> GetClaims(Player player) { yield return(new Claim(ClaimTypes.Name, player.Id.ToString())); yield return(new Claim(ClaimTypes.NameIdentifier, player.CallSign)); }
Session GetSession(Player player) { return(new () { Token = GetToken(player), Player = mapper.Map <Shared.Model.Player>(player) }); }