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