Compare commits

15 Commits

11 changed files with 95 additions and 209 deletions

View File

@@ -145,11 +145,16 @@ clj-totp.sh import <alias> "<url>"
- [x] Show several OTPs at once - [x] Show several OTPs at once
### v1.2 ### v1.2
- [x] Show progress bar
- [x] Styles for progress bar
- [x] Native compilation script corrections
### v2
- [ ] REST API - [ ] REST API
- [ ] User management - [ ] User management
- [ ] Robust BD backend (H2, datomic, or similar) - [ ] Robust BD backend (H2, datomic, or similar)
### v1.3 ### v3
- [ ] Simple web connected to REST API - [ ] Simple web connected to REST API

View File

@@ -2,7 +2,7 @@
(:require [clojure.tools.build.api :as b])) (:require [clojure.tools.build.api :as b]))
(def lib 'es.rcorral/clj-totp) (def lib 'es.rcorral/clj-totp)
(def version (format "1.1.%s" (b/git-count-revs nil))) (def version (format "1.2.%s" (b/git-count-revs nil)))
(def target-dir "target") (def target-dir "target")
(def class-dir (str target-dir "/classes")) (def class-dir (str target-dir "/classes"))
(def uber-file (format "target/%s-%s-standalone.jar" (name lib) version)) (def uber-file (format "target/%s-%s-standalone.jar" (name lib) version))

View File

@@ -1,42 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="BuildSystem">
<option name="buildSystemId" value="CLOJURE_DEPS" />
<option name="displayName" value="clj-totp" />
</component>
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/test" isTestSource="true" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Deps: org.clojure/clojure:1.12.1" level="project" />
<orderEntry type="library" name="Deps: lambdaisland/deep-diff2:2.11.216" level="project" />
<orderEntry type="library" name="Deps: org.clojure/core.specs.alpha:0.4.74" level="project" />
<orderEntry type="library" name="Deps: lambdaisland/kaocha:1.91.1392" level="project" />
<orderEntry type="library" name="Deps: expound:0.9.0" level="project" />
<orderEntry type="library" name="Deps: org.clojure/spec.alpha:0.5.238" level="project" />
<orderEntry type="library" name="Deps: org.clojure/tools.cli:1.1.230" level="project" />
<orderEntry type="library" name="Deps: lambdaisland/clj-diff:1.4.78" level="project" />
<orderEntry type="library" name="Deps: net.incongru.watchservice/barbary-watchservice:1.0" level="project" />
<orderEntry type="library" name="Deps: slingshot:0.12.2" level="project" />
<orderEntry type="library" name="Deps: fipp:0.6.26" level="project" />
<orderEntry type="library" name="Deps: com.nextjournal/beholder:1.0.2" level="project" />
<orderEntry type="library" name="Deps: aero:1.1.6" level="project" />
<orderEntry type="library" name="Deps: lambdaisland/tools.namespace:0.3.256" level="project" />
<orderEntry type="library" name="Deps: mvxcvi/arrangement:2.1.0" level="project" />
<orderEntry type="library" name="Deps: io.methvin/directory-watcher:0.17.3" level="project" />
<orderEntry type="library" name="Deps: progrock:0.1.2" level="project" />
<orderEntry type="library" name="Deps: org.clojure/java.classpath:1.0.0" level="project" />
<orderEntry type="library" name="Deps: clojure.java-time:1.4.3" level="project" />
<orderEntry type="library" name="Deps: org.clojure/core.rrb-vector:0.1.2" level="project" />
<orderEntry type="library" name="Deps: net.java.dev.jna/jna:5.12.1" level="project" />
<orderEntry type="library" name="Deps: org.clojure/tools.reader:1.3.6" level="project" />
<orderEntry type="library" name="Deps: org.tcrawley/dynapath:1.1.0" level="project" />
<orderEntry type="library" name="Deps: org.slf4j/slf4j-api:1.7.36" level="project" />
<orderEntry type="library" name="Deps: hawk:0.2.11" level="project" />
<orderEntry type="library" name="Deps: meta-merge:1.0.0" level="project" />
</component>
</module>

View File

@@ -3,13 +3,7 @@
io.github.clojure/tools.build {:mvn/version "0.10.10"} io.github.clojure/tools.build {:mvn/version "0.10.10"}
mvxcvi/alphabase {:mvn/version "3.0.185"} ;; https://github.com/greglook/alphabase 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 cli-matic/cli-matic {:mvn/version "0.5.4"} ;; https://github.com/l3nz/cli-matic
;; For SQLite com.github.clj-easy/graal-build-time {:mvn/version "1.0.5"} ;; Tutorial: https://shagunagrawal.me/posts/setup-clojure-with-graalvm-for-native-image/
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
;; Native image (GraalVM)
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 ;; Protobuf for java
com.google.protobuf/protobuf-java {:mvn/version "3.25.8"} com.google.protobuf/protobuf-java {:mvn/version "3.25.8"}
;; Progress bar ;; Progress bar

View File

@@ -1,34 +0,0 @@
@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

Binary file not shown.

Before

Width:  |  Height:  |  Size: 7.9 KiB

View File

@@ -8,7 +8,14 @@ clojure -T:build uber
UBERJAR=$(realpath --relative-to=target target/clj-totp-*-standalone.jar) UBERJAR=$(realpath --relative-to=target target/clj-totp-*-standalone.jar)
echo "Creating native image" echo "Creating native image"
$NATIVE -jar target/$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 $NATIVE -jar target/$UBERJAR -o target/$BIN_FILE\
-H:+ReportExceptionStackTraces\
-H:ReflectionConfigurationFiles=./reflect_config.json\
--verbose --no-fallback\
--features=clj_easy.graal_build_time.InitClojureClasses\
--report-unsupported-elements-at-runtime\
--initialize-at-build-time=org.fusesource.jansi.Ansi\
#--trace-class-initialization=org.fusesource.jansi.Ansi
echo "Executable created on target/$BIN_FILE" echo "Executable created on target/$BIN_FILE"
cp target/$BIN_FILE ~/bin cp target/$BIN_FILE ~/bin

View File

@@ -9,44 +9,58 @@
(:import [java.util TimerTask Timer]) (:import [java.util TimerTask Timer])
(:gen-class)) (:gen-class))
(def DEFAULT_BAR_STYLE :coloured-ascii-boxes)
(defn print-timer
([] (print-timer 1 30 DEFAULT_BAR_STYLE))
([bar-style] (print-timer 1 30 bar-style))
([start period bar-style]
(let [a (atom start)]
(pd/animate! a :opts {:total period
;:line 1
:label "Next TOTP: "
;:redraw-rate 60 ;; updates per second
:style (get pd/styles bar-style)}
;(println)
(run! (fn [_] (Thread/sleep 1000) (swap! a inc)) (range start (inc period)))
;(println)
))))
(defn- print-confinuous (defn- print-confinuous
([secret] (print-confinuous secret "sha1" 6 30)) ([secret] (print-confinuous secret "sha1" 6 30 true DEFAULT_BAR_STYLE))
([secret algorithm digits period] ([secret algorithm digits period bar bar-style]
(let [step-millis (* 1000 period) (let [step-millis (* 1000 period)
now (System/currentTimeMillis) now (System/currentTimeMillis)
delay (int (- step-millis (rem now step-millis))) delay (int (- step-millis (rem now step-millis)))
fn-show (fn [s] (println (format "[%d] %s" (System/currentTimeMillis) (get-otp s algorithm digits period)))) delay-sec (int (/ delay 1000))
fn-show (fn [s] (println (format "%n[%d] %s%n"
(System/currentTimeMillis)
(get-otp s algorithm digits period))))
task (proxy [TimerTask] [] task (proxy [TimerTask] []
(run [] (fn-show secret))) (run [] (println) (fn-show secret)))
a (atom (int (- period (/ delay 1000)))) task-bar (proxy [TimerTask] []
up-task (proxy [TimerTask] [] (run [] (print-timer bar-style)))
(run [] (pd/animate! a :opts {:total period task-init (proxy [TimerTask] []
:style (:coloured-ascii-boxes pd/styles) (run [] (print-timer (- period delay-sec) period bar-style)))]
:label "Next TOTP"
:redraw-rate 1
;:line 1
}
;(println "inc" @a)
(if (< @a period)
(swap! a inc)
(reset! a 0)
))))]
(println "\n <Generating continuosly, press enter to stop>\n") (println "\n <Generating continuosly, press enter to stop>\n")
;; (println "Now:" now ", Delay:" delay ", Next execution: " (+ now delay)) ;; (println "Now:" now ", Delay:" delay ", Next execution: " (+ now delay))
(println "Refresing in" (int (/ delay 1000)) "seconds") (println "Refresing in" delay-sec "seconds")
(fn-show secret) (fn-show secret)
(. (new Timer) (scheduleAtFixedRate up-task 0 1000)) ;; Now, start the tasks
(. (new Timer) (scheduleAtFixedRate task delay step-millis)) (when bar
) (. (new Timer) (schedule task-init 0))
(read-line))) ;; Waits for a key press (. (new Timer) (scheduleAtFixedRate task-bar delay step-millis)))
(. (new Timer) (scheduleAtFixedRate task delay step-millis)))
;; Waits for a key press
(read-line)))
(defn cmd-generate (defn cmd-generate
[& {:keys [secret continuous algorithm digits period]}] [& {:keys [secret continuous algorithm digits period bar bar-style]}]
;;(pp/pprint opts) ;;(pp/pprint opts)
(if continuous (if continuous
(print-confinuous secret algorithm digits period) (print-confinuous secret algorithm digits period bar bar-style)
(println (get-otp secret algorithm digits period)))) (println (get-otp secret algorithm digits period))))
@@ -65,28 +79,45 @@
(let [step-millis (* 1000 period) (let [step-millis (* 1000 period)
now (System/currentTimeMillis) now (System/currentTimeMillis)
delay (int (- step-millis (rem now step-millis))) delay (int (- step-millis (rem now step-millis)))
delay-sec (int (/ delay 1000))
fn-show (fn [s] fn-show (fn [s]
(println "\n")
(dorun (map print-app s)) (dorun (map print-app s))
(println "")) ;; Separate each (println) ;; Separate each
)
task (proxy [TimerTask] [] task (proxy [TimerTask] []
(run [] (fn-show apps)))] (run [] (fn-show apps)))]
(println "\n <Generating continuosly, press enter to stop>\n") (println "\n <Generating continuosly, press enter to stop>\n")
;; (println "Now:" now ", Delay:" delay ", Next execution: " (+ now delay)) ;; (println "Now:" now ", Delay:" delay ", Next execution: " (+ now delay))
(println "Refresing in" (int (/ delay 1000)) "seconds") (println "Refresing in" delay-sec "seconds")
(fn-show apps) (fn-show apps)
;; Now, start the tasks
(. (new Timer) (scheduleAtFixedRate task delay step-millis))) (. (new Timer) (scheduleAtFixedRate task delay step-millis)))
(read-line))) ;; Waits for a key press )) ;; Waits for a key press
(defn cmd-get-multi (defn cmd-get-multi
[& {:keys [continuous _arguments]}] [& {:keys [continuous bar bar-style _arguments]}]
;(pp/pprint opts) ;(pp/pprint opts)
(with-config (with-config
(let [apps (filter some? #_{:clj-kondo/ignore [:unresolved-symbol]} (let [apps (filter some? #_{:clj-kondo/ignore [:unresolved-symbol]}
(map #(get-app cfg %) _arguments))] (map #(get-app cfg %) _arguments))]
;(println "found apps: " apps) ;(println "found apps: " apps)
(if continuous (if continuous
(print-app-continuous 30 apps) (let [period 30
step-millis (* 1000 period)
now (System/currentTimeMillis)
delay (int (- step-millis (rem now step-millis)))
delay-sec (int (/ delay 1000))
task-bar (proxy [TimerTask] []
(run [] (print-timer bar-style)))
task-init (proxy [TimerTask] []
(run [] (print-timer (- period delay-sec) period bar-style)))]
(print-app-continuous period apps)
(when bar
(. (new Timer) (schedule task-init 0))
(. (new Timer) (scheduleAtFixedRate task-bar delay step-millis)))
(read-line))
(dorun (map #(print-app %) apps)))))) (dorun (map #(print-app %) apps))))))
@@ -169,7 +200,7 @@
(def cli-options (def cli-options
{:app {:command "totp" {:app {:command "totp"
:version "1.1" :version "1.2"
:description ["Generate a TOTP"]} :description ["Generate a TOTP"]}
:commands [;; Generate a TOTP with given params :commands [;; Generate a TOTP with given params
@@ -198,7 +229,15 @@
{:option "period" :short "p" {:option "period" :short "p"
:as "Validity time in seconds" :as "Validity time in seconds"
:type :int :type :int
:default 30}] :default 30}
{:option "bar" :short "b"
:as "Show progress bar"
:type :with-flag
:default true}
{:option "bar-style" :short "s"
:as "Progress bar style"
:type #{:ascii-basic :ascii-boxes :coloured-ascii-boxes :emoji-circles :emoji-boxes}
:default :coloured-ascii-boxes}]
:runs cmd-generate} :runs cmd-generate}
;; Generate a TOTP for a configured app ;; Generate a TOTP for a configured app
{:command "get" :short "g" {:command "get" :short "g"
@@ -214,7 +253,15 @@
{:option "continuous" :short "c" {:option "continuous" :short "c"
:as "Contiuous mode" :as "Contiuous mode"
:type :with-flag :type :with-flag
:default false}] :default false}
{:option "bar" :short "b"
:as "Show progress bar"
:type :with-flag
:default true}
{:option "bar-style" :short "s"
:as "Progress bar style"
:type #{:ascii-basic :ascii-boxes :coloured-ascii-boxes :emoji-circles :emoji-boxes}
:default :coloured-ascii-boxes}]
:runs cmd-get-multi} :runs cmd-get-multi}
;; Check and init your config file ;; Check and init your config file
{:command "config" :short "c" {:command "config" :short "c"

View File

@@ -1,68 +0,0 @@
(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

@@ -1,23 +0,0 @@
(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)
)

Binary file not shown.