-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
auth-source-keytar.el
98 lines (79 loc) · 3.44 KB
/
auth-source-keytar.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
;;; auth-source-keytar.el --- Integrate auth-source with keytar -*- lexical-binding: t; -*-
;; Copyright (C) 2021-2024 Shen, Jen-Chieh
;; Created date 2021-03-29 19:24:39
;; Author: Shen, Jen-Chieh <[email protected]>
;; URL: https://github.com/emacs-grammarly/auth-source-keytar
;; Version: 0.1.3
;; Package-Requires: ((emacs "24.4") (keytar "0.1.2") (s "1.12.0"))
;; Keywords: convenience keytar password credential secret security
;; This file is NOT part of GNU Emacs.
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;;
;; Integrates keytar (https://www.npmjs.com/package/keytar) within
;; auth-source.
;;
;;; Code:
(require 'auth-source)
(require 'keytar)
(require 's)
(defgroup auth-source-keytar nil
"Keytar integration within auth-source."
:prefix "auth-source-keytar-"
:group 'auth-source)
;;;###autoload
(defun auth-source-keytar-enable ()
"Enable auth-source-keytar."
(add-to-list 'auth-sources 'keytar)
(auth-source-forget-all-cached))
(cl-defun auth-source-keytar-search
(&rest spec &key service account host user &allow-other-keys)
"Given some search query, return matching credentials.
Common search keys: HOST, USER.
See `auth-source-search' for details on the parameters SPEC, SERVICE
and ACCOUNT."
(cond ((and service account) (keytar-get-password service account))
((and host user) (keytar-get-password host user))
(service (auth-source-keytar--build-result service))
(host (auth-source-keytar--build-result host))
(t (user-error "Missing key `service` in search query"))))
(defun auth-source-keytar--read-password (secret)
"Read password from SECRET."
(let* ((lst (split-string secret "password: '"))
(pass (nth 1 lst)))
(string-trim (s-replace "' }" "" pass))))
(defun auth-source-keytar--build-result (service)
"Build auth-source-keytar entry matching SERVICE."
(let ((creds '()) (result (keytar-find-credentials service)))
(setq result (s-replace "[" "" result)
result (s-replace "]" "" result)
result (split-string result "\n" t))
(dolist (secret result)
(setq secret (string-trim secret)
secret (s-replace-regexp ",$" "" secret)
secret (auth-source-keytar--read-password secret))
(push (list :secret secret) creds))
creds))
(defun auth-source-keytar-backend-parse (entry)
"Create a keytar auth-source backend from ENTRY."
(when (eq entry 'keytar)
(auth-source-backend-parse-parameters
entry
(auth-source-backend
:source "Keytar"
:type 'keytar
:search-function #'auth-source-keytar-search))))
(if (boundp 'auth-source-backend-parser-functions)
(add-hook 'auth-source-backend-parser-functions #'auth-source-keytar-backend-parse)
(advice-add 'auth-source-backend-parse :before-until #'auth-source-keytar-backend-parse))
(provide 'auth-source-keytar)
;;; auth-source-keytar.el ends here