able to authenticate against shaarli

This commit is contained in:
Anish Lakhwara 2022-06-18 18:26:53 +10:00
commit 15b952865f
9 changed files with 1765 additions and 0 deletions

View file

@ -0,0 +1 @@
/nix/store/0h4x3jy22s397dsq9yvijbddhf7ylwj5-source

View file

@ -0,0 +1 @@
/nix/store/g78hiahkl0j6wi51mb6vvp94f7b8lc6v-source

View file

@ -0,0 +1 @@
/nix/store/bwkkawmrs3bsif0x493nmzpbzyvd6hdy-nix-shell-env

File diff suppressed because it is too large Load diff

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
.envrc

9
README.md Normal file
View file

@ -0,0 +1,9 @@
## Description
A babashka script that checks a list of sources, and randomly picks one to be album of the day
## Expectations
Connect to Shaarli
Check incoming & downloaded?
Imports album through beets?

26
flake.lock Normal file
View file

@ -0,0 +1,26 @@
{
"nodes": {
"nixpkgs": {
"locked": {
"lastModified": 1655520991,
"narHash": "sha256-PF/peh2bfmYBtHyj6QjWGDPAObvy+OHkj05xzjNQSy0=",
"owner": "nixos",
"repo": "nixpkgs",
"rev": "1a46618e1fedd0bbf6ac813b430ff64bcb070b0e",
"type": "github"
},
"original": {
"owner": "nixos",
"repo": "nixpkgs",
"type": "github"
}
},
"root": {
"inputs": {
"nixpkgs": "nixpkgs"
}
}
},
"root": "root",
"version": 7
}

19
flake.nix Normal file
View file

@ -0,0 +1,19 @@
{
description = "An album of the day chooser";
inputs = {
nixpkgs.url = "github:nixos/nixpkgs";
};
outputs = { self, nixpkgs }:
let
pkgs = nixpkgs.legacyPackages.x86_64-linux;
in
{
devShell.x86_64-linux = pkgs.mkShell {
buildInputs = with pkgs; [
babashka
];
};
};
}

57
main.clj Normal file
View file

@ -0,0 +1,57 @@
(ns dhynan.main
(:require [clojure.java.shell :refer [sh]]
[cheshire.core :as json]
[babashka.curl :as curl])
(:import (javax.crypto Mac)
(javax.crypto.spec SecretKeySpec)))
(defn generate-token
[api-secret]
(let [encoder (java.util.Base64/getEncoder)
header-raw (->> (json/encode {"alg" "HS512" "typ" "JWT"})
.getBytes
(.encodeToString encoder))
; TODO can't figure out how to apply this with an anon function
header (clojure.string/replace header-raw "=" "")
date (-> (sh "date" "-u" "+%s")
:out
(clojure.string/replace "\n" ""))
payload-raw (->> (json/encode {"iat" date})
.getBytes
(.encodeToString encoder))
; TODO can't figure out how to apply this with an anon function
payload (clojure.string/replace payload-raw "=" "")
algo "HmacSHA512"
mac (Mac/getInstance algo)]
(.init mac (SecretKeySpec. (.getBytes api-secret) algo))
(let [sig (->> (.doFinal mac (.getBytes (str header "." payload)))
(.encodeToString encoder))
signature (-> sig
; TODO should do url encode here
(clojure.string/replace "/" "_")
(clojure.string/replace "=" "")
(clojure.string/replace "+" "-"))]
(str header "." payload "." signature))))
(let [token (generate-token (System/getenv secret-token))
data (-> (curl/get "http://links.mossnet.lan/api/v1/links?searchtags=music" {:headers {"Authorization" (str "Bearer " token)}})
(:body)
(json/parse-string true))
; filter to bandcamp (and maybe others?)
filtered-data (mapv (filter (fn [x] (contains? "bandcamp" (get x :url)))) data)]
filtered-data)
(comment
(println "somestring")
(.getBytes (str {"iat" (sh "date" "-u" "+%s")}))
(def signature (-> (Mac/getInstance "HmacSHA256")
(.init (SecretKeySpec. (.getBytes "test secret") "HmacSHA256"))
(.doFinal (.getBytes (str "header.payload" ) "UTF-8"))))
(def sig
(let [algo "HmacSHA256"
mac (Mac/getInstance algo)]
(.init mac (SecretKeySpec. (.getBytes "test secret") algo))
(str (.doFinal mac (.getBytes "header.payload" "UTF-8")))))
(println sig)
(str {"alg" "HS512" "typ" "JWT"}))