forked from morficus/Parameterized-Remote-Trigger-Plugin
-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #62 from bsquizz/master
Add support for Bearer Token Authentication
- Loading branch information
Showing
3 changed files
with
110 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
src/main/java/org/jenkinsci/plugins/ParameterizedRemoteTrigger/auth2/BearerTokenAuth.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters