示例#1
0
 /// <summary>
 /// Create a <see cref="YotiClient"/> with a specified <see cref="HttpClient"/>
 /// </summary>
 /// <param name="httpClient">Allows the specification of a HttpClient</param>
 /// <param name="sdkId">The client SDK ID provided on the Yoti Hub.</param>
 /// <param name="privateKeyStream">
 /// The private key file provided on the Yoti Hub as a <see cref="StreamReader"/>.
 /// </param>
 public YotiClient(HttpClient httpClient, string sdkId, StreamReader privateKeyStream)
     : this(httpClient, sdkId, CryptoEngine.LoadRsaKey(privateKeyStream))
 {
 }
示例#2
0
        private byte[] UnwrapKey(string wrappedKey, AsymmetricCipherKeyPair keyPair)
        {
            byte[] cipherBytes = Conversion.Base64ToBytes(wrappedKey);

            return(CryptoEngine.DecryptRsa(cipherBytes, keyPair));
        }
示例#3
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
                });
            }
        }
示例#4
0
        private string GetAuthKey(AsymmetricCipherKeyPair keyPair)
        {
            byte[] publicKey = CryptoEngine.GetDerEncodedPublicKey(keyPair);

            return(Conversion.BytesToBase64(publicKey));
        }