public async Task FindRoutes(string method, string path, string aResult) { TwinoMvc mvc = new TwinoMvc(); mvc.Init(); mvc.CreateRoutes(Assembly.GetExecutingAssembly()); HttpRequest request = new HttpRequest(); request.Method = method; request.Path = path; HttpResponse response = new HttpResponse(); RouteMatch match = mvc.RouteFinder.Find(mvc.Routes, request); Assert.NotNull(match); TwinoController controller = await mvc.ControllerFactory.CreateInstance(mvc, match.Route.ControllerType, request, response, mvc.Services.CreateScope()); var parameters = MvcConnectionHandler.FillParameters(request, match).Select(x => x.Value).ToArray(); Task <IActionResult> task = (Task <IActionResult>)match.Route.ActionType.Invoke(controller, parameters); IActionResult result = task.Result; string url = Encoding.UTF8.GetString(((MemoryStream)result.Stream).ToArray()); url.Should().Be(aResult); }
static void Main(string[] args) { TwinoMvc mvc = new TwinoMvc(); TwinoServer server = new TwinoServer(); mvc.Init(); server.UseMvc(mvc, HttpOptions.CreateDefault()); server.Start(); server.BlockWhileRunning(); }
static void Main(string[] args) { TwinoMvc mvc = new TwinoMvc(); TwinoServer server = new TwinoServer(); mvc.Init(); server.UseMvc(mvc); server.Start(5000); server.BlockWhileRunning(); }
/// <summary> /// Creates new instance of a TwinoController object /// </summary> public async Task <TwinoController> CreateInstance(TwinoMvc mvc, Type controllerType, HttpRequest request, HttpResponse response, IContainerScope scope) { ConstructorInfo[] constructors = controllerType.GetConstructors(); //some constructors may have not-registered parameters. //these constructor must not break the operation. //we need to find the right constructor (right means, ctor that we can provide all parameters from the service container) foreach (ConstructorInfo constructor in constructors) { ParameterInfo[] parameters = constructor.GetParameters(); object[] values = new object[parameters.Length]; bool skip = false; //each parameter must be provided from the service container for (int i = 0; i < parameters.Length; i++) { ParameterInfo p = parameters[i]; Type type = p.ParameterType; object value = await mvc.Services.Get(type, scope); //if the parameter could not provide, we need to skip this constructor if (value == null) { skip = true; break; } values[i] = value; } if (skip) { continue; } //if the application comes here, we are sure all parameters are created //now we can create instance with these parameter values TwinoController result = (TwinoController)Activator.CreateInstance(controllerType, values); //set the controller properties result.Request = request; result.Response = response; result.Server = mvc.Server; result.CurrentScope = scope; return(result); } return(null); }
/// <summary> /// Creates new instance of a TwinoController object /// </summary> public async Task <TwinoController> CreateInstance(TwinoMvc mvc, Type controllerType, HttpRequest request, HttpResponse response, IContainerScope scope) { ConstructorInfo[] constructors = controllerType.GetConstructors(BindingFlags.Public | BindingFlags.Instance); if (constructors.Length == 0) { throw new InvalidOperationException("There is no accessible constructor found in " + controllerType.FullName); } else if (constructors.Length > 1) { throw new InvalidOperationException($"{controllerType.FullName} must have only one accessible constructor."); } ConstructorInfo constructor = constructors[0]; ParameterInfo[] parameters = constructor.GetParameters(); object[] values = new object[parameters.Length]; //each parameter must be provided from the service container for (int i = 0; i < parameters.Length; i++) { ParameterInfo p = parameters[i]; Type type = p.ParameterType; object value = await mvc.Services.Get(type, scope); if (typeof(IContainerScope).IsAssignableFrom(type)) { values[i] = scope; } else { object v = await mvc.Services.Get(type, scope); values[i] = v; } values[i] = value; } //if the application comes here, we are sure all parameters are created //now we can create instance with these parameter values TwinoController result = (TwinoController)Activator.CreateInstance(controllerType, values); //set the controller properties result.Request = request; result.Response = response; result.Server = mvc.Server; result.CurrentScope = scope; return(result); }
/// <summary> /// Adds JWT Implementation to Twino MVC /// </summary> public static TwinoMvc AddJwt(this TwinoMvc twino, Action <JwtOptions> options) { JwtOptions jwtOptions = new JwtOptions(); options(jwtOptions); jwtOptions.SigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtOptions.Key)); twino.ClaimsPrincipalValidator = new JwtClaimsPrincipalValidator(jwtOptions); twino.Services.AddSingleton <JwtOptions, JwtOptions>(jwtOptions); twino.Services.AddScoped <IJwtProvider, JwtProvider>(); return(twino); }
static void Main(string[] args) { using TwinoMvc mvc = new TwinoMvc(); mvc.IsDevelopment = false; mvc.Init(twino => { twino.Services.AddScoped <IScopedService, ScopedService>(); twino.Services.AddTransient <IFirstService, FirstService>(); twino.Services.AddTransient <ISecondService, SecondService>(); twino.AddJwt(options => { options.Key = "Very_very_secret_key"; options.Issuer = "localhost"; options.Audience = "localhost"; options.Lifetime = TimeSpan.FromHours(1); options.ValidateAudience = false; options.ValidateIssuer = false; options.ValidateLifetime = true; }); twino.Policies.Add(Policy.RequireRole("Admin", "Admin")); twino.Policies.Add(Policy.RequireClaims("IT", "Database", "Cloud", "Source")); twino.Policies.Add(Policy.Custom("Custom", (d, c) => true)); twino.Services.AddHttpClient(); twino.StatusCodeResults.Add(HttpStatusCode.Unauthorized, new JsonResult(new { Message = "Access denied" })); }); CorsMiddleware cors = new CorsMiddleware(); cors.AllowAll(); mvc.Use(app => { app.UseMiddleware(cors); app.UseMiddleware <TMid>(); app.UseFiles("/download", "/home/mehmet/files"); }); TwinoServer server = new TwinoServer(); var opt = HttpOptions.CreateDefault(); opt.HttpConnectionTimeMax = 0; server.UseMvc(mvc, opt); server.Start(441); server.BlockWhileRunning(); }
/// <summary> /// Checks Policy property. /// If it's not empty and validation fails, return false. /// Otherwise returns true. /// </summary> private bool CheckPolicy(TwinoMvc mvc, ActionDescriptor descriptor, FilterContext context) { if (string.IsNullOrEmpty(Policy)) { return(true); } Policy policy = mvc.Policies.Get(Policy); if (policy != null) { if (!policy.Validate(descriptor, context)) { context.Result = StatusCodeResult.Unauthorized(); return(false); } } return(true); }
/// <summary> /// Verifies authority of action execution. /// If authorization fails, context.Result will be set to 403 or 401 /// </summary> public void VerifyAuthority(TwinoMvc mvc, ActionDescriptor descriptor, FilterContext context) { if (context.User == null) { context.Result = StatusCodeResult.Unauthorized(); return; } if (!CheckPolicy(mvc, descriptor, context)) { return; } if (!CheckRoles(descriptor, context)) { return; } CheckClaims(descriptor, context); }
public void Run() { TwinoMvc mvc = new TwinoMvc(); HomeController cont = new HomeController(); Assert.NotNull(cont); mvc.Init(); Assembly asm = Assembly.GetExecutingAssembly(); mvc.CreateRoutes(asm); TwinoServer server = new TwinoServer(ServerOptions.CreateDefault()); server.UseMvc(mvc, HttpOptions.CreateDefault()); server.Start(47442); System.Threading.Thread.Sleep(1000); HttpClient client = new HttpClient(); HttpResponseMessage response = client.GetAsync("http://127.0.0.1:47442/home/get").Result; Assert.Equal(HttpStatusCode.OK, response.StatusCode); }
public MiddlewareRunner(TwinoMvc mvc, IContainerScope scope) { _mvc = mvc; _scope = scope; }
public MvcAppBuilder(TwinoMvc mvc) { Mvc = mvc; }