-
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 #297 from michael-doubez/jws-signing-verification
Add JWKS parameters for verifying web token signatures
- Loading branch information
Showing
13 changed files
with
474 additions
and
26 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# Text by default | ||
* text=auto | ||
|
||
# Images | ||
*.jpg binary | ||
*.png binary |
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
90 changes: 90 additions & 0 deletions
90
src/main/java/org/jenkinsci/plugins/oic/OicJsonWebTokenVerifier.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,90 @@ | ||
/* | ||
* The MIT License | ||
* | ||
* Copyright (c) 2024 JenkinsCI oic-auth-plugin developers | ||
* | ||
* 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.oic; | ||
|
||
import com.google.api.client.auth.openidconnect.IdToken; | ||
import com.google.api.client.auth.openidconnect.IdTokenVerifier; | ||
import com.google.api.client.json.webtoken.JsonWebSignature; | ||
import hudson.Util; | ||
import java.io.IOException; | ||
|
||
/** | ||
* Extend IdTokenVerifier to verify UserInfo webtoken | ||
*/ | ||
public class OicJsonWebTokenVerifier extends IdTokenVerifier { | ||
|
||
/** Bypass Signature verification if no JWKS url configured */ | ||
private final boolean hasNoJwksServerUrl; | ||
|
||
/** Payload indicating userInfo */ | ||
private static final IdToken.Payload NO_PAYLOAD = new IdToken.Payload(); | ||
|
||
/** | ||
* Default verifier | ||
*/ | ||
public OicJsonWebTokenVerifier() { | ||
super(); | ||
hasNoJwksServerUrl = true; | ||
} | ||
|
||
/** | ||
* Verifier with custom builder | ||
*/ | ||
public OicJsonWebTokenVerifier(String jwksServerUrl, IdTokenVerifier.Builder builder) { | ||
super(builder.setCertificatesLocation(jwksServerUrl)); | ||
hasNoJwksServerUrl = (Util.fixEmptyAndTrim(jwksServerUrl) == null); | ||
} | ||
|
||
/** Verify real idtoken */ | ||
public boolean verifyIdToken(IdToken idToken) throws IOException { | ||
if (hasNoJwksServerUrl) { | ||
/* avoid Google's certificate fallback mechanism */ | ||
return super.verifyPayload(idToken); | ||
} | ||
return verifyOrThrow(idToken); | ||
} | ||
|
||
/** Verify userinfo jwt token */ | ||
public boolean verifyUserInfo(JsonWebSignature userinfo) throws IOException { | ||
if (hasNoJwksServerUrl) { | ||
/* avoid Google's certificate fallback mechanism */ | ||
return true; | ||
} | ||
IdToken idToken = new IdToken( | ||
userinfo.getHeader(), | ||
NO_PAYLOAD, /* bypass verification of payload */ | ||
userinfo.getSignatureBytes(), | ||
userinfo.getSignedContentBytes()); | ||
return verifyOrThrow(idToken); | ||
} | ||
|
||
/** hack: verify payload only if idtoken is not userinfo */ | ||
@Override | ||
protected boolean verifyPayload(IdToken idToken) { | ||
if (idToken.getPayload() == NO_PAYLOAD) { | ||
return true; | ||
} | ||
return super.verifyPayload(idToken); | ||
} | ||
} |
Oops, something went wrong.