Example #1
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            services.AddAuthorization();

            IPostService        postService        = new PostService();
            IRemoteTokenService remoteTokenService = new RemoteTokenService(postService);

            services.AddAuthorization(options =>
            {
                options.AddPolicy(AuthorizationConfiguration.PolicyName, policy =>
                                  policy.RequireAssertion(context =>
                                                          context.User.HasClaim(c =>
                                                                                (c.Type == AuthorizationConfiguration.PolicyName) &&
                                                                                remoteTokenService.VerifyToken(c.Value))));
            });

            services.AddDistributedMemoryCache();
            services.AddSession();
            services.AddMvc();

            // Database
            services.AddDbContext <AuthServerContext>(
                options => options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));

            return(DependencyInjectionConfigurator.ConfigureContainer(services));
        }
        public void VerifyTokenShouldReturnFalseBecauseOfNotValidServerAnswer()
        {
            // arrange
            var postServiceMock = new Mock <IPostService>();

            postServiceMock.Setup(
                serv => serv.SendAndGetAnswer(It.IsAny <string>(), It.IsAny <Dictionary <string, string> >()))
            .Returns("actually not a valid answer");

            IRemoteTokenService remoteTokenService = new RemoteTokenService(postServiceMock.Object);
            // act
            var answer = remoteTokenService.VerifyToken("sample_jwt");

            // assert
            Assert.Equal(false, answer);
        }
        public void VerifyTokenShouldReturnTrue()
        {
            // arrange
            var postServiceMock = new Mock <IPostService>();

            postServiceMock.Setup(
                serv => serv.SendAndGetAnswer(It.IsAny <string>(), It.IsAny <Dictionary <string, string> >()))
            .Returns(new JsonObject
            {
                Result = JsonValues.TokenValid
            }.ToJson());

            IRemoteTokenService remoteTokenService = new RemoteTokenService(postServiceMock.Object);

            // act
            var answer = remoteTokenService.VerifyToken("sample_jwt");

            // assert
            Assert.Equal(true, answer);
        }