Skip to content

Commit

Permalink
Support new style token-based authentication
Browse files Browse the repository at this point in the history
Phabricator has moved on from the clunky user+certificate-based authentication
scheme into one that provides the API user with a single token that can be
used directly. This change adds support for that and prefers it when available.

Fixes disqus#22.
  • Loading branch information
iocost-bot committed Jun 29, 2015
1 parent b399e20 commit 4a5e65b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
16 changes: 13 additions & 3 deletions phabricator/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,16 +308,20 @@ class Phabricator(Resource):
'json': lambda x: json.loads(x),
}

def __init__(self, username=None, certificate=None, host=None,
def __init__(self, token=None, username=None, certificate=None, host=None,
timeout=5, response_format='json', **kwargs):

# Set values in ~/.arcrc as defaults
if ARCRC:
self.host = host if host else ARCRC['hosts'].keys()[0]
self.username = username if username else ARCRC['hosts'][self.host]['user']
self.certificate = certificate if certificate else ARCRC['hosts'][self.host]['cert']
if token or ARCRC['hosts'][self.host].has_key('token'):
self.token = token if token else ARCRC['hosts'][self.host]['token']
else:
self.username = username if username else ARCRC['hosts'][self.host]['user']
self.certificate = certificate if certificate else ARCRC['hosts'][self.host]['cert']
else:
self.host = host
self.token = token
self.username = username
self.certificate = certificate

Expand All @@ -334,6 +338,12 @@ def _request(self, **kwargs):
raise SyntaxError('You cannot call the Conduit API without a resource.')

def connect(self):
if self.token:
self.conduit = {
'token': self.token
}
return

auth = Resource(api=self, method='conduit', endpoint='connect')

response = auth(user=self.username, host=self.host,
Expand Down
3 changes: 3 additions & 0 deletions phabricator/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
'maniphest.find': '{"result":{"PHID-TASK-4cgpskv6zzys6rp5rvrc":{"id":"722","phid":"PHID-TASK-4cgpskv6zzys6rp5rvrc","authorPHID":"PHID-USER-5022a9389121884ab9db","ownerPHID":"PHID-USER-5022a9389121884ab9db","ccPHIDs":["PHID-USER-5022a9389121884ab9db","PHID-USER-ba8aeea1b3fe2853d6bb"],"status":"3","priority":"Needs Triage","title":"Relations should be two-way","description":"When adding a differential revision you can specify Maniphest Tickets to add the relation. However, this doesnt add the relation from the ticket -> the differently.(This was added via the commit message)","projectPHIDs":["PHID-PROJ-358dbc2e601f7e619232","PHID-PROJ-f58a9ac58c333f106a69"],"uri":"https:\/\/secure.phabricator.com\/T722","auxiliary":[],"objectName":"T722","dateCreated":"1325553508","dateModified":"1325618490"}},"error_code":null,"error_info":null}'
}

# Protect against local user's .arcrc interference.
phabricator.ARCRC = {}

class PhabricatorTest(unittest.TestCase):
def setUp(self):
self.api = phabricator.Phabricator(username='test', certificate='test', host='http://localhost')
Expand Down

0 comments on commit 4a5e65b

Please sign in to comment.