Exemplo n.º 1
0
        public IHttpActionResult Login()
        {
            BasicAuthentication.Authenticate(Request, out TeamsOfUser teamsOfUser);

            if (teamsOfUser != null)
            {
                return(Ok(new { teamsOfUser.User.UserId, teamsOfUser.Token, teamsOfUser.Teams }));
            }

            return(BadRequest("Token wasn't generated."));
        }
Exemplo n.º 2
0
        public void Can_Authenticate_With_Basic_Auth()
        {
            AuthContext     context = new BasicAuthContext(username, password);
            XAuthentication xas     = new BasicAuthentication(context);

            var request = new RestRequest("/api/login", Method.POST);

            xas.Authenticate(client, request);
            var response = client.Execute(request);

            Assert.IsTrue(response.Content.Contains("authenticated"));
        }
Exemplo n.º 3
0
        public IActionResult Test()
        {
            if (!BasicAuthentication.Authenticate(Secrets, Request))
            {
                return(new UnauthorizedResult());
            }

            StringBuilder output = new StringBuilder();

            for (int i = 0; i < 100; i++)
            {
                Dungeon dungeon = new Dungeon(0, 7, 0, Direction.Up);
                output.AppendLine(dungeon.VisualizeAsText(true, true));
                output.AppendLine(dungeon.BuildStats() + Environment.NewLine);
                Console.WriteLine("Iteration: " + i.ToString());
            }

            return(new ObjectResult(output.ToString()));
        }
Exemplo n.º 4
0
        public IActionResult Dungeon(int level, int startX, int startY, string direction)
        {
            if (!BasicAuthentication.Authenticate(Secrets, Request))
            {
                return(new UnauthorizedResult());
            }

            Direction start = Direction.NoDir;

            switch (direction)
            {
            case "U":
                start = Direction.Up;
                break;

            case "D":
                start = Direction.Down;
                break;

            case "L":
                start = Direction.Left;
                break;

            case "R":
                start = Direction.Right;
                break;

            default:
                break;
            }

            Dungeon       dungeon = new Dungeon(level, startX, startY, start);
            StringBuilder output  = new StringBuilder();

            output.AppendLine(dungeon.VisualizeAsText(false, false));
            output.AppendLine(dungeon.BuildStats() + Environment.NewLine);

            return(new ObjectResult(output.ToString()));
        }
Exemplo n.º 5
0
        public IIdentity Authenticate(
            IncomingWebRequestContext request,
            OutgoingWebResponseContext response,
            object[] parameters,
            Type validatorType,
            bool secure,
            bool requiresTransportLayerSecurity,
            string source)
        {
            if (requiresTransportLayerSecurity && !secure)
            {
                throw new BasicRequiresTransportSecurityException();
            }
            var authentication = new BasicAuthentication(request.Headers);
            var validator      = validatorType != null
                ? DependencyResolver.Current.GetOperationService <UserNamePasswordValidator>(OperationContainer.GetCurrent(), validatorType)
                : DependencyResolver.Current.GetOperationService <UserNamePasswordValidator>(OperationContainer.GetCurrent()).ThrowIfNull();

            if (!authentication.Authenticate(validator))
            {
                throw new BasicUnauthorizedException(source);
            }
            return(new GenericIdentity(authentication.Username, "WebBasicAuthenticationHandler"));
        }
 public void TestAuth()
 {
     _auth.Authenticate("Basic " + "QWxhZGRpbjpvcGVuIHNlc2FtZQ==", "myrealm", "POST", false);
     //OnAuth will to the checks
 }
Exemplo n.º 7
0
 public bool Authenticate(LoginRequest request)
 {
     return(_inner.Authenticate(request));
 }
Exemplo n.º 8
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            var jsonServices     = JObject.Parse(File.ReadAllText("appsettings.json"))["Services"];
            var requiredServices = JsonConvert.DeserializeObject <List <Service> >(jsonServices.ToString());

            foreach (var service in requiredServices)
            {
                services.Add(new ServiceDescriptor(
                                 serviceType: Type.GetType(
                                     service.ServiceType,
                                     (name) => AssemblyResolver(name),
                                     null,
                                     true),
                                 implementationType: Type.GetType(
                                     service.ImplementationType,
                                     (name) => AssemblyResolver(name),
                                     null,
                                     true),
                                 lifetime: service.Lifetime));
            }

            services.AddSingleton(sp => Configuration);
            services.AddSingleton <IHttpContextAccessor, HttpContextAccessor>();

            services.AddMvc();
            services.Configure <MvcOptions>(options =>
            {
                options.Filters.Add(new RequireHttpsAttribute());
            });

            // Register the Swagger generator, defining one or more Swagger documents
            services.AddSwaggerGen(options =>
            {
                options.SwaggerDoc("v1",
                                   new Info
                {
                    Title       = "Buying Catalog API",
                    Version     = "v1",
                    Description = "NHS Digital GP IT Futures Buying Catalog API"
                });
                options.SwaggerDoc("porcelain",
                                   new Info
                {
                    Title       = "Buying Catalog API",
                    Version     = "porcelain",
                    Description = "NHS Digital GP IT Futures Buying Catalog API"
                });

                options.DocInclusionPredicate((docName, apiDesc) =>
                {
                    var controllerActionDescriptor = apiDesc.ActionDescriptor as ControllerActionDescriptor;
                    if (controllerActionDescriptor == null)
                    {
                        return(false);
                    }

                    var versions = controllerActionDescriptor.MethodInfo.DeclaringType
                                   .GetCustomAttributes(true)
                                   .OfType <ApiVersionAttribute>()
                                   .SelectMany(attr => attr.Versions);
                    var tags = controllerActionDescriptor.MethodInfo.DeclaringType
                               .GetCustomAttributes(true)
                               .OfType <ApiTagAttribute>();

                    return(versions.Any(
                               v => $"v{v.ToString()}" == docName) ||
                           tags.Any(tag => tag.Tag == docName));
                });

                // Set the comments path for the Swagger JSON and UI.
                var xmlPath = Path.Combine(AppContext.BaseDirectory, "NHSD.GPITF.BuyingCatalog.xml");
                options.IncludeXmlComments(xmlPath);
                options.DescribeAllEnumsAsStrings();
                options.OperationFilter <ExamplesOperationFilter>();
            });

            services
            .AddAuthentication(BasicAuthenticationDefaults.AuthenticationScheme)
            .AddBasicAuthentication(
                options =>
            {
                options.Realm  = "NHSD.GPITF.BuyingCatalog";
                options.Events = new BasicAuthenticationEvents
                {
                    OnValidatePrincipal = context =>
                    {
                        return(BasicAuthentication.Authenticate(context));
                    }
                };
            });

            services
            .AddAuthentication(options =>
            {
                options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                options.DefaultChallengeScheme    = JwtBearerDefaults.AuthenticationScheme;
            })
            .AddJwtBearer(options =>
            {
                options.Authority = Configuration["Jwt:Authority"];
                options.Audience  = Configuration["Jwt:Audience"];
                options.Events    = new JwtBearerEvents
                {
                    OnTokenValidated = async context =>
                    {
                        await BearerAuthentication.Authenticate(Configuration, context);
                    }
                };
            });
        }