diff --git a/devkit-core/src/main/java/cn/org/codecrafters/devkit/core/exceptions/package-info.java b/devkit-core/src/main/java/cn/org/codecrafters/devkit/core/exceptions/package-info.java index 42a5c6d..826c497 100644 --- a/devkit-core/src/main/java/cn/org/codecrafters/devkit/core/exceptions/package-info.java +++ b/devkit-core/src/main/java/cn/org/codecrafters/devkit/core/exceptions/package-info.java @@ -19,6 +19,6 @@ * Commonly used exceptions will be used in JDevKit. * * @author Zihlu Wang - * @since 29 Jul 2023 + * @since 1.0.0 */ package cn.org.codecrafters.devkit.core.exceptions; \ No newline at end of file diff --git a/devkit-core/src/main/java/cn/org/codecrafters/devkit/core/package-info.java b/devkit-core/src/main/java/cn/org/codecrafters/devkit/core/package-info.java index 873da84..e073004 100644 --- a/devkit-core/src/main/java/cn/org/codecrafters/devkit/core/package-info.java +++ b/devkit-core/src/main/java/cn/org/codecrafters/devkit/core/package-info.java @@ -16,16 +16,20 @@ */ /** + *

* This package is a part of JDevKit, an open-source Java Development Kit that * provides a set of convenient tools to streamline code development and * enhance productivity. This package serves as the core package containing * common exceptions that are used throughout the entireJDevKit project. + *

*

* JDevKit is designed to be modular, and other specific feature modules within * the library may rely on these exceptions from the core package. + *

*

* For more information and the latest version of JDevKit, please visit our * website codecrafters.org.cn. + *

*

*

Contact

* + *

* * @since 1.0.0 */ diff --git a/devkit-core/src/main/java/cn/org/codecrafters/devkit/package-info.java b/devkit-core/src/main/java/cn/org/codecrafters/devkit/package-info.java index 5914285..c8c76e1 100644 --- a/devkit-core/src/main/java/cn/org/codecrafters/devkit/package-info.java +++ b/devkit-core/src/main/java/cn/org/codecrafters/devkit/package-info.java @@ -22,9 +22,11 @@ * that provides a set of convenient tools to streamline code development and * enhance productivity. This package serves as the root package for several * modules, containing devkit-core, guid and dev-utils module. + *

*

* For more information and the latest version of JDevKit, please visit our * website codecrafters.org.cn. + *

*

*

Contact

* + *

+ * + * @since 1.0.0 */ package cn.org.codecrafters.devkit; \ No newline at end of file diff --git a/guid/src/main/java/cn/org/codecrafters/devkit/guid/GuidCreator.java b/guid/src/main/java/cn/org/codecrafters/guid/GuidCreator.java similarity index 95% rename from guid/src/main/java/cn/org/codecrafters/devkit/guid/GuidCreator.java rename to guid/src/main/java/cn/org/codecrafters/guid/GuidCreator.java index fd9031d..74cf115 100644 --- a/guid/src/main/java/cn/org/codecrafters/devkit/guid/GuidCreator.java +++ b/guid/src/main/java/cn/org/codecrafters/guid/GuidCreator.java @@ -15,13 +15,16 @@ * limitations under the License. */ -package cn.org.codecrafters.devkit.guid; +package cn.org.codecrafters.guid; /** + *

* The {@code GuidCreator} is a generic interface for generating globally unique * identifiers (GUIDs) of a specific type. + *

*

* The type of ID is determined by the class implementing this interface. + *

* * @param this represents the type of the Global Unique Identifier * @author Zihlu Wang diff --git a/guid/src/main/java/cn/org/codecrafters/devkit/guid/SnowflakeGuidCreator.java b/guid/src/main/java/cn/org/codecrafters/guid/SnowflakeGuidCreator.java similarity index 95% rename from guid/src/main/java/cn/org/codecrafters/devkit/guid/SnowflakeGuidCreator.java rename to guid/src/main/java/cn/org/codecrafters/guid/SnowflakeGuidCreator.java index bed4080..fa66b49 100644 --- a/guid/src/main/java/cn/org/codecrafters/devkit/guid/SnowflakeGuidCreator.java +++ b/guid/src/main/java/cn/org/codecrafters/guid/SnowflakeGuidCreator.java @@ -15,28 +15,30 @@ * limitations under the License. */ -package cn.org.codecrafters.devkit.guid; +package cn.org.codecrafters.guid; -import cn.org.codecrafters.devkit.guid.exceptions.TimingException; +import cn.org.codecrafters.guid.exceptions.TimingException; import java.time.LocalDateTime; import java.time.ZoneOffset; /** + *

* SnowflakeGuidCreator - GUID generator based on the Snowflake algorithm. - * + *

*

* The SnowflakeGuidCreator generates unique identifiers using the Snowflake * algorithm, which combines a timestamp, worker ID, and data center ID to * create 64-bit long integers. The bit distribution for the generated IDs is * as follows: *

+ *

* *

* When initializing the SnowflakeGuidCreator, you must provide the worker ID @@ -45,6 +47,7 @@ import java.time.ZoneOffset; * increments for IDs generated within the same millisecond. If the system * clock moves backward, an exception is thrown to prevent generating IDs with * repeated timestamps. + *

* * @author Zihlu Wang * @version 1.0.0 diff --git a/guid/src/main/java/cn/org/codecrafters/devkit/guid/exceptions/TimingException.java b/guid/src/main/java/cn/org/codecrafters/guid/exceptions/TimingException.java similarity index 96% rename from guid/src/main/java/cn/org/codecrafters/devkit/guid/exceptions/TimingException.java rename to guid/src/main/java/cn/org/codecrafters/guid/exceptions/TimingException.java index cea2bb5..57ae91e 100644 --- a/guid/src/main/java/cn/org/codecrafters/devkit/guid/exceptions/TimingException.java +++ b/guid/src/main/java/cn/org/codecrafters/guid/exceptions/TimingException.java @@ -15,25 +15,30 @@ * limitations under the License. */ -package cn.org.codecrafters.devkit.guid.exceptions; +package cn.org.codecrafters.guid.exceptions; /** + *

* The TimingException class represents an exception that is thrown when there * is an error related to time sequence. + *

*

* This class extends the RuntimeException class, which means that instances of * TimingException do not need to be declared in a method or constructor's * throws clause. + *

*

* Instances of TimingException can be created with or without a message and a * cause. The message provides a description of the exception, while the cause * represents the underlying cause of the exception and provides additional * information about the error. + *

*

* TimingException is typically used to handle exceptions related to timing, * such as timeouts or synchronization issues. It is a subclass of * RuntimeException, which means it is an unchecked exception and does not need * to be caught or declared. + *

* * @author Zihlu Wang * @since 1.0.0 diff --git a/guid/src/main/java/cn/org/codecrafters/guid/exceptions/package-info.java b/guid/src/main/java/cn/org/codecrafters/guid/exceptions/package-info.java new file mode 100644 index 0000000..1fac43d --- /dev/null +++ b/guid/src/main/java/cn/org/codecrafters/guid/exceptions/package-info.java @@ -0,0 +1,49 @@ +/* + * 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. + */ + +/** + *

+ * This package contains the custom exception classes related to GUID + * generation. These exceptions are thrown when there are issues or errors + * during the generation or processing of global unique identifiers (GUIDs). + *

+ * + *

+ * The main exception class in this package is {@link + * cn.org.codecrafters.guid.exceptions.TimingException}, which is a runtime + * exception and serves as the base exception for all other custom exceptions + * related to GUID generation. + *

+ * + *

+ * Custom exceptions in this package provide specific information about the + * type of error that occurred during GUID generation, making it easier for + * developers to handle and respond to different scenarios when dealing with + * GUIDs. They are designed to enhance the robustness and reliability of the + * GUID generation process by providing clear and meaningful error messages to + * the developers. + *

+ * + *

+ * Developers using the GUID generation module should be aware of the possible + * exceptions that can be thrown and handle them appropriately to ensure smooth + * operation and error handling in their applications. + *

+ * + * @since 1.0.0 + */ +package cn.org.codecrafters.guid.exceptions; \ No newline at end of file diff --git a/guid/src/main/java/cn/org/codecrafters/devkit/guid/package-info.java b/guid/src/main/java/cn/org/codecrafters/guid/package-info.java similarity index 94% rename from guid/src/main/java/cn/org/codecrafters/devkit/guid/package-info.java rename to guid/src/main/java/cn/org/codecrafters/guid/package-info.java index 7b0e0a5..c102a7d 100644 --- a/guid/src/main/java/cn/org/codecrafters/devkit/guid/package-info.java +++ b/guid/src/main/java/cn/org/codecrafters/guid/package-info.java @@ -16,12 +16,15 @@ */ /** + *

* The package provides a set of tools for generating globally unique * identifiers (GUIDs). + *

*

* The goal of this library is to provide an efficient, reliable way to * generate globally unique identifiers without requiring any specific * environment or configuration. + *

*

* Key features include: *

+ *

* * @since 1.0.0 */ -package cn.org.codecrafters.devkit.guid; \ No newline at end of file +package cn.org.codecrafters.guid; \ No newline at end of file diff --git a/simple-jwt-authzero/src/main/java/cn/org/codecrafters/simplejwt/authzero/AuthzeroTokenResolver.java b/simple-jwt-authzero/src/main/java/cn/org/codecrafters/simplejwt/authzero/AuthzeroTokenResolver.java index 7e4e52d..b0259bc 100644 --- a/simple-jwt-authzero/src/main/java/cn/org/codecrafters/simplejwt/authzero/AuthzeroTokenResolver.java +++ b/simple-jwt-authzero/src/main/java/cn/org/codecrafters/simplejwt/authzero/AuthzeroTokenResolver.java @@ -17,7 +17,7 @@ package cn.org.codecrafters.simplejwt.authzero; -import cn.org.codecrafters.devkit.guid.GuidCreator; +import cn.org.codecrafters.guid.GuidCreator; import cn.org.codecrafters.simplejwt.TokenPayload; import cn.org.codecrafters.simplejwt.TokenResolver; import cn.org.codecrafters.simplejwt.annotations.ExcludeFromPayload; @@ -37,16 +37,19 @@ import java.time.ZoneId; import java.util.*; /** + *

* The {@code AuthzeroTokenResolver} class is an implementation of the {@link * cn.org.codecrafters.simplejwt.TokenResolver} interface. It uses the {@code * com.auth0:java-jwt} library to handle JSON Web Token (JWT) resolution. This * resolver provides functionality to create, extract, verify, and renew JWT * tokens using various algorithms and custom payload data. + *

*

* Dependencies: * 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. + *

*

* Usage: * To use the {@code AuthzeroTokenResolver}, first, create an instance of this @@ -58,6 +61,7 @@ import java.util.*; * "Token Issuer", * "Token Secret"); * } + *

*

* Then, you can utilize the various methods provided by this resolver to * handle JWT tokens: @@ -78,12 +82,14 @@ import java.util.*; * String renewedToken = * tokenResolver.renew(token, Duration.ofMinutes(30), customPayloads); * } + *

*

* Note: * It is essential to configure the appropriate algorithms, secret, and issuer * according to your specific use case when using this resolver. * Additionally, ensure that the {@code com.auth0:java-jwt} library is * correctly configured in your project's dependencies. + *

* * @author Zihlu Wang * @version 1.0.0 @@ -232,13 +238,16 @@ public class AuthzeroTokenResolver implements TokenResolver { } /** + *

* Builds the custom claims of the JSON Web Token (JWT) using the provided * Map of claims and adds them to the JWTCreator.Builder. *

+ *

* This method is used to add custom claims to the JWT. It takes a Map of * claims, where each entry represents a custom claim name (key) and its * corresponding value (value). The custom claims will be added to the JWT * using the JWTCreator.Builder. + *

* * @param claims a Map containing the custom claims to be added to the JWT * @param builder the JWTCreator.Builder instance to which the custom @@ -253,9 +262,12 @@ public class AuthzeroTokenResolver implements TokenResolver { } /** + *

* Finish creating a token. + *

*

* This is the final step of create a token, to sign this token. + *

* * @param builder the builder to build this JWT * @return the generated token as a {@code String} diff --git a/simple-jwt-authzero/src/main/java/cn/org/codecrafters/simplejwt/authzero/config/AuthzeroTokenResolverConfig.java b/simple-jwt-authzero/src/main/java/cn/org/codecrafters/simplejwt/authzero/config/AuthzeroTokenResolverConfig.java index e78ee2c..2d370f9 100644 --- a/simple-jwt-authzero/src/main/java/cn/org/codecrafters/simplejwt/authzero/config/AuthzeroTokenResolverConfig.java +++ b/simple-jwt-authzero/src/main/java/cn/org/codecrafters/simplejwt/authzero/config/AuthzeroTokenResolverConfig.java @@ -62,27 +62,35 @@ import java.util.function.Function; public final class AuthzeroTokenResolverConfig implements TokenResolverConfig> { /** + *

* Constructs a new instance of AuthzeroTokenResolverConfig. + *

*

* The constructor is set as private to enforce the singleton pattern for * this configuration class. Instances of AuthzeroTokenResolverConfig * should be obtained through the {@link #getInstance()} method. + *

*/ private AuthzeroTokenResolverConfig() { } /** + *

* The singleton instance of AuthzeroTokenResolverConfig. + *

*

* This instance is used to ensure that only one instance of * AuthzeroTokenResolverConfig is created and shared throughout the * application. The singleton pattern is implemented to provide centralized * configuration and avoid redundant object creation. + *

*/ private static AuthzeroTokenResolverConfig instance; /** + *

* The supported algorithms and their corresponding algorithm functions. + *

*

* This map stores the supported algorithms as keys and their corresponding * algorithm functions as values. The algorithm functions represent the @@ -115,14 +123,17 @@ public final class AuthzeroTokenResolverConfig implements TokenResolverConfig * Gets the algorithm function corresponding to the specified * TokenAlgorithm. + *

*

* This method returns the algorithm function associated with the given * TokenAlgorithm. The provided TokenAlgorithm represents the specific * algorithm for which the corresponding algorithm function is required. * The returned AlgorithmFunction represents the function implementation * that can be used by the TokenResolver to handle the specific algorithm. + *

* * @param algorithm the TokenAlgorithm for which the algorithm function is * required diff --git a/simple-jwt-authzero/src/main/java/cn/org/codecrafters/simplejwt/authzero/config/package-info.java b/simple-jwt-authzero/src/main/java/cn/org/codecrafters/simplejwt/authzero/config/package-info.java new file mode 100644 index 0000000..a5d8517 --- /dev/null +++ b/simple-jwt-authzero/src/main/java/cn/org/codecrafters/simplejwt/authzero/config/package-info.java @@ -0,0 +1,64 @@ +/* + * 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. + */ + +/** + *

+ * The package {@code cn.org.codecrafters.simplejwt.authzero.config} contains + * configuration classes related to the {@link + * cn.org.codecrafters.simplejwt.authzero.AuthzeroTokenResolver} + * implementation. + *

+ * + *

+ * The classes in this package provide configuration options and settings for + * the {@link cn.org.codecrafters.simplejwt.authzero.AuthzeroTokenResolver}, + * which is used for resolving JSON Web Tokens (JWT) using the Auth0 library. + *

+ * + *

+ * The {@link + * cn.org.codecrafters.simplejwt.authzero.config.AuthzeroTokenResolverConfig} + * class is a configuration class that defines the mapping between standard + * {@link cn.org.codecrafters.simplejwt.constants.TokenAlgorithm} and the + * corresponding function implementation used by {@link + * cn.org.codecrafters.simplejwt.authzero.AuthzeroTokenResolver} for handling + * JWT algorithms. It enables developers to specify and customize the + * algorithm functions according to the chosen JWT algorithm and the library + * being used. + *

+ * + *

+ * The configuration options in this package help developers integrate and + * configure the {@link + * cn.org.codecrafters.simplejwt.authzero.AuthzeroTokenResolver} seamlessly + * into their Spring Boot applications. Developers can fine-tune the token + * resolution process and customize algorithm handling to align with their + * specific requirements and desired level of security. + *

+ * + *

+ * It is recommended to explore the classes in this package to understand how + * to configure and use the {@link + * cn.org.codecrafters.simplejwt.authzero.AuthzeroTokenResolver} effectively + * in the Spring Boot environment to handle JWT authentication and + * authorization securely and efficiently. + *

+ * + * @since 1.0.0 + */ + +package cn.org.codecrafters.simplejwt.authzero.config; \ No newline at end of file diff --git a/simple-jwt-authzero/src/main/java/cn/org/codecrafters/simplejwt/authzero/package-info.java b/simple-jwt-authzero/src/main/java/cn/org/codecrafters/simplejwt/authzero/package-info.java new file mode 100644 index 0000000..0ac8dd8 --- /dev/null +++ b/simple-jwt-authzero/src/main/java/cn/org/codecrafters/simplejwt/authzero/package-info.java @@ -0,0 +1,69 @@ +/* + * 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. + */ + +/** + *

cn.org.codecrafters.simplejwt.authzero

+ * + *

+ * This package contains classes related to the integration of the {@code + * com.auth0:java-jwt} library in the Simple JWT project. {@code + * com.auth0:java-jwt} is a powerful and widely-used Identity as a Service + * (IDaaS) platform that provides secure authentication and authorization + * solutions for web and mobile applications. The classes in this package + * provide the necessary functionality to handle JSON Web Tokens (JWTs) using + * the {@code com.auth0:java-jwt} library. + *

+ * + *

+ * The main class in this package is the {@link + * cn.org.codecrafters.simplejwt.authzero.AuthzeroTokenResolver}, which + * implements the {@link cn.org.codecrafters.simplejwt.TokenResolver} interface + * and uses the {@code com.auth0:java-jwt} library to handle JWT operations. It + * provides the functionality to create, validate, and extract JWTs using the + * {@code com.auth0:java-jwt} library. Developers can use this class as the + * main token resolver in the Simple JWT project when integrating {@code + * com.auth0:java-jwt} as the JWT management library. + *

+ * + *

+ * The {@code AuthzeroTokenResolver} relies on the {@code com.auth0:java-jwt} + * library to handle the underlying JWT operations, including token creation, + * validation, and extraction. It utilizes the {@code com.auth0:java-jwt} + * {@code Algorithm} class to define and use different algorithms for JWT + * signing and verification. + *

+ * + *

+ * To use the {@code AuthzeroTokenResolver}, developers must provide the + * necessary configurations and dependencies, such as the {@code GuidCreator} + * for generating unique JWT IDs (JTI), the supported algorithm function, the + * issuer name, and the secret key used for token signing and validation. The + * {@code AuthzeroTokenResolverConfig} class provides a convenient way to + * configure these dependencies. + *

+ * + *

+ * Developers using the {@code com.auth0:java-jwt} integration should be + * familiar with the concepts and usage of the {@code com.auth0:java-jwt} + * library and follow the official {@code com.auth0:java-jwt} documentation for + * best practices and security considerations. + *

+ * + * @since 1.0.0 + */ + +package cn.org.codecrafters.simplejwt.authzero; \ No newline at end of file diff --git a/simple-jwt-facade/src/main/java/cn/org/codecrafters/simplejwt/TokenPayload.java b/simple-jwt-facade/src/main/java/cn/org/codecrafters/simplejwt/TokenPayload.java index 6b1beed..298a8e7 100644 --- a/simple-jwt-facade/src/main/java/cn/org/codecrafters/simplejwt/TokenPayload.java +++ b/simple-jwt-facade/src/main/java/cn/org/codecrafters/simplejwt/TokenPayload.java @@ -20,17 +20,21 @@ package cn.org.codecrafters.simplejwt; import java.util.Map; /** + *

* TokenPayload - Interface for JWT Payload Data Classes. + *

*

* The {@code TokenPayload} interface is used to mark a data class as suitable * for being used as the payload in a JSON Web Token (JWT). Any class * implementing this interface can be used to represent the payload data that * will be included in a JWT. + *

*

* Implementing this interface indicates that the data class contains * information that needs to be securely transmitted and verified as part of a * JWT. The payload typically contains claims or attributes that provide * additional information about the JWT subject or context. + *

*

* Usage: * To use a class as a JWT payload, simply implement the {@code TokenPayload} @@ -40,6 +44,7 @@ import java.util.Map; * // Class implementation with payload data... * } * + *

* * @since 1.0.0 * @version 1.0.0 diff --git a/simple-jwt-facade/src/main/java/cn/org/codecrafters/simplejwt/TokenResolver.java b/simple-jwt-facade/src/main/java/cn/org/codecrafters/simplejwt/TokenResolver.java index 3b93d7c..d256c0c 100644 --- a/simple-jwt-facade/src/main/java/cn/org/codecrafters/simplejwt/TokenResolver.java +++ b/simple-jwt-facade/src/main/java/cn/org/codecrafters/simplejwt/TokenResolver.java @@ -21,26 +21,31 @@ import java.time.Duration; import java.util.Map; /** + *

* The {@code TokenResolver} interface defines methods for creating, * extracting, and renewing tokens, particularly JSON Web Tokens (JWTs). It * provides a set of methods to generate tokens with various payload * configurations, extract payloads from tokens, and renew expired tokens. + *

*

* Token Creation: * The interface provides overloaded methods for creating tokens with different * payload configurations, including expiration time, audience, subject, and * custom payload data. Clients can choose the appropriate method based on * their specific token requirements. + *

*

* Token Extraction: * The interface includes methods to extract payload information from a given * token. Clients can use these methods to obtain the payload data encoded in * the token. + *

*

* Token Renewal: * The interface also offers methods for token renewal. Clients can renew an * expired token by providing a new expiration time, audience, subject, and * optional custom payload data. + *

* * @param the type of the result obtained by the * third-party library when parsing JWTs diff --git a/simple-jwt-facade/src/main/java/cn/org/codecrafters/simplejwt/annotations/ExcludeFromPayload.java b/simple-jwt-facade/src/main/java/cn/org/codecrafters/simplejwt/annotations/ExcludeFromPayload.java index e774597..5ae4591 100644 --- a/simple-jwt-facade/src/main/java/cn/org/codecrafters/simplejwt/annotations/ExcludeFromPayload.java +++ b/simple-jwt-facade/src/main/java/cn/org/codecrafters/simplejwt/annotations/ExcludeFromPayload.java @@ -30,6 +30,7 @@ import java.lang.annotation.Target; * excluded from being automatically injected into the JSON Web Token (JWT) * payload during token generation. When a property is annotated with this * annotation, it will not be included as part of the JWT payload. + *

* *

Usage: * To exclude a property from the JWT payload, annotate the property with @@ -40,18 +41,20 @@ import java.lang.annotation.Target; * private String username; * * // This property will not be included in the JWT payload - * {@literal @}ExcludeFromPayload + * @ExcludeFromPayload * private String sensitiveData; * * // Getters and setters... * } * } + *

* *

Note: * This annotation should be used only on properties that are not intended to * be included in the JWT payload due to their sensitive nature or for other * reasons. It is important to carefully choose which properties are excluded * from the payload to ensure the JWT remains secure and efficient. + *

* * @since 1.0.0 * @version 1.0.0 diff --git a/simple-jwt-facade/src/main/java/cn/org/codecrafters/simplejwt/config/TokenResolverConfig.java b/simple-jwt-facade/src/main/java/cn/org/codecrafters/simplejwt/config/TokenResolverConfig.java index 3b4473c..d8aafc5 100644 --- a/simple-jwt-facade/src/main/java/cn/org/codecrafters/simplejwt/config/TokenResolverConfig.java +++ b/simple-jwt-facade/src/main/java/cn/org/codecrafters/simplejwt/config/TokenResolverConfig.java @@ -20,13 +20,16 @@ package cn.org.codecrafters.simplejwt.config; import cn.org.codecrafters.simplejwt.constants.TokenAlgorithm; /** + *

* The TokenResolverConfig interface provides a mechanism to configure a * TokenResolver with algorithm functions. + *

*

* This generic interface is used to define the configuration details for a * TokenResolver that utilizes algorithm functions. The interface allows for * specifying algorithm functions corresponding to different TokenAlgorithm * instances supported by the TokenResolver implementation. + *

* * @param the type representing algorithm functions used by * the TokenResolver diff --git a/guid/src/main/java/cn/org/codecrafters/devkit/guid/exceptions/package-info.java b/simple-jwt-facade/src/main/java/cn/org/codecrafters/simplejwt/config/package-info.java similarity index 51% rename from guid/src/main/java/cn/org/codecrafters/devkit/guid/exceptions/package-info.java rename to simple-jwt-facade/src/main/java/cn/org/codecrafters/simplejwt/config/package-info.java index 4b438bc..bce91de 100644 --- a/guid/src/main/java/cn/org/codecrafters/devkit/guid/exceptions/package-info.java +++ b/simple-jwt-facade/src/main/java/cn/org/codecrafters/simplejwt/config/package-info.java @@ -16,8 +16,20 @@ */ /** - * The package provides all exceptions be used by module guid. + *

+ * The classes in this package provide configuration options and settings for + * the Simple JWT library. They are used to customize the behavior of the + * library and allow developers to fine-tune various aspects of JWT generation + * and validation. + *

+ * + *

+ * Other configuration classes may be added in the future to support additional + * customization options and features. Developers using the Simple JWT library + * should be familiar with the available configuration options to ensure proper + * integration and usage of the library. + *

* * @since 1.0.0 */ -package cn.org.codecrafters.devkit.guid.exceptions; \ No newline at end of file +package cn.org.codecrafters.simplejwt.config; \ No newline at end of file diff --git a/simple-jwt-facade/src/main/java/cn/org/codecrafters/simplejwt/constants/PredefinedKeys.java b/simple-jwt-facade/src/main/java/cn/org/codecrafters/simplejwt/constants/PredefinedKeys.java index 4f0ac16..093ec63 100644 --- a/simple-jwt-facade/src/main/java/cn/org/codecrafters/simplejwt/constants/PredefinedKeys.java +++ b/simple-jwt-facade/src/main/java/cn/org/codecrafters/simplejwt/constants/PredefinedKeys.java @@ -20,12 +20,47 @@ package cn.org.codecrafters.simplejwt.constants; import java.util.List; /** - * PredefinedKeys + *

+ * This class contains predefined keys that are commonly used in JSON Web + * Tokens (JWT). + *

* - * @author Zihlu Wang + *

+ * JWTs consist of a set of claims represented as key-value pairs. These claims + * store various pieces of information, such as the subject, issuer, expiration + * time, and more. To ensure consistency and avoid spelling mistakes, this + * class provides constants for the standard keys used in JWT claims. + *

+ * + *

+ * Developers using this JWT library can use these predefined keys to set and + * retrieve claims in a safer and more readable way. By using constants instead + * of plain strings, it helps prevent typos and makes the code more + * maintainable. + *

+ * + * @since 1.0.0 */ public final class PredefinedKeys { - public static final List KEYS = List.of("aud", "sub", "nbf", "iss", "exp", "iat", "jti"); + // Constants for standard JWT claims + public static final String ISSUER = "iss"; + public static final String SUBJECT = "sub"; + + public static final String AUDIENCE = "aud"; + + public static final String EXPIRATION_TIME = "exp"; + + public static final String NOT_BEFORE = "nbf"; + + public static final String ISSUED_AT = "iat"; + + public static final String JWT_ID = "jti"; + + private PredefinedKeys() { + // Private constructor to prevent instantiation + } + + public static final List KEYS = List.of(ISSUER, SUBJECT, AUDIENCE, EXPIRATION_TIME, NOT_BEFORE, ISSUED_AT, JWT_ID); } diff --git a/simple-jwt-facade/src/main/java/cn/org/codecrafters/simplejwt/constants/TokenAlgorithm.java b/simple-jwt-facade/src/main/java/cn/org/codecrafters/simplejwt/constants/TokenAlgorithm.java index 6254a64..ad560fe 100644 --- a/simple-jwt-facade/src/main/java/cn/org/codecrafters/simplejwt/constants/TokenAlgorithm.java +++ b/simple-jwt-facade/src/main/java/cn/org/codecrafters/simplejwt/constants/TokenAlgorithm.java @@ -17,21 +17,16 @@ package cn.org.codecrafters.simplejwt.constants; -import ch.qos.logback.core.subst.Token; import lombok.Getter; -import java.lang.reflect.Array; -import java.util.Arrays; -import java.util.List; -import java.util.stream.Stream; - /** + *

* The {@code TokenAlgorithm} enum class defines the algorithms that can be * used for signing and verifying JSON Web Tokens (JWT). JWT allows various * cryptographic algorithms to be used for secure token generation and * validation. This enum provides a list of supported algorithms to ensure * consistent usage and avoid potential security issues. - * + *

*

Supported Algorithms: * This enum includes the following supported algorithms: *

    @@ -45,9 +40,10 @@ import java.util.stream.Stream; *
  • {@link TokenAlgorithm#ES384}: ECDSA with SHA-384
  • *
  • {@link TokenAlgorithm#ES512}: ECDSA with SHA-512
  • *
+ *

* - * @since 1.0.0 * @version 1.0.0 + * @since 1.0.0 */ @Getter public enum TokenAlgorithm { diff --git a/simple-jwt-facade/src/main/java/cn/org/codecrafters/simplejwt/exceptions/UnsupportedAlgorithmException.java b/simple-jwt-facade/src/main/java/cn/org/codecrafters/simplejwt/exceptions/UnsupportedAlgorithmException.java index 54d14a5..e5756cc 100644 --- a/simple-jwt-facade/src/main/java/cn/org/codecrafters/simplejwt/exceptions/UnsupportedAlgorithmException.java +++ b/simple-jwt-facade/src/main/java/cn/org/codecrafters/simplejwt/exceptions/UnsupportedAlgorithmException.java @@ -18,18 +18,21 @@ package cn.org.codecrafters.simplejwt.exceptions; /** + *

* This {@code UnsupportedAlgorithmException} is to indicates that the given * algorithm is not supported by TokenResolver yet. + *

*

* To support a specified algorithm, you could + *

* * @author Zihlu Wang */ public class UnsupportedAlgorithmException extends RuntimeException { /** - * Constructs a new runtime exception with {@code null} as its - * detail message. The cause is not initialized, and may subsequently be + * Constructs a new runtime exception with {@code null} as its detail + * message. The cause is not initialized, and may subsequently be * initialized by a call to {@link #initCause}. */ public UnsupportedAlgorithmException() { @@ -48,10 +51,14 @@ public class UnsupportedAlgorithmException extends RuntimeException { } /** + *

* Constructs a new runtime exception with the specified detail message and - * cause.

Note that the detail message associated with + * cause. + *

+ *

Note that the detail message associated with * {@code cause} is not automatically incorporated in * this runtime exception's detail message. + *

* * @param message the detail message (which is saved for later retrieval * by the {@link #getMessage()} method). diff --git a/simple-jwt-facade/src/main/java/cn/org/codecrafters/simplejwt/exceptions/package-info.java b/simple-jwt-facade/src/main/java/cn/org/codecrafters/simplejwt/exceptions/package-info.java new file mode 100644 index 0000000..b9fec6e --- /dev/null +++ b/simple-jwt-facade/src/main/java/cn/org/codecrafters/simplejwt/exceptions/package-info.java @@ -0,0 +1,43 @@ +/* + * 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. + */ + +/** + *

+ * The {@code cn.org.codecrafters.simplejwt.exceptions} package contains + * custom exception classes related to the Simple JWT library. These + * exceptions are thrown when there are issues or errors during the generation + * , validation, or processing of JSON Web Tokens (JWTs) in Java applications. + *

+ * + *

+ * Custom exception classes in this package are designed to enhance the + * robustness and reliability of the JWT handling process by providing clear + * and meaningful error messages to the developers. They help developers + * identify and troubleshoot issues related to JWT generation, validation, or + * extraction and ensure smooth operation and error handling in their + * applications. + *

+ * + *

+ * Developers using the Simple JWT library should be aware of the possible + * exceptions that can be thrown and handle them appropriately to ensure secure + * and reliable JWT handling in their Java applications. + *

+ * + * @since 1.0.0 + */ +package cn.org.codecrafters.simplejwt.exceptions; \ No newline at end of file diff --git a/simple-jwt-facade/src/main/java/cn/org/codecrafters/simplejwt/package-info.java b/simple-jwt-facade/src/main/java/cn/org/codecrafters/simplejwt/package-info.java index 9d0ce75..dd2441c 100644 --- a/simple-jwt-facade/src/main/java/cn/org/codecrafters/simplejwt/package-info.java +++ b/simple-jwt-facade/src/main/java/cn/org/codecrafters/simplejwt/package-info.java @@ -16,8 +16,30 @@ */ /** - * package-info + *

+ * The {@code cn.org.codecrafters.simplejwt} package is the core package of the + * Simple JWT project, which provides a lightweight and easy-to-use library for + * working with JSON Web Tokens (JWTs) in Java applications. JWT is a + * widely-used standard for representing claims between two parties, typically + * used to secure web and mobile applications. This library aims to simplify + * the JWT handling process and provide convenient abstractions for JWT + * generation, validation, and extraction. + *

* - * @author Zihlu Wang + *

+ * The Simple JWT library is designed to be flexible and customizable, allowing + * developers to use different algorithms, token resolvers, and token payload + * classes based on their specific application requirements. It aims to + * simplify the JWT handling process while maintaining security and best + * practices for working with JWTs. + *

+ * + *

+ * Developers should refer to the official documentation and examples for the + * Simple JWT project to understand how to use the library effectively and + * securely in their Java applications. + *

+ * + * @since 1.0.0 */ package cn.org.codecrafters.simplejwt; \ No newline at end of file diff --git a/simple-jwt-spring-boot-starter/src/main/java/cn/org/codecrafters/simplejwt/SimpleJwtAutoConfiguration.java b/simple-jwt-spring-boot-starter/src/main/java/cn/org/codecrafters/simplejwt/autoconfiguration/SimpleJwtAutoConfiguration.java similarity index 50% rename from simple-jwt-spring-boot-starter/src/main/java/cn/org/codecrafters/simplejwt/SimpleJwtAutoConfiguration.java rename to simple-jwt-spring-boot-starter/src/main/java/cn/org/codecrafters/simplejwt/autoconfiguration/SimpleJwtAutoConfiguration.java index 88a0d23..5e3008a 100644 --- a/simple-jwt-spring-boot-starter/src/main/java/cn/org/codecrafters/simplejwt/SimpleJwtAutoConfiguration.java +++ b/simple-jwt-spring-boot-starter/src/main/java/cn/org/codecrafters/simplejwt/autoconfiguration/SimpleJwtAutoConfiguration.java @@ -15,11 +15,12 @@ * limitations under the License. */ -package cn.org.codecrafters.simplejwt; +package cn.org.codecrafters.simplejwt.autoconfiguration; -import cn.org.codecrafters.devkit.guid.GuidCreator; +import cn.org.codecrafters.guid.GuidCreator; +import cn.org.codecrafters.simplejwt.TokenResolver; import cn.org.codecrafters.simplejwt.authzero.AuthzeroTokenResolver; -import cn.org.codecrafters.simplejwt.properties.SimpleJwtProperties; +import cn.org.codecrafters.simplejwt.autoconfiguration.properties.SimpleJwtProperties; import com.auth0.jwt.interfaces.DecodedJWT; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; @@ -33,35 +34,91 @@ import org.springframework.context.annotation.Configuration; import java.util.UUID; /** - * SimpleJwtAutoConfiguration + *

+ * SimpleJwtAutoConfiguration is responsible for automatically configuring the + * Simple JWT library when used in a Spring Boot application. It provides + * default settings and configurations to ensure that the library works + * smoothly without requiring manual configuration. + *

+ * + *

+ * This auto-configuration class sets up the necessary beans and components + * required for JWT generation and validation. It automatically creates and + * configures the {@link TokenResolver} bean based on the available options and + * properties. + *

+ * + *

+ * Developers using the Simple JWT library with Spring Boot do not need to + * explicitly configure the library, as the auto-configuration takes care of + * setting up the necessary components and configurations automatically. + * However, developers still have the flexibility to customize the behavior of + * the library by providing their own configurations and properties. + *

* * @author Zihlu Wang + * @version 1.0.0 + * @since 1.0.0 */ @Slf4j @Configuration @EnableConfigurationProperties(value = {SimpleJwtProperties.class}) public class SimpleJwtAutoConfiguration { + /** + * The GuidCreator instance to be used for generating JWT IDs (JTI). + */ private GuidCreator jtiCreator; + /** + * Sets the GuidCreator instance to be used for generating JWT IDs (JTI). + * + * @param jtiCreator the {@code GuidCreator} instance + */ @Autowired public void setJtiCreator(GuidCreator jtiCreator) { this.jtiCreator = jtiCreator; } + /** + * The {@code SimpleJwtProperties} instance containing the configuration + * properties for Simple JWT. + */ private final SimpleJwtProperties simpleJwtProperties; + /** + * Constructs a new {@code SimpleJwtAutoConfiguration} instance with the + * provided SimpleJwtProperties. + * + * @param simpleJwtProperties the SimpleJwtProperties instance + */ @Autowired public SimpleJwtAutoConfiguration(SimpleJwtProperties simpleJwtProperties) { this.simpleJwtProperties = simpleJwtProperties; } + /** + * Creates a new {@code GuidCreator} bean if no existing bean with the name + * "jtiCreator" is found. The created {@code GuidCreator} is used for + * generating JWT IDs (JTI). + * + * @return the GuidCreator instance + */ @Bean @ConditionalOnMissingBean(value = GuidCreator.class, name = "jtiCreator") public GuidCreator jtiCreator() { return (GuidCreator) UUID::randomUUID; } + /** + * Creates a new {@link TokenResolver} bean using {@link + * AuthzeroTokenResolver} if no existing {@link TokenResolver} bean is + * found. The {@link AuthzeroTokenResolver} is configured with the + * provided {@link GuidCreator}, {@code algorithm}, {@code issuer}, and + * {@code secret} properties from {@link SimpleJwtProperties}. + * + * @return the {@link TokenResolver} instance + */ @Bean @ConditionalOnClass({DecodedJWT.class, AuthzeroTokenResolver.class}) @ConditionalOnMissingBean({TokenResolver.class}) diff --git a/simple-jwt-spring-boot-starter/src/main/java/cn/org/codecrafters/simplejwt/autoconfiguration/package-info.java b/simple-jwt-spring-boot-starter/src/main/java/cn/org/codecrafters/simplejwt/autoconfiguration/package-info.java new file mode 100644 index 0000000..79c9f29 --- /dev/null +++ b/simple-jwt-spring-boot-starter/src/main/java/cn/org/codecrafters/simplejwt/autoconfiguration/package-info.java @@ -0,0 +1,38 @@ +/* + * 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. + */ + +/** + *

+ * The classes in this package are responsible for automatically configuring + * the Simple JWT library when it is used in a Spring Boot application. They + * provide default settings and configurations to ensure that the library works + * smoothly and seamlessly without requiring developers to manually configure + * it. + *

+ * + *

+ * Developers using the Simple JWT library with Spring Boot do not need to + * explicitly configure the library, as the auto-configuration classes take + * care of setting up the necessary components and configurations + * automatically. However, developers still have the flexibility to customize + * the behavior of the library by providing their own configurations and + * properties. + *

+ * + * @since 1.0.0 + */ +package cn.org.codecrafters.simplejwt.autoconfiguration; \ No newline at end of file diff --git a/simple-jwt-spring-boot-starter/src/main/java/cn/org/codecrafters/simplejwt/autoconfiguration/properties/SimpleJwtProperties.java b/simple-jwt-spring-boot-starter/src/main/java/cn/org/codecrafters/simplejwt/autoconfiguration/properties/SimpleJwtProperties.java new file mode 100644 index 0000000..57d00a8 --- /dev/null +++ b/simple-jwt-spring-boot-starter/src/main/java/cn/org/codecrafters/simplejwt/autoconfiguration/properties/SimpleJwtProperties.java @@ -0,0 +1,96 @@ +/* + * 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.simplejwt.autoconfiguration.properties; + +import cn.org.codecrafters.simplejwt.constants.TokenAlgorithm; +import lombok.Data; +import org.springframework.boot.context.properties.ConfigurationProperties; + +/** + *

+ * {@code SimpleJwtProperties} is a configuration properties class used to + * store the properties related to Simple JWT library configuration. These + * properties can be configured in the application's properties file (e.g., + * application.properties) with the prefix "code-crafters.simple-jwt". + *

+ * + *

+ * SimpleJwtProperties provides configuration options for the JWT algorithm, + * issuer, and secret. The properties are used by the {@link + * cn.org.codecrafters.simplejwt.autoconfiguration.SimpleJwtAutoConfiguration} + * class to set up the necessary configurations for JWT generation and + * validation. + *

+ * + *

+ * Developers can customize the JWT algorithm, issuer, and secret by setting + * the corresponding properties in the application's properties file. The + * SimpleJwtAutoConfiguration class reads these properties and uses them to + * create the TokenResolver bean with the desired configuration. + *

+ * + * @since 1.0.0 + */ +@Data +@ConfigurationProperties(prefix = "code-crafters.simple-jwt") +public class SimpleJwtProperties { + + /** + * The algorithm used for JWT generation and validation. + */ + private TokenAlgorithm algorithm; + + /** + * The issuer value to be included in the generated JWT. + */ + private String issuer; + + /** + * The secret key used for JWT generation and validation. + */ + private String secret; + + /** + * Returns the JWT algorithm configured in the properties. + * + * @return the JWT algorithm + */ + public final TokenAlgorithm algorithm() { + return algorithm; + } + + /** + * Returns the issuer value configured in the properties. + * + * @return the issuer value + */ + public final String issuer() { + return issuer; + } + + /** + * Returns the secret key configured in the properties. + * + * @return the secret key + */ + public final String secret() { + return secret; + } + +} + diff --git a/simple-jwt-spring-boot-starter/src/main/java/cn/org/codecrafters/simplejwt/autoconfiguration/properties/package-info.java b/simple-jwt-spring-boot-starter/src/main/java/cn/org/codecrafters/simplejwt/autoconfiguration/properties/package-info.java new file mode 100644 index 0000000..4c154ea --- /dev/null +++ b/simple-jwt-spring-boot-starter/src/main/java/cn/org/codecrafters/simplejwt/autoconfiguration/properties/package-info.java @@ -0,0 +1,39 @@ +/* + * 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. + */ + +/** + *

+ * The "cn.org.codecrafters.simplejwt.autoconfiguration.properties" package + * contains configuration properties classes used for Simple JWT library + * auto-configuration. These classes define the properties that can be + * configured in the application's properties file (e.g., + * application.properties) to customize the behavior and settings of the Simple + * JWT library. + *

+ * + *

+ * Developers can customize the JWT algorithm, issuer, and secret by setting + * the corresponding properties in the application's properties file with the + * prefix "code-crafters.simple-jwt". The SimpleJwtAutoConfiguration class + * reads these properties and uses them to create the {@link + * cn.org.codecrafters.simplejwt.TokenResolver} bean with the desired + * configuration. + *

+ * + * @since 1.0.0 + */ +package cn.org.codecrafters.simplejwt.autoconfiguration.properties; \ No newline at end of file diff --git a/simple-jwt-spring-boot-starter/src/main/java/cn/org/codecrafters/simplejwt/properties/SimpleJwtProperties.java b/simple-jwt-spring-boot-starter/src/main/java/cn/org/codecrafters/simplejwt/properties/SimpleJwtProperties.java deleted file mode 100644 index 34d7d66..0000000 --- a/simple-jwt-spring-boot-starter/src/main/java/cn/org/codecrafters/simplejwt/properties/SimpleJwtProperties.java +++ /dev/null @@ -1,53 +0,0 @@ -/* - * 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.simplejwt.properties; - -import cn.org.codecrafters.devkit.guid.GuidCreator; -import cn.org.codecrafters.simplejwt.constants.TokenAlgorithm; -import lombok.Data; -import lombok.Getter; -import org.springframework.boot.context.properties.ConfigurationProperties; - -/** - * SimpleJwtProperties - * - * @author Zihlu Wang - */ -@Data -@ConfigurationProperties(prefix = "code-crafters.simple-jwt") -public class SimpleJwtProperties { - - private TokenAlgorithm algorithm; - - private String issuer; - - private String secret; - - public final TokenAlgorithm algorithm() { - return algorithm; - } - - public final String issuer() { - return issuer; - } - - public final String secret() { - return secret; - } - -} diff --git a/sms-sender-facade/pom.xml b/sms-sender-facade/pom.xml deleted file mode 100644 index 0707250..0000000 --- a/sms-sender-facade/pom.xml +++ /dev/null @@ -1,44 +0,0 @@ - - - - - 4.0.0 - - cn.org.codecrafters - jdevkit - 1.0.0 - - - sms-sender-facade - - - 17 - 17 - UTF-8 - - - - - cn.org.codecrafters - devkit-core - - - - \ No newline at end of file