/// <summary> /// Acquires credentials. /// </summary> /// <param name="cred">Receives the credentials if the operation is successful.</param> /// <param name="user">The username.</param> /// <param name="methods">The credential types allowed. The only supported one is <see cref="UsernamePasswordCredentials"/>. May be empty but should not be null.</param> /// <returns>0 if successful; a non-zero error code that came from <see cref="Proxy.git_transport_smart_credentials"/> otherwise.</returns> public int AcquireCredentials(out Credentials cred, string user, params Type[] methods) { // Convert the user-provided types to libgit2's flags int allowed = 0; foreach (var method in methods) { if (method == typeof(UsernamePasswordCredentials)) { allowed |= (int)GitCredentialType.UserPassPlaintext; } else if (method == typeof(DefaultCredentials)) { allowed |= (int)GitCredentialType.Default; } else { throw new InvalidOperationException("Unknown type passes as allowed credential"); } } IntPtr credHandle = IntPtr.Zero; int res = Proxy.git_transport_smart_credentials(out credHandle, Transport, user, allowed); if (res != 0) { cred = null; return res; } if (credHandle == IntPtr.Zero) { throw new InvalidOperationException("credentials callback indicated success but returned no credentials"); } unsafe { var baseCred = (GitCredential*) credHandle; switch (baseCred->credtype) { case GitCredentialType.UserPassPlaintext: cred = UsernamePasswordCredentials.FromNative((GitCredentialUserpass*) credHandle); return 0; case GitCredentialType.Default: cred = new DefaultCredentials(); return 0; default: throw new InvalidOperationException("User returned an unkown credential type"); } } }
public int AcquireCredentials(out Credentials cred, string user, params Type[] methods) { // Convert the user-provided types to libgit2's flags int allowed = 0; foreach (var method in methods) { if (method == typeof(UsernamePasswordCredentials)) { allowed |= (int)GitCredentialType.UserPassPlaintext; } else { throw new InvalidOperationException("Unknown type passes as allowed credential"); } } IntPtr credHandle = IntPtr.Zero; int res = Proxy.git_transport_smart_credentials(out credHandle, Transport, user, allowed); if (res != 0) { cred = null; return(res); } var baseCred = credHandle.MarshalAs <GitCredential>(); switch (baseCred.credtype) { case GitCredentialType.UserPassPlaintext: cred = UsernamePasswordCredentials.FromNative(credHandle.MarshalAs <GitCredentialUserpass>()); return(0); default: throw new InvalidOperationException("User returned an unkown credential type"); } }