Ejemplo n.º 1
0
        public AuthTokens GenerateTokens(Entities.AppUser user)
        {
            var refresh = GenerateRefreshToken();
            var access  = GenerateAccessToken(user);

            return(new AuthTokens(access, refresh));
        }
Ejemplo n.º 2
0
        public string GenerateAccessToken(Entities.AppUser user)
        {
            var now = DateTime.UtcNow;
            var jwt = new JwtSecurityToken(
                issuer: AuthOptions.ISSUER,
                audience: AuthOptions.AUDIENCE,
                claims: new[]
            {
                new Claim(ClaimTypes.Name, user.Username),
                new Claim("profileId", user.UserProfileId.ToString())
            },
                expires: now.AddMinutes(AuthOptions.LIFETIME),
                signingCredentials: new SigningCredentials(
                    AuthOptions.GetSymmetricSecurityKey(),
                    SecurityAlgorithms.HmacSha256
                    )
                );

            var handler = new JwtSecurityTokenHandler();

            return(handler.WriteToken(jwt));
        }
Ejemplo n.º 3
0
        public async Task <AuthTokens> RegisterAsync(RegistrationData data)
        {
            Validate.NotNull(data, "Registration data");
            Validate.NotNull(data.FirstName, "First name");
            Validate.NotNull(data.LastName, "Last name");
            Validate.NotNull(data.Email, "Email");
            Validate.NotNull(data.LanguageCode, "Language code");

            {
                var temp = (await uow.UserProfileRepository.GetByConditionAsync(x => x.Email == data.Email)).FirstOrDefault();
                if (temp != null)
                {
                    throw new Exception("Email already in use");
                }
            }

            var language = await FindEntityAsync(uow.LanguageRepository, x => x.ShortCode == data.LanguageCode);

            var enc = cs.EncryptPassword(data.Password);

            var rootFolder = new Entities.Folder
            {
                Name           = "Root",
                ParentFolderId = null
            };

            var settings = new Entities.UserSettings
            {
                LanguageId = language.Id,
                Folder     = rootFolder
            };

            var profile = new Entities.UserProfile
            {
                FirstName    = data.FirstName,
                LastName     = data.LastName,
                Email        = data.Email,
                UserSettings = new System.Collections.Generic.List <Entities.UserSettings> {
                    settings
                }
            };

            var user = new Entities.AppUser
            {
                Username     = "******",
                PasswordHash = enc,
                UserProfile  = profile
            };

            await uow.AppUserRepository.CreateAsync(user);

            user.Username = cs.GenerateUsername();
            uow.AppUserRepository.Update(user);

            var tokens = GenerateTokens(user);

            var session = new Entities.Session
            {
                AppUserId = user.Id,
                Token     = tokens.RefreshToken,
                CreatedAt = DateTime.UtcNow.ToLongTimeString()
            };

            await uow.SessionRepository.CreateAsync(session);

            return(tokens);
        }