Search in sources :

Example 1 with PersonalAccessToken

use of org.jenkinsci.plugin.gitea.credentials.PersonalAccessToken in project gitea-plugin by jenkinsci.

the class GiteaSCMBuilder method checkoutUriTemplate.

/**
 * Returns a {@link UriTemplate} for checkout according to credentials configuration.
 * Expects the parameters {@code owner} and {@code repository} to be populated before expansion.
 *
 * @param context       the context within which to resolve the credentials.
 * @param serverUrl     the server url
 * @param sshRemote     any valid SSH remote URL for the server.
 * @param credentialsId the credentials.
 * @return a {@link UriTemplate}
 */
public static UriTemplate checkoutUriTemplate(@CheckForNull Item context, @NonNull String serverUrl, @CheckForNull String sshRemote, @CheckForNull String credentialsId) {
    if (credentialsId != null && sshRemote != null) {
        URIRequirementBuilder builder = URIRequirementBuilder.create();
        URI serverUri = URI.create(serverUrl);
        if (serverUri.getHost() != null) {
            builder.withHostname(serverUri.getHost());
        }
        StandardCredentials credentials = CredentialsMatchers.firstOrNull(CredentialsProvider.lookupCredentials(StandardCredentials.class, context, context instanceof Queue.Task ? Tasks.getDefaultAuthenticationOf((Queue.Task) context) : ACL.SYSTEM, builder.build()), CredentialsMatchers.allOf(CredentialsMatchers.withId(credentialsId), CredentialsMatchers.instanceOf(StandardCredentials.class)));
        if (credentials instanceof SSHUserPrivateKey) {
            int atIndex = sshRemote.indexOf('@');
            int colonIndex = sshRemote.indexOf(':');
            if (atIndex != -1 && colonIndex != -1 && atIndex < colonIndex) {
                // this is an scp style url, we will translate to ssh style
                return UriTemplate.buildFromTemplate("ssh://" + sshRemote.substring(0, colonIndex)).path(UriTemplateBuilder.var("owner")).path(UriTemplateBuilder.var("repository")).literal(".git").build();
            }
            URI sshUri = URI.create(sshRemote);
            return UriTemplate.buildFromTemplate("ssh://git@" + sshUri.getHost() + (sshUri.getPort() != 22 ? ":" + sshUri.getPort() : "")).path(UriTemplateBuilder.var("owner")).path(UriTemplateBuilder.var("repository")).literal(".git").build();
        }
        if (credentials instanceof PersonalAccessToken) {
            try {
                // TODO is there a way we can get git plugin to redact the secret?
                URI tokenUri = new URI(serverUri.getScheme(), ((PersonalAccessToken) credentials).getToken().getPlainText(), serverUri.getHost(), serverUri.getPort(), serverUri.getPath(), serverUri.getQuery(), serverUri.getFragment());
                return UriTemplate.buildFromTemplate(tokenUri.toASCIIString()).path(UriTemplateBuilder.var("owner")).path(UriTemplateBuilder.var("repository")).literal(".git").build();
            } catch (URISyntaxException e) {
            // ok we are at the end of the road
            }
        }
    }
    return UriTemplate.buildFromTemplate(serverUrl).path(UriTemplateBuilder.var("owner")).path(UriTemplateBuilder.var("repository")).literal(".git").build();
}
Also used : PersonalAccessToken(org.jenkinsci.plugin.gitea.credentials.PersonalAccessToken) URIRequirementBuilder(com.cloudbees.plugins.credentials.domains.URIRequirementBuilder) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) Queue(hudson.model.Queue) StandardCredentials(com.cloudbees.plugins.credentials.common.StandardCredentials) SSHUserPrivateKey(com.cloudbees.jenkins.plugins.sshcredentials.SSHUserPrivateKey)

Example 2 with PersonalAccessToken

use of org.jenkinsci.plugin.gitea.credentials.PersonalAccessToken in project gitea-plugin by jenkinsci.

the class GiteaAuthSourceTest method given__tokenCredential__when__convert__then__tokenAuth.

@Test
public void given__tokenCredential__when__convert__then__tokenAuth() throws Exception {
    // we use a mock to ensure that java.lang.reflect.Proxy implementations of the credential interface work
    PersonalAccessToken credential = Mockito.mock(PersonalAccessToken.class);
    Mockito.when(credential.getToken()).thenReturn(Secret.fromString("b5bc10f13665362bd61de931c731e3c74187acc4"));
    GiteaAuth auth = AuthenticationTokens.convert(GiteaAuth.class, credential);
    assertThat(auth, instanceOf(GiteaAuthToken.class));
    assertThat(((GiteaAuthToken) auth).getToken(), is("b5bc10f13665362bd61de931c731e3c74187acc4"));
}
Also used : GiteaAuthToken(org.jenkinsci.plugin.gitea.client.api.GiteaAuthToken) PersonalAccessToken(org.jenkinsci.plugin.gitea.credentials.PersonalAccessToken) GiteaAuth(org.jenkinsci.plugin.gitea.client.api.GiteaAuth) Test(org.junit.Test)

Aggregations

PersonalAccessToken (org.jenkinsci.plugin.gitea.credentials.PersonalAccessToken)2 SSHUserPrivateKey (com.cloudbees.jenkins.plugins.sshcredentials.SSHUserPrivateKey)1 StandardCredentials (com.cloudbees.plugins.credentials.common.StandardCredentials)1 URIRequirementBuilder (com.cloudbees.plugins.credentials.domains.URIRequirementBuilder)1 Queue (hudson.model.Queue)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 GiteaAuth (org.jenkinsci.plugin.gitea.client.api.GiteaAuth)1 GiteaAuthToken (org.jenkinsci.plugin.gitea.client.api.GiteaAuthToken)1 Test (org.junit.Test)1