From de955f1dc36ce498d406c3981256251e6876a41e Mon Sep 17 00:00:00 2001 From: M3DZIK Date: Sun, 19 Nov 2023 20:37:58 +0100 Subject: [PATCH] Update javadoc and some other changes --- .../dev/medzik/librepass/client/Client.kt | 59 +++++++++---------- .../librepass/client/errors/ApiException.kt | 3 + .../librepass/client/utils/JsonUtils.kt | 8 +-- pom.xml | 2 +- .../server/components/AuthorizedUser.kt | 6 +- .../librepass/server/components/RequestIP.kt | 9 ++- .../librepass/responses/ResponseError.kt | 10 ++++ .../medzik/librepass/types/adapters/Date.kt | 3 +- .../dev/medzik/librepass/types/api/Auth.kt | 2 +- .../librepass/types/cipher/CipherType.kt | 7 +-- .../librepass/types/cipher/EncryptedCipher.kt | 1 + 11 files changed, 57 insertions(+), 53 deletions(-) diff --git a/client/src/main/kotlin/dev/medzik/librepass/client/Client.kt b/client/src/main/kotlin/dev/medzik/librepass/client/Client.kt index c9373bbe..61273f5a 100644 --- a/client/src/main/kotlin/dev/medzik/librepass/client/Client.kt +++ b/client/src/main/kotlin/dev/medzik/librepass/client/Client.kt @@ -11,26 +11,21 @@ import okhttp3.RequestBody.Companion.toRequestBody import java.io.IOException import java.util.concurrent.TimeUnit -/** - * LibrePass API Servers - */ +/** LibrePass API Servers */ object Server { - /** - * Production server instance. - */ + /** Production server instance. */ const val PRODUCTION = "https://api.librepass.medzik.dev" - /** - * Test server instance. The database from this instance can be deleted at any time! - */ + /** Test server instance. The database from this instance can be deleted at any time! */ @Suppress("UNUSED") const val TEST = "https://api.test.librepass.medzik.dev" } /** * HTTP Client for sending requests to the API. - * @param apiURL api url address - * @param accessToken access token to use for authorization + * + * @param apiURL The API URL to use. + * @param accessToken The access token to use for authorization. */ class Client( private val apiURL: String, @@ -47,8 +42,9 @@ class Client( /** * Send a GET request to the API. - * @param endpoint endpoint of the API - * @return response body + * + * @param endpoint The API endpoint to send the request to. + * @return The response from the API. */ @Throws(ClientException::class, ApiException::class) fun get(endpoint: String): String { @@ -64,8 +60,9 @@ class Client( /** * Send a DELETE request to the API. - * @param endpoint endpoint of the API - * @return response body + * + * @param endpoint The API endpoint to send the request to. + * @return The response from the API. */ @Throws(ClientException::class, ApiException::class) fun delete(endpoint: String): String { @@ -81,9 +78,10 @@ class Client( /** * Send a DELETE request to the API. - * @param endpoint endpoint of the API - * @param json JSON body of the request - * @return response body + * + * @param endpoint The API endpoint to send the request to. + * @param json The JSON to send in the request body. + * @return The response from the API. */ @Throws(ClientException::class, ApiException::class) fun delete( @@ -104,9 +102,10 @@ class Client( /** * Send a POST request to the API. - * @param endpoint endpoint of the API - * @param json JSON body of the request - * @return response body + * + * @param endpoint The API endpoint to send the request to. + * @param json The JSON to send in the request body. + * @return The response from the API. */ @Throws(ClientException::class, ApiException::class) fun post( @@ -127,9 +126,10 @@ class Client( /** * Send a PATCH request to the API. - * @param endpoint endpoint of the API - * @param json JSON body of the request - * @return response body + * + * @param endpoint The API endpoint to send the request to. + * @param json The JSON to send in the request body. + * @return The response from the API. */ @Throws(ClientException::class, ApiException::class) fun patch( @@ -150,9 +150,10 @@ class Client( /** * Send a PUT request to the API. - * @param endpoint endpoint of the API - * @param json JSON body of the request - * @return response body + * + * @param endpoint The API endpoint to send the request to. + * @param json The JSON to send in the request body. + * @return The response from the API. */ @Throws(ClientException::class, ApiException::class) fun put( @@ -171,9 +172,7 @@ class Client( return executeAndExtractBody(request) } - /** - * Execute a request and extract the body from the response. - */ + /** Execute a request and extract the body from the response. */ @Throws(ClientException::class, ApiException::class) private fun executeAndExtractBody(request: Request): String { try { diff --git a/client/src/main/kotlin/dev/medzik/librepass/client/errors/ApiException.kt b/client/src/main/kotlin/dev/medzik/librepass/client/errors/ApiException.kt index 7a34c4e4..f74297dc 100644 --- a/client/src/main/kotlin/dev/medzik/librepass/client/errors/ApiException.kt +++ b/client/src/main/kotlin/dev/medzik/librepass/client/errors/ApiException.kt @@ -4,6 +4,9 @@ import dev.medzik.librepass.responses.ResponseError /** * Exception thrown when the API returns an error. + * + * @property status The HTTP Status code returned by the API. + * @property error The error message returned by the API. */ class ApiException( val status: Number, diff --git a/client/src/main/kotlin/dev/medzik/librepass/client/utils/JsonUtils.kt b/client/src/main/kotlin/dev/medzik/librepass/client/utils/JsonUtils.kt index b98922b8..9c592f3e 100644 --- a/client/src/main/kotlin/dev/medzik/librepass/client/utils/JsonUtils.kt +++ b/client/src/main/kotlin/dev/medzik/librepass/client/utils/JsonUtils.kt @@ -4,13 +4,9 @@ import com.google.gson.Gson import com.google.gson.reflect.TypeToken object JsonUtils { - /** - * Serialize an object to JSON string. - */ + /** Serialize an object to JSON string. */ fun serialize(data: Any): String = Gson().toJson(data) - /** - * Deserialize JSON string to object. - */ + /** Deserialize JSON string to object. */ inline fun deserialize(data: String): T = Gson().fromJson(data, object : TypeToken() {}.type) } diff --git a/pom.xml b/pom.xml index 38c95a27..f2976141 100644 --- a/pom.xml +++ b/pom.xml @@ -9,7 +9,7 @@ pom LibrePass The core LibrePass infrastructure - https://librepass.medzik.dev + https://github.com/LibrePass/LibrePass-Server diff --git a/server/src/main/kotlin/dev/medzik/librepass/server/components/AuthorizedUser.kt b/server/src/main/kotlin/dev/medzik/librepass/server/components/AuthorizedUser.kt index 4cf9572e..13f7d1fb 100644 --- a/server/src/main/kotlin/dev/medzik/librepass/server/components/AuthorizedUser.kt +++ b/server/src/main/kotlin/dev/medzik/librepass/server/components/AuthorizedUser.kt @@ -17,14 +17,12 @@ import org.springframework.web.method.support.HandlerMethodArgumentResolver import org.springframework.web.method.support.ModelAndViewContainer import java.util.* -/** - * Annotation for getting authorized user from request. - * @see AuthorizedUserArgumentResolver - */ +/** Annotation for getting authorized user from request. */ @Retention(AnnotationRetention.RUNTIME) @Target(AnnotationTarget.VALUE_PARAMETER, AnnotationTarget.ANNOTATION_CLASS) annotation class AuthorizedUser +/** Implementation of the [AuthorizedUser] annotation */ @Component class AuthorizedUserArgumentResolver @Autowired diff --git a/server/src/main/kotlin/dev/medzik/librepass/server/components/RequestIP.kt b/server/src/main/kotlin/dev/medzik/librepass/server/components/RequestIP.kt index 58a1667d..2aaa7ba2 100644 --- a/server/src/main/kotlin/dev/medzik/librepass/server/components/RequestIP.kt +++ b/server/src/main/kotlin/dev/medzik/librepass/server/components/RequestIP.kt @@ -10,19 +10,18 @@ import org.springframework.web.context.request.NativeWebRequest import org.springframework.web.method.support.HandlerMethodArgumentResolver import org.springframework.web.method.support.ModelAndViewContainer -/** - * Annotation for getting request IP from request. - * @see RequestIPArgumentResolver - */ +/** Annotation for getting request IP from request. */ @Retention(AnnotationRetention.RUNTIME) @Target(AnnotationTarget.VALUE_PARAMETER, AnnotationTarget.ANNOTATION_CLASS) annotation class RequestIP +/** Implementation of the [RequestIP] annotation. */ @Component class RequestIPArgumentResolver @Autowired constructor( - @Value("\${http.ip.header}") private val ipHeader: String + @Value("\${http.ip.header}") + private val ipHeader: String ) : HandlerMethodArgumentResolver { override fun supportsParameter(parameter: MethodParameter): Boolean { return parameter.hasParameterAnnotation(RequestIP::class.java) diff --git a/shared/src/main/kotlin/dev/medzik/librepass/responses/ResponseError.kt b/shared/src/main/kotlin/dev/medzik/librepass/responses/ResponseError.kt index c5b01575..00b2cca2 100644 --- a/shared/src/main/kotlin/dev/medzik/librepass/responses/ResponseError.kt +++ b/shared/src/main/kotlin/dev/medzik/librepass/responses/ResponseError.kt @@ -1,5 +1,10 @@ package dev.medzik.librepass.responses +/** + * ResponseError represents the error responses from the API. + * + * @property statusCode The HTTP status code. + */ enum class ResponseError(val statusCode: HttpStatus) { INVALID_BODY(HttpStatus.BAD_REQUEST), INVALID_CREDENTIALS(HttpStatus.UNAUTHORIZED), @@ -18,6 +23,11 @@ enum class ResponseError(val statusCode: HttpStatus) { UNEXPECTED_SERVER_ERROR(HttpStatus.INTERNAL_SERVER_ERROR) } +/** + * HttpStatus represents the HTTP error status codes that can be returned by the API. + * + * @property code The HTTP status code. + */ enum class HttpStatus(val code: Int) { BAD_REQUEST(400), UNAUTHORIZED(401), diff --git a/shared/src/main/kotlin/dev/medzik/librepass/types/adapters/Date.kt b/shared/src/main/kotlin/dev/medzik/librepass/types/adapters/Date.kt index 828b0974..3a40c366 100644 --- a/shared/src/main/kotlin/dev/medzik/librepass/types/adapters/Date.kt +++ b/shared/src/main/kotlin/dev/medzik/librepass/types/adapters/Date.kt @@ -23,8 +23,7 @@ class DateAdapter : JsonSerializer, JsonDeserializer { context: JsonDeserializationContext? ): Date { return if (json != null) { - val timestamp = json.asLong * 1000 // Convert seconds to milliseconds - Date(timestamp) + Date(json.asLong * 1000) // Convert seconds to milliseconds } else { Date() } diff --git a/shared/src/main/kotlin/dev/medzik/librepass/types/api/Auth.kt b/shared/src/main/kotlin/dev/medzik/librepass/types/api/Auth.kt index d896bd16..97591d5f 100644 --- a/shared/src/main/kotlin/dev/medzik/librepass/types/api/Auth.kt +++ b/shared/src/main/kotlin/dev/medzik/librepass/types/api/Auth.kt @@ -77,7 +77,7 @@ data class TwoFactorRequest( * @property userId The identifier of the user. * @property apiKey The API key. * @property verified If false, you need to authenticate the API key with OTP code to use the API. (Only if the user enabled - * 2FA authentication) + * 2FA authentication) */ data class UserCredentialsResponse( val userId: UUID, diff --git a/shared/src/main/kotlin/dev/medzik/librepass/types/cipher/CipherType.kt b/shared/src/main/kotlin/dev/medzik/librepass/types/cipher/CipherType.kt index 548bc4a8..caed4a82 100644 --- a/shared/src/main/kotlin/dev/medzik/librepass/types/cipher/CipherType.kt +++ b/shared/src/main/kotlin/dev/medzik/librepass/types/cipher/CipherType.kt @@ -1,8 +1,6 @@ package dev.medzik.librepass.types.cipher -/** - * CipherType is an enum class that represents the type of cipher. - */ +/** CipherType represents the type of cipher. */ enum class CipherType { Login, SecureNote, @@ -11,7 +9,8 @@ enum class CipherType { companion object { /** * Returns the [CipherType] from the given type integer. - * @param type The type of the cipher + * + * @param type The type of the cipher. */ fun from(type: Int) = values()[type] } diff --git a/shared/src/main/kotlin/dev/medzik/librepass/types/cipher/EncryptedCipher.kt b/shared/src/main/kotlin/dev/medzik/librepass/types/cipher/EncryptedCipher.kt index 92bdc2c7..9ea1f547 100644 --- a/shared/src/main/kotlin/dev/medzik/librepass/types/cipher/EncryptedCipher.kt +++ b/shared/src/main/kotlin/dev/medzik/librepass/types/cipher/EncryptedCipher.kt @@ -37,6 +37,7 @@ data class EncryptedCipher( ) { /** * Creates a new [EncryptedCipher] object from the [Cipher]. + * * @param cipher The [Cipher] to encrypt. * @param secretKey The key to use for encryption. * @return The encrypted cipher.