Skip to content

Commit

Permalink
Enable platform independency
Browse files Browse the repository at this point in the history
- Fail-safe import of (windows-only) dependecy requests_negotiate_sspi
- Optional dependency to requests_negotiate_sspi in setup.py only in
windows os

Fixes #127
  • Loading branch information
MariusWirtz committed May 28, 2019
1 parent 7ae0840 commit 8020427
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 21 deletions.
49 changes: 32 additions & 17 deletions TM1py/Services/RESTService.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
# -*- coding: utf-8 -*-
import functools
import sys
import warnings
from base64 import b64encode, b64decode

import requests
from requests.adapters import HTTPAdapter
from requests_negotiate_sspi import HttpNegotiateAuth

# SSO not supported for Linux
try:
from requests_negotiate_sspi import HttpNegotiateAuth
except ImportError:
warnings.warn("requests_negotiate_sspi failed to import. SSO will not work", ImportWarning)

from TM1py.Exceptions import TM1pyException

Expand Down Expand Up @@ -274,24 +280,33 @@ def _build_authorization_token(user, password, namespace=None, gateway=None, **k
""" Build the Authorization Header for CAM and Native Security
"""
if namespace:
if gateway:
response = requests.get(gateway, auth=HttpNegotiateAuth())
if not response.status_code == 200:
raise RuntimeError(
"Failed to authenticate through CAM. Expected status_code 200, received status_code: "
+ str(response.status_code))
elif 'cam_passport' not in response.cookies:
raise RuntimeError(
"Failed to authenticate through CAM. HTTP response does not contain 'cam_passport' cookie")
else:
token = 'CAMPassport ' + response.cookies['cam_passport']
return RESTService._build_authorization_token_cam(user, password, namespace, gateway)
else:
return RESTService._build_authorization_token_basic(user, password)

@staticmethod
def _build_authorization_token_cam(user=None, password=None, namespace=None, gateway=None):
if gateway:
if not HttpNegotiateAuth:
raise RuntimeError(
"SSO failed due to missing dependency requests_negotiate_sspi.HttpNegotiateAuth. "
"SSO only supported for Windows")
response = requests.get(gateway, auth=HttpNegotiateAuth())
if not response.status_code == 200:
raise RuntimeError(
"Failed to authenticate through CAM. Expected status_code 200, received status_code: "
+ str(response.status_code))
elif 'cam_passport' not in response.cookies:
raise RuntimeError(
"Failed to authenticate through CAM. HTTP response does not contain 'cam_passport' cookie")
else:
token = 'CAMNamespace ' + b64encode(
str.encode("{}:{}:{}".format(user, password, namespace))).decode("ascii")
return 'CAMPassport ' + response.cookies['cam_passport']
else:
token = 'Basic ' + b64encode(
str.encode("{}:{}".format(user, password))).decode("ascii")
return token
return 'CAMNamespace ' + b64encode(str.encode("{}:{}:{}".format(user, password, namespace))).decode("ascii")

@staticmethod
def _build_authorization_token_basic(user, password):
return 'Basic ' + b64encode(str.encode("{}:{}".format(user, password))).decode("ascii")

@staticmethod
def disable_http_warnings():
Expand Down
4 changes: 2 additions & 2 deletions Tests/config.ini
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
[tm1srv02]
[tm1srv01]
address=10.77.19.60
port=12354
user=Admin
password=apple
ssl=True

[tm1srv01]
[tm1srv02]
address=10.77.19.60
port=9699
user=Admin
Expand Down
8 changes: 6 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from setuptools import setup

SCHEDULE_VERSION = '1.3.0'
SCHEDULE_VERSION = '1.3.1'
SCHEDULE_DOWNLOAD_URL = (
'https://github.com/Cubewise-code/TM1py/tarball/' + SCHEDULE_VERSION
)
Expand Down Expand Up @@ -35,6 +35,10 @@
'Programming Language :: Python :: 3.6',
'Natural Language :: English',
],
install_requires=['requests', 'pandas', 'pytz', 'requests_negotiate_sspi'],
install_requires=[
'requests',
'pandas',
'pytz',
'requests_negotiate_sspi;platform_system=="Windows"'],
python_requires='>=3.5',
)

0 comments on commit 8020427

Please sign in to comment.