Skip to content

Commit

Permalink
[JENKINS-34775] Don't cast inconvertible un/pw token
Browse files Browse the repository at this point in the history
Fixes JENKINS-34775.

The loadUserByUsername method expects to be able to get the current
user's token with
`SecurityContextHolder.getContext().getAuthentication()`, and assumes
this method will return a GithubAuthenticationToken. When it returns a
UserPasswordAuthenticationToken instead, a fatal cast was performed.

We now handle the case where the current authentication context contains
a UserPasswordAuthenticationToken (by throwing an exception - so, not
successfully handled, but this prevents the loadUserByUsername failure
bubbling up to become a job failure).
  • Loading branch information
Dominic Scheirlinck committed May 25, 2016
1 parent be21b48 commit d6bc021
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/main/java/org/jenkinsci/plugins/GithubSecurityRealm.java
Expand Up @@ -628,6 +628,7 @@ public DescriptorImpl getDescriptor() {
*
* @param username
* @return
* @throws UserMayOrMayNotExistException
* @throws UsernameNotFoundException
* @throws DataAccessException
*/
Expand All @@ -636,12 +637,20 @@ public UserDetails loadUserByUsername(String username)
throws UsernameNotFoundException, DataAccessException {
GHUser user = null;

GithubAuthenticationToken authToken = (GithubAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
Authentication token = SecurityContextHolder.getContext().getAuthentication();

if (authToken == null) {
if (token == null) {
throw new UserMayOrMayNotExistException("Could not get auth token.");
}

GithubAuthenticationToken authToken;

if (token instanceof GithubAuthenticationToken) {
authToken = (GithubAuthenticationToken) token;
} else {
throw new UserMayOrMayNotExistException("Unexpected authentication type: " + token);
}

try {
GithubOAuthUserDetails userDetails = authToken.getUserDetails(username);
if (userDetails == null)
Expand Down

0 comments on commit d6bc021

Please sign in to comment.