/// <summary> /// Retrieve the keyring for an instance of an object /// </summary> /// <typeparam name="T">Type of encrypted object</typeparam> /// <param name="objectInstance">Instance of an encrypted object</param> /// <returns>Local (instance) keyring</returns> public static Keyring GetLocalKeyring <T>(this T objectInstance) where T : class { var trackedInstance = EncryptedInstanceFactory.GetTrackedInstance(objectInstance); if (trackedInstance == null) { throw new Exception("Object instance is not an encrypted instance"); } return(trackedInstance.InstanceKeyring); }
/// <summary> /// Return a copy of the given object with proxy bindings (this could either be a relink or a creation operation) /// </summary> /// <param name="inputObject">Object being linked or wrapped in an encryption proxy</param> /// <param name="keyring">Local keyring to use in resulting linked object</param> /// <param name="configuration">Local configuration to use in resulting linked object</param> /// <returns>Object of same type as input, with proxy bindings</returns> public static object AsEncrypted(this object inputObject, Keyring keyring = null, EncryptionProxyConfiguration configuration = null) { if (keyring == null) { keyring = new Keyring(); } if (!AttemptRelink(inputObject, keyring, configuration)) { var trackedInstance = EncryptedInstanceFactory.GenerateTrackedInstance(inputObject.GetType(), configuration); trackedInstance.GetLocalKeyring().Import(keyring); CopyObjectProperties(inputObject, trackedInstance); return(trackedInstance); } else { return(inputObject); } }
private static bool AttemptRelink(object inputObject, Keyring keyring, EncryptionProxyConfiguration configuration) { // Is the object already linked? if (HasValidEncryptionExtensions(inputObject)) { EncryptedInstanceFactory.AttachInterceptor(inputObject, configuration); inputObject.GetLocalKeyring().Import(keyring); return(true); } // Does this object already have the bits we can attach to? if (HasUnlinkedEncryptionExtensions(inputObject)) { EncryptedInstanceFactory.AttachToExistingObject(inputObject, configuration); inputObject.GetLocalKeyring().Import(keyring); return(true); } return(false); }
/// <summary> /// Retrieve the keyring for an object's Type /// </summary> /// <typeparam name="T">Type of encrypted object</typeparam> /// <param name="objectInstance">Instance of an encrypted object</param> /// <returns>Type keyring</returns> public static Keyring GetTypeKeyring <T>(this T objectInstance) where T : class { var trackedType = EncryptedInstanceFactory.GetTrackedType(typeof(T)); return(trackedType.Keyring); }
/// <summary> /// Get the regular (non-encrypted) type for the given encrypted type /// </summary> /// <param name="encryptedType">Encrypted object type ("PatientProxy")</param> /// <returns>Regular object type ("Patient")</returns> public static Type GetNonEncryptedType(this Type encryptedType) { var trackedType = EncryptedInstanceFactory.GetTrackedTypeByEncrypted(encryptedType); return(trackedType == null ? null : trackedType.OriginalType); }
/// <summary> /// Get the proxy (encrypted) type for the given regular type /// </summary> /// <param name="nonEncryptedType">"Regular" object type ("Patient")</param> /// <returns>Encrypted object type ("PatientProxy")</returns> public static Type GetEncryptedType(this Type nonEncryptedType) { var trackedType = EncryptedInstanceFactory.GetTrackedTypeOrNull(nonEncryptedType); return(trackedType == null ? null : trackedType.ProxyType); }