emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[elpa] externals/openpgp 671030cb32 01/30: Initial revision


From: ELPA Syncer
Subject: [elpa] externals/openpgp 671030cb32 01/30: Initial revision
Date: Sun, 26 Mar 2023 10:59:26 -0400 (EDT)

branch: externals/openpgp
commit 671030cb32f955764bd8964124555a3e14dc664f
Author: Philip Kaludercic <philip.kaludercic@fau.de>
Commit: Philip Kaludercic <philip.kaludercic@fau.de>

    Initial revision
---
 openpgp.el | 97 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 97 insertions(+)

diff --git a/openpgp.el b/openpgp.el
new file mode 100644
index 0000000000..13b9512d84
--- /dev/null
+++ b/openpgp.el
@@ -0,0 +1,97 @@
+;;; $Id$
+;;; Implementation of the keys.openpgp.org protocol as specified by
+;;; https://keys.openpgp.org/about/api
+
+(defcustom openpgp-keyserver "keys.openpgp.org"
+  "Domain of keyserver to use.
+
+NOTE: currently the default value is the only working keyserver,
+as federation hasn't been implemented yet.")
+
+(defsubst openpgp--api-url (endpoint &optional arg)
+  "Construct VKS request querying ENDPOINT.
+
+The optional argument ARG will be concatenated to the end of the
+URL, if non-nil."
+  (format "https://%s/vks/v1/%s%s";
+         openpgp-keyserver endpoint
+         (if arg (concat "/" arg) "")))
+
+(defun openpgp--process-key (status)
+  (when (plist-get status :error)
+    (error "Request failed: %s"
+          (caddr (assq (caddr (plist-get status :error))
+                       url-http-codes))))
+  (forward-paragraph)
+  (forward-line)
+  (epa-import-armor-in-region (point) (point-max)))
+
+(defun openpgp-fetch-key-by-fingerprint (fingerprint)
+  "Query key via FINGERPRINT and add to keychain."
+  (let ((fingerprint (string-remove-prefix "0X" (upcase fingerprint))))
+    (url-retrieve (openpgp--api-url "by-fingerprint" fingerprint)
+                 #'openpgp--process-key)))
+
+(defun openpgp-fetch-key-by-keyid (keyid)
+  "Query key via KEYID and add to keychain."
+  (let ((keyid (string-remove-prefix "0X" (upcase keyid))))
+    (url-retrieve (openpgp--api-url "by-keyid" keyid)
+                 #'openpgp--process-key)))
+
+(defun openpgp-fetch-key-by-email (email)
+  "Query key via EMAIL and add to keychain."
+  (url-retrieve (openpgp--api-url "by-email" email)
+               #'openpgp--process-key))
+
+(defun openpgp--verify-callback (status)
+  (when (plist-get status :error)
+    (error "Request failed: %s"
+          (caddr (assq (caddr (plist-get status :error))
+                       url-http-codes))))
+  (forward-paragraph)
+  (let ((data (json-parse-buffer)))
+    (when (assq 'error data)
+      (error "Error in response: %s" (cdr (assq 'error data))))
+    (message "Verification successfully requested.")))
+
+(defun openpgp-request-verify (email token)
+  "Request verification email for address EMAIL.
+
+TOKEN should be supplied by a previous \"upload-key\" request."
+  (let ((url-request-method "POST")
+       (url-request-extra-headers '(("Content-Type" . "application/json")))
+       (url-request-data (json-encode `(("token" . ,token)
+                                        ("addresses" . (,email))))))
+    (url-retrieve (openpgp--api-url "request-verify")
+                 #'openpgp--verify-callback)))
+
+(defun openpgp--upload-callback (status email)
+  (when (plist-get status :error)
+    (error "Request failed: %s"
+          (caddr (assq (caddr (plist-get status :error))
+                       url-http-codes))))
+  (forward-paragraph)
+  (let ((data (json-parse-buffer :object-type 'alist)))
+    (when (assq 'error data)
+      (error "Error in response: %s" (cdr (assq 'error data))))
+    (openpgp-request-verify email (cdr (assq 'token data)))))
+
+(defun openpgp-upload-key (email key)
+  "Upload KEY for address EMAIL to keyserver.
+
+The KEY should be a string, containing a ASCII armoured public
+key."
+  (let ((url-request-method "POST")
+       (url-request-extra-headers '(("Content-Type" . "application/json")))
+       (url-request-data (json-encode `(("keytext" . ,key)))))
+    (url-retrieve (openpgp--api-url "upload")
+                 #'openpgp--upload-callback
+                 (list email))))
+
+(defun openpgp-upload-key-file (email key-file)
+  "Upload key from KEY-FILE for address EMAIL."
+  (interactive (list (read-string "Email: ")
+                    (read-file-name "Key file: ")))
+  (with-temp-buffer
+    (insert-file-contents key-file)
+    (openpgp-upload-key email (buffer-string))))



reply via email to

[Prev in Thread] Current Thread [Next in Thread]