Skip to content

Commit

Permalink
Merge branch 'bigjools-develop' into develop
Browse files Browse the repository at this point in the history
* bigjools-develop:
  Add ability to use an auth token for login
  • Loading branch information
swayf committed Nov 12, 2017
2 parents 2eeb21e + 466a9e8 commit eaa31ef
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
23 changes: 21 additions & 2 deletions proxmoxer/backends/https.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,17 @@ def __call__(self, r):
return r


class ProxmoxHTTPTokenAuth(ProxmoxHTTPAuth):
"""Use existing ticket/token to create a session.
Overrides ProxmoxHTTPAuth so that an existing auth cookie and csrf token
may be used instead of passing username/password.
"""
def __init__(self, auth_token, csrf_token):
self.pve_auth_cookie = auth_token
self.csrf_prevention_token = csrf_token


class JsonSerializer(object):

content_types = [
Expand Down Expand Up @@ -96,9 +107,13 @@ def request(self, method, url, params=None, data=None, headers=None, cookies=Non


class Backend(object):
def __init__(self, host, user, password, port=8006, verify_ssl=True, mode='json', timeout=5):
def __init__(self, host, user, password, port=8006, verify_ssl=True,
mode='json', timeout=5, auth_token=None, csrf_token=None):
self.base_url = "https://{0}:{1}/api2/{2}".format(host, port, mode)
self.auth = ProxmoxHTTPAuth(self.base_url, user, password)
if auth_token is not None:
self.auth = ProxmoxHTTPTokenAuth(auth_token, csrf_token)
else:
self.auth = ProxmoxHTTPAuth(self.base_url, user, password)
self.verify_ssl = verify_ssl
self.mode = mode
self.timeout = timeout
Expand All @@ -118,3 +133,7 @@ def get_base_url(self):
def get_serializer(self):
assert self.mode == 'json'
return JsonSerializer()

def get_tokens(self):
"""Return the in-use auth and csrf tokens."""
return self.auth.pve_auth_cookie, self.auth.csrf_prevention_token
12 changes: 11 additions & 1 deletion proxmoxer/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
__copyright__ = '(c) Oleg Butovich 2013-2017'
__licence__ = 'MIT'

import os
import importlib
import posixpath
import logging
Expand Down Expand Up @@ -104,9 +103,20 @@ def __init__(self, host, backend='https', **kwargs):

#load backend module
self._backend = importlib.import_module('.backends.%s' % backend, 'proxmoxer').Backend(host, **kwargs)
self._backend_name = backend

self._store = {
"base_url": self._backend.get_base_url(),
"session": self._backend.get_session(),
"serializer": self._backend.get_serializer(),
}

def get_tokens(self):
"""Return the auth and csrf tokens.
Returns (None, None) if the backend is not https.
"""
if self._backend_name != 'https':
return None, None

return self._backend.get_tokens()

0 comments on commit eaa31ef

Please sign in to comment.