Exemplo n.º 1
0
        public static ResolutionResult Resolve(JObject json, SCIMSchema mainSchema, ICollection <SCIMSchema> extensionSchemas)
        {
            var rows    = new List <ResolutionRowResult>();
            var schemas = new List <SCIMSchema>
            {
                mainSchema
            };

            schemas.AddRange(extensionSchemas);
            foreach (var kvp in json)
            {
                if (kvp.Key == StandardSCIMRepresentationAttributes.Schemas || SCIMConstants.StandardSCIMCommonRepresentationAttributes.Contains(kvp.Key))
                {
                    continue;
                }

                if (extensionSchemas.Any(s => kvp.Key.StartsWith(s.Id, StringComparison.InvariantCultureIgnoreCase)))
                {
                    rows.AddRange(ResolveFullQualifiedName(kvp, extensionSchemas));
                    continue;
                }

                rows.Add(Resolve(kvp, schemas));
            }

            return(new ResolutionResult(schemas, rows));
        }
 public SCIMRepresentationAttributeBuilder(string parentId, SCIMSchema schema, SCIMSchemaAttribute scimSchemaAttribute)
 {
     _schema              = schema;
     _parentId            = parentId;
     _scimSchemaAttribute = scimSchemaAttribute;
     _attributes          = new List <SCIMRepresentationAttribute>();
 }
 public SCIMSchemaAttributeBuilder(string parentId, string fullPath, SCIMSchema schema, SCIMSchemaAttribute scimSchemaAttribute)
 {
     _schema = schema;
     _scimSchemaAttribute = scimSchemaAttribute;
     _fullPath = fullPath;
     _parentId = parentId;
 }
        public SCIMSchemaAttributeBuilder AddAttribute(SCIMSchema schema, string name, SCIMSchemaAttributeTypes type, Action<SCIMSchemaAttributeBuilder> callback = null, bool caseExact = false, bool required = false,
            SCIMSchemaAttributeMutabilities mutability = SCIMSchemaAttributeMutabilities.READWRITE,
            SCIMSchemaAttributeReturned returned = SCIMSchemaAttributeReturned.DEFAULT,
            SCIMSchemaAttributeUniqueness uniqueness = SCIMSchemaAttributeUniqueness.NONE, string description = null, bool multiValued = false, List<string> canonicalValues = null)
        {
            var fullPath = $"{_fullPath}.{name}";
            var builder = new SCIMSchemaAttributeBuilder(_scimSchemaAttribute.Id, fullPath, schema, new SCIMSchemaAttribute(Guid.NewGuid().ToString())
            {
                Name = name,
                MultiValued = multiValued,
                CaseExact = caseExact,
                Required = required,
                Mutability = mutability,
                Returned = returned,
                Uniqueness = uniqueness,
                Type = type,
                Description = description,
                CanonicalValues = canonicalValues,
            });
            if (callback != null)
            {
                callback(builder);
            }

            _schema.AddAttribute(builder.Build());
            return this;
        }
Exemplo n.º 5
0
        private JObject ToDto(SCIMSchema schema)
        {
            var location = $"{Request.GetAbsoluteUriWithVirtualPath()}/{SCIMConstants.SCIMEndpoints.ResourceTypes}/{schema.ResourceType}";

            return(new JObject
            {
                { SCIMConstants.ResourceTypeAttribute.Schemas, new JArray(new List <string>  {
                        SCIMConstants.StandardSchemas.ResourceTypeSchema.Id
                    }) },
                { SCIMConstants.ResourceTypeAttribute.Id, schema.ResourceType },
                { SCIMConstants.ResourceTypeAttribute.Name, schema.Name },
                { SCIMConstants.ResourceTypeAttribute.Description, schema.Description },
                { SCIMConstants.ResourceTypeAttribute.Endpoint, $"/{schema.ResourceType}" },
                { SCIMConstants.ResourceTypeAttribute.Schema, schema.Id },
                { SCIMConstants.ResourceTypeAttribute.SchemaExtensions, new JArray(schema.SchemaExtensions.Select(s => new JObject
                    {
                        { SCIMConstants.ResourceTypeAttribute.Schema, s.Schema },
                        { SCIMConstants.ResourceTypeAttribute.Required, s.Required }
                    })) },
                { SCIMConstants.ResourceTypeAttribute.Meta, new JObject
                  {
                      { SCIMConstants.StandardSCIMMetaAttributes.Location, location },
                      { SCIMConstants.StandardSCIMMetaAttributes.ResourceType, schema.ResourceType }
                  } }
            });
        }
        protected JObject ToDto(SCIMSchema schema, List <ResourceTypeResolutionResult> resolutionResults)
        {
            var location         = $"{Request.GetAbsoluteUriWithVirtualPath()}/{SCIMEndpoints.ResourceType}/{schema.ResourceType}";
            var endpoint         = string.Empty;
            var resolutionResult = resolutionResults.FirstOrDefault(r => r.ResourceType == schema.ResourceType);

            if (resolutionResult != null)
            {
                endpoint = $"{Request.GetRelativePath()}/{resolutionResult.ControllerName}";
            }

            return(new JObject
            {
                { ResourceTypeAttribute.Schemas, new JArray(new List <string>  {
                        StandardSchemas.ResourceTypeSchema.Id
                    }) },
                { ResourceTypeAttribute.Id, schema.ResourceType },
                { ResourceTypeAttribute.Name, schema.ResourceType },
                { ResourceTypeAttribute.Description, schema.Description },
                { ResourceTypeAttribute.Endpoint, endpoint },
                { ResourceTypeAttribute.Schema, schema.Id },
                { ResourceTypeAttribute.SchemaExtensions, new JArray(schema.SchemaExtensions.Select(s => new JObject
                    {
                        { ResourceTypeAttribute.Schema, s.Schema },
                        { ResourceTypeAttribute.Required, s.Required }
                    })) },
                { ResourceTypeAttribute.Meta, new JObject
                  {
                      { SCIMConstants.StandardSCIMMetaAttributes.Location, location },
                      { SCIMConstants.StandardSCIMMetaAttributes.ResourceType, StandardSchemas.ResourceTypeSchema.ResourceType }
                  } }
            });
        }
Exemplo n.º 7
0
 public SCIMSchemaBuilder(SCIMSchema scimSchema)
 {
     _id          = scimSchema.Id;
     _attributes  = scimSchema.Attributes;
     _name        = scimSchema.Name;
     _description = scimSchema.Description;
 }
Exemplo n.º 8
0
 private static void CheckRequiredAttributes(SCIMSchema mainSchema, ICollection <SCIMSchema> extensionSchemas, JObject json)
 {
     CheckRequiredAttributes(mainSchema, json);
     foreach (var extensionSchema in extensionSchemas)
     {
         CheckRequiredAttributes(extensionSchema, json);
     }
 }
Exemplo n.º 9
0
        private static void CheckRequiredAttributes(SCIMSchema schema, IEnumerable <SCIMSchemaAttribute> schemaAttributes, JObject json)
        {
            var missingRequiredAttributes = schemaAttributes.Where(a => a.Required && !json.HasNotEmptyElement(a.Name, schema.Id));

            if (missingRequiredAttributes.Any())
            {
                throw new SCIMSchemaViolatedException(string.Format(Global.RequiredAttributesAreMissing, string.Join(",", missingRequiredAttributes.Select(a => $"{schema.Id}:{a.Name}"))));
            }
        }
Exemplo n.º 10
0
        public static ResolutionResult Resolve(JObject json, SCIMSchema schema, ICollection <SCIMSchemaAttribute> schemaAttributes)
        {
            var rows = new List <ResolutionRowResult>();

            foreach (var kvp in json)
            {
                rows.Add(Resolve(kvp, schema, schemaAttributes));
            }

            return(new ResolutionResult(rows));
        }
Exemplo n.º 11
0
 public SCIMSchemaBuilder(string id, string name, string resourceType, string description, bool isRootSchema = true) : this(id, name, resourceType)
 {
     _description  = description;
     _isRootSchema = isRootSchema;
     _schema       = new SCIMSchema
     {
         Id               = _id,
         Name             = _name,
         ResourceType     = _resourceType,
         SchemaExtensions = _extensions,
         Description      = _description,
         Attributes       = _attributes,
         IsRootSchema     = _isRootSchema
     };
 }
Exemplo n.º 12
0
 public SCIMSchemaBuilder(string id, string name, string resourceType) : this(id)
 {
     _name         = name;
     _resourceType = resourceType;
     _schema       = new SCIMSchema
     {
         Id               = _id,
         Name             = _name,
         ResourceType     = _resourceType,
         SchemaExtensions = _extensions,
         Description      = _description,
         Attributes       = _attributes,
         IsRootSchema     = _isRootSchema
     };
 }
Exemplo n.º 13
0
        private static void Enrich(SCIMSchema schema, ICollection <SCIMSchemaAttribute> attributes, IDictionary <string, OpenApiSchema> properties)
        {
            foreach (var attr in attributes)
            {
                var sc = new OpenApiSchema
                {
                    Description = attr.Description,
                    Type        = attr.MultiValued ? "array" : MAPPING_ENUM_TO_NAMES[attr.Type],
                    Properties  = new Dictionary <string, OpenApiSchema>()
                };
                if (!attr.MultiValued && MAPPING_ENUM_TO_FORMAT.ContainsKey(attr.Type))
                {
                    sc.Format = MAPPING_ENUM_TO_FORMAT[attr.Type];
                }

                properties.Add(new KeyValuePair <string, OpenApiSchema>(attr.Name, sc));
                if (attr.MultiValued && attr.Type != SCIMSchemaAttributeTypes.COMPLEX)
                {
                    sc.Items = new OpenApiSchema
                    {
                        Type = MAPPING_ENUM_TO_NAMES[attr.Type]
                    };
                    if (MAPPING_ENUM_TO_FORMAT.ContainsKey(attr.Type))
                    {
                        sc.Format = MAPPING_ENUM_TO_FORMAT[attr.Type];
                    }

                    continue;
                }

                var values = schema.GetChildren(attr).ToList();
                if (attr.MultiValued && attr.Type == SCIMSchemaAttributeTypes.COMPLEX)
                {
                    sc.Items = new OpenApiSchema
                    {
                        Type       = "object",
                        Properties = new Dictionary <string, OpenApiSchema>()
                    };
                    Enrich(schema, values, sc.Items.Properties);
                    continue;
                }

                if (attr.Type == SCIMSchemaAttributeTypes.COMPLEX && !attr.MultiValued)
                {
                    Enrich(schema, values, sc.Properties);
                }
            }
        }
Exemplo n.º 14
0
 public SCIMSchemaBuilder(SCIMSchema scimSchema)
 {
     _id          = scimSchema.Id;
     _attributes  = scimSchema.Attributes;
     _name        = scimSchema.Name;
     _description = scimSchema.Description;
     _schema      = new SCIMSchema
     {
         Id               = _id,
         Name             = _name,
         ResourceType     = _resourceType,
         SchemaExtensions = _extensions,
         Description      = _description,
         Attributes       = _attributes,
         IsRootSchema     = _isRootSchema
     };
 }
Exemplo n.º 15
0
 public SCIMSchemaBuilder(string id)
 {
     _id           = id;
     _attributes   = new List <SCIMSchemaAttribute>();
     _extensions   = new List <SCIMSchemaExtension>();
     _isRootSchema = true;
     _schema       = new SCIMSchema
     {
         Id               = _id,
         Name             = _name,
         ResourceType     = _resourceType,
         SchemaExtensions = _extensions,
         Description      = _description,
         Attributes       = _attributes,
         IsRootSchema     = _isRootSchema
     };
 }
        public static JObject ToResponse(this SCIMSchema schema)
        {
            var jObj = new JObject
            {
                { SCIMConstants.StandardSCIMRepresentationAttributes.Id, schema.Id },
                { SCIMConstants.StandardSCIMRepresentationAttributes.Name, schema.Name },
                { SCIMConstants.StandardSCIMRepresentationAttributes.Description, schema.Description }
            };

            var attributes = new JArray();

            foreach (var attribute in schema.Attributes)
            {
                attributes.Add(SerializeSCIMSchemaAttribute(attribute));
            }

            jObj.Add(SCIMConstants.StandardSCIMRepresentationAttributes.Attributes, attributes);
            return(jObj);
        }
Exemplo n.º 17
0
        public static SCIMSchemaModel ToModel(this SCIMSchema schema)
        {
            var result = new SCIMSchemaModel
            {
                Description      = schema.Description,
                Id               = schema.Id,
                IsRootSchema     = schema.IsRootSchema,
                Name             = schema.Name,
                ResourceType     = schema.ResourceType,
                SchemaExtensions = schema.SchemaExtensions.Select(s => new SCIMSchemaExtensionModel
                {
                    Id       = s.Id,
                    Required = s.Required,
                    Schema   = s.Schema
                }).ToList(),
                Attributes = schema.Attributes.Select(s => ToModel(s, schema.Id)).ToList()
            };

            return(result);
        }
Exemplo n.º 18
0
        public static SCIMSchema ToDomain(this SCIMSchemaModel schema)
        {
            var result = new SCIMSchema
            {
                Id               = schema.Id,
                Description      = schema.Description,
                IsRootSchema     = schema.IsRootSchema,
                Name             = schema.Name,
                ResourceType     = schema.ResourceType,
                Attributes       = schema.Attributes.Where(a => string.IsNullOrEmpty(a.ParentId)).Select(a => ToDomain(a)).ToList(),
                SchemaExtensions = schema.SchemaExtensions.Select(s => new SCIMSchemaExtension
                {
                    Id       = s.Id,
                    Required = s.Required,
                    Schema   = s.Schema
                }).ToList()
            };

            return(result);
        }
 private static void CheckRequiredAttributes(SCIMSchema schema, JObject json)
 {
     CheckRequiredAttributes(schema, schema.Attributes, json);
 }
Exemplo n.º 20
0
        public static SCIMSchemaBuilder Load(SCIMSchema scimSchema)
        {
            var result = new SCIMSchemaBuilder((SCIMSchema)scimSchema.Clone());

            return(result);
        }
Exemplo n.º 21
0
        private IQueryable <SCIMRepresentation> ParseAndExecuteFilter(IQueryable <SCIMRepresentation> representations, string filter, SCIMSchema customSchema)
        {
            var parsed = SCIMFilterParser.Parse(filter, new List <SCIMSchema> {
                StandardSchemas.UserSchema, customSchema
            });
            var evaluatedExpression = parsed.Evaluate(representations);

            return((IQueryable <SCIMRepresentation>)evaluatedExpression.Compile().DynamicInvoke(representations));
        }
Exemplo n.º 22
0
 public ResolutionRowResult(SCIMSchema schema, SCIMSchemaAttribute schemaAttribute, JToken content) : this(schemaAttribute, content)
 {
     Schema = schema;
 }
Exemplo n.º 23
0
        public static ICollection <SCIMRepresentationAttribute> BuildAttributes(JArray jArr, SCIMSchemaAttribute schemaAttribute, SCIMSchema schema, bool ignoreUnsupportedCanonicalValues)
        {
            var result = new List <SCIMRepresentationAttribute>();

            if (schemaAttribute.Mutability == SCIMSchemaAttributeMutabilities.READONLY)
            {
                return(result);
            }

            var attributeId = Guid.NewGuid().ToString();

            if (schemaAttribute.Type == SCIMSchemaAttributeTypes.COMPLEX)
            {
                if (!jArr.Any())
                {
                    result.Add(new SCIMRepresentationAttribute(Guid.NewGuid().ToString(), attributeId, schemaAttribute, schema.Id));
                }
                else
                {
                    foreach (var jsonProperty in jArr)
                    {
                        var rec = jsonProperty as JObject;
                        if (rec == null)
                        {
                            throw new SCIMSchemaViolatedException(string.Format(Global.NotValidJSON, jsonProperty.ToString()));
                        }

                        var subAttributes = schema.GetChildren(schemaAttribute).ToList();
                        CheckRequiredAttributes(schema, subAttributes, rec);
                        var resolutionResult = Resolve(rec, schema, subAttributes);
                        var parent           = new SCIMRepresentationAttribute(Guid.NewGuid().ToString(), attributeId, schemaAttribute, schema.Id);
                        var children         = BuildRepresentationAttributes(resolutionResult, subAttributes, ignoreUnsupportedCanonicalValues);
                        foreach (var child in children)
                        {
                            if (SCIMRepresentation.GetParentPath(child.FullPath) == parent.FullPath)
                            {
                                child.ParentAttributeId = parent.Id;
                            }

                            result.Add(child);
                        }

                        result.Add(parent);
                    }
                }
            }
            else
            {
                switch (schemaAttribute.Type)
                {
                case SCIMSchemaAttributeTypes.BOOLEAN:
                    var valuesBooleanResult = Extract <bool>(jArr);
                    if (valuesBooleanResult.InvalidValues.Any())
                    {
                        throw new SCIMSchemaViolatedException(string.Format(Global.NotValidBoolean, string.Join(",", valuesBooleanResult.InvalidValues)));
                    }

                    foreach (var b in valuesBooleanResult.Values)
                    {
                        var record = new SCIMRepresentationAttribute(Guid.NewGuid().ToString(), attributeId, schemaAttribute, schema.Id, valueBoolean: b);
                        result.Add(record);
                    }
                    break;

                case SCIMSchemaAttributeTypes.INTEGER:
                    var valuesIntegerResult = Extract <int>(jArr);
                    if (valuesIntegerResult.InvalidValues.Any())
                    {
                        throw new SCIMSchemaViolatedException(string.Format(Global.NotValidInteger, string.Join(",", valuesIntegerResult.InvalidValues)));
                    }

                    foreach (var i in valuesIntegerResult.Values)
                    {
                        var record = new SCIMRepresentationAttribute(Guid.NewGuid().ToString(), attributeId, schemaAttribute, schema.Id, valueInteger: i);
                        result.Add(record);
                    }
                    break;

                case SCIMSchemaAttributeTypes.DATETIME:
                    var valuesDateTimeResult = Extract <DateTime>(jArr);
                    if (valuesDateTimeResult.InvalidValues.Any())
                    {
                        throw new SCIMSchemaViolatedException(string.Format(Global.NotValidDateTime, string.Join(",", valuesDateTimeResult.InvalidValues)));
                    }

                    foreach (var d in valuesDateTimeResult.Values)
                    {
                        var record = new SCIMRepresentationAttribute(Guid.NewGuid().ToString(), attributeId, schemaAttribute, schema.Id, valueDateTime: d);
                        result.Add(record);
                    }
                    break;

                case SCIMSchemaAttributeTypes.STRING:
                    var strs = jArr.Select(j => j.ToString()).ToList();
                    if (schemaAttribute.CanonicalValues != null &&
                        schemaAttribute.CanonicalValues.Any() &&
                        !ignoreUnsupportedCanonicalValues &&
                        !strs.All(_ => schemaAttribute.CaseExact ?
                                  schemaAttribute.CanonicalValues.Contains(_)
                                : schemaAttribute.CanonicalValues.Contains(_, StringComparer.OrdinalIgnoreCase))
                        )
                    {
                        throw new SCIMSchemaViolatedException(string.Format(Global.NotValidCanonicalValue, schemaAttribute.Name));
                    }

                    foreach (var s in strs)
                    {
                        var record = new SCIMRepresentationAttribute(Guid.NewGuid().ToString(), attributeId, schemaAttribute, schema.Id, valueString: s);
                        result.Add(record);
                    }
                    break;

                case SCIMSchemaAttributeTypes.REFERENCE:
                    var refs = jArr.Select(j => j.ToString()).ToList();
                    foreach (var reference in refs)
                    {
                        var record = new SCIMRepresentationAttribute(Guid.NewGuid().ToString(), attributeId, schemaAttribute, schema.Id, valueReference: reference);
                        result.Add(record);
                    }
                    break;

                case SCIMSchemaAttributeTypes.DECIMAL:
                    var valuesDecimalResult = Extract <decimal>(jArr);
                    if (valuesDecimalResult.InvalidValues.Any())
                    {
                        throw new SCIMSchemaViolatedException(string.Format(Global.NotValidDecimal, string.Join(",", valuesDecimalResult.InvalidValues)));
                    }

                    foreach (var d in valuesDecimalResult.Values)
                    {
                        var record = new SCIMRepresentationAttribute(Guid.NewGuid().ToString(), attributeId, schemaAttribute, schema.Id, valueDecimal: d);
                        result.Add(record);
                    }
                    break;

                case SCIMSchemaAttributeTypes.BINARY:
                    var invalidValues = new List <string>();
                    var valuesBinary  = new List <string>();
                    foreach (var rec in jArr)
                    {
                        try
                        {
                            Convert.FromBase64String(rec.ToString());
                            valuesBinary.Add(rec.ToString());
                        }
                        catch (FormatException)
                        {
                            invalidValues.Add(rec.ToString());
                        }
                    }

                    if (invalidValues.Any())
                    {
                        throw new SCIMSchemaViolatedException(string.Format(Global.NotValidBase64, string.Join(",", invalidValues)));
                    }

                    foreach (var b in valuesBinary)
                    {
                        var record = new SCIMRepresentationAttribute(Guid.NewGuid().ToString(), attributeId, schemaAttribute, schema.Id, valueBinary: b);
                        result.Add(record);
                    }
                    break;
                }
            }

            return(result);
        }
Exemplo n.º 24
0
        private static void CheckRequiredAttributes(SCIMSchema schema, JObject json)
        {
            var attributes = schema.HierarchicalAttributes.Select(h => h.Leaf);

            CheckRequiredAttributes(schema, attributes, json);
        }
Exemplo n.º 25
0
 public SCIMRepresentation ExtractSCIMRepresentationFromJSON(JObject json, string externalId, SCIMSchema mainSchema, ICollection <SCIMSchema> extensionSchemas)
 {
     CheckRequiredAttributes(mainSchema, extensionSchemas, json);
     return(BuildRepresentation(json, externalId, mainSchema, extensionSchemas, _options.IgnoreUnsupportedCanonicalValues));
 }
Exemplo n.º 26
0
        private static ResolutionRowResult Resolve(KeyValuePair <string, JToken> kvp, SCIMSchema schema, ICollection <SCIMSchemaAttribute> schemaAttributes)
        {
            var attrSchema = schemaAttributes.FirstOrDefault(a => a.Name == kvp.Key);

            if (attrSchema == null)
            {
                throw new SCIMSchemaViolatedException(string.Format(Global.AttributeIsNotRecognirzed, kvp.Key));
            }

            return(new ResolutionRowResult(schema, attrSchema, kvp.Value));
        }
        public static ICollection <SCIMRepresentationAttribute> BuildAttributes(JArray jArr, SCIMSchemaAttribute schemaAttribute, SCIMSchema schema, bool ignoreUnsupportedCanonicalValues)
        {
            var result = new List <SCIMRepresentationAttribute>();

            if (schemaAttribute.Type == SCIMSchemaAttributeTypes.COMPLEX)
            {
                if (!jArr.Any())
                {
                    result.Add(new SCIMRepresentationAttribute(Guid.NewGuid().ToString(), schemaAttribute));
                }
                else
                {
                    foreach (var jsonProperty in jArr)
                    {
                        var rec = jsonProperty as JObject;
                        if (rec == null)
                        {
                            throw new SCIMSchemaViolatedException(string.Format(Global.NotValidJSON, jsonProperty.ToString()));
                        }

                        CheckRequiredAttributes(schema, schemaAttribute.SubAttributes, rec);
                        var resolutionResult = Resolve(rec, schema, schemaAttribute.SubAttributes);
                        var record           = new SCIMRepresentationAttribute(Guid.NewGuid().ToString(), schemaAttribute)
                        {
                            Values = BuildRepresentationAttributes(resolutionResult, schemaAttribute.SubAttributes, ignoreUnsupportedCanonicalValues)
                        };

                        foreach (var subAttribute in record.Values)
                        {
                            subAttribute.Parent = record;
                        }

                        result.Add(record);
                    }
                }
            }
            else
            {
                var record = new SCIMRepresentationAttribute(Guid.NewGuid().ToString(), schemaAttribute);
                switch (schemaAttribute.Type)
                {
                case SCIMSchemaAttributeTypes.BOOLEAN:
                    var valuesBooleanResult = Extract <bool>(jArr);
                    if (valuesBooleanResult.InvalidValues.Any())
                    {
                        throw new SCIMSchemaViolatedException(string.Format(Global.NotValidBoolean, string.Join(",", valuesBooleanResult.InvalidValues)));
                    }

                    record.ValuesBoolean = valuesBooleanResult.Values;
                    break;

                case SCIMSchemaAttributeTypes.INTEGER:
                    var valuesIntegerResult = Extract <int>(jArr);
                    if (valuesIntegerResult.InvalidValues.Any())
                    {
                        throw new SCIMSchemaViolatedException(string.Format(Global.NotValidInteger, string.Join(",", valuesIntegerResult.InvalidValues)));
                    }

                    record.ValuesInteger = valuesIntegerResult.Values;
                    break;

                case SCIMSchemaAttributeTypes.DATETIME:
                    var valuesDateTimeResult = Extract <DateTime>(jArr);
                    if (valuesDateTimeResult.InvalidValues.Any())
                    {
                        throw new SCIMSchemaViolatedException(string.Format(Global.NotValidDateTime, string.Join(",", valuesDateTimeResult.InvalidValues)));
                    }

                    record.ValuesDateTime = valuesDateTimeResult.Values;
                    break;

                case SCIMSchemaAttributeTypes.STRING:
                    record.ValuesString = jArr.Select(j => j.ToString()).ToList();
                    if (schemaAttribute.CanonicalValues != null && schemaAttribute.CanonicalValues.Any() && !ignoreUnsupportedCanonicalValues && !record.ValuesString.All(_ => schemaAttribute.CanonicalValues.Contains(_)))
                    {
                        throw new SCIMSchemaViolatedException(string.Format(Global.NotValidCanonicalValue, schemaAttribute.Name));
                    }

                    break;

                case SCIMSchemaAttributeTypes.REFERENCE:
                    record.ValuesReference = jArr.Select(j => j.ToString()).ToList();
                    break;

                case SCIMSchemaAttributeTypes.DECIMAL:
                    var valuesDecimalResult = Extract <decimal>(jArr);
                    if (valuesDecimalResult.InvalidValues.Any())
                    {
                        throw new SCIMSchemaViolatedException(string.Format(Global.NotValidDecimal, string.Join(",", valuesDecimalResult.InvalidValues)));
                    }

                    record.ValuesDecimal = valuesDecimalResult.Values;
                    break;

                case SCIMSchemaAttributeTypes.BINARY:
                    var invalidValues = new List <string>();
                    var valuesBinary  = new List <byte[]>();
                    foreach (var rec in jArr)
                    {
                        try
                        {
                            valuesBinary.Add(Convert.FromBase64String(rec.ToString()));
                        }
                        catch (FormatException)
                        {
                            invalidValues.Add(rec.ToString());
                        }
                    }

                    if (invalidValues.Any())
                    {
                        throw new SCIMSchemaViolatedException(string.Format(Global.NotValidBase64, string.Join(",", invalidValues)));
                    }

                    record.ValuesBinary = valuesBinary;
                    break;
                }

                result.Add(record);
            }

            return(result);
        }
Exemplo n.º 28
0
        public static SCIMRepresentation BuildRepresentation(JObject json, string externalId, SCIMSchema mainSchema, ICollection <SCIMSchema> extensionSchemas, bool ignoreUnsupportedCanonicalValues)
        {
            var schemas = new List <SCIMSchema>
            {
                mainSchema
            };

            schemas.AddRange(extensionSchemas);
            var result = new SCIMRepresentation
            {
                ExternalId = externalId,
                Schemas    = schemas
            };

            result.Schemas = schemas;
            var resolutionResult = Resolve(json, mainSchema, extensionSchemas);

            result.FlatAttributes = BuildRepresentationAttributes(resolutionResult, resolutionResult.AllSchemaAttributes, ignoreUnsupportedCanonicalValues);
            var attr = result.FlatAttributes.FirstOrDefault(a => a.SchemaAttribute.Name == "displayName");

            if (attr != null)
            {
                result.DisplayName = attr.ValueString;
            }

            return(result);
        }