Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
517758fdcf | ||
|
|
923c3e185a | ||
|
|
d0487ddae9 | ||
|
|
31d500a955 | ||
|
|
b9e82711ec | ||
|
|
47d59988c2 | ||
|
|
8645471410 |
No files matched your search
@@ -0,0 +1,63 @@
|
|||||||
|
# totpin
|
||||||
|
|
||||||
|
A command-line tool to generate TOTP codes (RFC 6238).
|
||||||
|
|
||||||
|
Nothing is stored: totpin asks for a base32 secret, or a full
|
||||||
|
`otpauth://totp/...` URI (which also gives the algorithm, the number of
|
||||||
|
digits and the period), without echoing it. Two codes are then shown, the
|
||||||
|
current one and the next one, each with its countdown. Press any key (or
|
||||||
|
Ctrl-C) to quit.
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
```
|
||||||
|
totpin [-h] [-v] [--licenses]
|
||||||
|
```
|
||||||
|
|
||||||
|
| Option | Effect |
|
||||||
|
|------------------|-----------------------------------------------------------|
|
||||||
|
| `-h`, `--help` | Show the help, then exit. |
|
||||||
|
| `-v`, `--version`| Show the version, then exit. |
|
||||||
|
| `--licenses` | Show the license of totpin and those of the libraries it uses, then exit. |
|
||||||
|
|
||||||
|
Accepted input:
|
||||||
|
|
||||||
|
- a base32 secret (spaces and `=` are ignored);
|
||||||
|
- an `otpauth://totp/...` URI. The `secret`, `algorithm` (`SHA1`, `SHA256` or
|
||||||
|
`SHA512`), `digits` (6 or 8), `period` and `issuer` parameters are used.
|
||||||
|
Defaults: `SHA1`, 6 digits, 30 seconds.
|
||||||
|
|
||||||
|
## Build
|
||||||
|
|
||||||
|
Requires OCaml 5.4 or later, [dune](https://dune.build) and opam.
|
||||||
|
|
||||||
|
```
|
||||||
|
opam install . --deps-only
|
||||||
|
dune build
|
||||||
|
```
|
||||||
|
|
||||||
|
The executable is `_build/default/bin/main.exe`. `dune install` installs it
|
||||||
|
as `totpin`.
|
||||||
|
|
||||||
|
## Layout
|
||||||
|
|
||||||
|
- `bin/`: the executable.
|
||||||
|
- `totpin_base32/`: base32 decoding.
|
||||||
|
- `tools/gen_licenses.ml`: build-time tool that generates the third-party
|
||||||
|
license texts shown by `--licenses`.
|
||||||
|
|
||||||
|
### Third-party licenses
|
||||||
|
|
||||||
|
During the build, `tools/gen_licenses.exe` reads the `(libraries ...)` field
|
||||||
|
of `bin/dune`, finds with `ocamlfind` the libraries linked into the
|
||||||
|
executable (and their dependencies), and reads their opam package, version,
|
||||||
|
declared license and license file from the state of the current opam switch.
|
||||||
|
The result is embedded in the executable.
|
||||||
|
|
||||||
|
This is why the build needs `ocamlfind`, `bos`, and the opam libraries
|
||||||
|
(`opam-core`, `opam-format`, `opam-state`) installed in the switch, and why it
|
||||||
|
must run inside the opam switch that provides the dependencies.
|
||||||
|
|
||||||
|
## License
|
||||||
|
|
||||||
|
ISC, Copyright (c) 2026 Matthieu Dubuget. See [LICENSE](LICENSE).
|
||||||
+2
-1
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
(name totpin)
|
(name totpin)
|
||||||
|
|
||||||
(version 1.0)
|
(version 1.4)
|
||||||
|
|
||||||
(generate_opam_files true)
|
(generate_opam_files true)
|
||||||
|
|
||||||
@@ -36,6 +36,7 @@ countdown. Press any key (or Ctrl-C) to quit.")
|
|||||||
digestif
|
digestif
|
||||||
(bos :build)
|
(bos :build)
|
||||||
(ocamlfind :build)
|
(ocamlfind :build)
|
||||||
|
(opam-state :build)
|
||||||
(ocaml-lsp-server :with-dev-setup)
|
(ocaml-lsp-server :with-dev-setup)
|
||||||
(merlin :with-dev-setup)
|
(merlin :with-dev-setup)
|
||||||
(ocamlformat :with-dev-setup)
|
(ocamlformat :with-dev-setup)
|
||||||
|
|||||||
+1
-1
@@ -1,3 +1,3 @@
|
|||||||
(executable
|
(executable
|
||||||
(name gen_licenses)
|
(name gen_licenses)
|
||||||
(libraries bos fpath re))
|
(libraries bos fpath re opam-state opam-format opam-core))
|
||||||
+67
-60
@@ -4,9 +4,10 @@
|
|||||||
|
|
||||||
For each library, [ocamlfind] gives the transitive closure and the directory
|
For each library, [ocamlfind] gives the transitive closure and the directory
|
||||||
where it is installed. The opam package that owns a library is found from
|
where it is installed. The opam package that owns a library is found from
|
||||||
the [.changes] files of the switch, and its version and declared licence
|
the [.changes] files of the switch, and its version, declared licence and
|
||||||
from the [opam] file that opam keeps for it. The licence texts are the files
|
documentation directory from the state of the switch, read with the opam
|
||||||
that opam installed in [doc/<package>/]. *)
|
libraries. The licence texts are the files that opam installed in that
|
||||||
|
directory. *)
|
||||||
|
|
||||||
open Bos
|
open Bos
|
||||||
|
|
||||||
@@ -21,7 +22,10 @@ let rec map_result f = function
|
|||||||
Ok (y :: ys)
|
Ok (y :: ys)
|
||||||
|
|
||||||
let ocamlfind args = Cmd.(v "ocamlfind" %% of_list args)
|
let ocamlfind args = Cmd.(v "ocamlfind" %% of_list args)
|
||||||
let lines cmd = OS.Cmd.(run_out cmd |> to_lines)
|
|
||||||
|
let lines cmd =
|
||||||
|
let* l = OS.Cmd.(run_out cmd |> to_lines) in
|
||||||
|
Ok (List.map String.trim l)
|
||||||
|
|
||||||
let line cmd =
|
let line cmd =
|
||||||
let* l = lines cmd in
|
let* l = lines cmd in
|
||||||
@@ -29,14 +33,10 @@ let line cmd =
|
|||||||
| x :: _ -> Ok x
|
| x :: _ -> Ok x
|
||||||
| [] -> msg "no output from: %s" (Cmd.to_string cmd)
|
| [] -> msg "no output from: %s" (Cmd.to_string cmd)
|
||||||
|
|
||||||
(* Windows writes paths with backslashes, and drive letters in either case. *)
|
(* Windows writes paths with backslashes (doubled in the .changes files, where
|
||||||
let normalize s =
|
they are escaped), and drive letters in either case. *)
|
||||||
String.lowercase_ascii (String.map (function '\\' -> '/' | c -> c) s)
|
let slashes = Re.compile (Re.rep1 (Re.set "/\\"))
|
||||||
|
let normalize s = String.lowercase_ascii (Re.replace_string slashes ~by:"/" s)
|
||||||
let starts_with ~prefix s =
|
|
||||||
String.length s >= String.length prefix
|
|
||||||
&& String.sub s 0 (String.length prefix) = prefix
|
|
||||||
|
|
||||||
let libraries_re = Re.Perl.compile_pat "\\(libraries([^)]*)\\)"
|
let libraries_re = Re.Perl.compile_pat "\\(libraries([^)]*)\\)"
|
||||||
|
|
||||||
(* The libraries of the first [(libraries ...)] field of a dune file, minus
|
(* The libraries of the first [(libraries ...)] field of a dune file, minus
|
||||||
@@ -62,56 +62,62 @@ let sorted_entries dir =
|
|||||||
let* l = OS.Dir.contents ~dotfiles:true dir in
|
let* l = OS.Dir.contents ~dotfiles:true dir in
|
||||||
Ok (List.sort Fpath.compare l)
|
Ok (List.sort Fpath.compare l)
|
||||||
|
|
||||||
let load_changes switch =
|
(* The state of the current switch, loaded the way the [opam] command does. *)
|
||||||
let* files = sorted_entries Fpath.(switch / ".opam-switch" / "install") in
|
let with_switch_state f =
|
||||||
files
|
OpamSystem.init ();
|
||||||
|> List.filter (Fpath.has_ext ".changes")
|
OpamFormatConfig.init ();
|
||||||
|> map_result (fun f ->
|
OpamRepositoryConfig.init ();
|
||||||
let* content = OS.File.read f in
|
let root = OpamStateConfig.opamroot () in
|
||||||
Ok (Fpath.(basename (rem_ext f)), normalize content))
|
OpamStateConfig.init ~root_dir:root ();
|
||||||
|
ignore (OpamStateConfig.load_defaults ~lock_kind:`Lock_none root);
|
||||||
|
OpamGlobalState.with_ `Lock_none @@ fun gt ->
|
||||||
|
OpamSwitchState.with_ `Lock_none gt @@ fun st -> f root st
|
||||||
|
|
||||||
let owner ~stdlib ~changes lib =
|
(* The opam package that installed each file of the switch. *)
|
||||||
|
let load_owners root (st : _ OpamStateTypes.switch_state) =
|
||||||
|
let owners = Hashtbl.create 4096 in
|
||||||
|
OpamPackage.Set.iter
|
||||||
|
(fun nv ->
|
||||||
|
let name = OpamPackage.name nv in
|
||||||
|
let changes =
|
||||||
|
OpamFile.Changes.safe_read (OpamPath.Switch.changes root st.switch name)
|
||||||
|
in
|
||||||
|
OpamStd.String.Map.iter
|
||||||
|
(fun file _ ->
|
||||||
|
Hashtbl.replace owners (normalize file)
|
||||||
|
(OpamPackage.Name.to_string name))
|
||||||
|
changes)
|
||||||
|
st.installed;
|
||||||
|
owners
|
||||||
|
|
||||||
|
let owner ~stdlib ~owners lib =
|
||||||
let* dir = line (ocamlfind [ "query"; "-format"; "%d"; lib ]) in
|
let* dir = line (ocamlfind [ "query"; "-format"; "%d"; lib ]) in
|
||||||
if starts_with ~prefix:(normalize stdlib) (normalize dir) then Ok "ocaml"
|
if String.starts_with ~prefix:(normalize stdlib) (normalize dir) then
|
||||||
|
Ok "ocaml"
|
||||||
else
|
else
|
||||||
let top = List.hd (String.split_on_char '.' lib) in
|
let top = List.hd (String.split_on_char '.' lib) in
|
||||||
let meta =
|
match Hashtbl.find_opt owners (Printf.sprintf "lib/%s/meta" top) with
|
||||||
Re.compile (Re.str (normalize (Printf.sprintf "lib/%s/META" top)))
|
|
||||||
in
|
|
||||||
match
|
|
||||||
List.find_map
|
|
||||||
(fun (pkg, content) -> if Re.execp meta content then Some pkg else None)
|
|
||||||
changes
|
|
||||||
with
|
|
||||||
| Some pkg -> Ok pkg
|
| Some pkg -> Ok pkg
|
||||||
| None -> msg "no opam package found for the library %s" lib
|
| None -> msg "no opam package found for the library %s" lib
|
||||||
|
|
||||||
let license_re = Re.Perl.compile_pat ~opts:[ `Multiline ] "^license:[ \t]*(.*)$"
|
let package_info root (st : _ OpamStateTypes.switch_state) pkg =
|
||||||
|
let name = OpamPackage.Name.of_string pkg in
|
||||||
(* The version is the end of the name of the directory [<pkg>.<version>]. *)
|
match OpamSwitchState.find_installed_package_by_name st name with
|
||||||
let package_info ~switch pkg =
|
| exception Not_found -> msg "package %s is not installed" pkg
|
||||||
let* entries = sorted_entries Fpath.(switch / ".opam-switch" / "packages") in
|
| nv ->
|
||||||
let prefix = pkg ^ "." in
|
let version = OpamPackage.Version.to_string (OpamPackage.version nv) in
|
||||||
match
|
|
||||||
List.find_opt (fun e -> starts_with ~prefix (Fpath.basename e)) entries
|
|
||||||
with
|
|
||||||
| None -> msg "no opam metadata for %s" pkg
|
|
||||||
| Some e ->
|
|
||||||
let name = Fpath.basename e in
|
|
||||||
let version =
|
|
||||||
String.sub name (String.length prefix)
|
|
||||||
(String.length name - String.length prefix)
|
|
||||||
in
|
|
||||||
let* opam = OS.File.read Fpath.(e / "opam") in
|
|
||||||
let license =
|
let license =
|
||||||
match Re.exec_opt license_re opam with
|
match OpamFile.OPAM.license (OpamSwitchState.opam st nv) with
|
||||||
| Some g -> Re.Group.get g 1
|
| [] -> "unknown"
|
||||||
| None -> "unknown"
|
| l -> String.concat ", " l
|
||||||
in
|
in
|
||||||
Ok (version, license)
|
let doc =
|
||||||
|
OpamPath.Switch.doc root st.switch st.switch_config name
|
||||||
|
|> OpamFilename.Dir.to_string |> Fpath.v
|
||||||
|
in
|
||||||
|
Ok (version, license, doc)
|
||||||
|
|
||||||
let license_file ~switch pkg =
|
let license_file dir =
|
||||||
let dir = Fpath.(switch / "doc" / pkg) in
|
|
||||||
let* exists = OS.Dir.exists dir in
|
let* exists = OS.Dir.exists dir in
|
||||||
if not exists then Ok None
|
if not exists then Ok None
|
||||||
else
|
else
|
||||||
@@ -120,26 +126,27 @@ let license_file ~switch pkg =
|
|||||||
(List.find_opt
|
(List.find_opt
|
||||||
(fun e ->
|
(fun e ->
|
||||||
let n = String.uppercase_ascii (Fpath.basename e) in
|
let n = String.uppercase_ascii (Fpath.basename e) in
|
||||||
starts_with ~prefix:"LICENSE" n || starts_with ~prefix:"COPYING" n)
|
String.starts_with ~prefix:"LICENSE" n
|
||||||
|
|| String.starts_with ~prefix:"COPYING" n)
|
||||||
entries)
|
entries)
|
||||||
|
|
||||||
let run file =
|
let run file =
|
||||||
|
with_switch_state @@ fun root st ->
|
||||||
let* roots = roots file in
|
let* roots = roots file in
|
||||||
let* destdir = line (ocamlfind [ "printconf"; "destdir" ]) in
|
|
||||||
let switch = Fpath.(parent (v destdir)) in
|
|
||||||
let* stdlib = line (ocamlfind [ "printconf"; "stdlib" ]) in
|
let* stdlib = line (ocamlfind [ "printconf"; "stdlib" ]) in
|
||||||
let* changes = load_changes switch in
|
let owners = load_owners root st in
|
||||||
let* libs = lines (ocamlfind ([ "query"; "-r"; "-format"; "%p" ] @ roots)) in
|
let* libs = lines (ocamlfind ([ "query"; "-r"; "-format"; "%p" ] @ roots)) in
|
||||||
let* owners =
|
let* owners =
|
||||||
map_result (owner ~stdlib ~changes) (List.sort_uniq compare libs)
|
map_result (owner ~stdlib ~owners) (List.sort_uniq compare libs)
|
||||||
in
|
in
|
||||||
let packages = List.sort_uniq compare owners in
|
let packages = List.sort_uniq compare owners in
|
||||||
if packages = [] then msg "no library found in %s" file else
|
if packages = [] then msg "no library found in %s" file
|
||||||
|
else
|
||||||
let* infos =
|
let* infos =
|
||||||
map_result
|
map_result
|
||||||
(fun p ->
|
(fun p ->
|
||||||
let* version, license = package_info ~switch p in
|
let* version, license, doc = package_info root st p in
|
||||||
let* file = license_file ~switch p in
|
let* file = license_file doc in
|
||||||
Ok (p, version, license, file))
|
Ok (p, version, license, file))
|
||||||
packages
|
packages
|
||||||
in
|
in
|
||||||
|
|||||||
Reference in new issue
Block a user