Merge pull request #17 from CodeCraftersCN/dev/v1.1.1

Upgrade to v1.1.1
This commit is contained in:
Theodore Hills
2023-10-11 04:05:30 +08:00
committed by GitHub
19 changed files with 141 additions and 79 deletions
+1 -1
View File
@@ -23,7 +23,7 @@
<parent>
<groupId>cn.org.codecrafters</groupId>
<artifactId>jdevkit</artifactId>
<version>1.1.0</version>
<version>1.1.1</version>
</parent>
<artifactId>devkit-core</artifactId>
+1 -1
View File
@@ -6,7 +6,7 @@
<parent>
<groupId>cn.org.codecrafters</groupId>
<artifactId>jdevkit</artifactId>
<version>1.1.0</version>
<version>1.1.1</version>
</parent>
<artifactId>devkit-utils</artifactId>
+1 -1
View File
@@ -23,7 +23,7 @@
<parent>
<groupId>cn.org.codecrafters</groupId>
<artifactId>jdevkit</artifactId>
<version>1.1.0</version>
<version>1.1.1</version>
</parent>
<artifactId>guid</artifactId>
+1 -1
View File
@@ -29,7 +29,7 @@
<groupId>cn.org.codecrafters</groupId>
<artifactId>jdevkit</artifactId>
<version>1.1.0</version>
<version>1.1.1</version>
<inceptionYear>2023</inceptionYear>
<packaging>pom</packaging>
+1 -1
View File
@@ -23,7 +23,7 @@
<parent>
<groupId>cn.org.codecrafters</groupId>
<artifactId>jdevkit</artifactId>
<version>1.1.0</version>
<version>1.1.1</version>
</parent>
<artifactId>property-guard-spring-boot-starter</artifactId>
+1 -1
View File
@@ -23,7 +23,7 @@
<parent>
<groupId>cn.org.codecrafters</groupId>
<artifactId>jdevkit</artifactId>
<version>1.1.0</version>
<version>1.1.1</version>
</parent>
<artifactId>simple-jwt-authzero</artifactId>
@@ -45,11 +45,6 @@ import java.util.*;
* resolver provides functionality to create, extract, verify, and renew JWT
* tokens using various algorithms and custom payload data.
* <p>
* <b>Dependencies:</b>
* This implementation relies on the {@code com.auth0:java-jwt} library. Please
* ensure you have added this library as a dependency to your project before
* using this resolver.
* <p>
* <b>Usage:</b>
* To use the {@code AuthzeroTokenResolver}, first, create an instance of this
* class:
@@ -87,7 +82,7 @@ import java.util.*;
* correctly configured in your project's dependencies.
*
* @author Zihlu Wang
* @version 1.1.0
* @version 1.1.1
* @see GuidCreator
* @see Algorithm
* @see JWTVerifier
@@ -279,7 +274,7 @@ public class AuthzeroTokenResolver implements TokenResolver<DecodedJWT> {
builder.withClaim(name, v);
} else {
log.warn("""
Unable to determine the type of field {}, converting it to a string now.""", name);
Unable to determine the type of field {}, we will handle it as a String.""", name);
builder.withClaim(name, value.toString());
}
} else {
@@ -35,19 +35,20 @@ import java.util.function.Function;
* the {@link AuthzeroTokenResolver}.
* <p>
* This configuration is used to establish the mapping between the standard
* {@link TokenAlgorithm} defined within the {@link AuthzeroTokenResolver}
* facade and the specific algorithms used by the {@code com.auth0:java-jwt}
* library, which is the underlying library used by {@link
* AuthzeroTokenResolver} to handle JSON Web Tokens (JWTs).
* {@link TokenAlgorithm} defined in the
* {@code cn.org.codecrafters:simple-jwt-facade} and the specific algorithms
* used by the {@code com.auth0:java-jwt} library, which is the underlying
* library used by {@link AuthzeroTokenResolver} to handle JSON Web Tokens
* (JWTs).
* <p>
* <b>Algorithm Mapping:</b>
* The {@code AuthzeroTokenResolverConfig} allows specifying the relationship
* The {@code AuthzeroTokenResolverConfig} allows specifying the relationships
* between the standard {@link TokenAlgorithm} instances supported by
* {@link AuthzeroTokenResolver} and the corresponding algorithms used by the
* {@code com.auth0:java-jwt} library. The mapping is achieved using a Map,
* where the keys are the standard TokenAlgorithm instances, and the values
* represent the algorithm functions used by Auth0 Java JWT library for each
* corresponding key.
* where the keys are the standard {@link TokenAlgorithm} instances, and the
* values represent the algorithm functions used by {@code com.auth0:java-jwt}
* library for each corresponding key.
* <p>
* <b>Note:</b>
* The provided algorithm mapping should be consistent with the actual
@@ -56,7 +57,7 @@ import java.util.function.Function;
* validation and processing within the {@link AuthzeroTokenResolver}.
*
* @author Zihlu Wang
* @version 1.1.0
* @version 1.1.1
* @since 1.0.0
*/
public final class AuthzeroTokenResolverConfig implements TokenResolverConfig<Function<String, Algorithm>> {
+1 -1
View File
@@ -23,7 +23,7 @@
<parent>
<groupId>cn.org.codecrafters</groupId>
<artifactId>jdevkit</artifactId>
<version>1.1.0</version>
<version>1.1.1</version>
</parent>
<artifactId>simple-jwt-facade</artifactId>
@@ -21,8 +21,8 @@ import cn.org.codecrafters.simplejwt.TokenResolver;
import cn.org.codecrafters.simplejwt.constants.TokenAlgorithm;
/**
* The {@code TokenResolverConfig} provides a mechanism to configure a
* {@link TokenResolver} with algorithm functions.
* The {@code TokenResolverConfig} provides a mechanism to configure an
* implementation of {@link TokenResolver} with algorithm functions.
* <p>
* This generic interface is used to define the configuration details for a
* {@link TokenResolver} that utilizes algorithm functions. The interface
+1 -1
View File
@@ -23,7 +23,7 @@
<parent>
<groupId>cn.org.codecrafters</groupId>
<artifactId>jdevkit</artifactId>
<version>1.1.0</version>
<version>1.1.1</version>
</parent>
<artifactId>simple-jwt-jjwt</artifactId>
@@ -40,6 +40,7 @@ import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;
/**
@@ -49,11 +50,6 @@ import java.util.UUID;
* This resolver provides functionality to create, extract, verify, and renew
* JWT tokens using various algorithms and custom payload data.
* <p>
* <b>Dependencies:</b>
* This implementation relies on the {@code io.jsonwebtoken:jjwt} library. Please
* ensure you have added this library as a dependency to your project before
* using this resolver.
* <p>
* <b>Usage:</b>
* To use the {@code JjwtTokenResolver}, first, create an instance of this
* class:
@@ -113,7 +109,7 @@ public class JjwtTokenResolver implements TokenResolver<Jws<Claims>> {
private final JjwtTokenResolverConfig config = JjwtTokenResolverConfig.getInstance();
public JjwtTokenResolver(GuidCreator<?> jtiCreator, TokenAlgorithm algorithm, String issuer, String secret) {
if (secret == null || secret.isBlank()) {
if (Objects.isNull(secret) || secret.isBlank()) {
throw new IllegalArgumentException("A secret is required to build a JSON Web Token.");
}
@@ -209,8 +205,7 @@ public class JjwtTokenResolver implements TokenResolver<Jws<Claims>> {
*/
@Override
public String createToken(Duration expireAfter, String audience, String subject) {
var now = LocalDateTime.now();
return buildToken(expireAfter, audience, subject, now, null);
return buildToken(expireAfter, audience, subject, LocalDateTime.now(), null);
}
/**
@@ -225,8 +220,7 @@ public class JjwtTokenResolver implements TokenResolver<Jws<Claims>> {
*/
@Override
public String createToken(Duration expireAfter, String audience, String subject, Map<String, Object> payload) {
var now = LocalDateTime.now();
return buildToken(expireAfter, audience, subject, now, payload);
return buildToken(expireAfter, audience, subject, LocalDateTime.now(), payload);
}
/**
@@ -244,10 +238,9 @@ public class JjwtTokenResolver implements TokenResolver<Jws<Claims>> {
*/
@Override
public <T extends TokenPayload> String createToken(Duration expireAfter, String audience, String subject, T payload) {
var now = LocalDateTime.now();
try {
var claims = MapUtil.objectToMap(payload);
return buildToken(expireAfter, audience, subject, now, claims);
return buildToken(expireAfter, audience, subject, LocalDateTime.now(), claims);
} catch (IllegalAccessException e) {
log.error("An error occurs while accessing the fields of the object");
}
@@ -28,19 +28,20 @@ import java.util.HashMap;
import java.util.Map;
/**
* The {@code JjwtTokenResolverConfig} class provides the configuration for the
* {@link JjwtTokenResolver}.
* The {@code JjwtTokenResolverConfig} class provides the configuration for
* the {@link JjwtTokenResolver}.
* <p>
* This configuration class is used to establish the mapping between the
* standard {@link TokenAlgorithm} defined within the
* {@code JjwtTokenResolverConfig} and the specific algorithms used by the
* {@code io.jsonwebtoken:jjwt} library, which is the underlying library used
* by {@code JjwtTokenResolver} to handle JSON Web Tokens (JWTs).
* This configuration is used to establish the mapping between the standard
* {@link TokenAlgorithm} defined in the
* {@code cn.org.codecrafters:simple-jwt-facade} and the specific algorithms
* used by the {@code io.jsonwebtoken:jjwt} library, which is the underlying
* library used by {@link JjwtTokenResolver} to handle JSON Web Tokens
* (JWTs).
* <p>
* <b>Algorithm Mapping:</b>
* The {@code JjwtTokenResolverConfig} allows specifying the relationship
* between the standard {@link TokenAlgorithm} instances supported by {@link
* JjwtTokenResolver} and the corresponding algorithms used by the
* The {@code JjwtTokenResolverConfig} allows specifying the relationships
* between the standard {@link TokenAlgorithm} instances supported by
* {@link JjwtTokenResolver} and the corresponding algorithms used by the
* {@code io.jsonwebtoken:jjwt} library. The mapping is achieved using a Map,
* where the keys are the standard {@link TokenAlgorithm} instances, and the
* values represent the algorithm functions used by
@@ -53,7 +54,7 @@ import java.util.Map;
* validation and processing within the {@link JjwtTokenResolver}.
*
* @author Zihlu Wang
* @version 1.1.0
* @version 1.1.1
* @since 1.0.0
*/
public final class JjwtTokenResolverConfig implements TokenResolverConfig<SignatureAlgorithm> {
+1 -1
View File
@@ -23,7 +23,7 @@
<parent>
<groupId>cn.org.codecrafters</groupId>
<artifactId>jdevkit</artifactId>
<version>1.1.0</version>
<version>1.1.1</version>
</parent>
<artifactId>simple-jwt-spring-boot-starter</artifactId>
+1 -1
View File
@@ -23,7 +23,7 @@
<parent>
<groupId>cn.org.codecrafters</groupId>
<artifactId>jdevkit</artifactId>
<version>1.1.0</version>
<version>1.1.1</version>
</parent>
<artifactId>webcal</artifactId>
@@ -19,6 +19,7 @@ package cn.org.codecrafters.webcal;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
/**
* {@code WebCalendar} class represents a web calendar in iCalendar format.
@@ -189,18 +190,22 @@ public final class WebCalendar {
* @return the resolved iCalendar string
*/
public String resolve() {
var events = new StringBuilder();
var eventBuilder = new StringBuilder();
if (!nodes.isEmpty()) {
nodes.forEach(item ->
events.append(item.setDomainName(domainName)
.resolve()));
for (var node : nodes) {
if (Objects.isNull(node.getDomainName()) || node.getDomainName().isBlank()) {
node.setDomainName(this.domainName);
}
eventBuilder.append(node.resolve());
}
}
return "BEGIN:" + TAG + "\n" +
"PRODID:-//" + companyName + "//" + productName + "//EN\n" +
"VERSION:" + version + "\n" +
"X-WR-CALNAME:" + name + "\n" +
events + "\n" +
eventBuilder + "\n" +
"END:" + TAG;
}
@@ -18,10 +18,12 @@
package cn.org.codecrafters.webcal;
import cn.org.codecrafters.webcal.config.Classification;
import cn.org.codecrafters.webcal.config.Formatter;
import java.text.MessageFormat;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.ZoneId;
import java.util.Arrays;
import java.util.Collection;
import java.util.Optional;
@@ -257,30 +259,34 @@ public final class WebCalendarEvent extends WebCalendarNode {
*/
@Override
public String resolve() {
return "\nBEGIN:" + TAG + "\n" +
"UID:" + Optional.ofNullable(uid).orElse(UUID.randomUUID().toString()) + "@" + domainName + "\n" +
Optional.ofNullable(summary).map((item) -> "SUMMARY:" + item + "\n").orElse("") +
"DTSTART" + Optional.ofNullable(timezone).map(item -> ";TZID=" + item).orElse("") + ":" + start.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME) + "\n" +
var now = LocalDateTime.now().atZone(ZoneId.systemDefault());
return MessageFormat.format("""
BEGIN:{0}
UID:{1}
DTSTAMP:{2}
DTSTART:{3}
DURATION:PT{6}S
{4}{5}{7}{8}{9}{10}{11}{12}
END:{0}""",
TAG, // 0 - tag
Optional.ofNullable(uid).orElse(UUID.randomUUID().toString()) + "@" + domainName, // 1 - uid
now.format(Formatter.getUtcDatetimeFormatter()), // 2 - dtstamp
start.atZone(ZoneId.systemDefault()).format(Formatter.getUtcDatetimeFormatter()), // 3 - start time
Optional.ofNullable(summary).map((item) -> "\nSUMMARY:" + item).orElse(""), // 4 - summary
Optional.ofNullable(categories)
.map((item) -> {
if (!item.isEmpty()) {
return "CATEGORIES:" + resolveCategories() + "\n";
}
return null;
}).orElse("") +
.map((item) -> !item.isEmpty() ? "\nCATEGORIES:" + resolveCategories() : null).orElse(""), // 5 - categories
Optional.ofNullable(duration)
.map((item) -> "DURATION:PT" + item.getSeconds() + "S\n").orElse("") +
Optional.ofNullable(end)
.map((item) -> "DTEND" + Optional.ofNullable(timezone).map(tz -> ";TZID=" + tz).orElse("") + ":" +
end.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME) + "\n").orElse("") +
Optional.ofNullable(classification)
.map((item) -> "CLASS:" + item.name() + "\n").orElse("") +
Optional.ofNullable(comment).map((item) -> "COMMENT:" + item + "\n").orElse("") +
Optional.ofNullable(description).map((item) -> "DESCRIPTION:" + item + "\n").orElse("") +
Optional.ofNullable(location).map((item) -> "LOCATION:" + item + "\n").orElse("") +
Optional.ofNullable(percentComplete).map((item) -> "PERCENT-COMPLETE:" + item + "\n").orElse("") +
Optional.ofNullable(priority).map((item) -> "PRIORITY:" + item + "\n").orElse("") +
"END:" + TAG + "\n";
.map((_duration) -> String.valueOf(_duration.getSeconds()))
.orElse(Optional.ofNullable(end)
.map((_end) -> String.valueOf(Duration.between(_end, start).getSeconds()))
.orElse("0")), // 6 - duration
Optional.ofNullable(classification).map((_classification) -> "\nCLASS:" + _classification + "\n").orElse(""), /* 7 - classification */
Optional.ofNullable(comment).map((_comment) -> "\nCOMMENT:" + _comment + "\n").orElse(""), /* 8 - comment */
Optional.ofNullable(location).map((_location) -> "\nLOCATION:" + _location).orElse("") /* 9 - location */,
Optional.ofNullable(percentComplete).map((_percentComplete) -> "\nPERCENT-COMPLETE:" + _percentComplete).orElse("") /* 10 = percentComplete */,
Optional.ofNullable(description).map((_description) -> "\nDESCRIPTION:" + _description).orElse("") /* 11 - description */,
Optional.ofNullable(priority).map((_priority) -> "\nPRIORITY:" + _priority).orElse("") /* 12 - priority */
);
}
}
@@ -90,6 +90,15 @@ public abstract sealed class WebCalendarNode
return this;
}
/**
* Get the domain name.
*
* @return the domain name of this event
*/
public String getDomainName() {
return this.domainName;
}
/**
* Resolve the list of categories into a comma-separated string.
*
@@ -0,0 +1,52 @@
/*
* Copyright (C) 2023 CodeCraftersCN.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cn.org.codecrafters.webcal.config;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Objects;
/**
* DatetimeFormatters
*
* @author Zihlu Wang
* @since 21 Sept, 2023
*/
public final class Formatter {
public static DateTimeFormatter getUtcDatetimeFormatter() {
if (Objects.isNull(utcDateTimeFormatter)) {
utcDateTimeFormatter = DateTimeFormatter.ofPattern("yyyyMMdd'T'HHmmss'Z'").withZone(ZoneOffset.UTC);
}
return utcDateTimeFormatter;
}
// public static DateTimeFormatter getLocalDatetimeFormatter() {
// if (Objects.isNull(localDatetimeFormatter)) {
// localDatetimeFormatter = DateTimeFormatter.ofPattern("yyyyMMdd'T'HHmmss");
// }
//
// return localDatetimeFormatter;
// }
private static DateTimeFormatter utcDateTimeFormatter;
// private static DateTimeFormatter localDatetimeFormatter;
}