Example #1
0
        // Given id and password, authenticate the user
        public static bool Authenticate(HttpContext context)
        {
            bool authenticated = false;

            // get user id and password
            string id            = WebTools.Get(context, "id");
            string password      = WebTools.Get(context, "password");
            string navigation_id = WebTools.GetNavigationId(context);

            if (string.IsNullOrEmpty(id) == false && string.IsNullOrEmpty(navigation_id) == false)
            {
                // find user with matching id and password
                var db    = (SQL)context.Items["db"];
                var param = new Dictionary <string, object>();
                param["id"] = id; param["navigation_id"] = navigation_id;
                var users = db.Query(
                    "SELECT * FROM core_user WHERE id = @id AND navigation_id = @navigation_id"
                    , param);

                if (users != null && users.Count() == 1)
                {
                    var user = users.First();

                    bool valid = false;
                    // if password is DBNull and also empty then pass
                    if (user.Get("password") is DBNull || string.IsNullOrEmpty($"{user.Get("password")}"))
                    {
                        valid = true;
                    }

                    // Verify the password
                    else if (SecurePasswordHasher.Verify(password, $"{user.Get("password")}"))
                    {
                        valid = true;
                    }

                    if (valid)
                    {
                        // create a new token
                        var token = JwtTool.CreateToken(
                            context
                            , $"{user["id"]}"
                            , $"{user["name"]}"
                            , RolesOfUser(context, $"{user["_id"]}")
                            );

                        RefreshHeader(context, token);
                        // is authenticated
                        authenticated = true;
                    }
                }
            }

            return(authenticated);
        }
Example #2
0
        public static object Run(
            HttpContext context
            , string configuration
            , IList <object> dataservices
            )
        {
            // Get Configuration
            if (string.IsNullOrEmpty(configuration))
            {
                return new { error = "No configuration specified." }
            }
            ;
            JObject config = JsonConvert.DeserializeObject <JObject>(configuration);

            // check parameters
            string table = $"{config["table"]}"; if (string.IsNullOrEmpty(table) == true)

            {
                return new { error = "No table specified." }
            }
            ;
            string filepathCol = $"{config["filepath"]}"; if (string.IsNullOrEmpty(filepathCol) == true)

            {
                return new { error = "No filepath configured." }
            }
            ;
            string contentCol = $"{config["content"]}"; if (string.IsNullOrEmpty(contentCol) == true)

            {
                return new { error = "No content specified." }
            }
            ;

            // Get Navigation ID
            string navigation_id = WebTools.GetNavigationId(context);

            if (string.IsNullOrEmpty(navigation_id))
            {
                return new { error = "No X-App-Key specified" }
            }
            ;

            // Retrieve DataService
            if (dataservices == null || dataservices.Count == 0)
            {
                return new { error = "Data Services not provided" }
            }
            ;
            SQL db = (SQL)dataservices.FirstOrDefault();

            if (db == null)
            {
                return new { error = "Data Service not provided" }
            }
            ;

            // Get filepath
            string uploadFolder = WebTools.Get(context, "folder");

            if (string.IsNullOrEmpty(uploadFolder) == true)
            {
                return new { error = "No folder specified" }
            }
            ;

            // Get File
            IList <string> result = new List <string>();
            var            files  = context.Request.Form.Files;

            foreach (var file in files)
            {
                var parameters = new Dictionary <string, object>();

                parameters[filepathCol] = Path.Combine(uploadFolder, file.FileName);

                // copy file stream to byte array
                using (var uploadStream = file.OpenReadStream())
                {
                    uploadStream.Seek(0, SeekOrigin.Begin);
                    using (var reader = new BinaryReader(uploadStream))
                        parameters[contentCol] = reader.ReadBytes((int)uploadStream.Length);

                    // save to SQL
                    db.Execute($@"
                        BEGIN TRAN
                           UPDATE {table} SET {filepathCol} = @{filepathCol}, {contentCol} = @{contentCol}    
                           WHERE {filepathCol} = @{filepathCol}
                           IF @@rowcount = 0
                           BEGIN
                              INSERT INTO {table} ({filepathCol}, {contentCol}) values (@{filepathCol}, @{contentCol})
                           END
                        COMMIT TRAN"
                               , parameters);
                }
                result.Add($"{parameters[filepathCol]}");
            }

            return(JsonConvert.SerializeObject(result));
        }
    }
}
Example #3
0
        public static object Run(
            HttpContext context
            , string configuration
            , IList <object> dataservices
            )
        {
            // Get Configuration
            if (string.IsNullOrEmpty(configuration))
            {
                return new { error = "No configuration specified." }
            }
            ;
            JObject config = JsonConvert.DeserializeObject <JObject>(configuration);

            // check parameters
            string table = $"{config["table"]}"; if (string.IsNullOrEmpty(table) == true)

            {
                return new { error = "No table specified." }
            }
            ;
            string filepathCol = $"{config["filepath"]}"; if (string.IsNullOrEmpty(filepathCol) == true)

            {
                return new { error = "No filepath configured." }
            }
            ;
            string contentCol = $"{config["content"]}"; if (string.IsNullOrEmpty(contentCol) == true)

            {
                return new { error = "No content specified." }
            }
            ;

            // Get Navigation ID
            string navigation_id = WebTools.GetNavigationId(context);

            if (string.IsNullOrEmpty(navigation_id))
            {
                return new { error = "No X-App-Key specified" }
            }
            ;

            // Retrieve DataService
            if (dataservices == null || dataservices.Count == 0)
            {
                return new { error = "Data Services not provided" }
            }
            ;
            SQL db = (SQL)dataservices.FirstOrDefault();

            if (db == null)
            {
                return new { error = "Data Service not provided" }
            }
            ;

            // Get filepath
            string filepath = WebTools.Get(context, "filepath");

            if (string.IsNullOrEmpty(filepath) == true)
            {
                return new { error = "No filepath specified" }
            }
            ;

            // Get File
            context.Response.Headers["Content-Disposition"] = $"inline;FileName={Path.GetFileName(filepath)}";

            // Read file from the SQL table
            using (MemoryStream ms = new MemoryStream())
            {
                var parameters = new Dictionary <string, object>();

                parameters[filepathCol] = filepath;

                var result = db.Query($@"SELECT {contentCol} FROM {table} WHERE {filepathCol}=@{filepathCol}", parameters);
                if (result != null && result.Count() > 0)
                {
                    return(result[0][contentCol]);
                }
            }

            return(null);
        }
    }
}
Example #4
0
        public static object Run(
            HttpContext context
            , string configuration
            , IList <object> dataservices
            )
        {
            // Get Configuration
            if (string.IsNullOrEmpty(configuration))
            {
                return new { error = "No configuration specified." }
            }
            ;
            JObject config = JsonConvert.DeserializeObject <JObject>(configuration);

            // check parameters
            string folder = $"{config["folder"]}"; if (string.IsNullOrEmpty(folder) == true)

            {
                return new { error = "No folder specified." }
            }
            ;

            // Get Navigation ID
            string navigation_id = WebTools.GetNavigationId(context);

            if (string.IsNullOrEmpty(navigation_id))
            {
                return new { error = "No X-App-Key specified" }
            }
            ;

            // Get upload folder
            string uploadFolder = Path.Combine(folder, WebTools.Get(context, "folder"));

            if (string.IsNullOrEmpty(uploadFolder) == true)
            {
                return new { error = "No folder specified" }
            }
            ;

            // Get File
            IList <string> result = new List <string>();
            var            files  = context.Request.Form.Files;

            foreach (var file in files)
            {
                string uploadPath = Path.Combine(uploadFolder, file.FileName);

                using (var fileStream = File.Create(uploadPath))
                {
                    var uploadStream = file.OpenReadStream();
                    uploadStream.Seek(0, SeekOrigin.Begin);
                    uploadStream.CopyTo(fileStream);
                }
                result.Add(uploadPath);
            }

            return(JsonConvert.SerializeObject(result));
        }
    }
}