io.github.nichetoolkit.rest.worker.jwt.JwtBuilder Maven / Gradle / Ivy
Show all versions of rest-toolkit-utils Show documentation
package io.github.nichetoolkit.rest.worker.jwt;
import io.fusionauth.jwt.domain.JWT;
import io.github.nichetoolkit.rest.util.BeanUtils;
import io.github.nichetoolkit.rest.util.GeneralUtils;
import java.time.ZonedDateTime;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;
/**
* JwtBuilder
* The jwt builder class.
* @author Cyan ([email protected])
* @since Jdk1.8
*/
public class JwtBuilder {
/**
* audience
* {@link java.lang.Object} The audience
field.
* @see java.lang.Object
*/
public Object audience;
/**
* expiration
* {@link java.time.ZonedDateTime} The expiration
field.
* @see java.time.ZonedDateTime
*/
public ZonedDateTime expiration;
/**
* issuedAt
* {@link java.time.ZonedDateTime} The issuedAt
field.
* @see java.time.ZonedDateTime
*/
public ZonedDateTime issuedAt;
/**
* issuer
* {@link java.lang.String} The issuer
field.
* @see java.lang.String
*/
public String issuer;
/**
* notBefore
* {@link java.time.ZonedDateTime} The notBefore
field.
* @see java.time.ZonedDateTime
*/
public ZonedDateTime notBefore;
/**
* otherClaims
* {@link java.util.Map} The otherClaims
field.
* @see java.util.Map
*/
public Map otherClaims = new LinkedHashMap<>();
/**
* subject
* {@link java.lang.String} The subject
field.
* @see java.lang.String
*/
public String subject;
/**
* uniqueId
* {@link java.lang.String} The uniqueId
field.
* @see java.lang.String
*/
public String uniqueId;
/**
* JwtBuilder
* Instantiates a new jwt builder.
*/
public JwtBuilder() {
}
/**
* audience
* The audience method.
* @param audience {@link java.lang.Object} The audience parameter is Object
type.
* @return {@link io.github.nichetoolkit.rest.worker.jwt.JwtBuilder} The audience return object is JwtBuilder
type.
* @see java.lang.Object
*/
public JwtBuilder audience(Object audience) {
this.audience = audience;
return this;
}
/**
* expiration
* The expiration method.
* @param expiration {@link java.time.ZonedDateTime} The expiration parameter is ZonedDateTime
type.
* @return {@link io.github.nichetoolkit.rest.worker.jwt.JwtBuilder} The expiration return object is JwtBuilder
type.
* @see java.time.ZonedDateTime
*/
public JwtBuilder expiration(ZonedDateTime expiration) {
this.expiration = expiration;
return this;
}
/**
* issuedAt
* The issued at method.
* @param issuedAt {@link java.time.ZonedDateTime} The issued at parameter is ZonedDateTime
type.
* @return {@link io.github.nichetoolkit.rest.worker.jwt.JwtBuilder} The issued at return object is JwtBuilder
type.
* @see java.time.ZonedDateTime
*/
public JwtBuilder issuedAt(ZonedDateTime issuedAt) {
this.issuedAt = issuedAt;
return this;
}
/**
* issuer
* The issuer method.
* @param issuer {@link java.lang.String} The issuer parameter is String
type.
* @return {@link io.github.nichetoolkit.rest.worker.jwt.JwtBuilder} The issuer return object is JwtBuilder
type.
* @see java.lang.String
*/
public JwtBuilder issuer(String issuer) {
this.issuer = issuer;
return this;
}
/**
* notBefore
* The not before method.
* @param notBefore {@link java.time.ZonedDateTime} The not before parameter is ZonedDateTime
type.
* @return {@link io.github.nichetoolkit.rest.worker.jwt.JwtBuilder} The not before return object is JwtBuilder
type.
* @see java.time.ZonedDateTime
*/
public JwtBuilder notBefore(ZonedDateTime notBefore) {
this.notBefore = notBefore;
return this;
}
/**
* subject
* The subject method.
* @param subject {@link java.lang.String} The subject parameter is String
type.
* @return {@link io.github.nichetoolkit.rest.worker.jwt.JwtBuilder} The subject return object is JwtBuilder
type.
* @see java.lang.String
*/
public JwtBuilder subject(String subject) {
this.subject = subject;
return this;
}
/**
* uniqueId
* The unique id method.
* @param uniqueId {@link java.lang.String} The unique id parameter is String
type.
* @return {@link io.github.nichetoolkit.rest.worker.jwt.JwtBuilder} The unique id return object is JwtBuilder
type.
* @see java.lang.String
*/
public JwtBuilder uniqueId(String uniqueId) {
this.uniqueId = uniqueId;
return this;
}
/**
* addClaim
* The add claim method.
* @param name {@link java.lang.String} The name parameter is String
type.
* @param value {@link java.lang.Object} The value parameter is Object
type.
* @return {@link io.github.nichetoolkit.rest.worker.jwt.JwtBuilder} The add claim return object is JwtBuilder
type.
* @see java.lang.String
* @see java.lang.Object
*/
public JwtBuilder addClaim(String name, Object value) {
this.otherClaims.putIfAbsent(name,value);
return this;
}
/**
* addClaim
* The add claim method.
* @param claimMap {@link java.util.Map} The claim map parameter is Map
type.
* @return {@link io.github.nichetoolkit.rest.worker.jwt.JwtBuilder} The add claim return object is JwtBuilder
type.
* @see java.util.Map
*/
public JwtBuilder addClaim(Map claimMap) {
this.otherClaims.putAll(claimMap);
return this;
}
/**
* addClaim
* The add claim method.
* @param entries {@link java.util.Collection} The entries parameter is Collection
type.
* @return {@link io.github.nichetoolkit.rest.worker.jwt.JwtBuilder} The add claim return object is JwtBuilder
type.
* @see java.util.Collection
*/
public JwtBuilder addClaim(Collection> entries) {
if (GeneralUtils.isNotEmpty(entries)) {
entries.forEach(entry -> this.otherClaims.putIfAbsent(entry.getKey(),entry.getValue()));
}
return this;
}
/**
* toBean
* The to bean method.
* @return {@link io.fusionauth.jwt.domain.JWT} The to bean return object is JWT
type.
* @see io.fusionauth.jwt.domain.JWT
*/
public JWT toBean() {
JWT jwt = new JWT();
BeanUtils.copyNonnullProperties(this,jwt);
return jwt;
}
/**
* build
* The build method.
* @return {@link io.fusionauth.jwt.domain.JWT} The build return object is JWT
type.
* @see io.fusionauth.jwt.domain.JWT
*/
public JWT build() {
JWT jwt = new JWT().setAudience(this.audience)
.setExpiration(this.expiration)
.setIssuedAt(this.issuedAt)
.setIssuer(this.issuer)
.setNotBefore(this.notBefore)
.setSubject(this.subject)
.setUniqueId(this.uniqueId);
if (GeneralUtils.isNotEmpty(this.otherClaims)) {
this.otherClaims.forEach(jwt::addClaim);
}
return jwt;
}
/**
* builder
* The builder method.
* @return {@link io.github.nichetoolkit.rest.worker.jwt.JwtBuilder} The builder return object is JwtBuilder
type.
*/
public static JwtBuilder builder() {
return new JwtBuilder();
}
}