public async void CreateJwtToken_ReturnsJwtToken_ShouldBeReturnCorrectToken(string input, string expectedValue)
        {
            var jwtFactory = new JwtTokenFactory();

            var token = await jwtFactory.CreateJwtToken(input);

            Assert.Contains(expectedValue, token);
        }
Ejemplo n.º 2
0
        public static string CreateToken(string name, string email, string username, string role)
        {
            var tokenFactory = new JwtTokenFactory(new Base64UrlUtil(true), new RsaSigningUtil("caissekey.pem", "caissekeyp.pem", HashFunctionType.Sha256));

            return(tokenFactory.CreateToken("CaisseWebAPI", "CaisseWebApiAuth", name, email, 30, username, new List <string>()
            {
                role
            }));
        }
Ejemplo n.º 3
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();
            services.AddAutoMapper();
            services.AddCors();

            services.AddScoped <IUnitOfWork, UnitOfWork>();

            services.AddScoped <IUserService, UserService>();
            services.AddScoped <IUserRepository, UserRepository>();

            services.AddScoped <IRelationService, RelationService>();
            services.AddScoped <IRelationRepository, RelationRepository>();

            services.AddScoped <IMeetingService, MeetingService>();
            services.AddScoped <IMeetingRepository, MeetingRepository>();

            services.AddScoped <JwtTokenFactory>();
            services.AddScoped <IAuthService, AuthService>();

            var builder = services.AddIdentityCore <User>(opt =>
            {
                opt.Password.RequiredLength         = 3;
                opt.Password.RequireLowercase       = false;
                opt.Password.RequireUppercase       = false;
                opt.Password.RequireDigit           = false;
                opt.Password.RequireNonAlphanumeric = false;
            });

            builder = new IdentityBuilder(builder.UserType, typeof(UserRole), builder.Services);
            builder.AddEntityFrameworkStores <ApplicationDbContext>();
            builder.AddSignInManager <SignInManager <User> >();

            services
            .AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
            .AddJwtBearer(options =>
            {
                options.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidateIssuer           = true,
                    ValidateAudience         = true,
                    ValidateLifetime         = true,
                    ValidateIssuerSigningKey = true,

                    ValidIssuer      = Configuration.GetSection("JwtTokens")["Issuer"],
                    ValidAudience    = Configuration.GetSection("JwtTokens")["Audience"],
                    IssuerSigningKey = JwtTokenFactory.GetSecurityKey(Configuration.GetSection("JwtTokens")["Key"])
                };
            });

            services.AddDbContext <ApplicationDbContext>(options =>
            {
                options.UseSqlServer(Configuration.GetConnectionString("LocalDb"), b => b.MigrationsAssembly("Geoloc"));
            });
        }
        public void Setup()
        {
            _configMock = new Mock <IConfiguration>();
            var sectionMock = new Mock <IConfigurationSection>();

            sectionMock.Setup(x => x[It.IsAny <string>()]).Returns("1");
            sectionMock.Setup(x => x["Key"]).Returns("very_secure_test_key");
            _configMock.Setup(x => x.GetSection(It.IsAny <string>())).Returns(sectionMock.Object);

            _factory = new JwtTokenFactory(_configMock.Object);
        }
        public void GetSecurityKey_GivenTheSameKeys_ReturnsIdenticalSecurityKeyId()
        {
            // Arrange
            const string key = "test_key";

            // Act
            var result1 = JwtTokenFactory.GetSecurityKey(key);
            var result2 = JwtTokenFactory.GetSecurityKey(key);

            // Assert
            Assert.AreEqual(result1.KeyId, result2.KeyId);
        }
Ejemplo n.º 6
0
 public TokenController(
     JwtTokenFactory tokenFactory,
     AuthenticationService service,
     ILogger logger,
     IHttpContextAccessor httpContextAccessor,
     IHostingEnvironment hostingEnvironment,
     IConfiguration config,
     MerlinReadContext readContext)
 {
     this.tokenFactory        = tokenFactory;
     this.service             = service;
     this.logger              = logger;
     this.httpContextAccessor = httpContextAccessor;
     this.hostingEnvironment  = hostingEnvironment;
     this.config              = config;
     this.readContext         = readContext;
 }
Ejemplo n.º 7
0
        public async Task <Result <string> > Login(ILoginContext model)
        {
            var result = await this.signInManager.PasswordSignInAsync(
                model.Username,
                model.Password,
                model.RememberMe,
                lockoutOnFailure : false);

            if (!result.Succeeded)
            {
                return(Result <string> .Failure(result.ToString()));
            }

            var jwtToken = JwtTokenFactory.Create(
                model.Username,
                configuration["Jwt:Key"],
                configuration["Jwt:Issuer"],
                TimeSpan.FromDays(int.Parse(configuration["Jwt:ExpirationInDays"])));

            return(Result <string> .Success(jwtToken));
        }
        public void CreateJwtToken_ReturnsException_ShouldBeReturnException(string input)
        {
            var jwtFactory = new JwtTokenFactory();

            Assert.ThrowsAsync <ArgumentException>(async() => await jwtFactory.CreateJwtToken(input));
        }
Ejemplo n.º 9
0
 public AuthenticationService(SignInManager <User> signInManager, JwtTokenFactory jwtTokenFactory)
 {
     _signInManager   = signInManager;
     _jwtTokenFactory = jwtTokenFactory;
 }
Ejemplo n.º 10
0
 public AccountController(UserManager <User> userManager, JwtTokenFactory jwtTokenFactory)
 {
     this.userManager     = userManager;
     this.jwtTokenFactory = jwtTokenFactory;
 }