public async Task <String> Login(string username, string password) { // Find and validate the user: Users user = GetUserByUsername(username); if (user == null || (!BCrypt.Net.BCrypt.Verify(password, user.Password))) { //TODO: zamijenit response kodom return(null); } // Create the identity var identity = new ClaimsIdentity(JwtBearerDefaults.AuthenticationScheme); var claims = new List <Claim>(); identity.AddClaim(new Claim("Name", user.Username)); identity.AddClaim(new Claim("Id", user.Id)); //Add roles if (user.Roles != null) { foreach (var role in user.Roles.Select(u => u.Name)) { identity.AddClaim(new Claim("Role", role)); //claims.Add(new Claim("Role", role)); } } //Add permissions -- samo za permisije, nezavisno od rola, nezavisna autorizacija if (user.Permissions != null) { foreach (var permission in user.Permissions.Select(u => u.Name)) { identity.AddClaim(new Claim("Permission", permission)); } } //Add role-permissions-- ovo je za role povezane sa permisijama if (user.RolePermissions != null) { foreach (var rolePermission in user.RolePermissions.ToList()) { identity.AddClaim(new Claim("RolePermission", rolePermission.Role.Name + "," + rolePermission.Permission.Name)); } } var token = _token.GenerateAccessToken(identity.Claims); return(token); }
public TokenVO ValidateCredentials(UserVO userCredentials) { // Validando se o usuário existe var user = _repo.ValidateCredentials(userCredentials); if (user == null) { return(null); } // Definindo as claims var claims = new List <Claim>(); claims.Add(new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString("N"))); claims.Add(new Claim(JwtRegisteredClaimNames.UniqueName, userCredentials.Username)); // Definição do access token e refresh token var accessToken = _tokenService.GenerateAccessToken(claims); // Quando o access token expirar, este será usado var refreshToken = _tokenService.GenerateRefreshToken(); user.RefreshToken = refreshToken; user.RefreshTokenExpiryTime = DateTime.Now.AddDays(_tokenConfig.DaysToExpire); _repo.RefreshUserInfo(user); var createDate = DateTime.Now; var expirationDate = createDate.AddMinutes(_tokenConfig.Minutes); return(new TokenVO( true, createDate.ToString(DATE_FORMAT), expirationDate.ToString(DATE_FORMAT), accessToken, refreshToken )); }