-
Notifications
You must be signed in to change notification settings - Fork 49
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Initial JWT builder code and tests #153
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
37 changes: 37 additions & 0 deletions
37
implementation/src/main/java/io/smallrye/jwt/build/Jwt.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,37 @@ | ||
package io.smallrye.jwt.build; | ||
|
||
import io.smallrye.jwt.build.spi.JwtProvider; | ||
|
||
/** | ||
* Factory class for creating {@link JwtClaimsBuilder} objects. | ||
* | ||
* <p> | ||
* The following example shows how to create a {@link JwtClaimsBuilder} to start creating a signed JWT token: | ||
* | ||
* <pre> | ||
* <code> | ||
* JwtClaimsBuilder claims = Jwt.claims(); | ||
* </code> | ||
* </pre> | ||
*/ | ||
public final class Jwt { | ||
|
||
/** | ||
* Creates a new instance of {@link JwtClaimsBuilder} | ||
* | ||
* @return {@link JwtClaimsBuilder} | ||
*/ | ||
public static JwtClaimsBuilder claims() { | ||
return JwtProvider.provider().claims(); | ||
} | ||
|
||
/** | ||
* Creates a new instance of {@link JwtClaimsBuilder} from a JSON resource. | ||
* | ||
* @param jsonLocation JSON resource location | ||
* @return {@link JwtClaimsBuilder} | ||
*/ | ||
public static JwtClaimsBuilder claims(String jsonLocation) { | ||
return JwtProvider.provider().claims(jsonLocation); | ||
} | ||
} |
115 changes: 115 additions & 0 deletions
115
implementation/src/main/java/io/smallrye/jwt/build/JwtClaimsBuilder.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,115 @@ | ||
package io.smallrye.jwt.build; | ||
|
||
import java.util.Collection; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
import javax.json.JsonObject; | ||
|
||
/** | ||
* JWT Claims Builder. | ||
* <p> | ||
* JwtClaimsBuilder implementations should set the 'iat' (issued at time), 'exp' (expiration time) | ||
* and 'jit' (unique token identifier) claims unless they have already been provided. | ||
* <p> | ||
* Note that implementations are not required to be thread-safe. | ||
*/ | ||
public interface JwtClaimsBuilder extends JwtSigner { | ||
|
||
/** | ||
* Set an issuer 'iss' claim | ||
* | ||
* @param issuer the issuer | ||
* @return JwtClaimsBuilder | ||
*/ | ||
JwtClaimsBuilder issuer(String issuer); | ||
|
||
/** | ||
* Set a subject 'sub' claim | ||
* | ||
* @param subject the subject | ||
* @return JwtClaimsBuilder | ||
*/ | ||
JwtClaimsBuilder subject(String subject); | ||
|
||
/** | ||
* Set a upn claim | ||
* | ||
* @param upn the upn | ||
* @return JwtClaimsBuilder | ||
*/ | ||
JwtClaimsBuilder upn(String upn); | ||
|
||
/** | ||
* Set a preferred user name 'preferred_username' claim | ||
* | ||
* @param preferredUserName the preferred user name | ||
* @return JwtClaimsBuilder | ||
*/ | ||
JwtClaimsBuilder preferredUserName(String preferredUserName); | ||
|
||
/** | ||
* Set an issuedAt 'iat' claim | ||
* | ||
* @param issuedAt the issuedAt time in seconds | ||
* @return JwtClaimsBuilder | ||
*/ | ||
JwtClaimsBuilder issuedAt(long issuedAt); | ||
|
||
/** | ||
* Set an expiry 'exp' claim | ||
* | ||
* @param expiredAt the expiry time | ||
* @return JwtClaimsBuilder | ||
*/ | ||
JwtClaimsBuilder expiresAt(long expiredAt); | ||
|
||
/** | ||
* Set a single value 'groups' claim | ||
* | ||
* @param group the groups | ||
* @return JwtClaimsBuilder | ||
*/ | ||
JwtClaimsBuilder groups(String group); | ||
|
||
/** | ||
* Set a multiple value 'groups' claim | ||
* | ||
* @param groups the groups | ||
* @return JwtClaimsBuilder | ||
*/ | ||
JwtClaimsBuilder groups(Set<String> groups); | ||
|
||
/** | ||
* Set a single value audience 'aud' claim | ||
* | ||
* @param audience the audience | ||
* @return JwtClaimsBuilder | ||
*/ | ||
JwtClaimsBuilder audience(String audience); | ||
|
||
/** | ||
* Set a multiple value audience 'aud' claim | ||
* | ||
* @param audiences the audiences | ||
* @return JwtClaimsBuilder | ||
*/ | ||
JwtClaimsBuilder audience(Set<String> audiences); | ||
|
||
/** | ||
* Set a custom claim. Claim value is converted to String unless it is | ||
* an instance of {@link Boolean}, {@link Number}, {@link Collection}, {@link Map} or {@link JsonObject}. | ||
* | ||
* @param name the claim name | ||
* @param value the claim value | ||
* @return JwtClaimsBuilder | ||
*/ | ||
JwtClaimsBuilder claim(String name, Object value); | ||
|
||
/** | ||
* Move to the headers builder | ||
* | ||
* @return JwtHeadersBuilder | ||
*/ | ||
JwtHeadersBuilder headers(); | ||
} |
22 changes: 22 additions & 0 deletions
22
implementation/src/main/java/io/smallrye/jwt/build/JwtException.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,22 @@ | ||
package io.smallrye.jwt.build; | ||
|
||
/** | ||
* JWT Exception | ||
*/ | ||
@SuppressWarnings("serial") | ||
public class JwtException extends RuntimeException { | ||
public JwtException() { | ||
} | ||
|
||
public JwtException(String errorMessage) { | ||
super(errorMessage); | ||
} | ||
|
||
public JwtException(Throwable t) { | ||
super(t); | ||
} | ||
|
||
public JwtException(String errorMessage, Throwable t) { | ||
super(errorMessage, t); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
implementation/src/main/java/io/smallrye/jwt/build/JwtHeadersBuilder.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,29 @@ | ||
package io.smallrye.jwt.build; | ||
|
||
/** | ||
* JWT Headers Builder. | ||
* <p> | ||
* JwtHeadersBuilder implementations should set the 'alg' (signing algorithm) to 'RS256' and 'typ' (token type) to 'JWT' | ||
* unless they have been already set by the users. | ||
* <p> | ||
* Note that the implementations are not required to be thread-safe. | ||
*/ | ||
public interface JwtHeadersBuilder extends JwtSigner { | ||
|
||
/** | ||
* Set a 'kid' key id header | ||
* | ||
* @param keyId the key id | ||
* @return JwtHeadersBuilder | ||
*/ | ||
JwtHeadersBuilder keyId(String keyId); | ||
|
||
/** | ||
* Custom JWT header | ||
* | ||
* @param name the header name | ||
* @param value the header value | ||
* @return JwtHeadersBuilder | ||
*/ | ||
JwtHeadersBuilder header(String name, Object value); | ||
} |
38 changes: 38 additions & 0 deletions
38
implementation/src/main/java/io/smallrye/jwt/build/JwtSigner.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,38 @@ | ||
package io.smallrye.jwt.build; | ||
|
||
import java.security.PrivateKey; | ||
|
||
import javax.crypto.SecretKey; | ||
|
||
/** | ||
* JWT Signer | ||
*/ | ||
public interface JwtSigner { | ||
|
||
/** | ||
* Sign a token using {@link PrivateKey} | ||
* | ||
* @param signingKey the signing key | ||
* @return signed JWT token | ||
* @throws JwtException the exception if the signing operation has failed | ||
*/ | ||
String sign(PrivateKey signingKey) throws JwtException; | ||
|
||
/** | ||
* Sign a token using {@link SecretKey} | ||
* | ||
* @param signingKey the signing key | ||
* @return signed JWT token | ||
* @throws JwtException the exception if the signing operation has failed | ||
*/ | ||
String sign(SecretKey signingKey) throws JwtException; | ||
|
||
/** | ||
* Sign a token using a key loaded from the location set with the "smallrye.jwt.sign.private-key-location" property. | ||
* | ||
* @return signed JWT token | ||
* @throws JwtException the exception if the signing operation has failed | ||
*/ | ||
String sign() throws JwtException; | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do new classes to SmallRye projects require a copyright header or do these rely on a root LICENCE file now?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@darranl on the root one AFAIK
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, root LICENCE file