Compare commits

7 Commits

Author SHA1 Message Date
3a6fd107c0 Generate a version if no one is provided 2025-10-12 12:34:48 +02:00
4c31950a88 Reestructured in subprojects 2025-10-12 12:28:40 +02:00
aa71cb1d76 Fix bug 2025-10-07 23:22:08 +02:00
c746675045 Force compression even if file exists 2025-10-07 23:20:24 +02:00
4052995ba8 Native compilation for Windows 2025-10-01 16:07:23 +02:00
44f48fced8 better native compile script 2025-10-01 15:49:23 +02:00
c78e89a94b Use example 2025-10-01 00:08:27 +02:00
123 changed files with 1500 additions and 42 deletions

View File

@@ -17,6 +17,8 @@ You can read more about the algorithm here:
## How to use
![Use example](use_example.gif)
First, you must have installed a Java Runtime Environment. Check https://adoptium.net/es if you are
unsure how to install.

122
build.clj
View File

@@ -1,37 +1,101 @@
(ns build
(:require [clojure.tools.build.api :as b]))
(:refer-clojure :exclude [test])
(:require [clojure.tools.build.api :as b]
[clojure.java.io :as io]))
(def lib 'es.rcorral/clj-totp)
(def version (format "1.2.%s" (b/git-count-revs nil)))
(def target-dir "target")
(def class-dir (str target-dir "/classes"))
(def uber-file (format "target/%s-%s-standalone.jar" (name lib) version))
(def lib-group "es.rcorral")
(def artifact-prefix "clj-totp")
(def curr-version (format "2.0.%s" (b/git-count-revs nil)))
;; delay to defer side effects (artifact downloads)
(def basis (delay (b/create-basis {:project "deps.edn"})))
;; Builds artifact's full descriptor for each subproject
(defn lib [subproj]
(symbol (str lib-group "/" artifact-prefix "-" subproj )))
(defn clean [_]
(b/delete {:path "target"}))
;; Basis for each subproject, using their own deps.edn
(defn basis [subproj]
;(b/create-basis {:project (str subproj "/deps.edn")}))
(delay (b/create-basis {:project (str "projects/" subproj "/deps.edn")})))
(defn compile-java [_]
(b/javac {:src-dirs ["java"]
:class-dir class-dir
;; Target dir for each subproject
(defn target-dir [subproj]
(str "target/" subproj))
;; Path for compiled classes
(defn class-dir [subproj]
(str (target-dir subproj) "/" "classes"))
;; Jar file for each subproject. :uber type adds -standalone suffix
(defn jar-file [subproj version type]
(format "target/%s-%s-%s%s.jar" artifact-prefix subproj version
(if (= type :uber) "-standalone" "")))
;; Clean target dir for subproject
(defn clean [{:keys [subproj]}]
(b/delete {:path (target-dir subproj)})
(println "Project" subproj "cleaned"))
;; Compile java classes, only if java subdir exists
(defn compile-java [subproj]
(let [java-dir (str "projects/" subproj "/java")]
(if (.exists (io/file java-dir))
(do
(println "Compiling java code for" subproj)
(b/javac {:src-dirs [java-dir]
:class-dir (class-dir subproj)
:basis @(basis subproj)
:javac-opts ["-source" "11" "--target" "11" "-proc:none"]}))
(println "No java code for" subproj ", skipping compilation"))))
;; Create a jar file
(defn jar
"Build a simple jar file, with no dependencies included."
[{:keys [subproj version]}]
(let [real-version (if version version curr-version)
target-dir (target-dir subproj)
class-dir (class-dir subproj)
src-dir (str "projects/" subproj "/src")
resources-dir (str "projects/" subproj "/resources")
basis (basis subproj)
jar-file (jar-file subproj real-version :plain)]
;; Clean only class dir
(b/delete {:path class-dir})
;; Copy code
(b/copy-dir {:src-dirs [src-dir]
:target-dir class-dir})
;; Copy resources
(b/copy-dir {:src-dirs [resources-dir]
:target-dir target-dir})
;; Compile java code, if exists
(compile-java subproj)
;; Build jar
(b/jar {:class-dir class-dir
:basis @basis
:javac-opts ["-source" "11" "--target" "11" "-proc:none"]}))
:jar-file jar-file
:lib (lib subproj)
:version real-version})
(println "Generated jar file:" jar-file)))
;; Create an uber jar, with all dependencies inside
(defn uber
"Build a uberjar with all dependencies included"
[{:keys [subproj version main-ns]}]
(let [real-version (if version version curr-version)target-dir (target-dir subproj)
basis (basis subproj)
class-dir (class-dir subproj)
src-dir (str "projects/" subproj "/src")
resources-dir (str "projects/" subproj "/resources")
uber-file (jar-file subproj real-version :uber)]
(b/delete {:path class-dir})
(b/copy-dir {:src-dirs [src-dir]
:target-dir class-dir})
(b/copy-dir {:src-dirs [resources-dir]
:target-dir target-dir})
(compile-java subproj)
(b/compile-clj {:basis @basis
:src-dirs [src-dir] :class-dir class-dir})
(b/uber {:class-dir class-dir
:uber-file uber-file
:basis @basis
:main main-ns})
(println "Generated uberjar executable:" uber-file)))
#_{:clojure-lsp/ignore [:clojure-lsp/unused-public-var]}
(defn uber [_]
(clean nil)
(b/copy-dir {:src-dirs ["src"]
:target-dir class-dir})
(b/copy-file {:src "resources/clj-totp.sh"
:target "target/clj-totp.sh"})
(compile-java nil)
(b/compile-clj {:basis @basis
:ns-compile '[totp.app]
:class-dir class-dir})
(b/uber {:class-dir class-dir
:uber-file uber-file
:basis @basis
:main 'totp.app}))

View File

@@ -1,16 +1,9 @@
{:paths ["src" "resources" "target/classes"]
:deps {org.clojure/clojure {:mvn/version "1.12.1"}
io.github.clojure/tools.build {:mvn/version "0.10.10"}
mvxcvi/alphabase {:mvn/version "3.0.185"} ;; https://github.com/greglook/alphabase
cli-matic/cli-matic {:mvn/version "0.5.4"} ;; https://github.com/l3nz/cli-matic
com.github.clj-easy/graal-build-time {:mvn/version "1.0.5"} ;; Tutorial: https://shagunagrawal.me/posts/setup-clojure-with-graalvm-for-native-image/
;; Protobuf for java
com.google.protobuf/protobuf-java {:mvn/version "3.25.8"}
;; Progress bar
com.github.pmonks/spinner {:mvn/version "2.0.284"}
}
;; Native image (GraalVM)
com.github.clj-easy/graal-build-time {:mvn/version "1.0.5"}}
:aliases {;; Execute the app
:aliases {;; Execute the app. Tutorial: https://shagunagrawal.me/posts/setup-clojure-with-graalvm-for-native-image/
:run {:main-opts ["-m" "totp.app"]}
;; Kaocha runner. You can use the 'kaocha' wrapper located in ~/bin/kaocha
@@ -20,5 +13,46 @@
;; Run with clj -T:build function-in-build
:build {:deps {io.github.clojure/tools.build {:mvn/version "0.10.10"}}
:ns-default build}}}
:ns-default build}
;; COMMON ALIASES FOR ALL PROJECTS
:root/run-x {:exec-fn -main}
:root/extra-paths [:totp.core/extra-paths
:totp.cli/extra-paths
:totp.web/extra-paths]
:root/all {:extra-paths ["src" "resources"
:root/extra-paths]}
:root/test {:extra-paths ["test"]
:extra-deps {lambdaisland/kaocha {:mvn/version "1.91.1392"}}
:main-opts ["-m" "kaocha.runner"]}
:boot/build {:extra-paths ["build"]
:deps {io.github.clojure/tools.build {:mvn/version "0.10.10"}}
:ns-default build
;:exec-fn ci
;:exec-args {:app-alias :com.example.core}
}
:totp.core/extra-paths ["projects/core/src"
"projects/core/test"
"projects/core/java"
"projects/core/resources"]
:totp.core {:ns-default totp.core
:main-opts ["-m" "totp.core"]
:extra-deps {projects/core {:local/root "projects/core"}}
:exec-args {:dirs ["projects/core"]}}
:totp.cli/extra-paths ["projects/app/src"]
:totp.cli {:ns-default totp.cli
:main-opts ["-m" "totp.cli"]
:extra-deps {;; does not use parts/grugstack {:local/root "parts"}
projects/cli {:local/root "projects/cli"}}
:exec-args {:dirs ["projects/cli"]}}
:totp.web/extra-paths ["projects/web/src"]
}}

34
doc/db.plantuml Normal file
View File

@@ -0,0 +1,34 @@
@startuml
' configuration
skinparam linetype ortho
entity "user" as user {
id: number
--
login: varchar(64)
passw: varchar(512)
active: shortint
desc: varchar(512)
config: varchar(512)
}
entity "app" as app {
id: number
--
name: varchar(32)
desc: varchar(512)
secret: varchar(512)
period: int
config: varchar(512)
}
entity "user_app" as user_app {
user_id: number
app_id: number
--
}
user ||--o{ user_app
app ||--o{ user_app
@enduml

BIN
doc/db.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.9 KiB

View File

@@ -24,7 +24,7 @@ for /f "delims=" %%a in ('dir /b /s target\clj-totp-*-standalone.jar') do @set U
echo Created uberjar: %UBERJAR%
echo "Creating native image"
cmd /c %NATIVE% -jar %UBERJAR% -o target\%BIN_FILE% -H:+ReportExceptionStackTraces --features=clj_easy.graal_build_time.InitClojureClasses --report-unsupported-elements-at-runtime --verbose --no-fallback -H:ReflectionConfigurationFiles=reflect_config.json -H:-CheckToolchain
cmd /c %NATIVE% -jar %UBERJAR% -o target\%BIN_FILE% -H:+ReportExceptionStackTraces --features=clj_easy.graal_build_time.InitClojureClasses --report-unsupported-elements-at-runtime --verbose --no-fallback -H:ReflectionConfigurationFiles=reflect_config.json -H:-CheckToolchain --initialize-at-build-time=org.fusesource.jansi.Ansi
echo Executable created: target\%BIN_FILE%.exe

View File

@@ -14,9 +14,17 @@ $NATIVE -jar target/$UBERJAR -o target/$BIN_FILE\
--verbose --no-fallback\
--features=clj_easy.graal_build_time.InitClojureClasses\
--report-unsupported-elements-at-runtime\
--strict-image-heap\
-march=native\
-R:MaxHeapSize=10m\
--initialize-at-build-time=org.fusesource.jansi.Ansi\
#--trace-class-initialization=org.fusesource.jansi.Ansi
--initialize-at-build-time='org.fusesource.jansi.Ansi$Color'\
--initialize-at-build-time='org.fusesource.jansi.Ansi$Attribute'\
'--initialize-at-build-time=org.fusesource.jansi.Ansi$1'
echo "Executable created on target/$BIN_FILE"
cp target/$BIN_FILE ~/bin
echo "Copied to ~/bin/$BIN_FILE"
echo "Compress executable for distribution"
xz -fv target/$BIN_FILE

16
projects/cli/deps.edn Executable file
View File

@@ -0,0 +1,16 @@
{:paths ["src" "resources" "target/classes"]
:deps {clj-totp/core {:local/root "projects/core"}
org.clojure/clojure {:mvn/version "1.12.1"}
io.github.clojure/tools.build {:mvn/version "0.10.10"}
cli-matic/cli-matic {:mvn/version "0.5.4"} ;; https://github.com/l3nz/cli-matic
;; Progress bar
com.github.pmonks/spinner {:mvn/version "2.0.284"}}
:aliases {;; Execute the app
:run {:main-opts ["-m" "totp.app"]}
;; Kaocha runner. You can use the 'kaocha' wrapper located in ~/bin/kaocha
:test {:extra-paths ["test"]
:extra-deps {lambdaisland/kaocha {:mvn/version "1.91.1392"}}
:main-opts ["-m" "kaocha.runner"]}}}

1
projects/cli/tests.edn Normal file
View File

@@ -0,0 +1 @@
#kaocha/v1 {}

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,2 @@
-m
kaocha.runner

14
projects/core/deps.edn Executable file
View File

@@ -0,0 +1,14 @@
{:paths ["src" "resources" "target/classes"]
:deps {org.clojure/clojure {:mvn/version "1.12.1"}
io.github.clojure/tools.build {:mvn/version "0.10.10"}
mvxcvi/alphabase {:mvn/version "3.0.185"} ;; https://github.com/greglook/alphabase
cli-matic/cli-matic {:mvn/version "0.5.4"} ;; https://github.com/l3nz/cli-matic
;; Protobuf for java
com.google.protobuf/protobuf-java {:mvn/version "3.25.8"}
}
:aliases {;; Kaocha runner. You can use the 'kaocha' wrapper located in ~/bin/kaocha
:test {:extra-paths ["test"]
:extra-deps {lambdaisland/kaocha {:mvn/version "1.91.1392"}}
:main-opts ["-m" "kaocha.runner"]}}}

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -0,0 +1,101 @@
(ns totp.core
(:require [alphabase.base32 :as b32]
[clojure.math :as m])
(:import (javax.crypto Mac)
(javax.crypto.spec SecretKeySpec)
(java.util Base64 Arrays)
(java.nio ByteBuffer)))
(def ^:private byte-array-type (type (.getBytes "")))
(defn timestamp->steps
"Converts from UNIX timestamp in milliseconds to a number os steps of 's' seconds of duration"
[time, step-size]
(if (or (nil? time) (nil? step-size) (zero? step-size))
0
(int (quot time (* 1000 step-size)))))
(defn bytes-array?
"Return true if x is a byte[]"
[x]
(= byte-array-type (type x)))
(defn get-alg
[alg]
(case alg
"sha1" "HmacSHA1"
"sha256" "HmacSHA256"
"sha512" "HmacSHA512"
""))
(defmulti hmac
"Generates an HMAC. Algorithms supported: sha1, sha256, sha512.
The key and the message can be (both) string or array of bytes, nil otherwise"
(fn [algorithm key message]
(cond
(and (string? key) (string? message) (some? (get-alg algorithm))) :string
(and (bytes-array? key) (bytes-array? message) (some? (get-alg algorithm))) :byte
:else :nil)))
;; By default
(defmethod hmac :nil [_ _ _]
nil)
;; When key and message are strings
(defmethod hmac :string [algorithm key message]
(if (or (empty? key) (empty? message))
""
(let [mac (doto (Mac/getInstance (get-alg algorithm)) (.init (SecretKeySpec. (.getBytes key) (get-alg algorithm))))
hmac-bytes (.doFinal mac (.getBytes message))]
;; Return the Base64 encoded HMAC
(.encodeToString (Base64/getEncoder) hmac-bytes))))
;; When key and message are arrays of bytes
(defmethod hmac :byte [algorithm key message]
(if (nil? message)
(bytes (byte-array 0))
(let [mac (doto (Mac/getInstance (get-alg algorithm)) (.init (SecretKeySpec. key (get-alg algorithm))))
hmac-bytes (.doFinal mac message)]
;; Return the Base64 encoded HMAC
(Base64/getEncoder) hmac-bytes)))
(defn long->bytes
"Converts a long to an array of 8 bytes"
[l]
;;Java equivalent: ByteBuffer.allocate(Long.SIZE / Byte.SIZE).putLong(someLong).array();
(when (integer? l)
(-> (ByteBuffer/allocate (/ Long/SIZE Byte/SIZE))
(.putLong l)
(.array))))
(defn bytes->int
"Converts an array of 4 bytes to an integer"
[bytes]
;;Java equivalent: ByteBuffer.wrap(data).getInt()
(when (some? bytes)
(.getInt (ByteBuffer/wrap bytes))))
(defn get-otp
"Generate an OTP with the given secret (in base32) for the specified timestep"
([secret algorithm digits period] ;;algorithm digits period
(when (and secret period)
(let [step (timestamp->steps (System/currentTimeMillis) period)
k (b32/decode secret)
c (long->bytes step)
hs (hmac algorithm k c)
offset (bit-and (get hs (dec (count hs))) 0x0f) ;; int offset = hs[hs.length-1] & 0xf;
chunk (Arrays/copyOfRange hs offset (+ offset 4)) ;(take 4 (drop offset hs)) ;; byte[] chunk = Arrays.copyOfRange(hs, offset, offset+4)
]
(format (str "%0" digits "d")
(-> chunk
(bytes->int)
(bit-and 0x7fffffff)
(rem (int (m/pow 10 digits))))))))
([secret]
(get-otp secret "sha1" 6 30)))

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Some files were not shown because too many files have changed in this diff Show More