/// <summary> /// Gets a <see cref="Credential"/> from the storage used by the authentication object. /// <para/> /// Returns a `<see cref="Credential"/>` if successful; otherwise `<see langword="null"/>`. /// </summary> /// <param name="targetUri">The uniform resource indicator used to uniquely identify the credentials.</param> public override async Task <Credential> GetCredentials(TargetUri targetUri) { if (targetUri is null) { throw new ArgumentNullException(nameof(targetUri)); } Credential credentials = null; try { if ((credentials = await PersonalAccessTokenStore.ReadCredentials(targetUri)) != null) { Trace.WriteLine($"credentials for '{targetUri}' found."); } } catch (Exception exception) { System.Diagnostics.Debug.WriteLine(exception); Trace.WriteLine($"failed to read credentials from the secure store: {exception.GetType().Name}."); } return(credentials); }
/// <summary> /// Deletes a `<see cref="Credential"/>` from the storage used by the authentication object. /// </summary> /// <param name="targetUri">The uniform resource indicator used to uniquely identify the credentials.</param> public override void DeleteCredentials(TargetUri targetUri) { BaseSecureStore.ValidateTargetUri(targetUri); Credential credentials = PersonalAccessTokenStore.ReadCredentials(targetUri); // This ought to be async, but the base type's method isn't. Task.Run(async() => { // Attempt to validate the credentials, if they're truly invalid delete them. if (!await ValidateCredentials(targetUri, credentials)) { PersonalAccessTokenStore.DeleteCredentials(targetUri); // Remove any related entries from the tenant cache because tenant change // could the be source of the invalidation, and not purging the cache will // trap the user in a limbo state of invalid credentials. // Deserialize the cache and remove any matching entry. string tenantUrl = targetUri.ToString(); var cache = await DeserializeTenantCache(); // Attempt to remove the URL entry, if successful serialize the cache. if (cache.Remove(tenantUrl)) { await SerializeTenantCache(cache); } } }).Wait(); }
/// <summary> /// Gets a <see cref="Credential"/> from the storage used by the authentication object. /// <para/> /// Returns a `<see cref="Credential"/>` if successful; otherwise `<see langword="null"/>`. /// </summary> /// <param name="targetUri">The uniform resource indicator used to uniquely identify the credentials.</param> public override Credential GetCredentials(TargetUri targetUri) { BaseSecureStore.ValidateTargetUri(targetUri); Credential credentials = null; if ((credentials = PersonalAccessTokenStore.ReadCredentials(targetUri)) != null) { Trace.WriteLine("successfully retrieved stored credentials, updating credential cache"); return(credentials); } // try for a refresh token var refreshCredentials = PersonalAccessTokenStore.ReadCredentials(GetRefreshTokenTargetUri(targetUri)); if (refreshCredentials == null) { // no refresh token return null return(credentials); } Credential refreshedCredentials = Task.Run(() => RefreshCredentials(targetUri, refreshCredentials.Password, null)).Result; if (refreshedCredentials == null) { // refresh failed return null return(credentials); } else { credentials = refreshedCredentials; } return(credentials); }
/// <summary> /// Deletes a `<see cref="Credential"/>` from the storage used by the authentication object. /// </summary> /// <param name="targetUri">The uniform resource indicator used to uniquely identify the credentials.</param> public override async Task <bool> DeleteCredentials(TargetUri targetUri) { if (targetUri is null) { throw new ArgumentNullException(nameof(targetUri)); } Credential credentials = await PersonalAccessTokenStore.ReadCredentials(targetUri); // Attempt to validate the credentials, if they're truly invalid delete them. if (!await ValidateCredentials(targetUri, credentials)) { await PersonalAccessTokenStore.DeleteCredentials(targetUri); // Remove any related entries from the tenant cache because tenant change // could the be source of the invalidation, and not purging the cache will // trap the user in a limbo state of invalid credentials. // Deserialize the cache and remove any matching entry. string tenantUrl = targetUri.ToString(); var cache = await DeserializeTenantCache(Context); // Attempt to remove the URL entry, if successful serialize the cache. if (cache.Remove(tenantUrl)) { await SerializeTenantCache(Context, cache); } return(true); } return(false); }
/// <inheritdoc/> public override void DeleteCredentials(TargetUri targetUri, string username) { BaseSecureStore.ValidateTargetUri(targetUri); Trace.WriteLine("BitbucketAuthentication::DeleteCredentials"); Credential credentials = null; if ((credentials = PersonalAccessTokenStore.ReadCredentials(targetUri)) != null) { // try to delete the credentials for the explicit target uri first PersonalAccessTokenStore.DeleteCredentials(targetUri); Trace.WriteLine($"host credentials deleted for {targetUri.ActualUri}"); } // tidy up and refresh tokens var refreshTargetUri = GetRefreshTokenTargetUri(targetUri); if ((credentials = PersonalAccessTokenStore.ReadCredentials(refreshTargetUri)) != null) { // try to delete the credentials for the explicit target uri first PersonalAccessTokenStore.DeleteCredentials(refreshTargetUri); Trace.WriteLine($"host refresh credentials deleted for {refreshTargetUri.ActualUri}"); } }
/// <summary> /// Deletes a set of stored credentials by their target resource. /// </summary> /// <param name="targetUri">The 'key' by which to identify credentials.</param> public override void DeleteCredentials(TargetUri targetUri) { BaseSecureStore.ValidateTargetUri(targetUri); if (PersonalAccessTokenStore.ReadCredentials(targetUri) != null) { PersonalAccessTokenStore.DeleteCredentials(targetUri); } }
/// <summary> /// Deletes a <see cref="Credential"/> from the storage used by the authentication object. /// </summary> /// <param name="targetUri"> /// The uniform resource indicator used to uniquely identify the credentials. /// </param> public override void DeleteCredentials(TargetUri targetUri) { BaseSecureStore.ValidateTargetUri(targetUri); if (PersonalAccessTokenStore.ReadCredentials(targetUri) != null) { PersonalAccessTokenStore.DeleteCredentials(targetUri); Git.Trace.WriteLine($"credentials for '{targetUri}' deleted"); } }
/// <summary> /// Attempts to get a set of credentials from storage by their target resource. /// </summary> /// <param name="targetUri">The 'key' by which to identify credentials.</param> /// <param name="credentials"> /// Credentials associated with the URI if successful; <see langword="null"/> otherwise. /// </param> /// <returns><see langword="true"/> if successful; <see langword="false"/> otherwise.</returns> public override Credential GetCredentials(TargetUri targetUri) { BaseSecureStore.ValidateTargetUri(targetUri); Credential credentials = null; if ((credentials = PersonalAccessTokenStore.ReadCredentials(targetUri)) != null) { Git.Trace.WriteLine($"credentials for '{targetUri}' found."); } return(credentials); }
/// <inheritdoc/> public override async Task <bool> DeleteCredentials(TargetUri targetUri, string username) { if (targetUri is null) { throw new ArgumentNullException(nameof(targetUri)); } Trace.WriteLine($"Deleting Bitbucket Credentials for {targetUri.QueryUri}"); Credential credentials = null; if ((credentials = await PersonalAccessTokenStore.ReadCredentials(targetUri)) != null) { // try to delete the credentials for the explicit target uri first await PersonalAccessTokenStore.DeleteCredentials(targetUri); Trace.WriteLine($"host credentials deleted for {targetUri.QueryUri}"); } // tidy up and delete any related refresh tokens var refreshTargetUri = GetRefreshTokenTargetUri(targetUri); if ((credentials = await PersonalAccessTokenStore.ReadCredentials(refreshTargetUri)) != null) { // try to delete the credentials for the explicit target uri first await PersonalAccessTokenStore.DeleteCredentials(refreshTargetUri); Trace.WriteLine($"host refresh credentials deleted for {refreshTargetUri.QueryUri}"); } // if we deleted per user then we should try and delete the host level credentials too if // they match the username if (targetUri.TargetUriContainsUsername) { var hostTargetUri = new TargetUri(targetUri.ToString(false, true, true)); var hostCredentials = await GetCredentials(hostTargetUri); var encodedUsername = Uri.EscapeDataString(targetUri.TargetUriUsername); if (encodedUsername != username) { Trace.WriteLine($"username {username} != targetUri userInfo {encodedUsername}"); } if (hostCredentials != null && hostCredentials.Username.Equals(encodedUsername)) { await DeleteCredentials(hostTargetUri, username); } } return(true); }
/// <summary> /// Gets a <see cref="Credential"/> from the storage used by the authentication object. /// <para/> /// Returns a `<see cref="Credential"/>` if successful; otherwise `<see langword="null"/>`. /// </summary> /// <param name="targetUri">The uniform resource indicator used to uniquely identify the credentials.</param> public override async Task <Credential> GetCredentials(TargetUri targetUri) { BaseSecureStore.ValidateTargetUri(targetUri); Credential credentials = null; var normalizedTargetUri = NormalizeUri(targetUri); if ((credentials = await PersonalAccessTokenStore.ReadCredentials(normalizedTargetUri)) != null) { Trace.WriteLine($"credentials for '{normalizedTargetUri}' found."); } return(credentials); }
/// <summary> /// Deletes a `<see cref="Credential"/>` from the storage used by the authentication object. /// </summary> /// <param name="targetUri">The uniform resource indicator used to uniquely identify the credentials.</param> public override async Task <bool> DeleteCredentials(TargetUri targetUri) { BaseSecureStore.ValidateTargetUri(targetUri); bool result = false; var normalizedTargetUri = NormalizeUri(targetUri); if (await PersonalAccessTokenStore.ReadCredentials(normalizedTargetUri) != null) { result = await PersonalAccessTokenStore.DeleteCredentials(normalizedTargetUri); Trace.WriteLine($"credentials for '{normalizedTargetUri}' deleted"); } return(result); }
/// <summary> /// Returns a `<see cref="Credential"/>` from the storage used by the authentication object if successful; otherwise `<see langword="null"/>`. /// </summary> /// <param name="targetUri">The uniform resource indicator used to uniquely identify the credentials.</param> public override async Task <Credential> GetCredentials(TargetUri targetUri) { if (targetUri is null) { throw new ArgumentNullException(nameof(targetUri)); } Credential credentials = null; var normalizedTargetUri = NormalizeUri(targetUri); if ((credentials = await PersonalAccessTokenStore.ReadCredentials(normalizedTargetUri)) != null) { Trace.WriteLine($"credentials for '{normalizedTargetUri}' found."); } return(credentials); }
/// <inheritdoc/> public override void DeleteCredentials(TargetUri targetUri, string username) { BaseSecureStore.ValidateTargetUri(targetUri); Trace.WriteLine($"Deleting Bitbucket Credentials for {targetUri.ActualUri}"); Credential credentials = null; if ((credentials = PersonalAccessTokenStore.ReadCredentials(targetUri)) != null) { // try to delete the credentials for the explicit target uri first PersonalAccessTokenStore.DeleteCredentials(targetUri); Trace.WriteLine($"host credentials deleted for {targetUri.ActualUri}"); } // tidy up and delete any related refresh tokens var refreshTargetUri = GetRefreshTokenTargetUri(targetUri); if ((credentials = PersonalAccessTokenStore.ReadCredentials(refreshTargetUri)) != null) { // try to delete the credentials for the explicit target uri first PersonalAccessTokenStore.DeleteCredentials(refreshTargetUri); Trace.WriteLine($"host refresh credentials deleted for {refreshTargetUri.ActualUri}"); } // if we deleted per user then we shoudl try and delete the host level credentials too if // they match the username if (targetUri.TargetUriContainsUsername) { var hostTargetUri = targetUri.GetHostTargetUri(); var hostCredentials = GetCredentials(hostTargetUri); var encodedUsername = Uri.EscapeDataString(targetUri.TargetUriUsername); if (encodedUsername != username) { Trace.WriteLine($"username {username} != targetUri userInfo {encodedUsername}"); } if (hostCredentials != null && hostCredentials.Username.Equals(encodedUsername)) { DeleteCredentials(targetUri.GetHostTargetUri(), username); } } }
/// <summary> /// Deletes a `<see cref="Credential"/>` from the storage used by the authentication object. /// </summary> /// <param name="targetUri">The uniform resource indicator used to uniquely identify the credentials.</param> public override async Task <bool> DeleteCredentials(TargetUri targetUri) { if (targetUri is null) { throw new ArgumentNullException(nameof(targetUri)); } bool result = false; var normalizedTargetUri = NormalizeUri(targetUri); if (await PersonalAccessTokenStore.ReadCredentials(normalizedTargetUri) != null) { result = await PersonalAccessTokenStore.DeleteCredentials(normalizedTargetUri); Trace.WriteLine($"credentials for '{normalizedTargetUri}' deleted"); } return(result); }
/// <summary> /// Gets a <see cref="Credential"/> from the storage used by the authentication object. /// <para/> /// Returns a `<see cref="Credential"/>` if successful; otherwise `<see langword="null"/>`. /// </summary> /// <param name="targetUri">The uniform resource indicator used to uniquely identify the credentials.</param> public override Credential GetCredentials(TargetUri targetUri) { BaseSecureStore.ValidateTargetUri(targetUri); Credential credentials = null; try { if ((credentials = PersonalAccessTokenStore.ReadCredentials(targetUri)) != null) { Git.Trace.WriteLine($"credentials for '{targetUri}' found."); } } catch (Exception exception) { System.Diagnostics.Debug.WriteLine(exception); Git.Trace.WriteLine($"failed to read credentials from the secure store: {exception.GetType().Name}."); } return(credentials); }
/// <summary> /// Gets a <see cref="Credential"/> from the storage used by the authentication object. /// <para/> /// Returns a `<see cref="Credential"/>` if successful; otherwise `<see langword="null"/>`. /// </summary> /// <param name="targetUri">The uniform resource indicator used to uniquely identify the credentials.</param> public override async Task <Credential> GetCredentials(TargetUri targetUri) { if (targetUri is null) { throw new ArgumentNullException(nameof(targetUri)); } Credential credentials = null; if ((credentials = await PersonalAccessTokenStore.ReadCredentials(targetUri)) != null) { Trace.WriteLine("successfully retrieved stored credentials, updating credential cache"); return(credentials); } // try for a refresh token var refreshCredentials = await PersonalAccessTokenStore.ReadCredentials(GetRefreshTokenTargetUri(targetUri)); if (refreshCredentials == null) { // no refresh token return null return(credentials); } Credential refreshedCredentials = await RefreshCredentials(targetUri, refreshCredentials.Password, null); if (refreshedCredentials == null) { // refresh failed return null return(credentials); } else { credentials = refreshedCredentials; } return(credentials); }