f713831941d4

Add some Twitter auth machinery
[view raw] [browse files]
author Steve Losh <steve@stevelosh.com>
date Mon, 16 Jan 2017 13:49:03 +0000
parents bab96c6f7abc
children 21d537ce910a
branches/tags (none)
files creds-example.lisp magitek.asd src/auth.lisp

Changes

--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/creds-example.lisp	Mon Jan 16 13:49:03 2017 +0000
@@ -0,0 +1,10 @@
+(in-package :magitek)
+
+(defparameter *api-key* "...")
+(defparameter *api-secret* "...")
+
+(defparameter *credentials*
+  '(
+    (auth-plist-goes-here use dump-auth-plist to get it)
+    ; ...
+    ))
--- a/magitek.asd	Mon Jan 16 13:43:51 2017 +0000
+++ b/magitek.asd	Mon Jan 16 13:49:03 2017 +0000
@@ -18,4 +18,6 @@
                              (:file "quickutils")))
                (:file "package")
                (:module "src" :serial t
-                :components ((:file "main")))))
+                :components ((:file "creds")
+                             (:file "auth")
+                             (:file "main")))))
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/auth.lisp	Mon Jan 16 13:49:03 2017 +0000
@@ -0,0 +1,38 @@
+(in-package :magitek)
+
+(defparameter *accounts* (make-hash-table))
+
+(defun dump-auth-plist (account-name)
+  (list :account-name account-name
+        :api-key chirp:*oauth-api-key*
+        :api-secret chirp:*oauth-api-secret*
+        :access-token chirp:*oauth-access-token*
+        :access-secret chirp:*oauth-access-secret*))
+
+(defun add-account (account)
+  (setf (gethash (getf account :account-name) *accounts*)
+        account))
+
+(defun load-accounts ()
+  (map nil #'add-account *credentials*))
+
+(defun authorize (account-name)
+  (format t "Visit ~A to get a PIN~%"
+          (chirp:initiate-authentication :api-key *api-key*
+                                         :api-secret *api-secret*))
+  (princ "Enter PIN: ")
+  (finish-output)
+  (chirp:complete-authentication (read-line))
+  (chirp:account/verify-credentials)
+  (dump-auth-plist account-name))
+
+(defmacro with-account (account-name &body body)
+  (once-only (account-name)
+    `(if-found account (gethash ,account-name *accounts*)
+       (let ((chirp:*oauth-api-key* (getf account :api-key))
+             (chirp:*oauth-api-secret* (getf account :api-secret))
+             (chirp:*oauth-access-token* (getf account :access-token))
+             (chirp:*oauth-access-secret* (getf account :access-secret)))
+         ,@body)
+       (error "Account ~S not found, use (authorize ~S) to get creds"
+              ,account-name ,account-name))))