public CredentialsProviderScope(CredentialsProvider credentialsProvider)
        {
            if (credentialsProvider == null)
                throw new ArgumentNullException("credentialsProvider");

            _lastProvider = _currentProvider;
            _currentProvider = credentialsProvider;
        }
        public void Dispose()
        {
            if (!_disposed)
            {
                _currentProvider = _lastProvider;

                _disposed = true;
            }
        }
		/// <exception cref="NSch.JSchException"></exception>
		public override Session GetSession(string user, string pass, string host, int port
			, CredentialsProvider credentialsProvider, FS fs)
		{
			lock (this)
			{
				if (config == null)
				{
					config = OpenSshConfig.Get(fs);
				}
				OpenSshConfig.Host hc = config.Lookup(host);
				host = hc.GetHostName();
				if (port <= 0)
				{
					port = hc.GetPort();
				}
				if (user == null)
				{
					user = hc.GetUser();
				}
				Session session = CreateSession(hc, user, host, port, fs);
				if (pass != null)
				{
					session.SetPassword(pass);
				}
				string strictHostKeyCheckingPolicy = hc.GetStrictHostKeyChecking();
				if (strictHostKeyCheckingPolicy != null)
				{
					session.SetConfig("StrictHostKeyChecking", strictHostKeyCheckingPolicy);
				}
				string pauth = hc.GetPreferredAuthentications();
				if (pauth != null)
				{
					session.SetConfig("PreferredAuthentications", pauth);
				}
				if (credentialsProvider != null && (!hc.IsBatchMode() || !credentialsProvider.IsInteractive
					()))
				{
					session.SetUserInfo(new CredentialsProviderUserInfo(session, credentialsProvider)
						);
				}
				Configure(hc, session);
				return session;
			}
		}
Esempio n. 4
0
		/// <summary>Create a minimal transport instance not tied to a single repository.</summary>
		/// <remarks>Create a minimal transport instance not tied to a single repository.</remarks>
		/// <param name="uri"></param>
		protected internal Transport(URIish uri)
		{
			this.uri = uri;
			this.local = null;
			this.checkFetchedObjects = true;
			this.credentialsProvider = CredentialsProvider.GetDefault();
		}
 /// <summary>Wrap a CredentialsProvider to make it suitable for use with JSch.</summary>
 /// <remarks>Wrap a CredentialsProvider to make it suitable for use with JSch.</remarks>
 /// <param name="session">the JSch session this UserInfo will support authentication on.
 ///     </param>
 /// <param name="credentialsProvider">the provider that will perform the authentication.
 ///     </param>
 public CredentialsProviderUserInfo(Session session, CredentialsProvider credentialsProvider
                                    )
 {
     this.uri      = CreateURI(session);
     this.provider = credentialsProvider;
 }
Esempio n. 6
0
 /// <summary>Create a new transport instance.</summary>
 /// <remarks>Create a new transport instance.</remarks>
 /// <param name="local">
 /// the repository this instance will fetch into, or push out of.
 /// This must be the repository passed to
 /// <see cref="Open(NGit.Repository, URIish)">Open(NGit.Repository, URIish)</see>
 /// .
 /// </param>
 /// <param name="uri">
 /// the URI used to access the remote repository. This must be the
 /// URI passed to
 /// <see cref="Open(NGit.Repository, URIish)">Open(NGit.Repository, URIish)</see>
 /// .
 /// </param>
 protected internal Transport(Repository local, URIish uri)
 {
     TransferConfig tc = local.GetConfig().Get(TransferConfig.KEY);
     this.local = local;
     this.uri = uri;
     this.checkFetchedObjects = tc.IsFsckObjects();
     this.credentialsProvider = CredentialsProvider.GetDefault();
 }
Esempio n. 7
0
        /// <exception cref="NGit.Errors.TransportException"></exception>
        public override RemoteSession GetSession(URIish uri, CredentialsProvider credentialsProvider
			, FS fs, int tms)
        {
            lock (this)
            {
                string user = uri.GetUser();
                string pass = uri.GetPass();
                string host = uri.GetHost();
                int port = uri.GetPort();
                try
                {
                    if (config == null)
                    {
                        config = OpenSshConfig.Get(fs);
                    }
                    OpenSshConfig.Host hc = config.Lookup(host);
                    host = hc.GetHostName();
                    if (port <= 0)
                    {
                        port = hc.GetPort();
                    }
                    if (user == null)
                    {
                        user = hc.GetUser();
                    }
                    Session session = CreateSession(credentialsProvider, fs, user, pass, host, port,
                        hc);
                    int retries = 0;
                    while (!session.IsConnected() && retries < 3)
                    {
                        try
                        {
                            retries++;
                            session.Connect(tms);
                        }
                        catch (JSchException e)
                        {
                            session.Disconnect();
                            session = null;
                            // if authentication failed maybe credentials changed at the
                            // remote end therefore reset credentials and retry
                            if (credentialsProvider != null && e.InnerException == null && e.Message.Equals("Auth fail"
                                ))
                            {
                                credentialsProvider.Reset(uri);
                                session = CreateSession(credentialsProvider, fs, user, pass, host, port, hc);
                            }
                            else
                            {
                                throw;
                            }
                        }
                    }
                    return new JschSession(session, uri);
                }
                catch (JSchException je)
                {
                    Exception c = je.InnerException;
                    if (c is UnknownHostException)
                    {
                        throw new TransportException(uri, JGitText.Get().unknownHost);
                    }
                    if (c is ConnectException)
                    {
                        throw new TransportException(uri, c.Message);
                    }
                    throw new TransportException(uri, je.Message, je);
                }
            }
        }
Esempio n. 8
0
 /// <summary>Set the default credentials provider.</summary>
 /// <remarks>Set the default credentials provider.</remarks>
 /// <param name="p">the new default provider, may be null to select no default.</param>
 public static void SetDefault(CredentialsProvider p)
 {
     defaultProvider = p;
 }
 /// <exception cref="NGit.Errors.TransportException"></exception>
 public override RemoteSession GetSession(URIish uri, CredentialsProvider credentialsProvider
                                          , FS fs, int tms)
 {
     lock (this)
     {
         string user = uri.GetUser();
         string pass = uri.GetPass();
         string host = uri.GetHost();
         int    port = uri.GetPort();
         try
         {
             if (config == null)
             {
                 config = OpenSshConfig.Get(fs);
             }
             OpenSshConfig.Host hc = config.Lookup(host);
             host = hc.GetHostName();
             if (port <= 0)
             {
                 port = hc.GetPort();
             }
             if (user == null)
             {
                 user = hc.GetUser();
             }
             Session session = CreateSession(credentialsProvider, fs, user, pass, host, port,
                                             hc);
             int retries = 0;
             while (!session.IsConnected() && retries < 3)
             {
                 try
                 {
                     retries++;
                     session.Connect(tms);
                 }
                 catch (JSchException e)
                 {
                     session.Disconnect();
                     session = null;
                     // if authentication failed maybe credentials changed at the
                     // remote end therefore reset credentials and retry
                     if (credentialsProvider != null && e.InnerException == null && e.Message.Equals("Auth fail"
                                                                                                     ) && retries < 3)
                     {
                         credentialsProvider.Reset(uri);
                         session = CreateSession(credentialsProvider, fs, user, pass, host, port, hc);
                     }
                     else
                     {
                         throw;
                     }
                 }
             }
             return(new JschSession(session, uri));
         }
         catch (JSchException je)
         {
             Exception c = je.InnerException;
             if (c is UnknownHostException)
             {
                 throw new TransportException(uri, JGitText.Get().unknownHost);
             }
             if (c is ConnectException)
             {
                 throw new TransportException(uri, c.Message);
             }
             throw new TransportException(uri, je.Message, je);
         }
     }
 }
Esempio n. 10
0
 public GitProvider(IAuthor author, CredentialsProvider credentials)
     : this(author)
 {
     Credentials = credentials;
 }
Esempio n. 11
0
        /// <summary>Update this method with the credentials from the URIish.</summary>
        /// <remarks>Update this method with the credentials from the URIish.</remarks>
        /// <param name="uri">the URI used to create the connection.</param>
        /// <param name="credentialsProvider">
        /// the credentials provider, or null. If provided,
        /// <see cref="URIish.GetPass()">credentials in the URI</see>
        /// are ignored.
        /// </param>
        /// <returns>
        /// true if the authentication method is able to provide
        /// authorization for the given URI
        /// </returns>
        internal virtual bool Authorize(URIish uri, CredentialsProvider credentialsProvider
			)
        {
            string username;
            string password;
            if (credentialsProvider != null)
            {
                CredentialItem.Username u = new CredentialItem.Username();
                CredentialItem.Password p = new CredentialItem.Password();
                if (credentialsProvider.Supports(u, p) && credentialsProvider.Get(uri, u, p))
                {
                    username = u.GetValue();
                    password = new string(p.GetValue());
                    p.Clear();
                }
                else
                {
                    return false;
                }
            }
            else
            {
                username = uri.GetUser();
                password = uri.GetPass();
            }
            if (username != null)
            {
                Authorize(username, password);
                return true;
            }
            return false;
        }
Esempio n. 12
0
 /// <exception cref="NGit.Errors.TransportException"></exception>
 public override RemoteSession GetSession(URIish uri, CredentialsProvider credentialsProvider
                                          , FS fs, int tms)
 {
     lock (this)
     {
         string user = uri.GetUser();
         string pass = uri.GetPass();
         string host = uri.GetHost();
         int    port = uri.GetPort();
         try
         {
             if (config == null)
             {
                 config = OpenSshConfig.Get(fs);
             }
             OpenSshConfig.Host hc = config.Lookup(host);
             host = hc.GetHostName();
             if (port <= 0)
             {
                 port = hc.GetPort();
             }
             if (user == null)
             {
                 user = hc.GetUser();
             }
             Session session = CreateSession(hc, user, host, port, fs);
             if (pass != null)
             {
                 session.SetPassword(pass);
             }
             string strictHostKeyCheckingPolicy = hc.GetStrictHostKeyChecking();
             if (strictHostKeyCheckingPolicy != null)
             {
                 session.SetConfig("StrictHostKeyChecking", strictHostKeyCheckingPolicy);
             }
             string pauth = hc.GetPreferredAuthentications();
             if (pauth != null)
             {
                 session.SetConfig("PreferredAuthentications", pauth);
             }
             if (credentialsProvider != null && (!hc.IsBatchMode() || !credentialsProvider.IsInteractive
                                                     ()))
             {
                 session.SetUserInfo(new CredentialsProviderUserInfo(session, credentialsProvider)
                                     );
             }
             Configure(hc, session);
             if (!session.IsConnected())
             {
                 session.Connect(tms);
             }
             return(new JschSession(session, uri));
         }
         catch (JSchException je)
         {
             Exception c = je.InnerException;
             if (c is UnknownHostException)
             {
                 throw new TransportException(uri, JGitText.Get().unknownHost);
             }
             if (c is ConnectException)
             {
                 throw new TransportException(uri, c.Message);
             }
             throw new TransportException(uri, je.Message, je);
         }
     }
 }
		/// <exception cref="NGit.Errors.TransportException"></exception>
		public override RemoteSession GetSession(URIish uri, CredentialsProvider credentialsProvider
			, FS fs, int tms)
		{
			lock (this)
			{
				string user = uri.GetUser();
				string pass = uri.GetPass();
				string host = uri.GetHost();
				int port = uri.GetPort();
				try
				{
					if (config == null)
					{
						config = OpenSshConfig.Get(fs);
					}
					OpenSshConfig.Host hc = config.Lookup(host);
					host = hc.GetHostName();
					if (port <= 0)
					{
						port = hc.GetPort();
					}
					if (user == null)
					{
						user = hc.GetUser();
					}
					Session session = CreateSession(hc, user, host, port, fs);
					if (pass != null)
					{
						session.SetPassword(pass);
					}
					string strictHostKeyCheckingPolicy = hc.GetStrictHostKeyChecking();
					if (strictHostKeyCheckingPolicy != null)
					{
						session.SetConfig("StrictHostKeyChecking", strictHostKeyCheckingPolicy);
					}
					string pauth = hc.GetPreferredAuthentications();
					if (pauth != null)
					{
						session.SetConfig("PreferredAuthentications", pauth);
					}
					if (credentialsProvider != null && (!hc.IsBatchMode() || !credentialsProvider.IsInteractive
						()))
					{
						session.SetUserInfo(new CredentialsProviderUserInfo(session, credentialsProvider)
							);
					}
					Configure(hc, session);
					if (!session.IsConnected())
					{
						session.Connect(tms);
					}
					return new JschSession(session, uri);
				}
				catch (JSchException je)
				{
					Exception c = je.InnerException;
					if (c is UnknownHostException)
					{
						throw new TransportException(uri, JGitText.Get().unknownHost);
					}
					if (c is ConnectException)
					{
						throw new TransportException(uri, c.Message);
					}
					throw new TransportException(uri, je.Message, je);
				}
			}
		}
Esempio n. 14
0
 /// <exception cref="NGit.Errors.TransportException"></exception>
 public override RemoteSession GetSession(URIish uri2, CredentialsProvider credentialsProvider
                                          , FS fs, int tms)
 {
     return(new TransportGitSsh.ExtSession(_enclosing));
 }
		/// <summary>Wrap a CredentialsProvider to make it suitable for use with JSch.</summary>
		/// <remarks>Wrap a CredentialsProvider to make it suitable for use with JSch.</remarks>
		/// <param name="session">the JSch session this UserInfo will support authentication on.
		/// 	</param>
		/// <param name="credentialsProvider">the provider that will perform the authentication.
		/// 	</param>
		public CredentialsProviderUserInfo(Session session, CredentialsProvider credentialsProvider
			)
		{
			this.uri = CreateURI(session);
			this.provider = credentialsProvider;
		}
 /// <summary>Open (or reuse) a session to a host.</summary>
 /// <remarks>
 /// Open (or reuse) a session to a host.
 /// <p>
 /// A reasonable UserInfo that can interact with the end-user (if necessary)
 /// is installed on the returned session by this method.
 /// <p>
 /// The caller must connect the session by invoking <code>connect()</code>
 /// if it has not already been connected.
 /// </remarks>
 /// <param name="uri">URI information about the remote host</param>
 /// <param name="credentialsProvider">provider to support authentication, may be null.
 ///     </param>
 /// <param name="fs">
 /// the file system abstraction which will be necessary to
 /// perform certain file system operations.
 /// </param>
 /// <param name="tms">Timeout value, in milliseconds.</param>
 /// <returns>a session that can contact the remote host.</returns>
 /// <exception cref="NGit.Errors.TransportException">the session could not be created.
 ///     </exception>
 public abstract RemoteSession GetSession(URIish uri, CredentialsProvider credentialsProvider
                                          , FS fs, int tms);
Esempio n. 17
0
		/// <summary>Open (or reuse) a session to a host.</summary>
		/// <remarks>
		/// Open (or reuse) a session to a host.
		/// <p>
		/// A reasonable UserInfo that can interact with the end-user (if necessary)
		/// is installed on the returned session by this method.
		/// <p>
		/// The caller must connect the session by invoking <code>connect()</code>
		/// if it has not already been connected.
		/// </remarks>
		/// <param name="user">
		/// username to authenticate as. If null a reasonable default must
		/// be selected by the implementation. This may be
		/// <code>System.getProperty("user.name")</code>.
		/// </param>
		/// <param name="pass">
		/// optional user account password or passphrase. If not null a
		/// UserInfo that supplies this value to the SSH library will be
		/// configured.
		/// </param>
		/// <param name="host">hostname (or IP address) to connect to. Must not be null.</param>
		/// <param name="port">
		/// port number the server is listening for connections on. May be &lt;=
		/// 0 to indicate the IANA registered port of 22 should be used.
		/// </param>
		/// <param name="credentialsProvider">provider to support authentication, may be null.
		/// 	</param>
		/// <param name="fs">
		/// the file system abstraction which will be necessary to
		/// perform certain file system operations.
		/// </param>
		/// <returns>a session that can contact the remote host.</returns>
		/// <exception cref="NSch.JSchException">the session could not be created.</exception>
		public abstract Session GetSession(string user, string pass, string host, int port
			, CredentialsProvider credentialsProvider, FS fs);
Esempio n. 18
0
        /// <exception cref="NSch.JSchException"></exception>
        private Session CreateSession(CredentialsProvider credentialsProvider, FS fs, string
			 user, string pass, string host, int port, OpenSshConfig.Host hc)
        {
            Session session = CreateSession(hc, user, host, port, fs);
            if (pass != null)
            {
                session.SetPassword(pass);
            }
            string strictHostKeyCheckingPolicy = hc.GetStrictHostKeyChecking();
            if (strictHostKeyCheckingPolicy != null)
            {
                session.SetConfig("StrictHostKeyChecking", strictHostKeyCheckingPolicy);
            }
            string pauth = hc.GetPreferredAuthentications();
            if (pauth != null)
            {
                session.SetConfig("PreferredAuthentications", pauth);
            }
            if (credentialsProvider != null && (!hc.IsBatchMode() || !credentialsProvider.IsInteractive
                ()))
            {
                session.SetUserInfo(new CredentialsProviderUserInfo(session, credentialsProvider)
                    );
            }
            Configure(hc, session);
            return session;
        }
Esempio n. 19
0
		/// <summary>Set the default credentials provider.</summary>
		/// <remarks>Set the default credentials provider.</remarks>
		/// <param name="p">the new default provider, may be null to select no default.</param>
		public static void SetDefault(CredentialsProvider p)
		{
			defaultProvider = p;
		}
Esempio n. 20
0
        /// <summary>A credentials provider to assist with authentication connections..</summary>
        /// <remarks>A credentials provider to assist with authentication connections..</remarks>
        /// <param name="credentialsProvider">the credentials provider, or null if there is none
        /// 	</param>
        public virtual void SetCredentialsProvider(CredentialsProvider credentialsProvider
			)
        {
            this.credentialsProvider = credentialsProvider;
        }
Esempio n. 21
0
        /// <summary>Open (or reuse) a session to a host.</summary>
        /// <remarks>
        /// Open (or reuse) a session to a host.
        /// <p>
        /// A reasonable UserInfo that can interact with the end-user (if necessary)
        /// is installed on the returned session by this method.
        /// <p>
        /// The caller must connect the session by invoking <code>connect()</code>
        /// if it has not already been connected.
        /// </remarks>
        /// <param name="uri">URI information about the remote host</param>
        /// <param name="credentialsProvider">provider to support authentication, may be null.
        /// 	</param>
        /// <param name="fs">
        /// the file system abstraction which will be necessary to
        /// perform certain file system operations.
        /// </param>
        /// <param name="tms">Timeout value, in milliseconds.</param>
        /// <returns>a session that can contact the remote host.</returns>
        /// <exception cref="NGit.Errors.TransportException">the session could not be created.
        /// 	</exception>
        public abstract RemoteSession GetSession(URIish uri, CredentialsProvider credentialsProvider
			, FS fs, int tms);
Esempio n. 22
0
			/// <exception cref="NGit.Errors.TransportException"></exception>
			public override RemoteSession GetSession(URIish uri2, CredentialsProvider credentialsProvider
				, FS fs, int tms)
			{
				return new TransportGitSsh.ExtSession(_enclosing);
			}
Esempio n. 23
0
 /// <summary>Open (or reuse) a session to a host.</summary>
 /// <remarks>
 /// Open (or reuse) a session to a host.
 /// <p>
 /// A reasonable UserInfo that can interact with the end-user (if necessary)
 /// is installed on the returned session by this method.
 /// <p>
 /// The caller must connect the session by invoking <code>connect()</code>
 /// if it has not already been connected.
 /// </remarks>
 /// <param name="user">
 /// username to authenticate as. If null a reasonable default must
 /// be selected by the implementation. This may be
 /// <code>System.getProperty("user.name")</code>.
 /// </param>
 /// <param name="pass">
 /// optional user account password or passphrase. If not null a
 /// UserInfo that supplies this value to the SSH library will be
 /// configured.
 /// </param>
 /// <param name="host">hostname (or IP address) to connect to. Must not be null.</param>
 /// <param name="port">
 /// port number the server is listening for connections on. May be &lt;=
 /// 0 to indicate the IANA registered port of 22 should be used.
 /// </param>
 /// <param name="credentialsProvider">provider to support authentication, may be null.
 ///     </param>
 /// <param name="fs">
 /// the file system abstraction which will be necessary to
 /// perform certain file system operations.
 /// </param>
 /// <returns>a session that can contact the remote host.</returns>
 /// <exception cref="NSch.JSchException">the session could not be created.</exception>
 public abstract Session GetSession(string user, string pass, string host, int port
                                    , CredentialsProvider credentialsProvider, FS fs);