Skip to content

Commit 4f5bca8

Browse files
committed
Add support for cookie file authentication
1 parent a581c95 commit 4f5bca8

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

bitcoin/rpc.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -158,13 +158,19 @@ def __init__(self,
158158
conf['rpcport'] = int(conf.get('rpcport', service_port))
159159
conf['rpchost'] = conf.get('rpcconnect', 'localhost')
160160

161-
if 'rpcpassword' not in conf:
162-
raise ValueError('The value of rpcpassword not specified in the configuration file: %s' % btc_conf_file)
161+
service_url = ('%s://%s:%d' %
162+
('http', conf['rpchost'], conf['rpcport']))
163+
164+
cookie_file = os.path.dirname(btc_conf_file)
165+
cookie_file = os.path.join(cookie_file, ".cookie")
166+
with open(cookie_file, 'r') as fd:
167+
authpair = fd.read()
163168

164-
service_url = ('%s://%s:%s@%s:%d' %
165-
('http',
166-
conf['rpcuser'], conf['rpcpassword'],
167-
conf['rpchost'], conf['rpcport']))
169+
if 'rpcpassword' in conf:
170+
authpair = "%s:%s" % (conf['rpcuser'], conf['rpcpassword'])
171+
172+
if authpair is None:
173+
raise ValueError('The value of rpcpassword not specified in the configuration file: %s' % btc_conf_file)
168174

169175
self.__service_url = service_url
170176
self.__url = urlparse.urlparse(service_url)
@@ -177,7 +183,6 @@ def __init__(self,
177183
else:
178184
port = self.__url.port
179185
self.__id_count = 0
180-
authpair = "%s:%s" % (self.__url.username, self.__url.password)
181186
authpair = authpair.encode('utf8')
182187
self.__auth_header = b"Basic " + base64.b64encode(authpair)
183188

0 commit comments

Comments
 (0)