35 Commits

Author SHA1 Message Date
590f14d101 cleaning temporary files 2025-12-02 23:06:35 +01:00
d824b3ef72 cleaning and gui improvemenet 2025-11-24 00:48:16 +01:00
5b1c375934 Remove compiled classes 2025-11-23 22:25:44 +01:00
7080ea74fb Pure clojure import from proto 2025-11-23 22:09:34 +01:00
97d1d6e59b len type 2025-11-19 15:00:49 +01:00
9a858c8993 Array of bytes with varints to seq of ints 2025-11-19 09:52:50 +01:00
e003288004 implementing TLVs 2025-11-18 16:08:02 +01:00
d2c97bd5e5 pack TLV 2025-11-18 09:55:01 +01:00
1ec2db9583 Code cleaning 2025-11-17 15:50:18 +01:00
b8cd76b481 decimal to base uses euclidean division 2025-11-17 14:21:15 +01:00
7380362280 swap parameters order 2025-11-14 13:52:23 +01:00
34a365960a varint back to int 2025-11-14 12:00:34 +01:00
a52070dfa6 delete incorrect verification 2025-11-14 09:16:55 +01:00
76510be028 convert from base to decimal 2025-11-14 09:10:55 +01:00
a1fec08cc4 int to varint works! 2025-11-13 14:08:54 +01:00
556cc85cde binary nightmares 2025-11-12 16:02:45 +01:00
2ebac1676f Update TOTP every 30 seconds 2025-11-05 00:03:16 +01:00
b6749bdb29 update every second 2025-11-03 16:17:23 +01:00
1b141173cc Delete stale compiled files 2025-11-03 15:09:58 +01:00
06174de597 trying to update progress bar 2025-11-03 14:55:19 +01:00
e6523e0a7b status bar changes 2025-11-03 12:48:43 +01:00
78da3c37c0 Empty status bar 2025-11-03 11:52:48 +01:00
3d305a0d70 Unified delay function 2025-11-03 08:45:46 +01:00
6166e930fe Copy text to clipboard 2025-11-03 08:21:28 +01:00
5edbfa4ce4 GUI 2025-11-03 07:23:43 +01:00
1071d9e5ee Create a simple GUI with seesaw 2025-10-13 01:53:45 +02:00
017291f784 Full modularized project 2025-10-13 01:22:03 +02:00
17a7a09ab0 Fully modularized project 2025-10-13 01:21:36 +02:00
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
48 changed files with 3310 additions and 3009 deletions

2
.gitignore vendored
View File

@@ -1,5 +1,5 @@
/.clj-kondo/
/.cpcache/
/**/.cpcache/
/.lsp/
/target/
.nrepl-port

View File

@@ -152,6 +152,7 @@ clj-totp.sh import <alias> "<url>"
- [x] Native compilation script corrections
### v2
- [x] Restructurate as a multiproject
- [ ] REST API
- [ ] User management
- [ ] Robust BD backend (H2, datomic, or similar)
@@ -176,14 +177,14 @@ The first step is to install Java JDK, version 11 or newer (version 21 recommend
To execute manually the main function, simple use the `:run` alias:
```clojure
clojure -M:run <commands and parameters>
```bash
clojure -M:run/cli <commands and parameters>
```
To build the uberjar:
```clojure
clojure -T:build uber
```bash
clojure -T:build :uber/cli
```
There is a utility script to build a native executable using Graal VM. Please, edit the script and

201
build.clj
View File

@@ -1,37 +1,174 @@
(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]
[clojure.pprint :as pp]
[clojure.java.basis :as basis]))
(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))
;; delay to defer side effects (artifact downloads)
(def basis (delay (b/create-basis {:project "deps.edn"})))
(defn clean [_]
(b/delete {:path "target"}))
(defn compile-java [_]
(b/javac {:src-dirs ["java"]
:class-dir class-dir
:basis @basis
:javac-opts ["-source" "11" "--target" "11" "-proc:none"]}))
(def lib-group "es.rcorral")
(def artifact-prefix "clj-totp")
(def subprojs-base "projects")
(def curr-version (format "2.0.%s" (b/git-count-revs nil)))
;; Builds artifact's full descriptor for each subproject
(defn lib [subproj]
(symbol (str lib-group "/" artifact-prefix "-" subproj )))
;; Basis for each subproject, using their own deps.edn
;; Injects :extra-deps from :build as additional dependencies
(defn basis [subproj]
(delay (b/create-basis {:project (str subprojs-base "/" subproj "/deps.edn")
;; Inject extra deps as deps
:extra {:deps (get-in (basis/initial-basis) [:aliases :build :extra-deps])}
})))
;; Show basis generated for a subproject
#_{: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}))
(defn show-basis [subproj]
(println (with-out-str
(pp/pprint
@(basis subproj)
;(basis/initial-basis)
))))
(comment
(pp/pprint (keys (basis/initial-basis)))
(pp/pprint (:deps (basis/initial-basis)))
(pp/pprint (:libs (basis/initial-basis)))
(pp/pprint (sort (keys (:aliases (basis/initial-basis)))))
(get-in (basis/initial-basis) [:aliases :build :extra-deps])
)
;; 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 subprojs-base "/" subproj "/java")]
(when (.exists (io/file java-dir))
(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"]}))))
;; Create a jar file
(defn jar
"Build a simple jar file, with no dependencies included."
[{:keys [subproj version]
:or {version curr-version}}]
(let [target-dir (target-dir subproj)
class-dir (class-dir subproj)
src-dir (str subprojs-base "/" subproj "/src")
resources-dir (str subprojs-base "/" subproj "/resources")
basis (basis subproj)
jar-file (jar-file subproj 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
:jar-file jar-file
:lib (lib subproj)
:version version})
(println "Generated jar file:" jar-file)))
;; Create an uber jar, with all dependencies inside
#_{:clojure-lsp/ignore [:clojure-lsp/unused-public-var]}
(defn uber
"Build a uberjar with all dependencies included"
[{:keys [subproj version main-ns]
:or {version curr-version}}]
(let [target-dir (target-dir subproj)
basis (basis subproj)
class-dir (class-dir subproj)
src-dir (str subprojs-base "/" subproj "/src")
resources-dir (str subprojs-base "/" subproj "/resources")
uber-file (jar-file subproj version :uber)]
;(println "Using basis: ")(show-basis subproj)
(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)))
;; Multimethod to get the name of all subdirs in a dir.
;; Accepts strings or files
(defmulti get-subdirs type)
(defmethod get-subdirs
java.lang.String [dir]
(get-subdirs (io/file dir)))
(defmethod get-subdirs
java.io.File [dir]
(if (.isDirectory dir)
(filter #(.isDirectory %) (.listFiles dir))
(println "Directory" subprojs-base "doesn't exists!")))
;; Get the name of all subdir in a given directory
(defn get-subdir-names
"Get a list projects in the 'projects' directory"
[dir-name]
(map #(.getName %) (get-subdirs dir-name)))
(comment
(get-subdirs "projects")
(get-subdirs (io/file "projects"))
(get-subdir-names "projects")
)
;; Generate jar files for all projects
(defn jar-all
"Build jar files for all projects"
[& {:keys [version]
:or {version curr-version}}]
(dorun (map #(jar {:subproj % :version version}) (get-subdir-names subprojs-base))))
(comment
(jar-all )
)

View File

@@ -1 +1,11 @@
~/.sdkman/candidates/java/21.0.2-graalce/bin/java -agentlib:native-image-agent=config-output-dir=META-INF/native-image -jar target/clj-topt-1.0.32-standalone.jar g TUGOBTEHPSCMUCYAT6UPELNWGE -c
#!/usr/bin/env sh
JAVA_CMD=~/.sdkman/candidates/java/21.0.2-graalce/bin/java
UBERJAR=$(realpath --relative-to=. target/clj-totp-cli-*-standalone.jar)
echo "Using uberjar $UBERJAR"
$JAVA_CMD -agentlib:native-image-agent=config-output-dir=target/native-image\
-jar $UBERJAR import "deleteme" "otpauth-migration://offline?data=CkkKEJ0M4MyHfITKCwCfqPIttjESFHJ1YmVuY2pAMThCMTY5RDVGRjAwGgRTTldMIAEoATACQhMzYjkxMDQxNzI3NzgzNDIzNDYyEAIYASAA"
java -jar $UBERJAR delete "deleteme"

View File

@@ -1,4 +0,0 @@
#!/bin/env sh
protoc --java_out java/protoc/ resources/proto/otpauth-migration.proto
#javac -cp resources/protobuf-java-3.25.8.jar -d target/classes/proto src/OtpauthMigration.java

View File

@@ -1,24 +1,74 @@
{: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). Tutorial: https://shagunagrawal.me/posts/setup-clojure-with-graalvm-for-native-image/
com.github.clj-easy/graal-build-time {:mvn/version "1.0.5"}
;; Local subprojects
clj-totp/core {:local/root "projects/core"}
clj-totp/cli {:local/root "projects/cli"}
clj-totp/web {:local/root "projects/web"}
clj-totp/gui {:local/root "projects/gui"}
}
:aliases {;; Execute the app
:run {:main-opts ["-m" "totp.app"]}
:aliases {;; Execute the app.
:run {:main-opts ["-m" "totp.app"]}
;:run {:exec-fn totp.app/-main}
;; Execute the app (prepared for more subprojects)
:run/cli {:main-opts ["-m" "totp.app"]}
:run/gui {:main-opts ["-m" "totp.gui"]}
;; 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"}}
;; Check test.edn for kaocha runner's config
:test {:extra-deps {lambdaisland/kaocha {:mvn/version "1.91.1392"}
lambdaisland/kaocha-cloverage {:mvn/version "1.1.89"}}
:main-opts ["-m" "kaocha.runner"]}
;; Run with clj -T:build function-in-build
:build {:deps {io.github.clojure/tools.build {:mvn/version "0.10.10"}}
:ns-default build}}}
;; Used by all compilations
:extra-deps {clj-totp/core {:local/root "projects/core"}}
:ns-default build}
;; Aliases for easy building
:build/core {:deps {io.github.clojure/tools.build {:mvn/version "0.10.10"}}
:ns-default build
:exec-fn jar
:exec-args {:subproj "core"}}
:build/cli {:deps {io.github.clojure/tools.build {:mvn/version "0.10.10"}
clj-totp/core {:local/root "projects/core"}}
:ns-default build
:exec-fn jar
:exec-args {:subproj "cli"}}
:build/web {:deps {io.github.clojure/tools.build {:mvn/version "0.10.10"}}
:replace-deps {clj-totp/core {:local/root "projects/core"}}
:ns-default build
:exec-fn jar
:exec-args {:subproj "web"}}
:build/gui {:deps {io.github.clojure/tools.build {:mvn/version "0.10.10"}}
:replace-deps {clj-totp/core {:local/root "projects/core"}}
:ns-default build
:exec-fn jar
:exec-args {:subproj "gui"}}
;; Build the three libraries
:build/all {:deps {io.github.clojure/tools.build {:mvn/version "0.10.10"}}
:replace-deps {clj-totp/core {:local/root "projects/core"}}
:ns-default build
:exec-fn jar-all}
;; Build uber jar for CLI app
:uber/cli {:deps {io.github.clojure/tools.build {:mvn/version "0.10.10"}}
:ns-default build
:exec-fn uber
:exec-args {:subproj "cli" :main-ns "totp.app"}}
;; Build uber jar for CLI app
:uber/gui {:deps {io.github.clojure/tools.build {:mvn/version "0.10.10"}}
:ns-default build
:exec-fn uber
:exec-args {:subproj "gui" :main-ns "totp.app"}}}}

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

File diff suppressed because it is too large Load Diff

View File

@@ -4,8 +4,10 @@ NATIVE=~/.sdkman/candidates/java/21.0.2-graalce/bin/native-image
BIN_FILE=totp
echo "Creating uberjar"
#clojure -T:build uber
UBERJAR=$(realpath --relative-to=target target/clj-totp-*-standalone.jar)
#clojure -T:uber/cli
UBERJAR=$(realpath --relative-to=target target/clj-totp-cli-*-standalone.jar)
echo "Using uberjar $UBERJAR"
echo "Creating native image"
$NATIVE -jar target/$UBERJAR -o target/$BIN_FILE\
@@ -17,14 +19,17 @@ $NATIVE -jar target/$UBERJAR -o target/$BIN_FILE\
--strict-image-heap\
-march=native\
-R:MaxHeapSize=10m\
--trace-object-instantiation=java.lang.Thread\
--initialize-at-build-time=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'
--initialize-at-build-time='org.fusesource.jansi.Ansi$1'\
--initialize-at-run-time=cljc_long.constants__init
echo "Executable created on target/$BIN_FILE"
cp target/$BIN_FILE ~/bin
echo "Copied to ~/bin/$BIN_FILE"
echo "Compress executable for distribution"
xz target/$BIN_FILE
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 "../core"}
org.clojure/clojure {:mvn/version "1.12.1"}
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"}
com.github.clj-easy/graal-build-time {:mvn/version "1.0.5"}}
: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 {}

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"}
mvxcvi/alphabase {:mvn/version "3.0.185"} ;; https://github.com/greglook/alphabase
;; Protobuf for java
com.google.protobuf/protobuf-java {:mvn/version "3.25.8"}
;; Dynamic protobuf
com.github.s-expresso/clojobuf {:mvn/version "0.2.1"}
}
: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"]}}}

View File

@@ -99,3 +99,29 @@
(rem (int (m/pow 10 digits))))))))
([secret]
(get-otp secret "sha1" 6 30)))
(defn calculate-offset-millis
[period]
(let [step-millis (* 1000 period)
now (System/currentTimeMillis)]
(int (rem now step-millis))))
(defn calculate-delay-millis
[period]
(let [step-millis (* 1000 period)]
(- step-millis (calculate-offset-millis period))))
(comment
(let [now (System/currentTimeMillis)
off (calculate-offset-millis 30)
delay (calculate-delay-millis 30)
]
(println "Now: " (int (/ now 1000)) "secs" now "millis")
(println "Offset:" (int (/ off 1000)) "secs" off "millis")
(println "Delay: " (int (/ delay 1000)) "secs" delay "millis")
(println "Total: " (+ (int (/ off 1000)) (int (/ delay 1000))) "secs" (+ delay off) "millis")
)
)

View File

@@ -1,11 +1,11 @@
(ns totp.data
(:require [clojure.edn :as e]
(:require [totp.otp-proto :as proto]
[clojure.edn :as e]
[clojure.string :as str]
[clojure.java.io :as io]
[clojure.pprint :as pp]
[alphabase.base64 :as b64]
[alphabase.base32 :as b32])
(:import [protoc OtpauthMigration$MigrationPayload]))
[alphabase.base32 :as b32]))
(defn join-path
"Joins several subpaths using system's path separator (/ un *NIX and \\ in windows)"
@@ -89,11 +89,16 @@
(defn list-apps
[cfg]
(map :name
(filter #(contains? % :name) cfg)))
(->> cfg
(filter #(contains? % :name))
(map :name)))
(comment
(list-apps (load-config)))
(list-apps (load-config))
(with-config #_{:clj-kondo/ignore [:unresolved-symbol]}
(list-apps cfg))
)
(defn get-app
[cfg name]
@@ -202,28 +207,26 @@
(when (some? url)
(let [b64-data (second (str/split url #"=" -1))
data-b (b64/decode b64-data)
parsed (OtpauthMigration$MigrationPayload/parseFrom data-b)
payload (bean (.getOtpParameters parsed 0))
;{:keys [name secret name issuer digitsValue algorithmValue typeValue]} payload
secret-b (:secret payload)
secret (b32/encode (.toByteArray secret-b))
payload (proto/parse-data data-b)
;{:keys [name secret name issuer digits algorithm type]} payload
secret (:secret payload)
user (:name payload)
issuer (:issuer payload)
algorithm (case (:algorithmValuei payload) 2 "sha256" 3 "sha512" "sha1")
digits (case (:digitsValue payload) 2 8 6)
valid-type (= 2 (:typeValue payload))
algorithm (:algorithm payload)
digits (:digits payload)
valid-type (= 2 (:type payload))
]
(println "name:" name "user:" user "issuer:" issuer "digitsValue:" digits "algorithm:" algorithm "valid type?" valid-type)
;(println "name:" name "user:" user "issuer:" issuer "digits:" digits "algorithm:" algorithm "valid type?" valid-type)
(if valid-type
(create-app name secret user issuer algorithm digits 30)
(println "Invalid OTP type" (:typeValue payload)))
(println "Invalid OTP type" (:type payload)))
)))
(comment
(url-export->app "test"
"otpauth-migration://offline?data=CkkKEJ0M4MyHfITKCwCfqPIttjESFHJ1YmVuY2pAMThCMTY5RDVGRjAwGgRTTldMIAEoATACQhMzYjkxMDQxNzI3NzgzNDIzNDYyEAIYASAA"
)
"otpauth-migration://offline?data=CkkKEJ0M4MyHfITKCwCfqPIttjESFHJ1YmVuY2pAMThCMTY5RDVGRjAwGgRTTldMIAEoATACQhMzYjkxMDQxNzI3NzgzNDIzNDYyEAIYASAA"
)
)

View File

@@ -0,0 +1,385 @@
(ns totp.otp-import
(:require [alphabase.bytes :as b]
[alphabase.base16 :as hex]
[alphabase.base64 :as b64]
[alphabase.base2 :as b2]
[clojure.math :as m]
[clojure.string :as s]))
;; Original description of the export protocol uses Google's Protocol Buffers
;; https://protobuf.dev/
(def WIRE_TYPES {:varint 0 ;; Integeres of variable length encoding
:i64 1 ;; Fixed 64 bits numbers (integer or decimal)
:len 2 ;; Block of bytes with a predefined length
:sgroup 3 ;; Group end (deprecated)
:egroup 4 ;; Group start (deprecated)
:i32 5 ;; Fixed 32 bits number (integer or decimal)
})
(comment
(let [encoded "CkkKEJ0M4MyHfITKCwCfqPIttjESFHJ1YmVuY2pAMThCMTY5RDVGRjAwGgRTTldMIAEoATACQhMzYjkxMDQxNzI3NzgzNDIzNDYyEAIYASAA"
decoded (b64/decode encoded)]
(print (hex/encode decoded))
(print (b/to-string decoded))))
(defn to-fancy-bin
"Return a string with bits from number in a fancy manner"
[x]
(s/join " "
(map s/join
(partition 8 (b2/encode (byte-array (map #(b/to-byte %) x)))))))
(defn to-fancy-hex
"Return a string with hex values from number in a fancy manner"
([x]
(to-fancy-hex x 2))
([x group-size]
(s/join " "
(map s/join
(partition group-size (hex/encode (byte-array (map #(b/to-byte %) x))))))))
(defn len-bits
"How may blocks of n bits are needed to encode this number?
We use the following formula, for calculating the number of digits required
to encode the number n in the base b:
digits = ceil ( log_n ( x + 1 ) )
"
[n x]
(case x
nil 0 ;; nill is encoded with zero bytes
0 1 ;; One block to zero
9223372036854775807 (len-bits n (dec x)) ;; Beware the overflow!! it's best to lose some precision
(when (and (>= x 0) (some? n) (> n 0))
(long (m/ceil (/ (m/log (inc x)) (m/log (m/pow 2 n))))))))
(comment
(len-bits 8 513)
(len-bits 8 Long/MAX_VALUE)
(len-bits 8 (dec Long/MAX_VALUE)))
(defn len-bytes
"How may bytes are needed to encode this number?"
[x]
(len-bits 8 x))
(defn integer>bytes
"Converts an integer to a byte array"
[x]
(when x (let [len (len-bytes x)
hex-len (* 2 len)]
(hex/decode (String/format (str "%0" hex-len "x") (into-array [x]))))))
(comment
(integer>bytes 513)
10r3
3r10)
(defn euclidean-quot
"Euclidean division for integers
More info: https://en.wikipedia.org/wiki/Euclidean_division
"
[a b]
(when (zero? b)
(throw (IllegalArgumentException. "You can't divide by zero!")))
(let [b-abs (Math/abs b)
r (mod a b-abs)] ;; 0 <= r < |b|
(quot (- a r) b) ;; adjusts quotient with the positive remainder
))
(defn euclidean-rem
"Modulus for euclidean division for integers
More info: https://en.wikipedia.org/wiki/Euclidean_division
"
[a b]
(when (zero? b)
(throw (IllegalArgumentException. "You can't divide by zero!")))
(let [b-abs (Math/abs b)]
(mod a b-abs);; 0 <= r < |b|
))
(defn euclid-div
"Euclidean division. Returns quotient and remainder as a map. The remainder is
allways a positive number.
More info: https://en.wikipedia.org/wiki/Euclidean_division"
[a b]
(when (zero? b)
(throw (IllegalArgumentException. "You can't divide by zero!")))
(let [b-abs (Math/abs b)
r (mod a b-abs) ;; 0 <= r < |b|
q (quot (- a r) b)] ;; adjusts quotient with the positive remainder
{:q q :r r}))
(comment
(euclid-div 7 3)
(euclid-div -7 3)
(euclid-div 7 -3)
(euclid-div -7 -3))
(defn decimal->base
"Converts a decimal number to an arbitrary base. Each digit is encoded as an
integer with value between 0 and base-1.
For example, 10 in base 4 will be encoded as:
[2 0 2]
, 127 in base 126 will be:
[1 1]
and so on.
We will use the sucessive division method: do a integer division of the number
to the base until the quotient is zero, and take the reminders backwards. For
example, if we want to converto 127 to base 5:
127 / 5 = 25 rem 2
25 / 5 = 5 rem 0
5 / 5 = 1 rem 0
1 / 5 = 0 rem 1
so, 127 in base 5 is 1002.
The maximum digits this method can return is 65. Max size for a long is base 2
is 64, so if you obtain a result with 65 digits is very probable that the
conversion has failed.
If the number is negative and the base is possitive, we return the conversion
of the absolute value. You must take care of the sign in your implementation.
There are negative bases as -2 that can encode possitive and negative numbers.
"
([base n]
(decimal->base base n true))
([base n reverse?]
(if (or (nil? n) (nil? base) (zero? n) (zero? base))
[0] ;; Allways [0] for base zero or number zero
(loop [acc []
x (if (and (< n 0) (> base 0)) (abs n) n)] ;; If n < 0 and b > 0 => |n|, else n
(let [q (euclidean-quot x base)
r (euclidean-rem x base)]
;;(printf "Acc: %s Calculating: %s/%s -> %s rem %s%n" acc x base q r)
(if (or (zero? x) (> (count acc) (inc Long/SIZE))) ;; max digits is 65
(vec (if reverse? (reverse acc) acc)) ;; When x is zero, we have finished
(recur
(conj acc r) ;; Accumulate the remainder
q ;; Pass the quotient to the next step
)))))))
(comment
(decimal->base 2 8)
2r1000
(decimal->base 2 3)
(letfn [(step [x b name q r]
(printf "%s -> %d/%d = %d rem %d%n" name x b q r))
(examples [x b exp-q exp-r]
(step x b "EXPECTED " exp-q exp-r)
(step x b "IEEE " (int (m/IEEE-remainder x b)) (rem x b))
(step x b "With rem " (quot x b) (rem x b))
(step x b "With mod " (quot x b) (mod x b))
(step x b "floor " (m/floor-div x b) (m/floor-mod x b))
(step x b "Manual 1 " (int (clojure.math/round (double (/ x b)))) (rem x b))
(step x b "Manual 2 " (int (clojure.math/rint (double (/ x b)))) (rem x b))
(step x b "Unchecked " (unchecked-divide-int x b) (unchecked-remainder-int x b))
(step x b "Euclidean " (euclidean-quot x b) (euclidean-rem x b)))]
(examples -3 -2 2 1)
(println)
(examples 2 -2 -1 0)
(println)
(examples -1 -2 1 1)
(println)
(examples 1 -2 0 1)
(println)
(println)
(examples 7 3 2 1)
(println)
(examples -7 3 -3 2)
(println)
(examples 7 -3 -2 1)
(println)
(examples -7 -3 3 2)))
(defn base->decimal
"Converts from an array with values in an arbitrary base into decimal values"
[base n]
(if (or (nil? n) (nil? base) (zero? (count n)))
0
(long (reduce-kv
(fn [acc k v]
(+ acc (* v (m/pow base k))))
0 (vec (reverse n))))))
(comment
(reduce-kv #(+ %1 (* %3 (m/pow 2 %2))) 0 [1 0 0 0]) ;; backwards!
(base->decimal 2 [1 0 0 0]))
(defn int->varint
"Converts a integer value to a varint, that is encoded in 7 bits, where the first
bit is used to indicate if there are more bytes.
For example, the number 255 is usually encoded in a byte as follows:
11111111
but in a varint it will turn to:
10000001 01111111
First byte has the MSB set to 1, because there is another byte after it. The
second byte is the last one, so the MSB is set to 0.
The result is a byte array.
"
[x]
(let [b128 (decimal->base 128 x false)]
(byte-array (conj
(vec (map #(bit-or 2r10000000 %) (butlast b128)))
(bit-and 2r01111111 (peek b128))))))
(defn varint->int
"Converts a varint to an integer. Each byte in varint uses the MSB as a continuation
bit: if it's value is 1, there are more bites, if it's 0 it's the last block.
For example, this varint with two bytes:
10000001 01111111
first byte's MSB has value 1, so there is another byte. The second one has a zero
in the MSB, so it's the last block. To calculate the final value, you must ignore
the MSB and concatenate both bytes:
110010110 00000001 -> original value
0010110 0000001 -> delete MSB
0000001 0010110 -> reverse bytes (little-endian to big endian)
00000010010110 -> concatenate
00000000 10010110 -> fill the bytes
"
[x]
(base->decimal 128 (reverse (map #(bit-and 2r01111111 %) x))))
(comment
;; 150
(to-fancy-bin [-106, 1])
(to-fancy-bin (map #(bit-and 2r01111111 %) [-106, 1]))
(to-fancy-bin (reverse (map #(bit-and 2r01111111 %) [-106, 1])))
(varint->int [-106, 1])
(varint->int [-106, 1])
(varint->int [-105, 1])
;; Long/MAX_VALUE
(to-fancy-bin (map #(bit-and 2r01111111 %) [-1, -1, -1, -1, -1, -1, -1, -1, 128]))
(varint->int [-2, -1, -1, -1, -1, -1, -1, -1, 127]))
(defn more-blocks?
"True if the MSB bit is 1"
[b]
(when b
(< 0 (bit-and 2r10000000 b))))
(comment
(bit-and 2r10000000 2r10000001)
(bit-and 2r10000000 2r00000001)
( more-blocks? 2r10000001)
(more-blocks? 2r00000001)
)
(defn extract-varint-blocks
"Group varints in a byte array"
[bytes]
(loop [acc [] ;; Groups of varints
group [] ;; Current varint
r bytes] ;; Current tested byte
(if (empty? r) ;; Final condition: no more bytes to test
(if ((complement empty?) group)
(conj acc group) ;; Return acc with the last group added if not empty
acc) ;; if empty, return acc
(if (more-blocks? (first r))
;; If more blocks remains:
(recur acc
(conj group (first r)) ;; add current byte to current group
(next r))
;; If it's last in varint:
(recur (->> (first r)
(conj group) ;; add byte to group
(conj acc)) ;; add group to accumulator
[] ;; and start a new empty group
(next r))))))
(comment
(extract-varint-blocks [2r11111111 2r01111111, 2r10000001 2r10101001 2r00000001, 2r00000111])
)
(defn extract-varints
"Group varints in an byte array and convert them to decimal"
[bytes]
(map varint->int (extract-varint-blocks bytes)))
(comment
(extract-varints [2r11111111 2r01111111, 2r10000001 2r10101001 2r00000001, 2r00000111])
(varint->int [2r11111111 2r01111111])
(varint->int [2r10000001 2r10101001 2r00000001])
(varint->int [2r00000111])
)
(defn bytes->len-type
"Prepend byte array with length in varint format"
[bytes]
(concat (int->varint (count bytes))
bytes))
(comment
(to-fancy-hex (bytes->len-type (.getBytes "testing")))
)
(defn pack-bytes-as-tlv
"Pack the value as TLV.
Type can one of those 6 IDs:
ID Name Used For
0 VARINT int32, int64, uint32, uint64, sint32, sint64, bool, enum
1 I64 fixed64, sfixed64, double
2 LEN string, bytes, embedded messages, packed repeated fields
3 SGROUP group start (deprecated)
4 EGROUP group end (deprecated)
5 I32 fixed32, sfixed32, float
"
([{:keys [field type value]}]
(pack-bytes-as-tlv field type value))
([field-number type value]
(let [field-displaced (bit-shift-left field-number 3)
tag (int->varint (bit-or field-displaced type))]
(concat tag value))))
(defn unpack-tlv-bytes
"Returns a map with 3 pairs:
- :field is the field number
- :type is one of the values of WIRE_TYPES
- :value is the byte array with the payload"
[packed]
(let [first-byte (bit-and 2r01111111 (first packed)) ;; bye bye, MSB
type (bit-and 2r00000111 first-byte)
field-number (bit-shift-right first-byte 3)]
{:field field-number :type type :value (rest packed)}))

View File

@@ -0,0 +1,45 @@
(ns totp.otp-proto
(:require [clojobuf.core :refer [protoc decode]]
[alphabase.base64 :as b64]
[alphabase.base32 :as b32]))
;; Where lookup for proto files
(def registry (protoc ["resourcse/proto/"
"projects/core/resources/proto/"]
["otpauth-migration.proto"]))
(defn parse-data
[binary-data]
(let [decoded (decode registry :MigrationPayload binary-data)
msg (get-in decoded [:otp_parameters 0])]
;(println "Decoded:" msg)
(when msg
{:secret (b32/encode (:secret msg))
:name (:name msg)
:issuer (:issuer msg)
:algorithm (case (:algorithm msg)
:ALGORITHM_SHA256 "sha256"
:ALGORITHM_SHA512 "sha512"
:ALGORITHM_MD5 "md5"
"sha1") ;; sha1 by default
:digits (case (:digits msg)
:DIGIT_COUNT_EIGHT 8
6) ;; 6 digits by default
:type (case (:type msg)
:OTP_TYPE_UNSPECIFIED 0
:OTP_TYPE_HOTP 1
:OTP_TYPE_TOTP 2) ;; Only TOTP is supported
})))
(comment
(let [b64-data "CkkKEJ0M4MyHfITKCwCfqPIttjESFHJ1YmVuY2pAMThCMTY5RDVGRjAwGgRTTldMIAEoATACQhMzYjkxMDQxNzI3NzgzNDIzNDYyEAIYASAA"
bin-data (b64/decode b64-data)
decoded (decode registry :MigrationPayload bin-data)]
;(get-in decoded [:otp_parameters 0])
(parse-data bin-data)
)
)

View File

@@ -0,0 +1,149 @@
(ns totp.otp-import-test
#_{:clj-kondo/ignore [:refer-all]}
(:require [clojure.test :refer :all]
[totp.otp-import :refer :all]
[alphabase.bytes :as b]
[alphabase.base16 :as hex]
[alphabase.base64 :as b64]
[alphabase.base32 :as b32])
(:import (java.util Arrays)))
(deftest len-bits-test
(testing "Check required number of blocks to encode a number in n bits"
(is (nil? (len-bits nil 10)))
(is (= 0 (len-bits 10 nil)))
(is (= 1 (len-bits 2 1)))
(is (= 2 (len-bits 2 10)))
(is (= 2 (len-bits 2 15)))
(is (= 3 (len-bits 2 16)))
(is (= 1 (len-bits 8 255)))
(is (= 2 (len-bits 7 255)))
)
)
(deftest len-bytes-test
(testing "Check required number of bytes to encode a number"
(is (= 0 (len-bytes nil)))
(is (= 1 (len-bytes 0)))
(is (= 1 (len-bytes 1)))
(is (= 1 (len-bytes 255)))
(is (= 2 (len-bytes 256)))
(is (= 2 (len-bytes 65535)))
(is (= 3 (len-bytes 65536)))
)
)
(deftest integer>bytes-test
(testing "Convert several ints to a byte array"
(is (nil? (integer>bytes nil)))
(is (b/bytes= (b/init-bytes [0]) (integer>bytes 0)))
(is (b/bytes= (b/init-bytes [1]) (integer>bytes 1)))
(is (b/bytes= (b/init-bytes [-1]) (integer>bytes 255)))
(is (b/bytes= (b/init-bytes [1 0]) (integer>bytes 256)))
(is (b/bytes= (b/init-bytes [-1 -1]) (integer>bytes 65535)))
(is (b/bytes= (b/init-bytes [127 -1 -1 -1 -1 -1 -1 -1]) (integer>bytes Long/MAX_VALUE)))
)
)
(deftest decimal->base-test
(testing "Convert from decimal base to an arbitrary base"
(is (= [0] (decimal->base nil 10)))
(is (= [0] (decimal->base 10 nil)))
(is (= [0] (decimal->base 2 0)))
(is (= [0] (decimal->base 0 2)))
(is (= [1 0 0 0] (decimal->base 2 8)))
(is (= [2 2] (decimal->base 3 8)))
(is (= [2 0] (decimal->base 4 8)))
(is (= [1 3] (decimal->base 5 8)))
(is (= [3 1] (decimal->base 5 8 false)))
(is (= [0] (decimal->base 5 0)))
)
)
(deftest base->decimal-test
(testing "Convert from arbitrary base to decimal"
(is (zero? (base->decimal 2 nil)))
(is (zero? (base->decimal 2 [])))
(is (zero? (base->decimal nil [1])))
(is (= 8 (base->decimal 2 [1 0 0 0])))
(is (= 8 (base->decimal 3 [2 2])))
(is (= 8 (base->decimal 4 [2 0])))
(is (= 8 (base->decimal 5 [1 3])))
)
)
(deftest decimal->base-decimal-test
(testing "Check if convert from decimal to a base and back preserves the original number"
(is (= 8 (base->decimal 2 (decimal->base 2 8))))
(is (= 127 (base->decimal 2 (decimal->base 2 127))))
(is (= 417 (base->decimal 13 (decimal->base 13 417))))
)
)
(deftest int->varint-test
(testing "Convert from integer number (int, long, byte, etc) to varint"
(is (Arrays/equals (byte-array [0]) (int->varint nil)))
(is (Arrays/equals (byte-array [0]) (int->varint 0)))
(is (Arrays/equals (byte-array [2r10010110 2r00000001]) (int->varint 150)))
)
)
(deftest varint->int-test
(testing "Convert from barint to long"
(is (= 0 (varint->int nil)))
(is (= 0 (varint->int [0])))
(is (= 150 (varint->int [-106, 1])))
(is (= 150 (varint->int [22 1])))
(is (= 151 (varint->int [-105, 1])))
)
)
(deftest int->varint->int-test
(testing "Convert from int to varint and back to int"
(is (= 150 (varint->int (int->varint 150))))
(is (= 151 (varint->int (int->varint 151))))
)
)
(deftest more-blocks?-test
(testing "Check if there are more blocks in a varint type"
(is (nil? (more-blocks? nil)))
(is (true? (more-blocks? 2r10000001)))
(is (true? (more-blocks? 2r11111111)))
(is (false? (more-blocks? 2r00000001)))
(is (false? (more-blocks? 2r01111111)))
)
)
(deftest extract-varint-blocks-test
(testing "Group varints"
(is (empty? (extract-varint-blocks nil)))
(is (empty? (extract-varint-blocks [])))
(is (= [[2r00000111]] (extract-varint-blocks [2r00000111])))
(is (= [[2r00000111] [2r00000111]] (extract-varint-blocks [2r00000111 2r00000111])))
(is (= [[2r11111111 2r01111111] [2r10000001 2r10101001 2r00000001] [2r00000111]] (extract-varint-blocks [2r11111111 2r01111111, 2r10000001 2r10101001 2r00000001, 2r00000111])))
(is (= [[2r11111111 2r01111111] [2r10000001 2r10101001 2r00000001] [2r10000111]] (extract-varint-blocks [2r11111111 2r01111111, 2r10000001 2r10101001 2r00000001, 2r10000111])))
)
)
(deftest extract-varints-test
(testing "bytes with varints to decimal"
(is (empty? (extract-varints nil)))
(is (empty? (extract-varints [])))
(is (= [7] (extract-varints [2r00000111])))
(is (= [7 7] (extract-varints [2r00000111 2r00000111])))
(is (= [16383 21633 7] (extract-varints [2r11111111 2r01111111, 2r10000001 2r10101001 2r00000001, 2r00000111])))
(is (= [16383 21633 7] (extract-varints [2r11111111 2r01111111, 2r10000001 2r10101001 2r00000001, 2r10000111])))
)
)

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

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

8
projects/gui/deps.edn Executable file
View File

@@ -0,0 +1,8 @@
{:paths ["src" "target/classes"]
:deps {;;org.clojure/clojure {:mvn/version "1.12.1"}
cli-matic/cli-matic {:mvn/version "0.5.4"} ;; https://github.com/l3nz/cli-matic
;; GUI
seesaw/seesaw {:mvn/version "1.5.0"}
com.github.clj-easy/graal-build-time {:mvn/version "1.0.5"}}
}

View File

@@ -0,0 +1,172 @@
(ns totp.gui
#_{:clj-kondo/ignore [:refer-all]}
(:require [totp.core :refer :all]
[totp.data :refer :all]
[clojure.pprint :as pp]
[seesaw.core :refer :all]
[seesaw.mig :refer :all]
[seesaw.clipboard :as cp]
[seesaw.dev :refer :all])
(:import [java.util Date TimerTask Timer])
(:gen-class))
(defn copy-handler
"Copies TOTP from text with id field-id, to system clipboard"
[field-id e]
(let [b-name (str "#" field-id)
b-obj (select (to-root e) [(keyword b-name)])
b-text (config b-obj :text)]
(println "Copying text value: " b-text)
(cp/contents! b-text)))
(defn make-otp-list
"Make panel with OTPs"
[]
(scrollable
(mig-panel
:constraints ["wrap 3"
"[shrink 0]20px[200, grow, fill]10px[shrink 0]"]
:items (let [apps (with-config (filter some? #_{:clj-kondo/ignore [:unresolved-symbol]} cfg))]
(reduce (fn [acc a]
(let [{:keys [name secret algorithm digits period]} a
field-id (str "field-totp-" name)]
(-> acc
(conj [name])
(conj [(text :text (get-otp secret algorithm digits period)
:editable? false
:id field-id)])
(conj [(action :name "copy"
:handler (partial copy-handler field-id)
:command (str "cmd-" name))]))))
[] apps)))))
(comment
(make-otp-list))
(defn make-time-bar
"Make the progress bar with the remaining time"
[init-val]
(border-panel
:hgap 5
:center (progress-bar :id "timer-bar"
:value init-val
:max 30
:paint-string? true)
:east (text :id "timer-text"
:text init-val
:editable? false
:columns 2
:halign :right)))
(defn make-add-frame
[parent]
(frame :title "Add new TOTP"
:minimum-size [320 :by 200]
:size [320 :by 220]
:on-close :dispose
:content (border-panel
:center (mig-panel
:constraints ["wrap 2"
"[shrink 0]20px[200, grow, fill]"]
:items [["Name"] [(text :id "add-name" :columns 32)]
["Secret (B32)"] [(text :id "add-secret" :columns 32)]
["User (optional)"] [(text :id "add-user" :columns 32)]
["Issuer (optional)"] [(text :id "add-issuer" :columns 32)]
["Algorithm"] [(combobox :model ["sha1" "sha256" "sha512"])]
["Digits"] [(combobox :model ["6" "8"])]])
:south (flow-panel :align :right :items [(action :name "Cancel")
(action :name "Add")]))))
(defn make-main-buttons
[]
(flow-panel :align :right
:items ["button a" "button b"]))
(defn make-frame-content
[]
(border-panel :hgap 10 :vgap 10
:center (make-otp-list)
:north (make-time-bar (int (/ (calculate-offset-millis 30) 1000)))
:south (make-main-buttons)
;:east "EAST"
;:west "WEST"
))
(defn make-frame
"Make main frame's content"
[]
(frame :title "TOTP",
:content (make-frame-content)
;:minimum-size [320 :by 240]
;;:on-close :exit
:on-close :dispose))
(defn update-totps
"Update all totps"
[root]
(let [apps (with-config (filter some? #_{:clj-kondo/ignore [:unresolved-symbol]} cfg))]
(doseq [app apps]
(let [{:keys [name secret algorithm digits period]} app
field-id (str "field-totp-" name)
field (select root [(keyword (str "#" field-id))])
current-otp (get-otp secret algorithm digits period)]
;;(println "Updating" field-id "with otp" current-otp)
(config! field :text current-otp)))))
(defn update-progress
[root]
(let [time-bar (select root [:#timer-bar])
time-text (select root [:#timer-text])
offset (inc (int (/ (calculate-offset-millis 30) 1000)))]
;;(println "Updating at at" (System/currentTimeMillis))
(config! time-bar :value offset)
(config! time-text :text offset)
(when (= 1 offset)
;;(println "update TOTP")
(update-totps root))))
(defn start-updater
[root]
(let [now (System/currentTimeMillis)
now-seconds (int (/ now 1000))
delay (- 1000 (- now (* 1000 now-seconds)))]
(. (new Timer) (scheduleAtFixedRate
(proxy [TimerTask] []
(run [] (update-progress root)))
delay 1000))
(println "Now" now "Delay" delay)))
(defn -main [& args]
(native!)
(invoke-later
(-> (make-frame)
pack!
show!
(make-add-frame)
start-updater))
(println "Gui started"))
(comment
;; This kills your REPL connection
(-main)
(show-options (frame))
(show-options (text))
(-> (make-add-frame nil)
(pack!)
(show!)))

18
projects/web/deps.edn Executable file
View File

@@ -0,0 +1,18 @@
{:paths ["src" "resources" "target/classes"]
:deps {;clj-totp/core {:local/root "../core"}
org.clojure/clojure {:mvn/version "1.12.1"}
;; For SQLite
com.github.seancorfield/next.jdbc {:mvn/version "1.3.1048"}
org.xerial/sqlite-jdbc {:mvn/version "3.50.3.0"}
;; For Datomic local
com.datomic/local {:mvn/version "1.0.291"};; https://docs.datomic.com/datomic-local.html
}
: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"]}}}

View File

@@ -0,0 +1,68 @@
(ns totp.db.datomic
(:require [totp.data :as data]
[datomic.client.api :as d]))
(def cfg-path (data/join-path (System/getProperty "user.home") ".config" "totp"))
(def db-path (str cfg-path java.io.File/separator "data"))
(def cfg {:server-type :datomic-local
:system "local-data"
:storage-dir db-path})
(def client (d/client cfg))
;; Schema for our database
(def totp-schema [{:db/ident :app/name
:db/valueType :db.type/string
:db/cardinality :db.cardinality/one
:db/unique :db.unique/identity
:db/doc "Unique name of the application. Between 2 and 32 chars"
:db.attr/preds (fn [s] (<= 3 (count s) 15))}
{:db/ident :app/desc
:db/valueType :db.type/string
:db/cardinality :db.cardinality/one
:db/doc "(optional) Description of the application"}
{:db/ident :app/secret
:db/valueType :db.type/string
:db/cardinality :db.cardinality/one
:db/doc "Secret BASE32"}
{:db/ident :app/period
:db/valueType :db.type/long
:db/cardinality :db.cardinality/one
:db/doc "Time slice in seconds (30 by default)"}
{:db/ident :app/config
:db/valueType :db.type/string
:db/cardinality :db.cardinality/one
:db/doc "(Optional) Extra config"}
{:db/ident :user/login
:db/valueType :db.type/string
:db/cardinality :db.cardinality/one
:db/unique :db.unique/identity
:db/doc "Identifier for the user"}
{:db/ident :user/passwd
:db/valueType :db.type/string
:db/cardinality :db.cardinality/one
:db/doc "Password"}
{:db/ident :user/active
:db/valueType :db.type/boolean
:db/cardinality :db.cardinality/one
:db/doc "Is the user active?"}
{:db/ident :user/desc
:db/valueType :db.type/string
:db/cardinality :db.cardinality/one
:db/doc "(Optional) Description of the user"}
{:db/ident :user/config
:db/valueType :db.type/string
:db/cardinality :db.cardinality/one
:db/doc "(Optional) Extra config"}
{:db/ident :user/apps
:db/valueType :db.type/ref
:db/cardinality :db.cardinality/many
:db/doc "Applications for this user"}])
(defn init-db
[client]
(d/create-database client {:db-name "totp"}))

View File

@@ -0,0 +1,23 @@
(ns totp.db.sqlite
(:require [next.jdbc :as jdbc]))
;; DB configuration
(def db {:dbname "totp-data.sqlite"
:dbtype "sqlite"})
;; DB parsed config
(def data-source (jdbc/get-datasource db))
(defn init-db
"Create an empty DB"
[]
(jdbc/execute! data-source ["
create table apps (
id int auto_increment primary key,
name varchar(32),
desc varchar(255)
)"]))
(comment
(init-db)
)

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

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

Binary file not shown.

File diff suppressed because it is too large Load Diff

270
reflect_config.json.bak Normal file
View File

@@ -0,0 +1,270 @@
[
{
"name": "com.sun.crypto.provider.HmacSHA1",
"methods": [
{
"name": "<init>",
"parameterTypes": []
}
]
},
{
"name": "java.lang.reflect.Method",
"methods": [
{
"name": "canAccess",
"parameterTypes": [
"java.lang.Object"
]
}
]
},
{
"name": "java.util.Arrays",
"allDeclaredClasses": true,
"allPublicClasses": true,
"queryAllPublicMethods": true,
"methods": [
{
"name": "copyOfRange",
"parameterTypes": [
"byte[]",
"int",
"int"
]
}
]
},
{
"name": "java.util.Timer",
"queryAllPublicMethods": true,
"methods": [
{
"name": "scheduleAtFixedRate",
"parameterTypes": [
"java.util.TimerTask",
"long",
"long"
]
}
]
},
{
"name": "java.util.concurrent.locks.Lock"
},
{
"name": "java.util.concurrent.locks.ReentrantLock"
},
{
"name": "rubberbuf.ast_postprocess$eval194",
"methods": [
{
"name": "<init>",
"parameterTypes": []
}
]
},
{
"name": "rubberbuf.ast_postprocess$eval209",
"methods": [
{
"name": "<init>",
"parameterTypes": []
}
]
},
{
"name": "rubberbuf.ast_postprocess$eval224",
"methods": [
{
"name": "<init>",
"parameterTypes": []
}
]
},
{
"name": "rubberbuf.ast_postprocess$eval242",
"methods": [
{
"name": "<init>",
"parameterTypes": []
}
]
},
{
"name": "rubberbuf.ast_postprocess$eval331",
"methods": [
{
"name": "<init>",
"parameterTypes": []
}
]
},
{
"name": "rubberbuf.ast_postprocess__init"
},
{
"name": "rubberbuf.ast_preprocess$eval148",
"methods": [
{
"name": "<init>",
"parameterTypes": []
}
]
},
{
"name": "rubberbuf.ast_preprocess$eval15",
"methods": [
{
"name": "<init>",
"parameterTypes": []
}
]
},
{
"name": "rubberbuf.ast_preprocess$eval179",
"methods": [
{
"name": "<init>",
"parameterTypes": []
}
]
},
{
"name": "rubberbuf.ast_preprocess$eval254",
"methods": [
{
"name": "<init>",
"parameterTypes": []
}
]
},
{
"name": "rubberbuf.ast_preprocess$eval265",
"methods": [
{
"name": "<init>",
"parameterTypes": []
}
]
},
{
"name": "rubberbuf.ast_preprocess$eval279",
"methods": [
{
"name": "<init>",
"parameterTypes": []
}
]
},
{
"name": "rubberbuf.ast_preprocess$eval290",
"methods": [
{
"name": "<init>",
"parameterTypes": []
}
]
},
{
"name": "rubberbuf.ast_preprocess$eval301",
"methods": [
{
"name": "<init>",
"parameterTypes": []
}
]
},
{
"name": "rubberbuf.ast_preprocess$eval314",
"methods": [
{
"name": "<init>",
"parameterTypes": []
}
]
},
{
"name": "rubberbuf.ast_preprocess$eval321",
"methods": [
{
"name": "<init>",
"parameterTypes": []
}
]
},
{
"name": "rubberbuf.ast_preprocess__init"
},
{
"name": "rubberbuf.ast_util__init"
},
{
"name": "rubberbuf.core__init"
},
{
"name": "rubberbuf.ebnf__init"
},
{
"name": "rubberbuf.parse__init"
},
{
"name": "rubberbuf.parse_textformat__init"
},
{
"name": "rubberbuf.util__init"
},
{
"name":"clojobuf.constant__init"
},
{
"name":"clojobuf.core__init"
},
{
"name":"clojobuf.decode__init"
},
{
"name":"clojobuf.encode__init"
},
{
"name":"clojobuf.schema$eval367",
"methods":[{"name":"<init>","parameterTypes":[] }]
},
{
"name":"clojobuf.schema$eval396",
"methods":[{"name":"<init>","parameterTypes":[] }]
},
{
"name":"clojobuf.schema__init"
},
{
"name":"clojobuf.util__init"
},
{
"name":"clojobuf_codec.decode__init"
},
{
"name":"clojobuf_codec.deserialize__init"
},
{
"name":"clojobuf_codec.encode__init"
},
{
"name":"clojobuf_codec.io.reader.ByteReader"
},
{
"name":"clojobuf_codec.io.reader__init"
},
{
"name":"clojobuf_codec.io.writer.ByteWriter"
},
{
"name":"clojobuf_codec.io.writer__init"
},
{
"name":"clojobuf_codec.serialize__init"
},
{
"name":"clojobuf_codec.util__init"
}
]

View File

@@ -1 +1,6 @@
#kaocha/v1 {}
#kaocha/v1
{:tests [{:test-paths ["projects/core/src" "projects/core/test"]}]
:plugins [:kaocha.plugin/cloverage]
:cloverage/opts {:src-ns-path ["projects/core/src" "projects/core/test"]
:ns-regex ["totp\\..*(?<!test)$"] ;; All starting with "totp" but not ending by "test"
}}