1515"""
1616Module containing client session classes.
1717"""
18-
18+ import base64
1919import json
2020import os
2121
2222from requests import RequestException , Session
2323
24- from ._2to3 import url_join
24+ from ._2to3 import bytes_ , unicode_ , url_join
2525from .error import CloudantException
2626
2727
@@ -30,11 +30,34 @@ class ClientSession(Session):
3030 This class extends Session and provides a default timeout.
3131 """
3232
33- def __init__ (self , ** kwargs ):
33+ def __init__ (self , username = None , password = None , session_url = None , ** kwargs ):
3434 super (ClientSession , self ).__init__ ()
35+
36+ self ._username = username
37+ self ._password = password
38+ self ._session_url = session_url
39+
40+ self ._auto_renew = kwargs .get ('auto_renew' , False )
3541 self ._timeout = kwargs .get ('timeout' , None )
3642
37- def request (self , method , url , ** kwargs ): # pylint: disable=W0221
43+ def base64_user_pass (self ):
44+ """
45+ Composes a basic http auth string, suitable for use with the
46+ _replicator database, and other places that need it.
47+
48+ :returns: Basic http authentication string
49+ """
50+ if self ._username is None or self ._password is None :
51+ return None
52+
53+ hash_ = base64 .urlsafe_b64encode (bytes_ ("{username}:{password}" .format (
54+ username = self ._username ,
55+ password = self ._password
56+ )))
57+ return "Basic {0}" .format (unicode_ (hash_ ))
58+
59+ # pylint: disable=arguments-differ
60+ def request (self , method , url , ** kwargs ):
3861 """
3962 Overrides ``requests.Session.request`` to set the timeout.
4063 """
@@ -43,27 +66,75 @@ def request(self, method, url, **kwargs): # pylint: disable=W0221
4366
4467 return resp
4568
69+ def info (self ):
70+ """
71+ Get session information.
72+ """
73+ if self ._session_url is None :
74+ return None
4675
47- class CookieSession (ClientSession ):
76+ resp = self .get (self ._session_url )
77+ resp .raise_for_status ()
78+ return resp .json ()
79+
80+ def set_credentials (self , username , password ):
81+ """
82+ Set a new username and password.
83+
84+ :param str username: New username.
85+ :param str password: New password.
86+ """
87+ if username is not None :
88+ self ._username = username
89+
90+ if password is not None :
91+ self ._password = password
92+
93+ def login (self ):
94+ """
95+ No-op method - not implemented here.
96+ """
97+ pass
98+
99+ def logout (self ):
100+ """
101+ No-op method - not implemented here.
102+ """
103+ pass
104+
105+
106+ class BasicSession (ClientSession ):
48107 """
49- This class extends ClientSession and provides cookie authentication.
108+ This class extends ClientSession to provide basic access authentication.
50109 """
51110
52111 def __init__ (self , username , password , server_url , ** kwargs ):
53- super (CookieSession , self ).__init__ (** kwargs )
54- self . _username = username
55- self . _password = password
56- self . _auto_renew = kwargs . get ( 'auto_renew' , False )
57- self . _session_url = url_join ( server_url , '_session' )
112+ super (BasicSession , self ).__init__ (
113+ username = username ,
114+ password = password ,
115+ session_url = url_join ( server_url , '_session' ),
116+ ** kwargs )
58117
59- def info (self ):
118+ def request (self , method , url , ** kwargs ):
60119 """
61- Get cookie based login user information.
120+ Overrides ``requests.Session.request`` to provide basic access
121+ authentication.
62122 """
63- resp = self . get ( self . _session_url )
64- resp . raise_for_status ( )
123+ return super ( BasicSession , self ). request (
124+ method , url , auth = ( self . _username , self . _password ), ** kwargs )
65125
66- return resp .json ()
126+
127+ class CookieSession (ClientSession ):
128+ """
129+ This class extends ClientSession and provides cookie authentication.
130+ """
131+
132+ def __init__ (self , username , password , server_url , ** kwargs ):
133+ super (CookieSession , self ).__init__ (
134+ username = username ,
135+ password = password ,
136+ session_url = url_join (server_url , '_session' ),
137+ ** kwargs )
67138
68139 def login (self ):
69140 """
@@ -83,7 +154,7 @@ def logout(self):
83154 resp = super (CookieSession , self ).request ('DELETE' , self ._session_url )
84155 resp .raise_for_status ()
85156
86- def request (self , method , url , ** kwargs ): # pylint: disable=W0221
157+ def request (self , method , url , ** kwargs ):
87158 """
88159 Overrides ``requests.Session.request`` to renew the cookie and then
89160 retry the original request (if required).
@@ -105,42 +176,21 @@ def request(self, method, url, **kwargs): # pylint: disable=W0221
105176
106177 return resp
107178
108- def set_credentials (self , username , password ):
109- """
110- Set a new username and password.
111-
112- :param str username: New username.
113- :param str password: New password.
114- """
115- if username is not None :
116- self ._username = username
117-
118- if password is not None :
119- self ._password = password
120-
121179
122180class IAMSession (ClientSession ):
123181 """
124182 This class extends ClientSession and provides IAM authentication.
125183 """
126184
127185 def __init__ (self , api_key , server_url , ** kwargs ):
128- super (IAMSession , self ).__init__ (** kwargs )
186+ super (IAMSession , self ).__init__ (
187+ session_url = url_join (server_url , '_iam_session' ),
188+ ** kwargs )
189+
129190 self ._api_key = api_key
130- self ._auto_renew = kwargs .get ('auto_renew' , False )
131- self ._session_url = url_join (server_url , '_iam_session' )
132191 self ._token_url = os .environ .get (
133192 'IAM_TOKEN_URL' , 'https://iam.bluemix.net/oidc/token' )
134193
135- def info (self ):
136- """
137- Get IAM cookie based login user information.
138- """
139- resp = self .get (self ._session_url )
140- resp .raise_for_status ()
141-
142- return resp .json ()
143-
144194 def login (self ):
145195 """
146196 Perform IAM cookie based user login.
@@ -164,7 +214,7 @@ def logout(self):
164214 """
165215 self .cookies .clear ()
166216
167- def request (self , method , url , ** kwargs ): # pylint: disable=W0221
217+ def request (self , method , url , ** kwargs ):
168218 """
169219 Overrides ``requests.Session.request`` to renew the IAM cookie
170220 and then retry the original request (if required).
@@ -191,7 +241,7 @@ def request(self, method, url, **kwargs): # pylint: disable=W0221
191241
192242 return resp
193243
194- # pylint: disable=arguments-differ
244+ # pylint: disable=arguments-differ, unused-argument
195245 def set_credentials (self , username , api_key ):
196246 """
197247 Set a new IAM API key.
0 commit comments