From 76887ae12de58c700d67ac7c57ad508a8e7f9564 Mon Sep 17 00:00:00 2001 From: Sean Allred Date: Wed, 5 Nov 2014 14:19:33 -0500 Subject: Add basic HTML/JavaScript to enable authentication This should be accessible using `raw.github.com/...', but if not, I will put it on my webserver. For now, this seems like it's as good as it's going to get. When the page is accessed with an access token, this token is displayed (styled as CSS class `uservalue', big, dark, and green). Error conditions are not currently being handled, but they will be. --- auth/auth.htm | 22 ++++++++++++++++++++++ auth/hash.js | 12 ++++++++++++ auth/style.css | 9 +++++++++ 3 files changed, 43 insertions(+) create mode 100644 auth/auth.htm create mode 100644 auth/hash.js create mode 100644 auth/style.css diff --git a/auth/auth.htm b/auth/auth.htm new file mode 100644 index 0000000..013b245 --- /dev/null +++ b/auth/auth.htm @@ -0,0 +1,22 @@ + + + StackMode Authentication + + + + +

StackMode Authentication

+

+ Good news! + Authentication was successful. + Your authentication token is +

+ +
+ Please paste this into the prompt within Emacs now. +

+
+ Read the manual on GitHub +
+ + diff --git a/auth/hash.js b/auth/hash.js new file mode 100644 index 0000000..7cb701a --- /dev/null +++ b/auth/hash.js @@ -0,0 +1,12 @@ +function getHashValue(key) { + // http://stackoverflow.com/a/11920807 + return location.hash.match(new RegExp(key+'=([^&]*)'))[1]; +} + +function setValue(document_id, hash_key) { + document.getElementById(document_id).innerHTML = getHashValue(hash_key); +} + +function extractValues() { + setValue("access-token", "access_token"); +} diff --git a/auth/style.css b/auth/style.css new file mode 100644 index 0000000..f3fbe99 --- /dev/null +++ b/auth/style.css @@ -0,0 +1,9 @@ +div.uservalue { + font-family: monospace; + font-size: 14pt; + font-weight: bold; + color: darkgreen; + margin-top: 2ex; + margin-bottom: 2ex; + margin-left: 2em; +} -- cgit v1.2.3 From d0e0b26fb00a7cda45078398d056de524afc6d2d Mon Sep 17 00:00:00 2001 From: Sean Allred Date: Wed, 5 Nov 2014 19:25:19 -0500 Subject: Implement OAuth2 authentication Tested with commit 1749aa32c25d8eee9ca0753cb55bd7d134f320b7, current HEAD of the `gh-pages' branch. --- stack-auth.el | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ stack-core.el | 9 +++++++- 2 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 stack-auth.el diff --git a/stack-auth.el b/stack-auth.el new file mode 100644 index 0000000..d450754 --- /dev/null +++ b/stack-auth.el @@ -0,0 +1,73 @@ +;;; stack-auth.el --- user authentication for stack-mode -*- lexical-binding: t; -*- + +;; Copyright (C) 2014 Sean Allred + +;; Author: Sean Allred +;; Keywords: help, hypermedia, mail, news, tools + +;; 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 . + +;;; Commentary: + +;; + +;;; Code: + +(require 'stack-core) + +(defconst stack-auth-root + "https://stackexchange.com/oauth/dialog") +(defconst stack-auth--redirect-uri + "http://vermiculus.github.io/stack-mode/auth/auth.htm") +(defconst stack-auth--client-id + "3291") +(defconst stack-auth--key + "0TE6s1tveCpP9K5r5JNDNQ((") +(defvar stack-auth-access-token + nil + "Your access token. + +This is needed to use your account to write questions, make +comments, and read your inbox. Do not alter this unless you know +what you are doing!") + +(defun stack-authenticate () + "Authenticate this application. + +Authentication is required to read your personal data (such as +notifications) and to write with the API (asking and answering +questions)." + (interactive) + (setq + stack-auth-access-token + (when (browse-url + (let ((stack-core-api-root stack-auth-root) + (stack-core-api-batch-request-separator ",")) + (stack-core-build-request + nil + `((client_id . ,stack-auth--client-id) + (scope . (read_inbox + no_expiry + write_access + private_info)) + (redirect_uri . ,(url-hexify-string + stack-auth--redirect-uri)))))) + (read-string "Enter the access token displayed on the webpage: "))) + (if (string-equal "" stack-auth-access-token) + (progn (setq stack-auth-access-token nil) + (error "You must enter this code to use this client fully")) + (stack-cache-set "auth" `((access-token . ,stack-auth-access-token))))) + +(provide 'stack-auth) +;;; stack-auth.el ends here diff --git a/stack-core.el b/stack-core.el index c78c316..2e9b652 100644 --- a/stack-core.el +++ b/stack-core.el @@ -57,6 +57,10 @@ (format "http://api.stackexchange.com/%s/" stack-core-api-version) "The base URL to make requests from.") +(defconst stack-core-api-batch-request-separator + ";" + "The separator character to use when making batch requests.") + (defcustom stack-core-default-keyword-arguments-alist '(("filters/create") ("sites") @@ -110,7 +114,10 @@ a string, just return it." (cond ((stringp thing) thing) ((symbolp thing) (symbol-name thing)) - ((numberp thing) (number-to-string thing)))) + ((numberp thing) (number-to-string thing)) + ((sequencep thing) + (mapconcat #'stack-core-thing-as-string + thing stack-core-api-batch-request-separator)))) (defun stack-core-get-default-keyword-arguments (method) "Gets the correct keyword arguments for METHOD." -- cgit v1.2.3 From c12e332149f3183888119701fc3c257af6bd4324 Mon Sep 17 00:00:00 2001 From: Sean Allred Date: Wed, 5 Nov 2014 19:25:43 -0500 Subject: Further explain `stack-cache-directory' I looked at it for a few minutes and wondered how to use it -- and I wrote the thing! :) --- stack-core.el | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/stack-core.el b/stack-core.el index 2e9b652..585cc73 100644 --- a/stack-core.el +++ b/stack-core.el @@ -274,7 +274,9 @@ context of `stack-cache-directory'." "Set the content of CACHE to DATA. As with `stack-cache-get', CACHE is a file name within the -context of `stack-cache-directory'." +context of `stack-cache-directory'. + +DATA will be written as returned by `prin1'." (unless (file-exists-p stack-cache-directory) (mkdir stack-cache-directory)) (write-region (prin1-to-string data) nil -- cgit v1.2.3 From 713899dd0fdfec2e600eeab58986f9fefcbdb00b Mon Sep 17 00:00:00 2001 From: Sean Allred Date: Wed, 5 Nov 2014 19:28:38 -0500 Subject: Remove files duplicated in `gh-pages' These pages should be tracked by that branch; it is an explicitly separate part of the system. --- auth/auth.htm | 22 ---------------------- auth/hash.js | 12 ------------ auth/style.css | 9 --------- 3 files changed, 43 deletions(-) delete mode 100644 auth/auth.htm delete mode 100644 auth/hash.js delete mode 100644 auth/style.css diff --git a/auth/auth.htm b/auth/auth.htm deleted file mode 100644 index 013b245..0000000 --- a/auth/auth.htm +++ /dev/null @@ -1,22 +0,0 @@ - - - StackMode Authentication - - - - -

StackMode Authentication

-

- Good news! - Authentication was successful. - Your authentication token is -

- -
- Please paste this into the prompt within Emacs now. -

-
- Read the manual on GitHub -
- - diff --git a/auth/hash.js b/auth/hash.js deleted file mode 100644 index 7cb701a..0000000 --- a/auth/hash.js +++ /dev/null @@ -1,12 +0,0 @@ -function getHashValue(key) { - // http://stackoverflow.com/a/11920807 - return location.hash.match(new RegExp(key+'=([^&]*)'))[1]; -} - -function setValue(document_id, hash_key) { - document.getElementById(document_id).innerHTML = getHashValue(hash_key); -} - -function extractValues() { - setValue("access-token", "access_token"); -} diff --git a/auth/style.css b/auth/style.css deleted file mode 100644 index f3fbe99..0000000 --- a/auth/style.css +++ /dev/null @@ -1,9 +0,0 @@ -div.uservalue { - font-family: monospace; - font-size: 14pt; - font-weight: bold; - color: darkgreen; - margin-top: 2ex; - margin-bottom: 2ex; - margin-left: 2em; -} -- cgit v1.2.3 From 6f2fb104789fdcd7c7e10ca31f6517f0305579c1 Mon Sep 17 00:00:00 2001 From: Sean Allred Date: Wed, 5 Nov 2014 19:33:20 -0500 Subject: Use standard '.el' extenstion for cache This should probably be altered so that the caching function requires symbols; this would keep everything consistent since the caching function could apply the same pattern to every symbol it receives. --- stack-auth.el | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stack-auth.el b/stack-auth.el index d450754..99e0177 100644 --- a/stack-auth.el +++ b/stack-auth.el @@ -67,7 +67,7 @@ questions)." (if (string-equal "" stack-auth-access-token) (progn (setq stack-auth-access-token nil) (error "You must enter this code to use this client fully")) - (stack-cache-set "auth" `((access-token . ,stack-auth-access-token))))) + (stack-cache-set "auth.el" `((access-token . ,stack-auth-access-token))))) (provide 'stack-auth) ;;; stack-auth.el ends here -- cgit v1.2.3 From d56a1bb19c672d093cdb631591dcba5e977561f6 Mon Sep 17 00:00:00 2001 From: Sean Allred Date: Wed, 5 Nov 2014 19:57:25 -0500 Subject: Prune unnecessary variable This key is actually used in request-making on the whole, so it has been moved to `stack-core.el' as of 9cb376361983742f0a15775b233fc194d61b3ea7. --- stack-auth.el | 2 -- 1 file changed, 2 deletions(-) diff --git a/stack-auth.el b/stack-auth.el index 99e0177..e4f9a77 100644 --- a/stack-auth.el +++ b/stack-auth.el @@ -32,8 +32,6 @@ "http://vermiculus.github.io/stack-mode/auth/auth.htm") (defconst stack-auth--client-id "3291") -(defconst stack-auth--key - "0TE6s1tveCpP9K5r5JNDNQ((") (defvar stack-auth-access-token nil "Your access token. -- cgit v1.2.3 From ccb2f03dd57e4535c6151b72fc439b23acf1b643 Mon Sep 17 00:00:00 2001 From: Sean Allred Date: Thu, 6 Nov 2014 07:33:35 -0500 Subject: Change constant to variable As it is let-bound in the authentication flow, it is not truly a constant. Add a warning to the documentation appropriately. --- stack-core.el | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/stack-core.el b/stack-core.el index 585cc73..6c5e7fa 100644 --- a/stack-core.el +++ b/stack-core.el @@ -57,9 +57,11 @@ (format "http://api.stackexchange.com/%s/" stack-core-api-version) "The base URL to make requests from.") -(defconst stack-core-api-batch-request-separator +(defvar stack-core-api-batch-request-separator ";" - "The separator character to use when making batch requests.") + "The separator character to use when making batch requests. + +Do not change this unless you know what you are doing!") (defcustom stack-core-default-keyword-arguments-alist '(("filters/create") -- cgit v1.2.3 From ec48346fdbc86f52883083ec066d563cf5fae1f4 Mon Sep 17 00:00:00 2001 From: Sean Allred Date: Thu, 6 Nov 2014 07:34:27 -0500 Subject: Remove unused permission We are not using `private_info' yet, so there is no need to request this permission. It can, of course, be easily added later on if the information is ever usable. --- stack-auth.el | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/stack-auth.el b/stack-auth.el index e4f9a77..e55fae1 100644 --- a/stack-auth.el +++ b/stack-auth.el @@ -57,8 +57,7 @@ questions)." `((client_id . ,stack-auth--client-id) (scope . (read_inbox no_expiry - write_access - private_info)) + write_access)) (redirect_uri . ,(url-hexify-string stack-auth--redirect-uri)))))) (read-string "Enter the access token displayed on the webpage: "))) -- cgit v1.2.3