Ejemplo n.º 1
0
        public async Task <string> GenerateJwtToken(AdminAuthenticateUser 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(1),
                claims: authClaims,
                signingCredentials: credentials
                );
            var token = new JwtSecurityTokenHandler().WriteToken(jwtSecurityToken);

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

                if (model != null)
                {
                    AdminAuthenticateUser user = new AdminAuthenticateUser(model);
                    var jwtToken = await _adminUserService.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));
            }
        }
Ejemplo n.º 3
0
        public async Task CreateItem(AdminAuthenticateUserModel model)
        {
            model = await Validate(model);

            var adminUser = new AdminAuthenticateUser(model);

            adminUser.Password = _hashingService.EncryptString(model.Password);
            await _adminUsersManager.CreateItemAsync(adminUser);
        }
Ejemplo n.º 4
0
        public async Task <AdminUserModel> UpdateItem(AdminUserModel model)
        {
            var adminAuthenticateUser = await GetItem(new Guid(model.Id));

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

            adminAuthenticateUser = new AdminAuthenticateUser(model, adminAuthenticateUser);
            await _adminUsersManager.UpsertItemAsync(adminAuthenticateUser);

            return(new AdminUserModel(adminAuthenticateUser));
        }
 public AdminAuthenticateUserModel(AdminAuthenticateUser 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;
 }
 public async Task DeleteItemAsync(AdminAuthenticateUser user)
 {
     await _container.DeleteItemAsync <AdminAuthenticateUser>(user.Id, new PartitionKey(user.Username));
 }
        public async Task <AdminAuthenticateUser> UpsertItemAsync(AdminAuthenticateUser user)
        {
            var results = await _container.UpsertItemAsync <AdminAuthenticateUser>(user);

            return(results);
        }
        public async Task <AdminAuthenticateUser> CreateItemAsync(AdminAuthenticateUser adminAuthenticateUser)
        {
            var results = await _container.CreateItemAsync <AdminAuthenticateUser>(adminAuthenticateUser, new PartitionKey(adminAuthenticateUser.Username));

            return(results);
        }