public Dictionary <string, JToken> ToJson()
        {
            string utf8json = Conversion.BytesToUtf8(_data);
            Dictionary <string, JToken> deserializedJson = Newtonsoft.Json.JsonConvert.DeserializeObject <Dictionary <string, JToken> >(utf8json);

            return(deserializedJson);
        }
Exemplo n.º 2
0
        public static string DecryptToken(string encryptedConnectToken, AsymmetricCipherKeyPair keyPair)
        {
            // token was encoded as a urlsafe base64 so it can be transfered in a url
            byte[] cipherBytes = Conversion.UrlSafeBase64ToBytes(encryptedConnectToken);

            byte[] decipheredBytes = DecryptRsa(cipherBytes, keyPair);

            return(Conversion.BytesToUtf8(decipheredBytes));
        }
Exemplo n.º 3
0
        internal static string DecryptToken(string encryptedConnectToken, AsymmetricCipherKeyPair keyPair)
        {
            Validation.NotNullOrEmpty(encryptedConnectToken, "one time use token");

            // token was encoded as a URL-safe base64 so it can be transferred in a URL
            byte[] cipherBytes = Conversion.UrlSafeBase64ToBytes(encryptedConnectToken);

            byte[] decipheredBytes = DecryptRsa(cipherBytes, keyPair);

            return(Conversion.BytesToUtf8(decipheredBytes));
        }
Exemplo n.º 4
0
        public override string ToString()
        {
            switch (_type)
            {
            case TypeEnum.Jpeg:
            case TypeEnum.Png:
                return(Conversion.BytesToBase64(_data));

            default:
                return(Conversion.BytesToUtf8(_data));
            }
        }
        public static T ConvertType <T>(this byte[] bytes)
        {
            var type = Nullable.GetUnderlyingType(typeof(T)) ?? typeof(T);

            switch (Type.GetTypeCode(type))
            {
            case TypeCode.String:
                return((T)(object)Conversion.BytesToUtf8(bytes));

            case TypeCode.DateTime:
                DateTime date;
                if (DateTime.TryParseExact(
                        s: Conversion.BytesToUtf8(bytes),
                        format: "yyyy-MM-dd",
                        provider: CultureInfo.InvariantCulture,
                        style: DateTimeStyles.None,
                        result: out date))
                {
                    return((T)(object)date);
                }
                else
                {
                    throw new InvalidCastException("Unable to cast to DateTime");
                }

            case TypeCode.Boolean:
                bool parsed = Boolean.TryParse(Conversion.BytesToUtf8(bytes), out bool output);

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

                return((T)(object)output);

            case TypeCode.Object:
                return((T)(object)bytes);

            default:
                return((T)Convert.ChangeType(bytes, typeof(T)));
            }
        }
Exemplo n.º 6
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();
        }
Exemplo n.º 7
0
        private void LegacyAddAttribute(AttrpubapiV1.Attribute attribute, byte[] byteValue)
        {
            switch (attribute.Name)
            {
            case "selfie":

                switch (attribute.ContentType)
                {
                case AttrpubapiV1.ContentType.Jpeg:
                    _yotiUserProfile.Selfie = new Image
                    {
                        Type      = TypeEnum.Jpeg,
                        Data      = byteValue,
                        Base64URI = "data:image/jpeg;base64," + Convert.ToBase64String(byteValue)
                    };
                    break;

                case AttrpubapiV1.ContentType.Png:
                    _yotiUserProfile.Selfie = new Image
                    {
                        Type      = TypeEnum.Png,
                        Data      = byteValue,
                        Base64URI = "data:image/png;base64," + Convert.ToBase64String(byteValue)
                    };
                    break;
                }
                break;

            case "given_names":
                _yotiUserProfile.GivenNames = Conversion.BytesToUtf8(byteValue);
                break;

            case "family_name":
                _yotiUserProfile.FamilyName = Conversion.BytesToUtf8(byteValue);
                break;

            case "full_name":
                _yotiUserProfile.FullName = Conversion.BytesToUtf8(byteValue);
                break;

            case "phone_number":
                _yotiUserProfile.MobileNumber = Conversion.BytesToUtf8(byteValue);
                break;

            case "email_address":
                _yotiUserProfile.EmailAddress = Conversion.BytesToUtf8(byteValue);
                break;

            case "date_of_birth":
            {
                if (DateTime.TryParseExact(Conversion.BytesToUtf8(byteValue), "yyyy-MM-dd", CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out DateTime date))
                {
                    _yotiUserProfile.DateOfBirth = date;
                }
            }
            break;

            case "postal_address":
                _yotiUserProfile.Address = Conversion.BytesToUtf8(byteValue);
                break;

            case "structured_postal_address":
                string utf8json = Conversion.BytesToUtf8(byteValue);
                Dictionary <string, JToken> deserializedPostalAddress = Newtonsoft.Json.JsonConvert.DeserializeObject <Dictionary <string, JToken> >(utf8json);
                _yotiUserProfile.StructuredPostalAddress = deserializedPostalAddress;
                break;

            case "gender":
                _yotiUserProfile.Gender = Conversion.BytesToUtf8(byteValue);
                break;

            case "nationality":
                _yotiUserProfile.Nationality = Conversion.BytesToUtf8(byteValue);
                break;

            default:
                if (attribute.Name.StartsWith(YotiConstants.AttributeAgeOver) ||
                    attribute.Name.StartsWith(YotiConstants.AttributeAgeUnder))
                {
                    bool parsed = Boolean.TryParse(Conversion.BytesToUtf8(byteValue), out bool IsAgeVerified);

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

                    _yotiUserProfile.IsAgeVerified = IsAgeVerified;
                    break;
                }
                else
                {
                    HandleOtherAttributes(_yotiUserProfile, attribute, byteValue);
                    break;
                }
            }
        }
 public override string ToString()
 {
     return(Conversion.BytesToUtf8(_data));
 }
Exemplo n.º 9
0
        /// <summary>
        /// Asynchronously request a <see cref="ActivityDetails"/>  using the encrypted token provided by yoti during the login process.
        /// </summary>
        /// <param name="encryptedToken">The encrypted returned by Yoti after successfully authenticating.</param>
        /// <returns>The account details of the logged in user as a <see cref="ActivityDetails"/>. </returns>
        public async Task <ActivityDetails> GetActivityDetailsAsync(string encryptedConnectToken, string sdkId, AsymmetricCipherKeyPair keyPair)
        {
            // query parameters
            var token     = DecryptToken(encryptedConnectToken, keyPair);
            var nonce     = CryptoEngine.GenerateNonce();
            var timestamp = GetTimestamp();

            // create http endpoint
            var endpoint = GetEndpoint(token, nonce, timestamp, sdkId);

            // create request headers
            var authKey    = GetAuthKey(keyPair);
            var authDigest = GetAuthDigest(endpoint, keyPair);

            Dictionary <string, string> headers = new Dictionary <string, string>();

            headers.Add("X-Yoti-Auth-Key", authKey);
            headers.Add("X-Yoti-Auth-Digest", authDigest);

            var response = await _httpRequester.DoRequest(new Uri(_apiUrl + endpoint), headers);

            if (response.Success)
            {
                var parsedResponse = JsonConvert.DeserializeObject <ProfileDO>(response.Content);

                if (parsedResponse.receipt == null)
                {
                    return(new ActivityDetails
                    {
                        Outcome = ActivityOutcome.Failure
                    });
                }
                else if (parsedResponse.receipt.sharing_outcome != "SUCCESS")
                {
                    return(new ActivityDetails
                    {
                        Outcome = ActivityOutcome.SharingFailure
                    });
                }
                else
                {
                    var receipt = parsedResponse.receipt;

                    var attributes = DecryptCurrentUserReceipt(parsedResponse.receipt, keyPair);

                    var profile = new YotiUserProfile
                    {
                        Id = parsedResponse.receipt.remember_me_id
                    };

                    foreach (var attribute in attributes.Attributes)
                    {
                        var data = attribute.Value.ToByteArray();
                        switch (attribute.Name)
                        {
                        case "selfie":

                            switch (attribute.ContentType)
                            {
                            case ContentType.Jpeg:
                                profile.Selfie = new Image
                                {
                                    Type = ImageType.Jpeg,
                                    Data = data
                                };
                                break;

                            case ContentType.Png:
                                profile.Selfie = new Image
                                {
                                    Type = ImageType.Png,
                                    Data = data
                                };
                                break;
                            }
                            break;

                        case "given_names":
                            profile.GivenNames = Conversion.BytesToUtf8(data);
                            break;

                        case "family_name":
                            profile.FamilyName = Conversion.BytesToUtf8(data);
                            break;

                        case "phone_number":
                            profile.MobileNumber = Conversion.BytesToUtf8(data);
                            break;

                        case "email_address":
                            profile.EmailAddress = Conversion.BytesToUtf8(data);
                            break;

                        case "date_of_birth":
                        {
                            DateTime date;
                            if (DateTime.TryParseExact(Conversion.BytesToUtf8(data), "yyyy-MM-dd", CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out date))
                            {
                                profile.DateOfBirth = date;
                            }
                        }
                        break;

                        case "postal_address":
                            profile.Address = Conversion.BytesToUtf8(data);
                            break;

                        case "gender":
                            profile.Gender = Conversion.BytesToUtf8(data);
                            break;

                        case "nationality":
                            profile.Nationality = Conversion.BytesToUtf8(data);
                            break;

                        default:
                            switch (attribute.ContentType)
                            {
                            case ContentType.Date:
                                profile.OtherAttributes.Add(attribute.Name, new YotiAttributeValue(YotiAttributeValue.TypeEnum.Date, data));
                                break;

                            case ContentType.String:
                                profile.OtherAttributes.Add(attribute.Name, new YotiAttributeValue(YotiAttributeValue.TypeEnum.Text, data));
                                break;

                            case ContentType.Jpeg:
                                profile.OtherAttributes.Add(attribute.Name, new YotiAttributeValue(YotiAttributeValue.TypeEnum.Jpeg, data));
                                break;

                            case ContentType.Png:
                                profile.OtherAttributes.Add(attribute.Name, new YotiAttributeValue(YotiAttributeValue.TypeEnum.Png, data));
                                break;

                            case ContentType.Undefined:
                                // do not return attributes with undefined content types
                                break;

                            default:
                                break;
                            }
                            break;
                        }
                    }


                    return(new ActivityDetails
                    {
                        Outcome = ActivityOutcome.Success,
                        UserProfile = profile
                    });
                }
            }
            else
            {
                ActivityOutcome outcome = ActivityOutcome.Failure;
                switch (response.StatusCode)
                {
                case (int)HttpStatusCode.NotFound:
                {
                    outcome = ActivityOutcome.ProfileNotFound;
                }
                break;
                }

                return(new ActivityDetails
                {
                    Outcome = outcome
                });
            }
        }