Skip to content
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
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions implementation/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@
<artifactId>javax.json</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.smallrye.config</groupId>
<artifactId>smallrye-config</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
37 changes: 37 additions & 0 deletions implementation/src/main/java/io/smallrye/jwt/build/Jwt.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package io.smallrye.jwt.build;
Copy link
Contributor

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?

Copy link
Contributor Author

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

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, root LICENCE file


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);
}
}
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();
}
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);
}
}
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 implementation/src/main/java/io/smallrye/jwt/build/JwtSigner.java
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;

}
Loading