示例#1
0
        public string GenerateTokenForAudience(string audienceName, string secret)
        {
            //audienceName = username
            //Το secret πρέπει να ειναι base64 string του Sha512 του secret(password) του audience
            string password = string.Empty;

            using (var context = new TPDMSDbModel())
            {
                password = context.admUsers.Where(u => u.Username == audienceName)
                           .Select(u => u.Password).FirstOrDefault();
                if (string.IsNullOrWhiteSpace(secret))
                {
                    return(null);
                }
            }
            var result = SI.Identity.Helpers.SecurityHelper.GetHashedPassword(audienceName, secret);

            if (result != password)
            {
                return(null);
            }

            //secret = password:audienceName
            secret = $"{result}:{audienceName}";
            using (var dbContext = new TPDMSDbContext(WebApiConfig.Options))
            {
                var SpecificUser = dbContext.admUsers.FirstOrDefault(x => x.Username == audienceName && x.Password == password);
                //TODO: Να πάρουμε τον issuer και το expiresInMinutes από configuration
                int tokenDuration = SpecificUser.TokenDuration ?? 30;
                return(CreateToken(audienceName, secret, "self", tokenDuration));
            }
        }
示例#2
0
        public void DeclareObject(JObject entity, string user, string entityName = null, Error error = null)
        {
            var entryObject = EventGenerator.SetObject(entity, entityName, error);

            using (var dbContext = new TPDMSDbContext(WebApiConfig.Options))
            {
                {
                    // get type of object from json
                    var currentType = entryObject.GetType();
                    var props       = new List <PropertyInfo>(currentType.GetProperties());
                    // find the properties you need to use
                    var propert1  = props.FirstOrDefault(x => x.Name == "Propert1");
                    var property2 = props.FirstOrDefault(x => x.Name == "Propert2");
                    var property3 = props.FirstOrDefault(x => x.Name == "Propert3");
                    var property4 = props.FirstOrDefault(x => x.Name == "Propert4");
                    var property5 = props.FirstOrDefault(x => x.Name == "Propert5");
                    var property6 = props.FirstOrDefault(x => x.Name == "Propert6");

                    var propert7       = props.FirstOrDefault(x => x.Name == "Propert7");
                    var property8      = props.FirstOrDefault(x => x.Name == "Propert8");
                    var propert7Value  = propert7?.GetValue(propert7.GetGetMethod().IsStatic ? null : entryObject);
                    var property8Value = property8?.GetValue(property8.GetGetMethod().IsStatic ? null : entryObject);

                    // Handle duplicates for IEnumerable<object> with reflection
                    if (Duplicates.Handler(property8, propert7, aUIsValue, upUIsValue, entryObject))
                    {
                        propert7Value  = property8?.GetValue(property8.GetGetMethod().IsStatic ? null : entryObject);
                        property8Value = propert7?.GetValue(propert7.GetGetMethod().IsStatic ? null : entryObject);
                    }

                    #region some business for specific entity

                    #endregion some business for specific entity

                    #region some business for specific entity

                    #endregion some business for specific entity

                    #region some business for specific entity

                    #endregion some business for specific entity

                    #region some business for specific entity

                    #endregion some business for specific entity
                }
            }
        }
示例#3
0
 public IHttpActionResult Token([FromBody] TokenRequest request)
 {
     try
     {
         var auth  = new AuthenticationModule();
         var token = auth.GenerateTokenForAudience(request.audienceName, request.secret);
         if (string.IsNullOrWhiteSpace(token))
         {
             return(BadRequest("Not authorized.Invalid credentials.Please try again."));
         }
         Log.Information("User {UserName} requests token.", request.audienceName);
         using (var dbContext = new TPDMSDbContext(WebApiConfig.Options))
         {
             var SpecificUser = dbContext.admUsers.Where(x => x.Username == request.audienceName).FirstOrDefault();
             return(Ok(new { token, expiresin = SpecificUser.TokenDuration }));
         }
     }
     catch (Exception ex)
     {
         Log.Error(ex.Message);
         return(InternalServerError());
     }
 }
        public static object SetObject(JObject entity, string entityName = null, Error error = null)
        {
            using (var dbContext = new TPDMSDbContext(WebApiConfig.Options))
            {
                var mapping      = dbContext.Mappings.FirstOrDefault(x => x.Name == $"json{entityName}");
                var mappingField = dbContext.MappingFields.Where(x => x.MappingId == mapping.MappingId);
                var className    = entityName;
                var assembly     = AppDomain.CurrentDomain.GetAssemblies()
                                   .FirstOrDefault(t => t.GetName().Name == $"DataLayer");
                var type     = assembly.GetType(className);
                var instance = Activator.CreateInstance(type);

                foreach (var field in mappingField)
                {
                    DataType dataType   = null;
                    var      childField = dbContext.EntityFields.Single(y => y.EntityFieldId.ToString() == field.SourceMapping);
                    dataType = dbContext.DataTypes.Single(y => y.DataTypeID == childField.DataTypeId);
                    dataType = dataType.BaseDataTypeId != null?dbContext.DataTypes.Single(y => y.DataTypeID == dataType.BaseDataTypeId) : dataType;

                    var name = childField.Name;
                    switch (childField.Cardinality)
                    {
                    case "statement 1":
                    {
                        var prpName      = field.TargetMapping.Remove(0, 2);
                        var typePerField = assembly.GetType($"DataLayer.{prpName}");
                        entity.TryGetValue(name, out var result);
                        if (result == null || result.ToString() == string.Empty)
                        {
                            continue;
                        }
                        Type   listType     = null;
                        object instanceList = null;

                        foreach (var item in result?.Values())
                        {
                            var stringItem = item?.ToString();
                            if (childField.MaxLength < stringItem?.Length)
                            {
                                try
                                {
                                    throw new FieldSizeViolationException(childField.Name, childField.MaxLength, true);
                                }
                                catch (FieldSizeViolationException ex)
                                {
                                    error.HasWarning      = true;
                                    error.WarningMessages = error.WarningMessages ?? new Dictionary <string, string>();
                                    error.WarningMessages.Add($"{ex?.GetType()?.ToString()}_{childField?.Name}", ex.Message);
                                }
                            }
                            var instanceChild = Activator.CreateInstance(typePerField);
                            var property      = typePerField.GetProperty(name);
                            var checkDataType = Helper.ConvertJToken(item, dataType, property.Name);
                            if (checkDataType is string && (string)checkDataType == string.Empty)
                            {
                                property?.SetValue(instanceChild, null);
                            }

                            property.SetValue(instanceChild, checkDataType);
                            var enumerables = Helper.GetCollections(type);
                            foreach (var prp in enumerables.Where(p => p.PropertyType.GetGenericArguments().First() == typePerField))
                            {
                                listType     = listType ?? prp.PropertyType.GetGenericArguments().First();
                                instanceList = instanceList ?? Activator.CreateInstance(typeof(List <>).MakeGenericType(listType));
                                instanceList.GetType().GetMethod("Add").Invoke(instanceList, new[] { instanceChild });
                                prp?.SetValue(instance, instanceList, null);
                                break;
                            }
                        }

                        break;
                    }

                    case "Statement 2":
                    {
                        entity.TryGetValue(name, out var result);
                        var prpName = field.TargetMapping.Remove(0, 2);

                        if (result == null || result.ToString() == string.Empty)
                        {
                            continue;
                        }
                        var stringResult = result?.ToString();
                        if (childField.MaxLength < stringResult?.Length)
                        {
                            try
                            {
                                throw new FieldSizeViolationException(childField.Name, childField.MaxLength, true);
                            }
                            catch (FieldSizeViolationException ex)
                            {
                                error.HasWarning      = true;
                                error.WarningMessages = error.WarningMessages ?? new Dictionary <string, string>();
                                error.WarningMessages.Add($"{ex?.GetType()?.ToString()}_{childField?.Name}", ex.Message);
                            }
                        }
                        var checkDataTypeResult = Helper.ConvertJToken(result, dataType, prpName);
                        var property            = instance.GetType().GetProperties().FirstOrDefault(p => p.Name == prpName);

                        if (checkDataTypeResult is string && (string)checkDataTypeResult == string.Empty)
                        {
                            property?.SetValue(instance, null);
                        }
                        else
                        {
                            property?.SetValue(instance, checkDataTypeResult);
                        }

                        break;
                    }
                    }
                }
                return(instance);
            }
        }