Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[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
1 parent
6602105
commit 7e13146
Showing
7 changed files
with
649 additions
and
3 deletions.
There are no files selected for viewing
66 changes: 66 additions & 0 deletions
66
src/main/java/org/jenkinsci/plugins/GithubAccessTokenProperty.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,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; | ||
} | ||
} | ||
} |
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
62 changes: 62 additions & 0 deletions
62
src/main/java/org/jenkinsci/plugins/GithubSecretStorage.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,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); | ||
} | ||
} | ||
} |
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
Oops, something went wrong.