From 05775abb8c747b8147061a6897c3487efcd3abdf Mon Sep 17 00:00:00 2001 From: FernandoAvanzo Date: Sun, 20 Oct 2024 15:54:37 -0300 Subject: [PATCH] Start work in a auth lib to be used in the api scaffold --- authenticator/Readme.md | 16 +++++++++++ authenticator/build.gradle.kts | 25 +++++++++++++++++ .../main/kotlin/avanzo/ktor/auth/JwtConfig.kt | 14 ++++++++++ settings.gradle.kts | 4 ++- utils/Readme.md | 1 + utils/build-kotlin-folder-structure.sh | 27 +++++++++++++++++++ 6 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 authenticator/Readme.md create mode 100644 authenticator/build.gradle.kts create mode 100644 authenticator/src/main/kotlin/avanzo/ktor/auth/JwtConfig.kt create mode 100644 utils/Readme.md create mode 100755 utils/build-kotlin-folder-structure.sh diff --git a/authenticator/Readme.md b/authenticator/Readme.md new file mode 100644 index 0000000..36fa793 --- /dev/null +++ b/authenticator/Readme.md @@ -0,0 +1,16 @@ +### Authenticator Tool + +Tool to handle API authentication flows, including JWT token generation, validation, and management. + +### Roadmap + +- [ ] Create a Notion space to manage this project +- [ ] Finish building the AI scaffold suggestion +- [ ] Discover how to publish the library +- [ ] Discover how to use a local version of the library +- [ ] Add the library to the API project +- [ ] Implement an authentication flow in the API using the Auth library + + +### References +- [My Notion JSON Web Tokens](https://www.notion.so/fernando-avanzo/Doc-JSON-Web-Tokens-125b3def3e7c80099293f2b8ec520b9b?pvs=4) diff --git a/authenticator/build.gradle.kts b/authenticator/build.gradle.kts new file mode 100644 index 0000000..dde7a55 --- /dev/null +++ b/authenticator/build.gradle.kts @@ -0,0 +1,25 @@ +plugins { + kotlin("jvm") version "2.0.21" + `maven-publish` +} + +group = "avanzo.ktor.auth" +version = "1.0.0" + +repositories { + mavenCentral() +} + +dependencies { + implementation("io.ktor:ktor-server-auth:2.3.0") + implementation("io.ktor:ktor-server-auth-jwt:2.3.0") + testImplementation(kotlin("test")) +} + +publishing { + publications { + create("mavenJava") { + from(components["java"]) + } + } +} diff --git a/authenticator/src/main/kotlin/avanzo/ktor/auth/JwtConfig.kt b/authenticator/src/main/kotlin/avanzo/ktor/auth/JwtConfig.kt new file mode 100644 index 0000000..00d6271 --- /dev/null +++ b/authenticator/src/main/kotlin/avanzo/ktor/auth/JwtConfig.kt @@ -0,0 +1,14 @@ +package avanzo.ktor.auth + +import com.auth0.jwt.algorithms.Algorithm + + +data class JwtConfig( + val realm: String, + val issuer: String, + val secret: String, + val audience: String, + val expiration: Long // in milliseconds +) { + val algorithm: Algorithm = Algorithm.HMAC256(secret) +} diff --git a/settings.gradle.kts b/settings.gradle.kts index c3fce76..9d3c69c 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -4,4 +4,6 @@ plugins { } rootProject.name = "ktor-api-scaffold" -include("api") \ No newline at end of file +include("api") +include("authenticator") +include("utils") diff --git a/utils/Readme.md b/utils/Readme.md new file mode 100644 index 0000000..087b55f --- /dev/null +++ b/utils/Readme.md @@ -0,0 +1 @@ +# Scripts to help with boring tasks diff --git a/utils/build-kotlin-folder-structure.sh b/utils/build-kotlin-folder-structure.sh new file mode 100755 index 0000000..39c07c0 --- /dev/null +++ b/utils/build-kotlin-folder-structure.sh @@ -0,0 +1,27 @@ +#!/bin/bash +set -euo pipefail + +declare -r ROOT="$HOME/Projects/ktor-api-scaffold" +declare module=${1-"invalid path"} + + +if [ "$module" != "invalid path" ]; then + # Rest of your commands go here + declare -r module="$ROOT/$module" + +if [ -d "$module" ]; then + # Run your remaining commands here + mkdir -p "$module/src/main/kotlin" + mkdir -p "$module/src/main/resources" + mkdir -p "$module/src/test/kotlin" + mkdir -p "$module/src/test/resources" + +else + echo "Error: The provided path '$module' is not a valid folder." + exit 1 +fi + +else + echo "Path invalid" + exit 1 +fi