Exemplo n.º 1
0
        static IEnumerable <Tuple <string, string> > StoreCommand(IDictionary <string, string> args)
        {
            // Build the URL
            Uri    url      = ExtractUrl(args);
            string userName = args.GetOrDefault("username", String.Empty);
            string password = args.GetOrDefault("password", String.Empty);

            bool abort = false;

            if (abort |= String.IsNullOrEmpty(userName))
            {
                Console.Error.WriteLine("username parameter must be provided");
            }
            if (abort |= String.IsNullOrEmpty(password))
            {
                Console.Error.WriteLine("password parameter must be provided");
            }
            if (!abort)
            {
                string target                 = GetTargetName(url);
                IntPtr passwordPtr            = Marshal.StringToBSTR(password);
                NativeMethods.CREDENTIAL cred = new NativeMethods.CREDENTIAL()
                {
                    type           = 0x01, // Generic
                    targetName     = target,
                    credentialBlob = Marshal.StringToCoTaskMemUni(password),
                    persist        = 0x03, // Enterprise (roaming)
                    attributeCount = 0,
                    userName       = userName
                };
                cred.credentialBlobSize = Encoding.Unicode.GetByteCount(password);
                if (!NativeMethods.CredWrite(ref cred, 0))
                {
                    Console.Error.WriteLine(
                        "Failed to write credential: " +
                        GetLastErrorMessage());
                }
            }
            return(Enumerable.Empty <Tuple <string, string> >());
        }
Exemplo n.º 2
0
        static IEnumerable<Tuple<string, string>> StoreCommand(IDictionary<string, string> args)
        {
            // Build the URL
            Uri url = ExtractUrl(args);
            string userName = args.GetOrDefault("username", String.Empty);
            string password = args.GetOrDefault("password", String.Empty);

            bool abort = false;
            if(abort |= String.IsNullOrEmpty(userName)) {
                Console.Error.WriteLine("username parameter must be provided");
            }
            if(abort |= String.IsNullOrEmpty(password)) {
                Console.Error.WriteLine("password parameter must be provided");
            }
            if (!abort)
            {
                string target = GetTargetName(url);
                IntPtr passwordPtr = Marshal.StringToBSTR(password);
                NativeMethods.CREDENTIAL cred = new NativeMethods.CREDENTIAL()
                {
                    type = 0x01, // Generic
                    targetName = target,
                    credentialBlob = Marshal.StringToCoTaskMemUni(password),
                    persist = 0x03, // Enterprise (roaming)
                    attributeCount = 0,
                    userName = userName
                };
                cred.credentialBlobSize = Encoding.Unicode.GetByteCount(password);
                if (!NativeMethods.CredWrite(ref cred, 0))
                {
                    Console.Error.WriteLine(
                        "Failed to write credential: " +
                        GetLastErrorMessage());
                }
            }
            return Enumerable.Empty<Tuple<string, string>>();
        }
Exemplo n.º 3
0
        static IEnumerable <Tuple <string, string> > GetCommand(IDictionary <string, string> args)
        {
            // Build the URL
            Uri url = ExtractUrl(args);

            if (url == null)
            {
                yield break;
            }

            string userName = args.GetOrDefault("username", null);
            string password = null;

            IntPtr credPtr = IntPtr.Zero;

            try
            {
                // Check for a credential
                string target = GetTargetName(url);
                if (!NativeMethods.CredRead(target, NativeMethods.CRED_TYPE.GENERIC, 0, out credPtr))
                {
                    // Don't have a credential for this user. Are we on XP? If so, sorry no dice.
                    if (OnXP())
                    {
                        // Users will get a Git prompt for user name and password. We'll still store them.
                        yield break;
                    }
                    credPtr = IntPtr.Zero;

                    // If we have a username, pack an input authentication buffer
                    Tuple <int, IntPtr> inputBuffer = null;;
                    IntPtr outputBuffer             = IntPtr.Zero;
                    int    outputBufferSize         = 0;
                    try
                    {
                        inputBuffer = PackUserNameBuffer(userName);
                        if (inputBuffer == null)
                        {
                            yield break;
                        }

                        // Setup UI
                        NativeMethods.CREDUI_INFO ui = new NativeMethods.CREDUI_INFO()
                        {
                            pszCaptionText = "Git Credentials",
                            pszMessageText = "Enter your credentials for: " + GetHost(url)
                        };
                        ui.cbSize = Marshal.SizeOf(ui);

                        // Prompt!
                        int  authPackage = 0;
                        bool save        = false;
                        var  ret         = NativeMethods.CredUIPromptForWindowsCredentials(
                            uiInfo: ref ui,
                            authError: 0,
                            authPackage: ref authPackage,
                            InAuthBuffer: inputBuffer.Item2,
                            InAuthBufferSize: inputBuffer.Item1,
                            refOutAuthBuffer: out outputBuffer,
                            refOutAuthBufferSize: out outputBufferSize,
                            fSave: ref save,
                            flags: NativeMethods.PromptForWindowsCredentialsFlags.CREDUIWIN_GENERIC);
                        if (ret != NativeMethods.CredUIReturnCodes.NO_ERROR)
                        {
                            Console.Error.WriteLine("Error prompting for credentials: " + ret.ToString());
                            yield break;
                        }
                    }
                    finally
                    {
                        if (inputBuffer != null && inputBuffer.Item2 != IntPtr.Zero)
                        {
                            Marshal.FreeHGlobal(inputBuffer.Item2);
                        }
                    }

                    try
                    {
                        // Unpack
                        if (!UnPackAuthBuffer(outputBuffer, outputBufferSize, out userName, out password))
                        {
                            yield break;
                        }
                    }
                    finally
                    {
                        if (outputBuffer != IntPtr.Zero)
                        {
                            Marshal.FreeHGlobal(outputBuffer);
                        }
                    }
                }
                else
                {
                    // Decode the credential
                    NativeMethods.CREDENTIAL cred = (NativeMethods.CREDENTIAL)Marshal.PtrToStructure(credPtr, typeof(NativeMethods.CREDENTIAL));
                    userName = cred.userName;
                    password = Marshal.PtrToStringBSTR(cred.credentialBlob);
                }

                yield return(Tuple.Create("username", userName));

                yield return(Tuple.Create("password", password));
            }
            finally
            {
                if (credPtr != IntPtr.Zero)
                {
                    NativeMethods.CredFree(credPtr);
                }
            }
        }