Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add token authentication method #99

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 27 additions & 3 deletions pyteamcity/future/teamcity.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os

import requests
from requests.auth import AuthBase

from .core.manager import Manager
from .core.utils import parse_date_string, raise_on_status
Expand Down Expand Up @@ -34,9 +35,29 @@ def __repr__(self):
)


class TokenAuth(AuthBase):
"""Token Authentication to the given Request object."""

def __init__(self, token):
self.token = token

def __eq__(self, other):
return all([
self.token == getattr(other, 'token', None)
])

def __ne__(self, other):
return not self == other

def __call__(self, r):
r.headers['Authorization'] = f"Bearer {self.token}"
return r


class TeamCity(object):
username = None
password = None
token = None
server = None
port = None
protocol = None
Expand All @@ -46,14 +67,14 @@ class TeamCity(object):
def __init__(self,
username=None, password=None,
protocol='http', server='127.0.0.1', port=None,
session=None):
session=None, token=None):
self.username = username
self.password = password
self.protocol = protocol
self.server = server
self.port = port or (443 if protocol == 'https' else 80)
self.session = session or requests.Session()
self.session.auth = (username, password)
self.token = token
self.session.headers['Accept'] = 'application/json'
self.projects = Manager(
teamcity=self,
Expand Down Expand Up @@ -93,7 +114,10 @@ def __init__(self,
if self.protocol == 'https' and self.port != 443:
self.base_base_url += ':%d' % self.port

if self.username and self.password:
if token:
self.base_url = self.base_base_url
self.session.auth = TokenAuth(self.token)
elif self.username and self.password:
self.base_url = self.base_base_url + '/httpAuth'
self.auth = (self.username, self.password)
else:
Expand Down