Skip to content

Commit

Permalink
Add support for personal access tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
Vaelor committed Oct 6, 2017
1 parent cc33a51 commit d4a0435
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 23 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
2.2.0
'''''
Support for personal access tokens and MFA Token.

2.0.0
'''''

Expand Down
29 changes: 19 additions & 10 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ Usage
'url': 'mattermost.server.com',
'login_id': 'user.name',
'password': 'verySecret',
# Instead of login/password you can also use a personal access token
'token': 'YourPersonalAccessToken',
# Optional / defaults to
'scheme': 'https',
'port': 8065,
Expand All @@ -53,11 +55,15 @@ Usage
'verify': True,
# The interval the websocket will ping the server to keep the connection alive
'timeout': 30,
'mfa_token': 'YourMFAToken'
})
# Most of the requests need you to be logged in, so calling login()
# should be the first thing you do after you created your Driver instance.
# login() returns the raw response
# If using a personal access token, you still need to run login().
# In this case, does not make a login request, but a `get_user('me')`
# and sets everything up in the client.
foo.login()
# You can make api calls by using api['yourendpointofchoice'].
Expand Down Expand Up @@ -108,21 +114,24 @@ Usage
Available endpoints:
''''''''''''''''''''

- users
- teams
- base
- brand
- channels
- posts
- files
- preferences
- system
- webhooks
- cluster
- commands
- compliance
- cluster
- brand
- elasticsearch
- emoji
- files
- ldap
- oauth
- posts
- preferences
- saml
- ldap
- system
- teams
- users
- webhooks

See https://api.mattermost.com/v4/ to see which api requests are
available.
38 changes: 26 additions & 12 deletions src/mattermostdriver/driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,24 @@ class Driver:
'timeout': 30,
'login_id': None,
'password': None,
'token': None,
'mfa_token': None
}
"""
Required:
Either
- login_id
- password
Or
- token (https://docs.mattermost.com/developer/personal-access-tokens.html)
Optional:
- scheme
- url (though it would be a good idea to change that)
- port
- verify
- timeout
- mfa_token
Should not be changed:
- basepath - unlikeliy this would do any good
Expand Down Expand Up @@ -120,18 +126,26 @@ def login(self):
:return: The raw response from the request
"""
result = self.api['users'].login_user({
'login_id': self.options['login_id'],
'password': self.options['password'],
})
if self.options['token']:
self.client.token = self.options['token']
result = self.api['users'].get_user('me')
else:
result = self.api['users'].login_user({
'login_id': self.options['login_id'],
'password': self.options['password'],
'token': self.options['mfa_token']
})
if result.status_code == 200:
self.client.token = result.headers['Token']
self.client.cookies = result.cookies

log.debug(result)
if result.status_code == 200:
self.client.token = result.headers['Token']
self.client.cookies = result.cookies
if 'id' in result:
self.client.userid = result['id']
if 'username' in result:
self.client.username = result['username']

if 'id' in result:
self.client.userid = result['id']
if 'username' in result:
self.client.username = result['username']

return result

def logout(self):
Expand Down
2 changes: 1 addition & 1 deletion src/mattermostdriver/version.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
full_version = '2.1.4'
full_version = '2.2.0'
short_version = '.'.join(full_version.split('.', 2)[:2])

0 comments on commit d4a0435

Please sign in to comment.