From 40cc7b4d6470c0efb5214144f91a4ce1e1464ab2 Mon Sep 17 00:00:00 2001 From: Jonathan Leech-Pepin Date: Wed, 19 Nov 2014 17:09:54 -0500 Subject: Implement checking for filter and method authentication requirement. Currently no filter items are defined pending confirmation of which require it. --- sx-auth.el | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) (limited to 'sx-auth.el') diff --git a/sx-auth.el b/sx-auth.el index b470523..0f55814 100644 --- a/sx-auth.el +++ b/sx-auth.el @@ -41,6 +41,44 @@ 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!") +(defvar sx-auth-method-auth '((me . t) + (inbox . t) + (notifications . t) + (events . t) + (posts (comments add)) + (comments delete + edit + flags + upvote) + (answers accept + delete + downvote + edit + flags + upvote) + (questions answers + add + close + delete + downvote + edit + favorite + flags + render + upvote + (unanswered my-tags))) + "List of methods that require auth. +Methods are of form (METHOD SUBMETHODS) where SUBMETHODS + is (METHOD METHOD METHOD ...). + +If all SUBMETHODS require auth or there are no submethods, form +will be (METHOD . t)") + +(defvar sx-auth-filter-auth '() + "List of filter types that require auth. +Keywords are of form (OBJECT TYPES) where TYPES is (FILTER FILTER +FILTER).") + (defun sx-auth-authenticate () "Authenticate this application. @@ -68,6 +106,36 @@ questions)." (error "You must enter this code to use this client fully")) (sx-cache-set 'auth `((access_token . ,sx-auth-access-token))))) +(defun sx-auth--method-p (method &optional submethod) + "Check if METHOD is one that may require authentication. +If it has `auth required` SUBMETHODs, return t." + (let ((method-auth (cdr (assoc method sx-auth-method-auth))) + ;; If the submethod has additional options, they may all be + ;; eligible, in which case we only need to check the `car'. + (sub-head (if (listp submethod) + (car submethod)))) + (and method-auth + (or + ;; All submethods require auth. + (eq t method-auth) + ;; All sub-submethods require auth. + (member sub-head method-auth) + ;; Specific submethod requires auth. + (member submethod method-auth)))))) + +;; Temporary solution. When we switch to pre-defined filters we will +;; have to change the logic to match against specific filters. +(defun sx-auth--filter-p (object &optional filter) + "Check if OBJECT is one that may require authentication. +If it has `auth required` FILTERs, return t." + (let ((object-auth (cdr (assoc object sx-auth-filter-auth)))) + (and object-auth + (or + ;; All elements of object require auth. + (eq t object-auth) + ;; Specific filters on object require auth. + (member filter object-auth)))))) + (provide 'sx-auth) ;;; sx-auth.el ends here -- cgit v1.2.3 From 7f041f830c8d1f9aaca0eb8a898070873668a4fd Mon Sep 17 00:00:00 2001 From: Jonathan Leech-Pepin Date: Thu, 20 Nov 2014 10:25:33 -0500 Subject: Fix filter logic to account for alist of properties. sx-auth-filter-auth now returns a filter that will not require auth for use when auth not available. Add a few known auth-required types. --- sx-auth.el | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) (limited to 'sx-auth.el') diff --git a/sx-auth.el b/sx-auth.el index 0f55814..8085098 100644 --- a/sx-auth.el +++ b/sx-auth.el @@ -74,7 +74,8 @@ Methods are of form (METHOD SUBMETHODS) where SUBMETHODS If all SUBMETHODS require auth or there are no submethods, form will be (METHOD . t)") -(defvar sx-auth-filter-auth '() +(defvar sx-auth-filter-auth '(question.upvoted + question.downvoted) "List of filter types that require auth. Keywords are of form (OBJECT TYPES) where TYPES is (FILTER FILTER FILTER).") @@ -125,16 +126,26 @@ If it has `auth required` SUBMETHODs, return t." ;; Temporary solution. When we switch to pre-defined filters we will ;; have to change the logic to match against specific filters. -(defun sx-auth--filter-p (object &optional filter) - "Check if OBJECT is one that may require authentication. -If it has `auth required` FILTERs, return t." - (let ((object-auth (cdr (assoc object sx-auth-filter-auth)))) - (and object-auth - (or - ;; All elements of object require auth. - (eq t object-auth) - ;; Specific filters on object require auth. - (member filter object-auth)))))) +(defun sx-auth--filter-p (filter) + "Check if FILTER contains properties that require authentication. +If it has `auth-required' properties, return a filter that has +removed those properties." + (let ((auth-filters (cl-remove-if #'nil + ;; Only retrieve the elements that + ;; are issues. + (mapcar (lambda (prop) + (car + (member prop + sx-auth-filter-auth))) + filter))) + (clean-filter)) + ;; Auth-filters is the filters that are issues + (when auth-filters + (setq clean-filter + (cl-remove-if (lambda (prop) + (member prop auth-filters)) + filter))) + clean-filter)) (provide 'sx-auth) ;;; sx-auth.el ends here -- cgit v1.2.3 From 52a8faf61568cb198727aad50d2ec4388a751348 Mon Sep 17 00:00:00 2001 From: Jonathan Leech-Pepin Date: Thu, 20 Nov 2014 10:27:44 -0500 Subject: Fix sx-auth--method-p to work correctly when there is no submethod specified. --- sx-auth.el | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'sx-auth.el') diff --git a/sx-auth.el b/sx-auth.el index 8085098..e786466 100644 --- a/sx-auth.el +++ b/sx-auth.el @@ -109,7 +109,7 @@ questions)." (defun sx-auth--method-p (method &optional submethod) "Check if METHOD is one that may require authentication. -If it has `auth required` SUBMETHODs, return t." +If it has `auth-required' SUBMETHODs, or no submethod, return t." (let ((method-auth (cdr (assoc method sx-auth-method-auth))) ;; If the submethod has additional options, they may all be ;; eligible, in which case we only need to check the `car'. @@ -117,12 +117,14 @@ If it has `auth required` SUBMETHODs, return t." (car submethod)))) (and method-auth (or + ;; No submethod specified + (not submethod) ;; All submethods require auth. (eq t method-auth) ;; All sub-submethods require auth. (member sub-head method-auth) ;; Specific submethod requires auth. - (member submethod method-auth)))))) + (member submethod method-auth))))) ;; Temporary solution. When we switch to pre-defined filters we will ;; have to change the logic to match against specific filters. -- cgit v1.2.3 From 97dae4030bcfb3062bd0909f471f41b5967c88d2 Mon Sep 17 00:00:00 2001 From: Jonathan Leech-Pepin Date: Thu, 20 Nov 2014 12:58:53 -0500 Subject: Do not actually need to check for no submethods. Already tested for. --- sx-auth.el | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'sx-auth.el') diff --git a/sx-auth.el b/sx-auth.el index e786466..bd8c206 100644 --- a/sx-auth.el +++ b/sx-auth.el @@ -115,10 +115,9 @@ If it has `auth-required' SUBMETHODs, or no submethod, return t." ;; eligible, in which case we only need to check the `car'. (sub-head (if (listp submethod) (car submethod)))) + (lwarn " sx-auth method" :debug "Method %s requires auth" method-auth) (and method-auth (or - ;; No submethod specified - (not submethod) ;; All submethods require auth. (eq t method-auth) ;; All sub-submethods require auth. -- cgit v1.2.3 From 2e2619d7b1ec9a5d7338c5ec224ac0ba920e017c Mon Sep 17 00:00:00 2001 From: Jonathan Leech-Pepin Date: Thu, 20 Nov 2014 12:59:17 -0500 Subject: Improve method for returning clean filter. Logic now verifies `include` if `include`,`exclude`,`base` is used. Otherwise verify entire filter. --- sx-auth.el | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) (limited to 'sx-auth.el') diff --git a/sx-auth.el b/sx-auth.el index bd8c206..e67f9a1 100644 --- a/sx-auth.el +++ b/sx-auth.el @@ -131,22 +131,31 @@ If it has `auth-required' SUBMETHODs, or no submethod, return t." "Check if FILTER contains properties that require authentication. If it has `auth-required' properties, return a filter that has removed those properties." - (let ((auth-filters (cl-remove-if #'nil - ;; Only retrieve the elements that - ;; are issues. - (mapcar (lambda (prop) - (car - (member prop - sx-auth-filter-auth))) - filter))) - (clean-filter)) - ;; Auth-filters is the filters that are issues + (let* ((incl-filter (if (listp filter) (car filter))) + (rest-filter (if incl-filter (cdr filter))) + (auth-filters (cl-remove-if #'nil + ;; Only retrieve the elements that + ;; are issues. + (mapcar (lambda (prop) + (car + (member prop + sx-auth-filter-auth))) + (or incl-filter filter)))) + clean-filter out-filter) + (lwarn "sx-auth filter" :debug "Filter: %S" filter) + ;; Auth-filters is the filters that are issues (when auth-filters (setq clean-filter (cl-remove-if (lambda (prop) (member prop auth-filters)) - filter))) - clean-filter)) + (or incl-filter filter)))) + (if (and incl-filter clean-filter) + (setq out-filter + (cons clean-filter rest-filter)) + (setq out-filter clean-filter)) + (lwarn "sx-auth filter2" :debug "Filter property %s requires auth. %S" + auth-filters out-filter) + out-filter)) (provide 'sx-auth) ;;; sx-auth.el ends here -- cgit v1.2.3