Skip to content

Commit

Permalink
Merge pull request #62 from bsquizz/master
Browse files Browse the repository at this point in the history
Add support for Bearer Token Authentication
  • Loading branch information
cashlalala committed Jan 4, 2020
2 parents 4642fc6 + 84344f7 commit 2b067fe
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README_PipelineConfiguration.md
Expand Up @@ -74,6 +74,9 @@ Authentication can be configured globally in the system configuration or set/ove
The following authentication types are available:
- **Token Authentication** The specified user id and Jenkins API token is used.<br>
```auth: TokenAuth(apiToken: '<theApiToken>', userName: '<userName>')```
- **Bearer Token Authentication** The specified token is inserted to a "Authentication: Bearer" header in REST API requests.<br>
This is useful when the Jenkins deployment is fronted by a token authentication mechanism (such as when running on Red Hat OpenShift)<br>
```auth: BearerTokenAuth(token: '<token>')```
- **Credentials Authentication** The specified Jenkins Credentials are used. This can be either user/password or user/API Token.<br>
```auth: CredentialsAuth(credentials: '<credentialId>')```
- **No Authentication** No Authorization header will be sent, independent of the global 'remote host' settings.<br>
Expand Down
@@ -0,0 +1,93 @@
package org.jenkinsci.plugins.ParameterizedRemoteTrigger.auth2;

import java.io.IOException;
import java.net.URLConnection;

import org.jenkinsci.Symbol;

import org.jenkinsci.plugins.ParameterizedRemoteTrigger.BuildContext;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;

import hudson.Extension;
import hudson.model.Item;


public class BearerTokenAuth extends Auth2 {

private static final long serialVersionUID = 3614172320192170597L;

@Extension
public static final Auth2Descriptor DESCRIPTOR = new BearerTokenAuthDescriptor();

private String token;

@DataBoundConstructor
public BearerTokenAuth() {
this.token = null;
}

@DataBoundSetter
public void setToken(String token) {
this.token = token;
}

public String getToken() {
return this.token;
}

@Override
public void setAuthorizationHeader(URLConnection connection, BuildContext context) throws IOException {
connection.setRequestProperty("Authorization", "Bearer: " + getToken());
}

@Override
public String toString() {
return "'" + getDescriptor().getDisplayName() + "'";
}

@Override
public String toString(Item item) {
return toString();
}

@Override
public Auth2Descriptor getDescriptor() {
return DESCRIPTOR;
}

@Symbol("BearerTokenAuth")
public static class BearerTokenAuthDescriptor extends Auth2Descriptor {
@Override
public String getDisplayName() {
return "Bearer Token Authentication";
}
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((token == null) ? 0 : token.hashCode());
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (!this.getClass().isInstance(obj))
return false;
BearerTokenAuth other = (BearerTokenAuth) obj;
if (token == null) {
if (other.token == null) {
return true;
} else {
return false;
}
}
return token.equals(other.token);
}
}
Expand Up @@ -9,6 +9,20 @@

public class Auth2Test {

@Test
public void testBearerTokenAuthCloneBehaviour() throws CloneNotSupportedException {
BearerTokenAuth original = new BearerTokenAuth();
original.setToken("original");
BearerTokenAuth clone = (BearerTokenAuth)original.clone();
verifyEqualsHashCode(original, clone);

//Test changing clone
clone.setToken("changed");
verifyEqualsHashCode(original, clone, false);
assertEquals("original", original.getToken());
assertEquals("changed", clone.getToken());
}

@Test
public void testCredentialsAuthCloneBehaviour() throws CloneNotSupportedException {
CredentialsAuth original = new CredentialsAuth();
Expand Down

0 comments on commit 2b067fe

Please sign in to comment.