private static Dictionary <string, List <BaseAttribute> > ParseProfileContent(AsymmetricCipherKeyPair keyPair, string wrappedReceiptKey, string profileContent)
        {
            var parsedAttributes = new Dictionary <string, List <BaseAttribute> >();

            if (!string.IsNullOrEmpty(profileContent))
            {
                ProtoBuf.Attribute.AttributeList profileAttributeList = CryptoEngine.DecryptAttributeList(
                    wrappedReceiptKey,
                    profileContent,
                    keyPair);

                parsedAttributes = AttributeConverter.ConvertToBaseAttributes(profileAttributeList);
            }

            return(parsedAttributes);
        }
示例#2
0
        internal void AddAttributesToProfile(AttrpubapiV1.AttributeList attributes)
        {
            foreach (AttrpubapiV1.Attribute attribute in attributes.Attributes)
            {
                YotiAttribute <object> yotiAttribute = AttributeConverter.ConvertAttribute(attribute);
                byte[] byteValue = attribute.Value.ToByteArray();

                if (yotiAttribute == null)
                {
                    HandleOtherAttributes(_yotiProfile, attribute, byteValue);
                    return;
                }

                string stringValue = Conversion.BytesToUtf8(byteValue);

                LegacyAddAttribute(attribute, byteValue);

                PropertyInfo propertyInfo = GetProfilePropertyByProtobufName(yotiAttribute.GetName());

                if (propertyInfo == null)
                {
                    HandleOtherAttributes(_yotiProfile, attribute, byteValue);
                    return;
                }

                List <Yoti.Auth.Anchors.Anchor> anchors = yotiAttribute.GetAnchors();

                switch (attribute.ContentType)
                {
                case AttrpubapiV1.ContentType.Json:
                    if (attribute.Name == YotiConstants.AttributeStructuredPostalAddress)
                    {
                        var structuredPostalAddressAttributeValue = new YotiAttributeValue(TypeEnum.Json, byteValue);
                        var structuredPostalAddressAttribute      = new YotiAttribute <IEnumerable <Dictionary <string, JToken> > >(
                            YotiConstants.AttributeStructuredPostalAddress,
                            structuredPostalAddressAttributeValue,
                            anchors);

                        _yotiProfile.StructuredPostalAddress = structuredPostalAddressAttribute;
                        break;
                    }
                    else
                    {
                        HandleOtherAttributes(_yotiProfile, attribute, byteValue);
                    }
                    break;

                case AttrpubapiV1.ContentType.String:
                    if (yotiAttribute.GetName().StartsWith(YotiConstants.AttributeAgeOver) ||
                        yotiAttribute.GetName().StartsWith(YotiConstants.AttributeAgeUnder))
                    {
                        bool parsed = Boolean.TryParse(stringValue, out bool AgeVerified);

                        if (!parsed)
                        {
                            throw new FormatException(
                                      String.Format(
                                          "'{0}' byte value was unable to be parsed into a bool",
                                          byteValue));
                        }

                        var AgeVerifiedAttributeValue = new YotiAttributeValue(TypeEnum.Bool, byteValue);
                        _yotiProfile.AgeVerified = new YotiAttribute <bool?>(
                            propertyInfo.Name,
                            AgeVerifiedAttributeValue,
                            anchors);

                        break;
                    }

                    SetStringAttribute(propertyInfo, stringValue, anchors);
                    break;

                case AttrpubapiV1.ContentType.Jpeg:
                    var jpegYotiAttributeValue = new YotiAttributeValue(TypeEnum.Jpeg, byteValue);
                    _yotiProfile.Selfie = new YotiImageAttribute <Image>(
                        propertyInfo.Name,
                        jpegYotiAttributeValue,
                        anchors);
                    break;

                case AttrpubapiV1.ContentType.Png:
                    var pngYotiAttributeValue = new YotiAttributeValue(TypeEnum.Png, byteValue);
                    _yotiProfile.Selfie = new YotiImageAttribute <Image>(
                        propertyInfo.Name,
                        pngYotiAttributeValue,
                        anchors);
                    break;

                case AttrpubapiV1.ContentType.Date:

                    DateTime date;
                    if (DateTime.TryParseExact(stringValue, "yyyy-MM-dd", CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out date))
                    {
                        SetDateAttribute(propertyInfo, byteValue, anchors);
                    }
                    break;

                default:
                    HandleOtherAttributes(_yotiProfile, attribute, byteValue);
                    break;
                }
            }

            SetAddressToBeFormattedAddressIfNull();
        }