@@ -24,7 +24,9 @@ def __init__(self, domain, realm, consumer_key, consumer_secret,
2424 request_token_url = REQUEST_TOKEN_URL ,
2525 access_token_url = ACCESS_TOKEN_URL ,
2626 authorization_url = AUTHORIZATION_URL ,
27- api_path = API_PATH ):
27+ api_path = API_PATH ,
28+ email = None ,
29+ password = None ):
2830 """
2931
3032 domain -- Your UI domain. The API is accessed off this domain.
@@ -50,6 +52,10 @@ def __init__(self, domain, realm, consumer_key, consumer_secret,
5052 self .authorization_url = authorization_url
5153 self .api_path = api_path
5254
55+ # These get cleared after log on attempt.
56+ self ._email = email
57+ self ._password = password
58+
5359 # You shouldn't need to access the oauth2 consumer and token objects
5460 # directly so we'll keep them "private".
5561 self ._consumer = oauth .Consumer (self .consumer_key , self .consumer_secret )
@@ -129,8 +135,18 @@ def fetch_request_token(self):
129135 self ._token = oauth .Token .from_string (res .read ())
130136 return self ._token
131137
132- def authorize_token (self , email , password ):
138+ def authorize_token (self , email = None , password = None ):
133139 """Helper method to authorize."""
140+ # Give precedence to credentials passed in methods calls over those set
141+ # in the instance. This allows you to override user creds that may have
142+ # been loaded from a file.
143+ email = email if email else self ._email
144+ password = password if password else self ._password
145+
146+ if not email or not password :
147+ self ._email = self ._password = None
148+ raise Exception ('Missing email or password' )
149+
134150 data = {
135151 'email' : email ,
136152 'password' : password ,
@@ -142,6 +158,9 @@ def authorize_token(self, email, password):
142158 data = data ,
143159 sign = True )
144160
161+ # Clear user credentials.
162+ self ._email = self ._password = None
163+
145164 verifier = urlparse .parse_qs (res .read ())['oauth_verifier' ][0 ]
146165 self ._token .set_verifier (verifier )
147166
@@ -186,6 +205,27 @@ def validate_session(self):
186205 res = self .request (url = url , method = 'PUT' )
187206 return res .read ()
188207
208+ def logon (self , email = None , password = None ):
209+ """Returns self after authentication.
210+
211+ Single call to complete OAuth login process.
212+
213+ Keyword arguments:
214+ email -- user email address.
215+ password -- user password.
216+
217+ """
218+ self .fetch_request_token ()
219+ self .authorize_token (email = email , password = password )
220+ self .fetch_access_token ()
221+ self .validate_session ()
222+ return self
223+
224+ def logoff (self ):
225+ """Returns self after deleting authenticated session."""
226+ self .delete ('/a/session' )
227+ return self
228+
189229 def _resolve_url (self , url ):
190230 """"""
191231 parse_res = urlparse .urlparse (url )
0 commit comments