Skip to content

Commit

Permalink
[SCM-994] Add credentials provider for JGit
Browse files Browse the repository at this point in the history
This closes #154
  • Loading branch information
kwin authored and michael-o committed Jul 20, 2022
1 parent acb585f commit c8b7fa9
Show file tree
Hide file tree
Showing 4 changed files with 172 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,15 @@ ScmProviderRepository makeProviderScmRepository( String scmSpecificUrl, char del
ScmProviderRepository makeProviderScmRepository( File path )
throws ScmRepositoryException, UnknownRepositoryStructure;

/**
* Sets the interactive mode.
* @param interactive either {@code true} in case user may be prompted for information, otherwise {@code false}
* @since 2.0.0-M2
*/
default void setInteractive( boolean interactive )
{
}

/**
* Validate the scm url.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,17 @@
<groupId>org.apache.maven.scm</groupId>
<artifactId>maven-scm-provider-git-commons</artifactId>
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-interactivity-api</artifactId>
<version>1.1</version>
<exclusions>
<exclusion>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-container-default</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.eclipse.jgit</groupId>
<artifactId>org.eclipse.jgit</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
* under the License.
*/

import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;

Expand All @@ -29,6 +30,7 @@
import org.apache.maven.scm.command.info.InfoScmResult;
import org.apache.maven.scm.provider.git.AbstractGitScmProvider;
import org.apache.maven.scm.provider.git.command.GitCommand;
import org.apache.maven.scm.provider.git.jgit.command.PlexusInteractivityCredentialsProvider;
import org.apache.maven.scm.provider.git.jgit.command.add.JGitAddCommand;
import org.apache.maven.scm.provider.git.jgit.command.blame.JGitBlameCommand;
import org.apache.maven.scm.provider.git.jgit.command.branch.JGitBranchCommand;
Expand All @@ -43,6 +45,8 @@
import org.apache.maven.scm.provider.git.jgit.command.tag.JGitTagCommand;
import org.apache.maven.scm.provider.git.jgit.command.untag.JGitUntagCommand;
import org.apache.maven.scm.repository.ScmRepositoryException;
import org.codehaus.plexus.components.interactivity.Prompter;
import org.eclipse.jgit.transport.CredentialsProvider;

/**
* @author <a href="mailto:struberg@yahoo.de">Mark Struberg</a>
Expand All @@ -54,6 +58,21 @@
public class JGitScmProvider
extends AbstractGitScmProvider
{
private final PlexusInteractivityCredentialsProvider credentialsProvider;

@Inject
public JGitScmProvider( Prompter prompter )
{
credentialsProvider = new PlexusInteractivityCredentialsProvider( prompter );
CredentialsProvider.setDefault( credentialsProvider );
}

@Override
public void setInteractive( boolean interactive )
{
credentialsProvider.setInteractive( interactive );
}

/**
* {@inheritDoc}
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package org.apache.maven.scm.provider.git.jgit.command;

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/


import java.util.Arrays;

import org.codehaus.plexus.components.interactivity.Prompter;
import org.codehaus.plexus.components.interactivity.PrompterException;
import org.eclipse.jgit.errors.UnsupportedCredentialItem;
import org.eclipse.jgit.transport.CredentialItem;
import org.eclipse.jgit.transport.CredentialsProvider;
import org.eclipse.jgit.transport.URIish;

/**
* {@link CredentialsProvider} leveraging the {@link Prompter} component.
*
*/
public class PlexusInteractivityCredentialsProvider extends CredentialsProvider
{
private boolean interactive;
private final Prompter prompter;

public PlexusInteractivityCredentialsProvider( Prompter prompter )
{
this.interactive = true;
this.prompter = prompter;
}

@Override
public boolean get( URIish uri, CredentialItem... items ) throws UnsupportedCredentialItem
{
for ( CredentialItem item : items )
{
try
{
get( uri, item );
}
catch ( PrompterException e )
{
throw new IllegalStateException( "Cannot prompt user", e );
}
}
return true;
}

private void get( URIish uri, CredentialItem item ) throws PrompterException
{
if ( item instanceof CredentialItem.InformationalMessage )
{
// works even in non-interactive mode
prompter.showMessage( item.getPromptText() );
}
else
{
if ( !interactive )
{
throw new UnsupportedCredentialItem( uri,
"Cannot provide '" + item.getClass() + "' in non-interactive mode" );
}
if ( item instanceof CredentialItem.YesNoType )
{
CredentialItem.YesNoType yesNoItem = ( CredentialItem.YesNoType ) item;
String value = prompter.prompt( item.getPromptText(), Arrays.asList( "yes", "no" ) );
yesNoItem.setValue( value.equals( "yes" ) );
}
else if ( item instanceof CredentialItem.Password )
{
CredentialItem.Password passwordItem = ( CredentialItem.Password ) item;
String password = prompter.promptForPassword( passwordItem.getPromptText() );
passwordItem.setValue( password.toCharArray() );
}
else if ( item instanceof CredentialItem.Username )
{
CredentialItem.Username usernameItem = ( CredentialItem.Username ) item;
String username = prompter.prompt( usernameItem.getPromptText() );
usernameItem.setValue( username );
}
else if ( item instanceof CredentialItem.StringType )
{
CredentialItem.StringType stringItem = ( CredentialItem.StringType ) item;
String value = prompter.prompt( stringItem.getPromptText() );
stringItem.setValue( value );
}
else if ( item instanceof CredentialItem.CharArrayType )
{
CredentialItem.CharArrayType charArrayItem = ( CredentialItem.CharArrayType ) item;
String value = prompter.prompt( charArrayItem.getPromptText() );
charArrayItem.setValue( value.toCharArray() );
}
else
{
throw new UnsupportedCredentialItem( uri, "This provider does not support items of type '"
+ item.getClass().toString() + "'" );
}
}
}

public void setInteractive( boolean interactive )
{
this.interactive = interactive;
}

@Override
public boolean isInteractive()
{
return interactive;
}

@Override
public boolean supports( CredentialItem... items )
{
return true;
}

}

0 comments on commit c8b7fa9

Please sign in to comment.