Skip to content

Commit

Permalink
1. Added patch (See java/src/lockfix) to work around the issue - http…
Browse files Browse the repository at this point in the history
…s://dev.clojure.org/jira/browse/CLJ-1472

2. Added make file with command 'apply-patch' to compile the patch with clojure-1.10.0 in classpath
3. Added cli-matic as dependency to support this app as a command line app
  • Loading branch information
ashwinbhaskar committed May 20, 2019
1 parent c192c75 commit 9544ea3
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 14 deletions.
4 changes: 4 additions & 0 deletions Makefile
@@ -0,0 +1,4 @@
SHELL := /usr/bin/env bash -e

apply-patch:
javac ./java/src/lockfix/LockFix.java -cp ~/.m2/repository/org/clojure/clojure/1.10.0/clojure-1.10.0.jar
11 changes: 11 additions & 0 deletions java/src/lockfix/LockFix.java
@@ -0,0 +1,11 @@
package lockfix;

import clojure.lang.IFn;

public class LockFix {
static public Object lock(final Object lockee, final IFn f) {
synchronized (lockee) {
return f.invoke();
}
}
}
4 changes: 3 additions & 1 deletion project.clj
Expand Up @@ -4,6 +4,8 @@
:license {:name "EPL-2.0 OR GPL-2.0-or-later WITH Classpath-exception-2.0"
:url "https://www.eclipse.org/legal/epl-2.0/"}
:dependencies [[org.clojure/clojure "1.10.0"]
[commons-codec/commons-codec "1.12"]]
[commons-codec/commons-codec "1.12"]
[cli-matic "0.1.14"]]
:main google-authenticator.core
:java-source-paths ["java/src"]
:repl-options {:init-ns google-authenticator.core})
68 changes: 62 additions & 6 deletions src/google_authenticator/core.clj
@@ -1,11 +1,34 @@
(ns google-authenticator.core
(:require [cli-matic.core :refer [run-cmd]])
(:import
[org.apache.commons.codec.binary Base32]
(javax.crypto Mac)
(javax.crypto.spec SecretKeySpec))
(javax.crypto.spec SecretKeySpec)
(lockfix LockFix))
(:gen-class))


(defmacro locking* ;; patched version of clojure.core/locking to workaround GraalVM unbalanced monitor issue
"Executes exprs in an implicit do, while holding the monitor of x.
Will release the monitor of x in all circumstances."
{:added "1.0"}
[x & body]
`(let [lockee# ~x]
(LockFix/lock lockee# (^{:once true} fn* [] ~@body))))

(defn dynaload ;; patched version of clojure.spec.gen.alpha/dynaload to use patched locking macro
[s]
(let [ns (namespace s)]
(assert ns)
(locking* #'clojure.spec.gen.alpha/dynalock
(require (symbol ns)))
(let [v (resolve s)]
(if v
@v
(throw (RuntimeException. (str "Var " s " is not on the classpath")))))))

(alter-var-root #'clojure.spec.gen.alpha/dynaload (constantly dynaload))

(defn decode-base32 [base32str]
(.decode (new Base32) base32str))

Expand Down Expand Up @@ -62,9 +85,42 @@
(mod truncated-hash
(Math/pow 10 6))))))

(defn -main [path-to-secret-key & args]
(let [secret-key (apply str (remove #{\newline} (slurp path-to-secret-key)))
(defn gen-otp-and-write-to-file [{:keys [secret-key-file-path output-file]}]
(let [secret-key (apply str (remove #{\newline} (slurp secret-key-file-path)))
otp (get-otp secret-key)]
(if (empty? args)
(println otp)
(spit (nth args 0) otp))))
(spit output-file otp)))

(defn print-otp [{:keys [secret-key]}]
(println (get-otp secret-key)))


(def CONFIGURATION
{:app {:command "google-authenticator"
:description "A command-line to generate your google authenticator OTP"
:version "0.1"}
:global-opts [{:option "base"
:as "The number base for output"
:type :int
:default 10}]
:commands [{:command "gen-otp-to-tile" :short "o"
:description ["Reads secret key from a file, "
"Generates google authenticator otp and writes it to a file"
"Looks great, doesn't it?"]
:opts [{:option "secret-key-file-path" :short "skf" :as "Second addendum" :type :string}
{:option "output-file" :short "opf" :env "AA" :as "First addendum" :type :string}]
:runs gen-otp-and-write-to-file}
{:command "gen-otp" :short "p"
:description "Generates google authenticator otp and prints it to the console"
:opts [{:option "secret-key" :short "sk" :as "Parameter A" :type :string}]
:runs print-otp}]})

(defn -main
"This is our entry point.
Just pass parameters and configuration.
Commands (functions) will be invoked as appropriate."
[& args]
(run-cmd args CONFIGURATION))

(comment
(gen-otp-and-write-to-file {:secret-key-file-path "/Users/ashwinbhaskar/.secret_key" :output-file "/Users/ashwinbhaskar/.otp"})
(print-otp {:secret-key "j4ok7qmclj23gwa336rrjrv254usaeig"}))
7 changes: 0 additions & 7 deletions test/google_authenticator/core_test.clj

This file was deleted.

0 comments on commit 9544ea3

Please sign in to comment.