Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 141 lines (112 sloc) 4.5 kB
1ea40da @sigma First version
authored
1 ;;; gh-auth.el --- authentication for gh.el
2
3 ;; Copyright (C) 2011 Yann Hodique
4
5 ;; Author: Yann Hodique <yann.hodique@gmail.com>
c431c08 @sigma cleanup whitespaces
authored
6 ;; Keywords:
1ea40da @sigma First version
authored
7
8 ;; This file is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation; either version 2, or (at your option)
11 ;; any later version.
12
13 ;; This file is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
17
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with GNU Emacs; see the file COPYING. If not, write to
20 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 ;; Boston, MA 02111-1307, USA.
22
23 ;;; Commentary:
24
c431c08 @sigma cleanup whitespaces
authored
25 ;;
1ea40da @sigma First version
authored
26
27 ;;; Code:
bf60ba8 @sigma fix packaging
authored
28
29 (eval-when-compile
30 (require 'cl))
31
32 ;;;###autoload
1ea40da @sigma First version
authored
33 (require 'eieio)
bf60ba8 @sigma fix packaging
authored
34
1ea40da @sigma First version
authored
35 (require 'gh-common)
36
8b1298c @sigma use OAuth by default
authored
37 (defgroup gh-auth nil
38 "Github authentication."
39 :group 'gh)
40
1ea40da @sigma First version
authored
41 (defvar gh-auth-username nil)
42 (defvar gh-auth-password nil)
efbc149 @sigma use proper constructors to populate git auth variables
authored
43 (defvar gh-auth-oauth-token nil)
1ea40da @sigma First version
authored
44
45 (defun gh-auth-get-username ()
c431c08 @sigma cleanup whitespaces
authored
46 (let ((user (or gh-auth-username
efbc149 @sigma use proper constructors to populate git auth variables
authored
47 (setq gh-auth-username (gh-config "user")))))
48 (when (not user)
49 (setq user (read-string "GitHub username: "))
50 (setq gh-auth-username user)
51 (gh-set-config "user" user))
52 user))
1ea40da @sigma First version
authored
53
9e4f934 @sigma make password storage optional
authored
54 (defun gh-auth-get-password (remember)
c431c08 @sigma cleanup whitespaces
authored
55 (let ((pass (or gh-auth-password
efbc149 @sigma use proper constructors to populate git auth variables
authored
56 (setq gh-auth-password (gh-config "password")))))
57 (when (not pass)
58 (setq pass (read-passwd "GitHub password: "))
9e4f934 @sigma make password storage optional
authored
59 (when remember
a36d9d7 @sigma use oauth api to get token from password
authored
60 (setq gh-auth-password pass)
9e4f934 @sigma make password storage optional
authored
61 (gh-set-config "password" pass)))
efbc149 @sigma use proper constructors to populate git auth variables
authored
62 pass))
63
a36d9d7 @sigma use oauth api to get token from password
authored
64 (declare-function 'gh-oauth-auth-new "gh-oauth")
65
efbc149 @sigma use proper constructors to populate git auth variables
authored
66 (defun gh-auth-get-oauth-token ()
c431c08 @sigma cleanup whitespaces
authored
67 (let ((token (or gh-auth-oauth-token
239f44c @sigma github.token config was conflicting with v2 API token
authored
68 (setq gh-auth-oauth-token (gh-config "oauth-token")))))
efbc149 @sigma use proper constructors to populate git auth variables
authored
69 (when (not token)
a36d9d7 @sigma use oauth api to get token from password
authored
70 (let* ((api (make-instance 'gh-oauth-api))
71 (tok (and (fboundp 'gh-oauth-auth-new)
72 (oref (oref (funcall 'gh-oauth-auth-new api
73 '(user repo gist)) :data)
74 :token))))
75 (setq token (or tok (read-string "GitHub OAuth token: ")))
76 (setq gh-auth-oauth-token token)
239f44c @sigma github.token config was conflicting with v2 API token
authored
77 (gh-set-config "oauth-token" token)))
efbc149 @sigma use proper constructors to populate git auth variables
authored
78 token))
1ea40da @sigma First version
authored
79
c280f44 @sigma maintain autoloads file
authored
80 ;;;###autoload
1ea40da @sigma First version
authored
81 (defclass gh-authenticator ()
34fb311 @sigma add support for various data formats
authored
82 ((username :initarg :username :initform nil))
1ea40da @sigma First version
authored
83 "Abstract authenticator")
84
efbc149 @sigma use proper constructors to populate git auth variables
authored
85 (defmethod constructor :static ((auth gh-authenticator) newname &rest args)
86 (let ((obj (call-next-method)))
34fb311 @sigma add support for various data formats
authored
87 (or (oref obj :username)
88 (oset obj :username (gh-auth-get-username)))
efbc149 @sigma use proper constructors to populate git auth variables
authored
89 obj))
90
c280f44 @sigma maintain autoloads file
authored
91 ;;;###autoload
1ea40da @sigma First version
authored
92 (defclass gh-password-authenticator (gh-authenticator)
9e4f934 @sigma make password storage optional
authored
93 ((password :initarg :password :protection :private :initform nil)
94 (remember :allocation :class :initform t))
1ea40da @sigma First version
authored
95 "Password-based authenticator")
96
efbc149 @sigma use proper constructors to populate git auth variables
authored
97 (defmethod constructor :static ((auth gh-password-authenticator) newname &rest args)
98 (let ((obj (call-next-method)))
34fb311 @sigma add support for various data formats
authored
99 (or (oref obj :password)
9e4f934 @sigma make password storage optional
authored
100 (oset obj :password (gh-auth-get-password (oref obj remember))))
efbc149 @sigma use proper constructors to populate git auth variables
authored
101 obj))
102
dbfb29f @sigma default implementation should leave requests alone
authored
103 (defmethod gh-auth-modify-request ((auth gh-authenticator) req)
104 req)
1ea40da @sigma First version
authored
105
106 (defmethod gh-auth-modify-request ((auth gh-password-authenticator) req)
c431c08 @sigma cleanup whitespaces
authored
107 (object-add-to-list req :headers
108 (cons "Authorization"
1ea40da @sigma First version
authored
109 (concat "Basic "
110 (base64-encode-string
c431c08 @sigma cleanup whitespaces
authored
111 (format "%s:%s" (oref auth :username)
112 (encode-coding-string
1ea40da @sigma First version
authored
113 (oref auth :password) 'utf-8))))))
114 req)
115
c280f44 @sigma maintain autoloads file
authored
116 ;;;###autoload
1ea40da @sigma First version
authored
117 (defclass gh-oauth-authenticator (gh-authenticator)
34fb311 @sigma add support for various data formats
authored
118 ((token :initarg :token :protection :private :initform nil))
1ea40da @sigma First version
authored
119 "Oauth-based authenticator")
120
efbc149 @sigma use proper constructors to populate git auth variables
authored
121 (defmethod constructor :static ((auth gh-oauth-authenticator) newname &rest args)
122 (let ((obj (call-next-method)))
34fb311 @sigma add support for various data formats
authored
123 (or (oref obj :token)
124 (oset obj :token (gh-auth-get-oauth-token)))
efbc149 @sigma use proper constructors to populate git auth variables
authored
125 obj))
126
1ea40da @sigma First version
authored
127 (defmethod gh-auth-modify-request ((auth gh-oauth-authenticator) req)
c431c08 @sigma cleanup whitespaces
authored
128 (object-add-to-list req :headers
129 (cons "Authorization"
130 (format "token %s" (oref auth :token))))
1ea40da @sigma First version
authored
131 req)
132
133 (provide 'gh-auth)
a36d9d7 @sigma use oauth api to get token from password
authored
134 ;; to avoid circular dependencies...
135 (require 'gh-oauth)
1ea40da @sigma First version
authored
136 ;;; gh-auth.el ends here
960077a @sigma add Local Variables section to disable indent-tabs-mode
authored
137
138 ;; Local Variables:
139 ;; indent-tabs-mode: nil
140 ;; End:
Something went wrong with that request. Please try again.