Beispiel #1
0
        /// <summary>
        /// Deserialize the specified serializedNativeCredential.
        /// </summary>
        /// <param name="serializedNativeCredential">Serialized native credential.</param>
        /// <returns> Native credential </returns>
        static public NativeCredential Deserialize(string serializedNativeCredential)
        {
            var nativeCredential = new NativeCredential();

            foreach (var p in serializedNativeCredential.Split(Constants.CHAR_AMPERSAND))
            {
                var keyval = p.Split(Constants.CHAR_EQUAL);

                var key = Uri.UnescapeDataString(keyval[0]);
                var val = keyval.Length > 1 ? Uri.UnescapeDataString(keyval[1]) : String.Empty;

                if (key == NativeCredential.USERID)
                {
                    nativeCredential.UserID = val;
                }
                else
                {
                    if (val.Equals(String.Empty))
                    {
                        val = null;
                    }

                    nativeCredential.Properties[key] = val;
                }
            }

            return(nativeCredential);
        }
Beispiel #2
0
        /// <summary>
        /// Load the credential associted with the specified user ID.
        /// </summary>
        /// <param name="userID">User identifier used to access appropriate credential.</param>
        /// <param name="ssoGroupKey">SSO Group Key.</param>
        override public Credential Load(string userID, string ssoGroupKey)
        {
            Credential credential = null;

            try
            {
                NativeCredential nc = null;

                var credentials = FindCredentialsForOrg(ssoGroupKey);

                foreach (var c in credentials)
                {
                    if (userID.Equals(string.Empty) ||
                        userID.Equals(c.UserID))
                    {
                        nc = c;
                        break;
                    }
                }

                if (nc != null)
                {
                    credential = Credential.From(nc);
                }
            }
            catch (System.Exception e)
            {
                string msg = e.Message;
            }

            return(credential);
        }
Beispiel #3
0
        /// <summary>
        /// Store the specified userID and credential.
        /// </summary>
        /// <param name="userID">User identifier.</param>
        /// <param name="ssoGroupKey">SSO Group Key.</param>
        /// <param name="credential">Credential.</param>
        override public void Store(string userID, string ssoGroupKey, Credential credential)
        {
            Dictionary <string, string> properties = new Dictionary <string, string>();

            properties.Add(Constants.STR_ACCESS_TOKEN, (credential.AccessToken ?? string.Empty));
            properties.Add(Constants.STR_AUTH_TOKEN, (credential.AuthToken ?? string.Empty));
            properties.Add(Constants.STR_REFRESH_TOKEN, (credential.RefreshToken ?? string.Empty));
            properties.Add(Constants.STR_REDIRECT_URI, (credential.RedirectUri ?? string.Empty));
            properties.Add(Constants.STR_USERNAME, (credential.UserName ?? string.Empty));

            properties.Add(Constants.STR_ATTRIBUTES, (credential.Attributes != null ?
                                                      JsonConvert.SerializeObject(credential.Attributes) :
                                                      string.Empty));

            properties.Add(Constants.STR_USER_KMD, (credential.UserKMD != null ?
                                                    JsonConvert.SerializeObject(credential.UserKMD) :
                                                    string.Empty));


            NativeCredential nc = new NativeCredential(userID, properties);

            try
            {
                SaveNativeCredential(nc, ssoGroupKey);
            }
            catch (System.Exception e)
            {
                throw new KinveyException(EnumErrorCategory.ERROR_USER, EnumErrorCode.ERROR_USER_STORE_CREDENTIAL, "", e);
            }
        }
Beispiel #4
0
 public static Credential From(NativeCredential nc)
 {
     return(new Credential(nc.UserID,
                           nc.Properties[Constants.STR_ACCESS_TOKEN],
                           null,             // TODO add _socialIdentity object here
                           nc.Properties[Constants.STR_AUTH_TOKEN],
                           nc.Properties[Constants.STR_USERNAME],
                           JsonConvert.DeserializeObject <Dictionary <string, JToken> >(nc.Properties[Constants.STR_ATTRIBUTES]),
                           JsonConvert.DeserializeObject <KinveyUserMetaData>(nc.Properties[Constants.STR_USER_KMD]),
                           nc.Properties[Constants.STR_REFRESH_TOKEN],
                           nc.Properties[Constants.STR_REDIRECT_URI],
                           null,
                           null));
 }
Beispiel #5
0
        private NativeCredential GetCredentialFromAccount(Account account)
        {
            NativeCredential nc = null;

            try
            {
                var serializedNativeCredential = accountManager.GetUserData(account, Constants.STR_CREDENTIAL);
                nc = NativeCredential.Deserialize(serializedNativeCredential);
            }
            catch (Exception e)
            {
                throw new KinveyException(EnumErrorCategory.ERROR_USER, EnumErrorCode.ERROR_USER_GET_CREDENTIAL_FOR_ACCOUNT, "", e);
            }

            return(nc);
        }
Beispiel #6
0
        private NativeCredential FindCredential(string username, string ssoGroupKey)
        {
            NativeCredential nc = null;

            Account[] accounts = accountManager.GetAccountsByType(ssoGroupKey);

            foreach (var account in accounts)
            {
                if (account.Type.Equals(ssoGroupKey))
                {
                    nc = GetCredentialFromAccount(account);
                }
            }

            return(nc);
        }
Beispiel #7
0
        private void SaveNativeCredential(NativeCredential nativeCredential, string ssoGroupKey)
        {
            var serializedCredential = nativeCredential.Serialize();

            // If there exists a credential, delete before writing new credential
            var existingCredential = FindCredential(nativeCredential.UserID, ssoGroupKey);

            if (existingCredential != null)
            {
                accountManager.RemoveAccountExplicitly(new Account(existingCredential.UserID, ssoGroupKey));
            }

            // Add new credential
            Account account = new Account(nativeCredential.UserID, ssoGroupKey);

            Android.OS.Bundle bundle = new Android.OS.Bundle();
            bundle.PutString(Constants.STR_CREDENTIAL, serializedCredential);
            accountManager.AddAccountExplicitly(account, "", bundle);
        }
Beispiel #8
0
        private NativeCredential FindCredential(string username, string ssoGroupKey)
        {
            NativeCredential nc = null;

            var query = new SecRecord(SecKind.GenericPassword);

            query.Service = ssoGroupKey;
            query.Account = username;

            SecStatusCode result;
            var           record = SecKeyChain.QueryAsRecord(query, out result);

            if (record != null)
            {
                nc = GetCredentialFromRecord(record);
            }

            return(nc);
        }
Beispiel #9
0
        /// <summary>
        /// Load the specified userID.
        /// </summary>
        /// <param name="userID">User identifier.</param>
        /// <param name="ssoGroupKey">SSO Group Key.</param>
        override public Credential Load(string userID, string ssoGroupKey)
        {
            Credential credential = null;

            try
            {
                NativeCredential nc = null;

                var credentials = FindCredentialsForOrg(ssoGroupKey);

                foreach (var c in credentials)
                {
                    if (userID.Equals(string.Empty) ||
                        userID.Equals(c.UserID))
                    {
                        nc = c;
                        break;
                    }
                }

                if (nc != null)
                {
                    credential = Credential.From(nc);
                }
            }
            catch (Exception e)
            {
                throw new KinveyException(
                          EnumErrorCategory.ERROR_USER,
                          EnumErrorCode.ERROR_USER_LOAD_CREDENTIAL,
                          "",
                          e
                          );
            }

            return(credential);
        }
Beispiel #10
0
        private void SaveNativeCredential(NativeCredential nativeCredential, string ssoGroupKey)
        {
            var statusCode           = SecStatusCode.Success;
            var serializedCredential = nativeCredential.Serialize();
            var data = NSData.FromString(serializedCredential, NSStringEncoding.UTF8);

            // If there exists a credential, delete before writing new credential
            var existingCredential = FindCredential(nativeCredential.UserID, ssoGroupKey);

            if (existingCredential != null)
            {
                var query = new SecRecord(SecKind.GenericPassword);
                query.Service = ssoGroupKey;
                query.Account = nativeCredential.UserID;

                statusCode = SecKeyChain.Remove(query);
                if (statusCode != SecStatusCode.Success)
                {
                    throw new KinveyException(EnumErrorCategory.ERROR_USER, EnumErrorCode.ERROR_MIC_CREDENTIAL_SAVE, statusCode.ToString());
                }
            }

            // Add new credential
            var record = new SecRecord(SecKind.GenericPassword);

            record.Service    = ssoGroupKey;
            record.Account    = nativeCredential.UserID;
            record.Generic    = data;
            record.Accessible = SecAccessible.WhenUnlocked;

            statusCode = SecKeyChain.Add(record);

            if (statusCode != SecStatusCode.Success)
            {
                throw new KinveyException(EnumErrorCategory.ERROR_USER, EnumErrorCode.ERROR_MIC_CREDENTIAL_SAVE, statusCode.ToString());
            }
        }
Beispiel #11
0
        private NativeCredential GetCredentialFromRecord(SecRecord record)
        {
            var serializedNativeCredential = NSString.FromData(record.Generic, NSStringEncoding.UTF8);

            return(NativeCredential.Deserialize(serializedNativeCredential));
        }