-
Notifications
You must be signed in to change notification settings - Fork 95
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 #409 from jtnord/pac4j
Replace EOL Google Oauth library
- Loading branch information
Showing
38 changed files
with
1,146 additions
and
2,035 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
70 changes: 70 additions & 0 deletions
70
src/main/java/org/jenkinsci/plugins/oic/AnythingGoesTokenValidator.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,70 @@ | ||
package org.jenkinsci.plugins.oic; | ||
|
||
import com.nimbusds.jose.JWSAlgorithm; | ||
import com.nimbusds.jwt.JWT; | ||
import com.nimbusds.jwt.JWTClaimsSet; | ||
import com.nimbusds.oauth2.sdk.ParseException; | ||
import com.nimbusds.oauth2.sdk.auth.ClientAuthenticationMethod; | ||
import com.nimbusds.oauth2.sdk.id.Issuer; | ||
import com.nimbusds.openid.connect.sdk.Nonce; | ||
import com.nimbusds.openid.connect.sdk.SubjectType; | ||
import com.nimbusds.openid.connect.sdk.claims.IDTokenClaimsSet; | ||
import com.nimbusds.openid.connect.sdk.op.OIDCProviderMetadata; | ||
import java.net.URI; | ||
import java.net.URISyntaxException; | ||
import java.util.List; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
import org.pac4j.core.exception.TechnicalException; | ||
import org.pac4j.oidc.config.OidcConfiguration; | ||
import org.pac4j.oidc.profile.creator.TokenValidator; | ||
|
||
public class AnythingGoesTokenValidator extends TokenValidator { | ||
|
||
public static final Logger LOGGER = Logger.getLogger(AnythingGoesTokenValidator.class.getName()); | ||
|
||
public AnythingGoesTokenValidator() { | ||
super(createFakeOidcConfiguration()); | ||
} | ||
|
||
@Override | ||
public IDTokenClaimsSet validate(final JWT idToken, final Nonce expectedNonce) { | ||
// validation is disabled, so everything is valid. | ||
try { | ||
return new IDTokenClaimsSet(idToken.getJWTClaimsSet()); | ||
} catch (ParseException | java.text.ParseException e) { | ||
LOGGER.log( | ||
Level.WARNING, | ||
"Token validation is disabled, but the token is corrupt and claims will not be represted.", | ||
e); | ||
try { | ||
return new IDTokenClaimsSet(new JWTClaimsSet.Builder().build()); | ||
} catch (ParseException e1) { | ||
throw new TechnicalException("could not create and empty IDTokenClaimsSet"); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Annoyingly the super class needs an OidcConfiguration with some values set, | ||
* which if we are not validating we may not actually have (e.g. jwks_url). | ||
* So we need a configuration with this set just so the validator can say "this is valid". | ||
*/ | ||
private static OidcConfiguration createFakeOidcConfiguration() { | ||
try { | ||
OidcConfiguration config = new OidcConfiguration(); | ||
config.setClientId("ignored"); | ||
config.setSecret("ignored"); | ||
OIDCProviderMetadata providerMetadata = new OIDCProviderMetadata( | ||
new Issuer("http://ignored"), List.of(SubjectType.PUBLIC), new URI("http://ignored.and.invalid./")); | ||
providerMetadata.setIDTokenJWSAlgs(List.of(JWSAlgorithm.HS256)); | ||
config.setProviderMetadata(providerMetadata); | ||
config.setPreferredJwsAlgorithm(JWSAlgorithm.HS256); | ||
config.setClientAuthenticationMethod(ClientAuthenticationMethod.CLIENT_SECRET_BASIC); | ||
return config; | ||
} catch (URISyntaxException e) { | ||
// should never happen the urls we are using are valid | ||
throw new IllegalStateException(e); | ||
} | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
src/main/java/org/jenkinsci/plugins/oic/CustomOidcConfiguration.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,52 @@ | ||
package org.jenkinsci.plugins.oic; | ||
|
||
import com.nimbusds.oauth2.sdk.http.HTTPRequest; | ||
import hudson.ProxyConfiguration; | ||
import java.net.Proxy; | ||
import java.net.http.HttpRequest; | ||
import java.security.KeyManagementException; | ||
import java.security.NoSuchAlgorithmException; | ||
import javax.net.ssl.HostnameVerifier; | ||
import javax.net.ssl.SSLContext; | ||
import jenkins.model.Jenkins; | ||
import jenkins.security.FIPS140; | ||
import org.jenkinsci.plugins.oic.ssl.IgnoringHostNameVerifier; | ||
import org.jenkinsci.plugins.oic.ssl.TLSUtils; | ||
import org.pac4j.oidc.config.OidcConfiguration; | ||
|
||
/** | ||
* An OidcConfiguration that will customize {@link HttpRequest} with the Jenkins proxy, and iff TLS is disabled a lenient {@link HostnameVerifier} and {@link SSLContext}. | ||
*/ | ||
class CustomOidcConfiguration extends OidcConfiguration { | ||
|
||
private final boolean disableTLS; | ||
|
||
CustomOidcConfiguration(boolean disableTLS) { | ||
this.disableTLS = disableTLS; | ||
if (FIPS140.useCompliantAlgorithms() && disableTLS) { | ||
throw new IllegalStateException("Cannot disable TLS validation in FIPS-140 mode"); | ||
} | ||
} | ||
|
||
@Override | ||
public void configureHttpRequest(HTTPRequest request) { | ||
Proxy proxy = null; | ||
Jenkins jenkins = Jenkins.getInstanceOrNull(); | ||
if (jenkins != null) { // unit tests | ||
ProxyConfiguration pc = jenkins.getProxy(); | ||
if (pc != null) { | ||
proxy = pc.createProxy(request.getURL().getHost()); | ||
} | ||
} | ||
request.setProxy(proxy); | ||
if (disableTLS) { | ||
request.setHostnameVerifier(IgnoringHostNameVerifier.INSTANCE); | ||
try { | ||
request.setSSLSocketFactory(TLSUtils.createAnythingGoesSSLSocketFactory()); | ||
} catch (KeyManagementException | NoSuchAlgorithmException e) { | ||
throw new IllegalStateException("could not configure the SSLFactory, this should not be possible", e); | ||
} | ||
} | ||
super.configureHttpRequest(request); | ||
} | ||
} |
33 changes: 0 additions & 33 deletions
33
src/main/java/org/jenkinsci/plugins/oic/JenkinsAwareConnectionFactory.java
This file was deleted.
Oops, something went wrong.
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.