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();
}
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"));
}
Aggregations