@@ -68,7 +68,6 @@ of this software and associated documentation files (the "Software"), to deal
68
68
import org .apache .http .impl .client .CloseableHttpClient ;
69
69
import org .apache .http .impl .client .HttpClients ;
70
70
import org .apache .http .util .EntityUtils ;
71
- import org .jfree .util .Log ;
72
71
import org .kohsuke .args4j .Option ;
73
72
import org .kohsuke .github .GHEmail ;
74
73
import org .kohsuke .github .GHMyself ;
@@ -92,6 +91,7 @@ of this software and associated documentation files (the "Software"), to deal
92
91
import java .util .Arrays ;
93
92
import java .util .HashSet ;
94
93
import java .util .Set ;
94
+ import java .util .logging .Level ;
95
95
import java .util .logging .Logger ;
96
96
import javax .annotation .Nonnull ;
97
97
import javax .annotation .Nullable ;
@@ -382,18 +382,18 @@ public HttpResponse doFinishLogin(StaplerRequest request)
382
382
String expectedState = (String )request .getSession ().getAttribute (STATE_ATTRIBUTE );
383
383
384
384
if (code == null || code .trim ().length () == 0 ) {
385
- Log .info ("doFinishLogin: missing code." );
385
+ LOGGER .info ("doFinishLogin: missing code." );
386
386
return HttpResponses .redirectToContextRoot ();
387
387
}
388
388
389
389
if (state == null ){
390
- Log .info ("doFinishLogin: missing state parameter from Github response." );
390
+ LOGGER .info ("doFinishLogin: missing state parameter from Github response." );
391
391
return HttpResponses .redirectToContextRoot ();
392
392
} else if (expectedState == null ){
393
- Log .info ("doFinishLogin: missing state parameter from user's session." );
393
+ LOGGER .info ("doFinishLogin: missing state parameter from user's session." );
394
394
return HttpResponses .redirectToContextRoot ();
395
395
} else if (!state .equals (expectedState )){
396
- Log .info ("state parameter value [" +state +"] does not match the expected one [" +expectedState +"]" );
396
+ LOGGER .info ("state parameter value [" +state +"] does not match the expected one [" +expectedState +"]" );
397
397
return HttpResponses .redirectToContextRoot ();
398
398
}
399
399
@@ -445,7 +445,7 @@ public HttpResponse doFinishLogin(StaplerRequest request)
445
445
// or modifications in organizations will be not reflected when using API Token, due to that caching
446
446
// SecurityListener.fireLoggedIn(self.getLogin());
447
447
} else {
448
- Log .info ("Github did not return an access token." );
448
+ LOGGER .info ("Github did not return an access token." );
449
449
}
450
450
451
451
if (referer !=null ) return HttpResponses .redirectTo (referer );
@@ -458,7 +458,7 @@ private String getAccessToken(@Nonnull String code) throws IOException {
458
458
try (CloseableHttpClient httpClient = HttpClients .createDefault ()) {
459
459
HttpPost httpost = new HttpPost (githubWebUri
460
460
+ "/login/oauth/access_token?" + "client_id=" + clientID + "&"
461
- + "client_secret=" + clientSecret + "&" + "code=" + code );
461
+ + "client_secret=" + clientSecret . getPlainText () + "&" + "code=" + code );
462
462
HttpHost proxy = getProxy (httpost );
463
463
if (proxy != null ) {
464
464
RequestConfig requestConfig = RequestConfig .custom ().setProxy (proxy ).build ();
@@ -693,17 +693,17 @@ public UserDetails loadUserByUsername(String username)
693
693
694
694
Authentication token = SecurityContextHolder .getContext ().getAuthentication ();
695
695
696
- if ( token == null || ! username . equals ( token . getPrincipal ())) {
697
- if (localUser != null && GithubSecretStorage .contains (localUser )){
696
+ try {
697
+ if (localUser != null && GithubSecretStorage .contains (localUser )) {
698
698
String accessToken = GithubSecretStorage .retrieve (localUser );
699
- try {
700
- token = new GithubAuthenticationToken ( accessToken , getGithubApiUri ());
701
- } catch (IOException e ) {
702
- throw new UserMayOrMayNotExistException ( "Could not connect to GitHub API server, target URL = " + getGithubApiUri (), e );
703
- }
704
- SecurityContextHolder . getContext (). setAuthentication ( token );
705
- } else {
706
- throw new UserMayOrMayNotExistException ( "Could not get auth token." );
699
+ token = new GithubAuthenticationToken ( accessToken , getGithubApiUri ());
700
+ }
701
+ } catch (IOException | UsernameNotFoundException e ) {
702
+ if ( e instanceof IOException ) {
703
+ throw new UserMayOrMayNotExistException ( "Could not connect to GitHub API server, target URL = " + getGithubApiUri (), e );
704
+ } else {
705
+ // user not found so continuing normally re-using the current context holder
706
+ LOGGER . log ( Level . FINE , "Attempted to impersonate " + username + " but token in user property was invalid ." );
707
707
}
708
708
}
709
709
@@ -725,8 +725,9 @@ public UserDetails loadUserByUsername(String username)
725
725
726
726
try {
727
727
GithubOAuthUserDetails userDetails = authToken .getUserDetails (username );
728
- if (userDetails == null )
728
+ if (userDetails == null ) {
729
729
throw new UsernameNotFoundException ("Unknown user: " + username );
730
+ }
730
731
731
732
// Check the username is not an homonym of an organization
732
733
GHOrganization ghOrg = authToken .loadOrganization (username );
0 commit comments