Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
[JENKINS-47113] Populate the authorities after a successful authentic…
…ation to Github (#87) This change stores a GitHub token in a user property for reuse by other authorization method. Specifically, the token in which the user authorized for Jenkins to collect consenting through OAuth.
- Loading branch information
Showing
with
649 additions
and 3 deletions.
- +66 −0 src/main/java/org/jenkinsci/plugins/GithubAccessTokenProperty.java
- +3 −1 src/main/java/org/jenkinsci/plugins/GithubAuthenticationToken.java
- +62 −0 src/main/java/org/jenkinsci/plugins/GithubSecretStorage.java
- +28 −2 src/main/java/org/jenkinsci/plugins/GithubSecurityRealm.java
- +388 −0 src/test/java/org/jenkinsci/plugins/GithubAccessTokenPropertyTest.java
- +101 −0 src/test/java/org/jenkinsci/plugins/GithubSecretStorageTest.java
- +1 −0 src/test/java/org/jenkinsci/plugins/api/GihubAPITest.java
@@ -0,0 +1,66 @@ | ||
/* | ||
* The MIT License | ||
* | ||
* Copyright (c) 2017, CloudBees, Inc. | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
package org.jenkinsci.plugins; | ||
|
||
import hudson.Extension; | ||
import hudson.model.User; | ||
import hudson.model.UserProperty; | ||
import hudson.model.UserPropertyDescriptor; | ||
import hudson.util.Secret; | ||
import org.jenkinsci.Symbol; | ||
|
||
import javax.annotation.Nonnull; | ||
|
||
/** | ||
* Remembers the access token used to connect to the Github server | ||
* | ||
* @since TODO | ||
*/ | ||
public class GithubAccessTokenProperty extends UserProperty { | ||
private final Secret accessToken; | ||
|
||
public GithubAccessTokenProperty(String accessToken) { | ||
this.accessToken = Secret.fromString(accessToken); | ||
} | ||
|
||
public @Nonnull Secret getAccessToken() { | ||
return accessToken; | ||
} | ||
|
||
@Extension | ||
@Symbol("githubAccessToken") | ||
public static final class DescriptorImpl extends UserPropertyDescriptor { | ||
@Override | ||
public boolean isEnabled() { | ||
// does not show elements in /<user>/configure/ | ||
return false; | ||
} | ||
|
||
@Override | ||
public UserProperty newInstance(User user) { | ||
// no default property | ||
return null; | ||
} | ||
} | ||
} |
@@ -0,0 +1,62 @@ | ||
/* | ||
* The MIT License | ||
* | ||
* Copyright (c) 2017, CloudBees, Inc. | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
package org.jenkinsci.plugins; | ||
|
||
import hudson.model.User; | ||
import org.jfree.util.Log; | ||
|
||
import javax.annotation.CheckForNull; | ||
import javax.annotation.Nonnull; | ||
import java.io.IOException; | ||
|
||
public class GithubSecretStorage { | ||
|
||
private GithubSecretStorage(){ | ||
// no accessible constructor | ||
} | ||
|
||
public static boolean contains(@Nonnull User user) { | ||
return user.getProperty(GithubAccessTokenProperty.class) != null; | ||
} | ||
|
||
public static @CheckForNull String retrieve(@Nonnull User user) { | ||
GithubAccessTokenProperty property = user.getProperty(GithubAccessTokenProperty.class); | ||
if (property == null) { | ||
Log.debug("Cache miss for username: " + user.getId()); | ||
return null; | ||
} else { | ||
Log.debug("Token retrieved using cache for username: " + user.getId()); | ||
return property.getAccessToken().getPlainText(); | ||
} | ||
} | ||
|
||
public static void put(@Nonnull User user, @Nonnull String accessToken) { | ||
Log.debug("Populating the cache for username: " + user.getId()); | ||
try { | ||
user.addProperty(new GithubAccessTokenProperty(accessToken)); | ||
} catch (IOException e) { | ||
Log.warn("Received an exception when trying to add the GitHub access token to the user: " + user.getId(), e); | ||
} | ||
} | ||
} |
Oops, something went wrong.