Exemplo n.º 1
0
        /// <summary>
        /// Registers routes for interacting with machines.
        /// </summary>
        /// <param name="configurationRepositoryContextFactory">The repository context factory.</param>
        /// <param name="stateMachineFactory">The state machine factory.</param>
        /// <param name="logger"></param>
        public InstancesModule(
            REstateConfiguration configuration,
            StateEngineFactory stateEngineFactory,
            IPlatformLogger logger)
            : base(configuration, "/machines", claim => claim.Type == "claim" && claim.Value == "operator")
        {
            Logger             = logger;
            StateEngineFactory = stateEngineFactory;

            GetMachineState();

            IsInState();

            GetAvailableTriggers();

            FireTrigger();

            GetMachineDiagram();

            GetMachineDrawing();

            GetMachineMetadata();

            DeleteMachine();
        }
Exemplo n.º 2
0
 /// <summary>
 /// Requires the inheriting module to have
 /// authentication and a collection of claims.
 /// </summary>
 /// <param name="modulePath">The module path prefix.</param>
 /// <param name="claimPredicates"></param>
 protected SecuredModule(REstateConfiguration configuration, string modulePath, params Predicate <Claim>[] claimPredicates)
     : base(modulePath)
 {
     if (configuration.Authentication.UseAuthentication)
     {
         if (claimPredicates != null)
         {
             this.RequiresClaims(claimPredicates);
         }
         else
         {
             this.RequiresAuthentication();
         }
     }
 }
Exemplo n.º 3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ChronoModule"/> class.
        /// </summary>
        /// <param name="prefix">The prefix.</param>
        /// <param name="chronoRepository">The chrono repository.</param>
        public ChronoModule(REstateConfiguration configuration, IChronoRepositoryFactory chronoRepositoryFactory)
            : base(configuration, "/scheduler", claim => claim.Type == "claim" && claim.Value == "operator")
        {
            _chronoRepositoryFactory = chronoRepositoryFactory;

            Post("/triggers", async(parameters, ct) =>
            {
                var chronoRepository = _chronoRepositoryFactory.OpenRepository(Context.CurrentUser?.GetApiKey());

                var addChronoTriggerRequest = this.Bind <ChronoTrigger>();

                await chronoRepository.AddChronoTriggerAsync(addChronoTriggerRequest, ct).ConfigureAwait(false);

                return(HttpStatusCode.OK);
            });
        }
Exemplo n.º 4
0
        /// <summary>
        /// Registers routes for defining new machines.
        /// </summary>
        /// <param name="configurationRepositoryContextFactory">The repository context factory.</param>
        /// <param name="stateMachineFactory">The state machine factory.</param>
        /// <param name="logger"></param>
        public SchematicsModule(
            REstateConfiguration configuration,
            StateEngineFactory stateEngineFactory,
            IPlatformLogger logger)
            : base(configuration, "/schematics", claim => claim.Type == "claim" && claim.Value == "schematicBuilder")
        {
            Logger             = logger;
            StateEngineFactory = stateEngineFactory;

            GetSchematic();

            GetSchematicDiagram();

            GetSchematicDrawing();

            CreateSchematic();

            ListSchematics();

            InstantiateMachine();

            PreviewDiagram();
        }
Exemplo n.º 5
0
        public AuthenticationModule(REstateConfiguration configuration,
                                    IAuthRepositoryFactory authRepositoryFactory)
            : base("/auth")
        {
            //Get("/login", (parameters) => View["login.html"]);


            //Post("/login", async (parameters, ct) =>
            //{
            //    var credentials = this.Bind<CredentialAuthenticationRequest>();

            //    if (string.IsNullOrWhiteSpace(credentials?.Username) || string.IsNullOrWhiteSpace(credentials.Password))
            //        return 401

            //    var environment = Context.GetOwinEnvironment();
            //    var signInDelegate = (SignInDelegate)environment["jwtandcookie.signin"];

            //    var passwordHash = Convert.ToBase64String(crypto.HmacProvider
            //        .GenerateHmac(credentials.Password));

            //    IPrincipal principal;
            //    using (var repository = authRepositoryContextFactory.OpenAuthRepositoryContext())
            //    {
            //        principal = await repository
            //            .LoadPrincipalByCredentials(credentials.Username, passwordHash, ct);
            //    }

            //    if (principal == null) return 401;

            //    signInDelegate((jti) => new Dictionary<string, object>
            //    {
            //        { "sub", principal.UserOrApplicationName},
            //        { "apikey", principal.ApiKey},
            //        { "claims", principal.Claims }
            //    }, true);

            //    return 201;
            //});

            //We only have routes if authentication is turned on.
            if (configuration.Authentication.UseAuthentication)
            {
                Post("/apikey", async(parameters, ct) =>
                {
                    var apiKeyRequest = this.Bind <ApiKeyAuthenticationRequest>();

                    if (string.IsNullOrWhiteSpace(apiKeyRequest?.ApiKey))
                    {
                        return new Response
                        {
                            StatusCode   = HttpStatusCode.BadRequest,
                            ReasonPhrase = "Unable to detect ApiKey, check content-type headers."
                        }
                    }
                    ;

                    var apiKey = apiKeyRequest.ApiKey;

                    var environment    = Context.GetOwinEnvironment();
                    var signInDelegate = (SignInDelegate)environment["jwtandcookie.signin"];

                    IPrincipal principal;
                    using (var repository = authRepositoryFactory.OpenRepository())
                    {
                        principal = await repository.LoadPrincipalByApiKeyAsync(apiKey, ct).ConfigureAwait(false);
                    }

                    if (principal == null)
                    {
                        return(401);
                    }

                    var jwt = signInDelegate((jti) => new Dictionary <string, object>
                    {
                        { "sub", principal.UserOrApplicationName },
                        { "apikey", principal.ApiKey },
                        { "claims", principal.Claims }
                    }, false);

                    return(Negotiate
                           .WithModel(new JwtResponse {
                        Jwt = jwt
                    })
                           .WithAllowedMediaRange("application/json"));
                });
            }
        }
Exemplo n.º 6
0
 /// <summary>
 /// Requires the inheriting module to have authentication only.
 /// </summary>
 /// <param name="modulePath">The module path prefix.</param>
 protected SecuredModule(REstateConfiguration configuration, string modulePath)
     : this(configuration, modulePath, null)
 {
 }
Exemplo n.º 7
0
 /// <summary>
 /// Requires the inheriting module to have authentication only.
 /// </summary>
 protected SecuredModule(REstateConfiguration configuration)
     : this(configuration, null)
 {
 }