Skip to content
This repository was archived by the owner on Aug 3, 2019. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@
import com.cloudbees.plugins.credentials.common.UsernamePasswordCredentials;
import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.Extension;
import io.jenkins.plugins.gitlabserver.client.api.GitLabAuthToken;
import io.jenkins.plugins.gitlabserver.client.api.GitLabAuthUser;
import io.jenkins.plugins.gitlabserver.credentials.PersonalAccessToken;
import jenkins.authentication.tokens.api.AuthenticationTokenException;
import jenkins.authentication.tokens.api.AuthenticationTokenSource;

/**
* Converts {@link PersonalAccessToken} to {@link GitLabAuthToken} authentication.
* Converts {@link UsernamePasswordCredentials} to {@link GitLabAuthUser} authentication.
*/
@Extension
public class GitLabAuthUserSource extends AuthenticationTokenSource<GitLabAuthUser, UsernamePasswordCredentials> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.cloudbees.plugins.credentials.common.StandardCredentials;
import com.cloudbees.plugins.credentials.common.StandardListBoxModel;
import com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials;
import com.cloudbees.plugins.credentials.common.UsernamePasswordCredentials;
import edu.umd.cs.findbugs.annotations.CheckForNull;
import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.Extension;
Expand All @@ -13,9 +14,7 @@
import hudson.security.ACL;
import hudson.util.FormValidation;
import hudson.util.ListBoxModel;
import io.jenkins.plugins.gitlabserver.client.api.GitLabAuth;
import io.jenkins.plugins.gitlabserver.client.api.GitLabAuthToken;
import io.jenkins.plugins.gitlabserver.credentials.PersonalAccessTokenImpl;
import io.jenkins.plugins.gitlabserver.credentials.PersonalAccessToken;
import jenkins.authentication.tokens.api.AuthenticationTokens;
import jenkins.model.Jenkins;
import jenkins.scm.api.SCMName;
Expand Down Expand Up @@ -45,6 +44,9 @@

public class GitLabServer extends AbstractDescribableImpl<GitLabServer> {

/**
* Used as default server URL for the serverUrl field
*/
public static final String GITLAB_SERVER_URL = "https://gitlab.com";
/**
* Used as default token value if no any credentials found by given credentialsId.
Expand Down Expand Up @@ -156,24 +158,20 @@ public GitLabServer(@Nonnull String name, @NonNull String serverUrl, boolean man
}

/**
* Looks up the {@link StandardCredentials} to use for auto-management of hooks.
* Looks up the {@link UsernamePasswordCredentials} to use for auto-management of hooks.
*
* @return the credentials or {@code null}.
*/
@CheckForNull
public StandardCredentials credentials() {
public UsernamePasswordCredentials credentials() {
return StringUtils.isBlank(credentialsId) ? null : CredentialsMatchers.firstOrNull(
lookupCredentials(
StandardCredentials.class,
UsernamePasswordCredentials.class,
Jenkins.getActiveInstance(),
ACL.SYSTEM,
fromUri(serverUrl).build()
),
CredentialsMatchers.allOf(
AuthenticationTokens.matcher(GitLabAuth.class),
fromUri(serverUrl).build()),
CredentialsMatchers.withId(credentialsId)
)
);
);
}

/**
Expand Down Expand Up @@ -256,30 +254,24 @@ public ListBoxModel doFillCredentialsIdItems(@QueryParameter String serverUrl,
Jenkins.getInstance(),
StandardCredentials.class,
fromUri(serverUrl).build(),
credentials -> credentials instanceof PersonalAccessTokenImpl);
credentials -> credentials instanceof PersonalAccessToken);
}

private static String getToken(String serverUrl, String credentialsId) {
String privateToken = UNKNOWN_TOKEN;
Jenkins.getInstance().checkPermission(Jenkins.ADMINISTER);
StandardCredentials creds = CredentialsMatchers.firstOrNull(
PersonalAccessToken credentials = CredentialsMatchers.firstOrNull(
lookupCredentials(
StandardCredentials.class,
PersonalAccessToken.class,
Jenkins.getActiveInstance(),
ACL.SYSTEM,
fromUri(defaultIfBlank(serverUrl, GITLAB_SERVER_URL)).build()
),
CredentialsMatchers.allOf(
AuthenticationTokens.matcher(GitLabAuth.class),
fromUri(defaultIfBlank(serverUrl, GITLAB_SERVER_URL)).build()),
CredentialsMatchers.withId(credentialsId)
)
);
if (creds != null) {
GitLabAuth gitLabAuth = AuthenticationTokens.convert(GitLabAuth.class, creds);
if (gitLabAuth instanceof GitLabAuthToken) {
privateToken = ((GitLabAuthToken) gitLabAuth).getToken();
}
);
if (credentials != null) {
privateToken = credentials.getToken().getPlainText();
}

return privateToken;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import hudson.util.FormValidation;
import hudson.util.ListBoxModel;
import hudson.util.Secret;
import io.jenkins.plugins.gitlabserver.credentials.PersonalAccessToken;
import io.jenkins.plugins.gitlabserver.credentials.PersonalAccessTokenImpl;
import io.jenkins.plugins.gitlabserver.servers.GitLabServer;
import jenkins.model.Jenkins;
Expand Down Expand Up @@ -174,12 +175,12 @@ public FormValidation doCreateTokenByPassword(
* @param token GitLab Personal Access Token
* @param username used to add to description of newly created credentials
*
* @see #saveCredentials(String, PersonalAccessTokenImpl)
* @see #saveCredentials(String, PersonalAccessToken)
*/
private void createCredentials(@Nullable String serverUrl, String token, String username, String tokenName) {
String url = defaultIfBlank(serverUrl, GitLabServer.GITLAB_SERVER_URL);
String description = String.format("Auto Generated by %s server for %s user", url, username);
PersonalAccessTokenImpl credentials = new PersonalAccessTokenImpl(
PersonalAccessToken credentials = new PersonalAccessTokenImpl(
CredentialsScope.GLOBAL,
tokenName,
description,
Expand All @@ -196,7 +197,7 @@ private void createCredentials(@Nullable String serverUrl, String token, String
* @param serverUrl to extract (and create if no any) domain
* @param credentials to save credentials
*/
private void saveCredentials(String serverUrl, final PersonalAccessTokenImpl credentials) {
private void saveCredentials(String serverUrl, final PersonalAccessToken credentials) {
URI serverUri = URI.create(defaultIfBlank(serverUrl, GitLabServer.GITLAB_SERVER_URL));

List<DomainSpecification> specifications = asList(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import org.apache.commons.lang.RandomStringUtils
def f = namespace(FormTagLib)
def c = namespace(CredentialsTagLib)

f.entry(title: _("Name"), field: "name", "description": "A unique name for the connection") {
f.entry(title: _("Name"), field: "name", "description": "A unique name for the server") {
f.textbox(default: String.format("gitlab-%s", RandomStringUtils.randomNumeric(GitLabServer.SHORT_NAME_LENGTH)))
}

Expand Down