You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@maven.apache.org by GitBox <gi...@apache.org> on 2022/07/19 12:25:03 UTC

[GitHub] [maven-scm] michael-o commented on a diff in pull request #154: [SCM-994] Add credentials provider for JGit

michael-o commented on code in PR #154:
URL: https://github.com/apache/maven-scm/pull/154#discussion_r924430038


##########
maven-scm-providers/maven-scm-providers-git/maven-scm-provider-jgit/src/main/java/org/apache/maven/scm/provider/git/jgit/JGitScmProvider.java:
##########
@@ -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 isInteractive )

Review Comment:
   `boolean interactive`



##########
maven-scm-providers/maven-scm-providers-git/maven-scm-provider-jgit/pom.xml:
##########
@@ -44,6 +44,17 @@
       <groupId>org.apache.maven.scm</groupId>
       <artifactId>maven-scm-provider-git-commons</artifactId>
     </dependency>
+    <dependency>
+      <artifactId>plexus-interactivity-api</artifactId>
+      <groupId>org.codehaus.plexus</groupId>

Review Comment:
   swap groupId and artifactId



##########
maven-scm-api/src/main/java/org/apache/maven/scm/provider/ScmProvider.java:
##########
@@ -71,6 +71,15 @@ ScmProviderRepository makeProviderScmRepository( String scmSpecificUrl, char del
     ScmProviderRepository makeProviderScmRepository( File path )
         throws ScmRepositoryException, UnknownRepositoryStructure;
 
+    /**
+     * Sets the interactive mode. 
+     * @param isInteractive either {@code true} in case user may be prompted for information, otherwise {@code false}
+     * @since 2.0.0-M2
+     */
+    default void setInteractive( boolean isInteractive )

Review Comment:
   `boolean interactive`



##########
maven-scm-providers/maven-scm-providers-git/maven-scm-provider-jgit/src/main/java/org/apache/maven/scm/provider/git/jgit/command/PlexusInteractivityCredentialsProvider.java:
##########
@@ -0,0 +1,106 @@
+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 isInteractive;
+    private final Prompter prompter;
+
+    public PlexusInteractivityCredentialsProvider( Prompter prompter )
+    {
+        this.isInteractive = true;
+        this.prompter = prompter;
+    }
+
+    @Override
+    public boolean get( URIish uri, CredentialItem... items ) throws UnsupportedCredentialItem
+    {
+        for ( CredentialItem item : items )
+        {
+            try 
+            {
+                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.InformationalMessage )
+                {
+                    prompter.showMessage( item.getPromptText() );
+                }
+                else
+                {
+                    throw new UnsupportedCredentialItem( uri, item.getClass().toString() );
+                }
+            }
+            catch ( PrompterException e )
+            {
+                throw new IllegalStateException( "Can not prompt user " + e.getMessage() , e );
+            }
+        }
+        return true;
+    }
+
+    public void setInteractive( boolean isInteractive )

Review Comment:
   same here



##########
maven-scm-providers/maven-scm-providers-git/maven-scm-provider-jgit/src/main/java/org/apache/maven/scm/provider/git/jgit/command/PlexusInteractivityCredentialsProvider.java:
##########
@@ -0,0 +1,106 @@
+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 isInteractive;

Review Comment:
   same here



##########
maven-scm-providers/maven-scm-providers-git/maven-scm-provider-jgit/src/main/java/org/apache/maven/scm/provider/git/jgit/command/PlexusInteractivityCredentialsProvider.java:
##########
@@ -0,0 +1,106 @@
+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 isInteractive;
+    private final Prompter prompter;
+
+    public PlexusInteractivityCredentialsProvider( Prompter prompter )
+    {
+        this.isInteractive = true;
+        this.prompter = prompter;
+    }
+
+    @Override
+    public boolean get( URIish uri, CredentialItem... items ) throws UnsupportedCredentialItem
+    {
+        for ( CredentialItem item : items )
+        {
+            try 
+            {
+                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.InformationalMessage )
+                {
+                    prompter.showMessage( item.getPromptText() );
+                }
+                else
+                {
+                    throw new UnsupportedCredentialItem( uri, item.getClass().toString() );
+                }
+            }
+            catch ( PrompterException e )

Review Comment:
   This cascade ignores the `interactive` flag.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org