public async Task <string> GenerateJwtToken(SystemAuthenticateUser user)
        {
            string jwtSecretKey = _configuration.GetSection("jwt:secretKey").Value; //  from appsettings.json

            var authClaims = new List <Claim>
            {
                new Claim(ClaimTypes.Name, user.Username),
                new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()),
            };

            foreach (string role in user.Roles)
            {
                authClaims.Add(new Claim(ClaimTypes.Role, role));
            }

            var key              = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtSecretKey));
            var credentials      = new SigningCredentials(key, SecurityAlgorithms.HmacSha256Signature);
            var jwtSecurityToken = new JwtSecurityToken(
                issuer: "tangled.services",
                audience: "tangled.services",
                expires: DateTime.Now.AddDays(365),
                claims: authClaims,
                signingCredentials: credentials
                );
            var token = new JwtSecurityTokenHandler().WriteToken(jwtSecurityToken);

            return(token);
        }
Esempio n. 2
0
        public async Task <IActionResult> Login([FromHeader(Name = "Authorization")] string basicAuthHeader)
        {
            try
            {
                var model = await _systemUserService.AuthenticateAsync(basicAuthHeader);

                if (model != null)
                {
                    SystemAuthenticateUser user = new SystemAuthenticateUser(model);
                    var jwtToken = await _systemUserService.GenerateJwtToken(user);

                    responseModels.Add("token", jwtToken);
                    response = new ApiResponse(HttpStatusCode.OK, string.Format("User '{0}' authenticated successfully.", user.Username), responseModels);
                    return(Ok(new { response }));
                }

                response = new ApiResponse(HttpStatusCode.Unauthorized, "Login failed.", null, null);
                return(Unauthorized(new { response }));
            }
            catch (UserNotFoundException exception)
            {
                response = new ApiResponse(HttpStatusCode.Unauthorized, "Login failed.", exception, null);
                return(Unauthorized(new { response }));
            }
            catch (LoginFailedException exception)
            {
                response = new ApiResponse(HttpStatusCode.Unauthorized, "Login failed", exception, null);
                return(Unauthorized(new { response }));
            }
            catch (Exception exception)
            {
                return(BadRequest("Login failed. Error: " + exception.Message));
            }
        }
        public async Task CreateItem(SystemUserAuthenticateModel model)
        {
            model = await Validate(model);

            var systemUser = new SystemAuthenticateUser(model);

            systemUser.Password = _hashingService.EncryptString(model.Password);
            await _systemUsersManager.CreateItemAsync(systemUser);
        }
        public async Task <SystemUserModel> UpdateItem(SystemUserModel model)
        {
            var systemAuthenticateUser = await GetItem(new Guid(model.Id));

            if (systemAuthenticateUser == null)
            {
                throw new UserNotFoundException();
            }

            systemAuthenticateUser = new SystemAuthenticateUser(model, systemAuthenticateUser);
            await _systemUsersManager.UpsertItemAsync(systemAuthenticateUser);

            return(new SystemUserModel(systemAuthenticateUser));
        }
 public AdminAuthenticateUserModel(SystemAuthenticateUser entity)
 {
     Id              = entity.Id;
     NamePrefix      = entity.NamePrefix;
     NameFirst       = entity.NameFirst;
     NameLast        = entity.NameLast;
     NameSuffix      = entity.NameSuffix;
     Username        = entity.Username;
     Password        = entity.Password;
     DisplayAs       = entity.DisplayAs;
     ProfileImageUrl = entity.ProfileImageUrl;
     MustChangePasswordAtNextLogin = entity.MustChangePasswordAtNextLogin;
     PasswordExpirationDateTime    = entity.PasswordExpirationDateTime;
     Enabled        = entity.Enabled;
     EmailAddresses = AdminEmailAddressModel.Construct(entity.EmailAddresses);
     PhoneNumbers   = AdminPhoneNumberModel.Construct(entity.PhoneNumbers);
     Roles          = entity.Roles;
 }
Esempio n. 6
0
        public async Task <SystemAuthenticateUser> UpsertItemAsync(SystemAuthenticateUser user)
        {
            var results = await _container.UpsertItemAsync <SystemAuthenticateUser>(user);

            return(results);
        }
Esempio n. 7
0
        //public async Task<SystemUser> UpsertItemAsync(SystemUser user)
        //{
        //    var results = await _container.UpsertItemAsync<SystemUser>(user);
        //    return results;
        //}

        public async Task <SystemAuthenticateUser> CreateItemAsync(SystemAuthenticateUser user)
        {
            var results = await _container.CreateItemAsync <SystemAuthenticateUser>(user, new PartitionKey(user.Username));

            return(results);
        }
Esempio n. 8
0
        //public async Task<TransactionalBatchResponse> ResetUsernameAsync(string sourceUserName, SystemAuthenticateUser user)
        //{
        //    //  All or nothing!
        //    //  NoSQL databases do not support updating the partition key value of an existing item. Need to delete entire document and create new.
        //    //  https://devblogs.microsoft.com/cosmosdb/introducing-transactionalbatch-in-the-net-sdk/
        //    var response = await _container.CreateTransactionalBatch(new PartitionKey(sourceUserName))
        //      .DeleteItem(user.Id)
        //      .ExecuteAsync();

        //    return response;
        //}

        public async Task DeleteItemAsync(SystemAuthenticateUser user)
        {
            await _container.DeleteItemAsync <SystemAuthenticateUser>(user.Id, new PartitionKey(user.Username));
        }