Skip to content

Commit 06a0d94

Browse files
author
Stanislav Ochotnický
committed
Inject github API token in headers for gists
This makes it possible to actually use gist pasting (even potentially doing private pastes)
1 parent 7827266 commit 06a0d94

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

webpaste.el

+26
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ This uses `browse-url-generic' to open URLs."
135135

136136
("gist.github.com"
137137
:uri "https://api.github.com/gists"
138+
:headers-lambda webpaste--provider-gist-headers
138139
:post-field nil
139140
:post-field-lambda (lambda () (cl-function (lambda (&key text &allow-other-keys)
140141
(let ((filename (if (buffer-file-name)
@@ -321,6 +322,25 @@ This is the default failover hook that we use for most providers."
321322
(webpaste--return-url (cdr (assq 'link (json-read-from-string data)))))))
322323

323324

325+
(cl-defun webpaste--provider-gist-headers ()
326+
"Provide headers for gist API authentication.
327+
328+
This expects your authinfo to contain authorization for using gist API.
329+
You can generate your token at https://github.com/settings/tokens
330+
Only gist scope is necessary.
331+
332+
Then save the token in your authinfo as:
333+
machine api.github.com/gists login webpaste password <token>"
334+
(cl-function (lambda ()
335+
(cons `("Authorization"
336+
.
337+
,(concat "token "
338+
(auth-source-pick-first-password
339+
:host "api.github.com/gists"
340+
:login "webpaste")))
341+
nil))))
342+
343+
324344

325345

326346
(cl-defun webpaste--get-lang-alist-with-overrides (overrides)
@@ -344,6 +364,7 @@ This is the default failover hook that we use for most providers."
344364
(post-data '())
345365
(post-lang-field-name nil)
346366
(parser 'buffer-string)
367+
(headers-lambda nil)
347368
(lang-overrides '())
348369
(lang-uri-separator nil)
349370
(error-lambda 'webpaste--providers-error-lambda)
@@ -390,6 +411,10 @@ Optional params:
390411
`webpaste--providers-error-lambda-no-failover' available if
391412
you need a provider that isn't allowed to failover.
392413
414+
:headers-lambda Function that builds and returns headers that should be sent
415+
to the provider. This can be useful to add authentication
416+
for example.
417+
393418
:post-field-lambda Function that builds and returns the post data that should be
394419
sent to the provider. It should accept named parameters by
395420
the names TEXT, POST-FIELD and POST-DATA. POST-DATA should
@@ -432,6 +457,7 @@ Optional params:
432457
:post-lang-field-name post-lang-field-name
433458
:post-data post-data)
434459
:parser parser
460+
:headers (when headers-lambda (funcall (funcall headers-lambda)))
435461
:success (funcall success-lambda)
436462
:sync sync
437463
:error (funcall error-lambda :text text))))))

0 commit comments

Comments
 (0)