|
32 | 32 | import os
|
33 | 33 | import random
|
34 | 34 | import re
|
| 35 | +import requests |
35 | 36 | import six
|
36 | 37 | import warnings
|
37 | 38 | import weakref
|
38 |
| -from six.moves.urllib.parse import urlparse |
| 39 | +from six.moves.urllib.parse import urlparse, urlencode, urljoin |
39 | 40 | from . import rest
|
40 | 41 | from .. import clib
|
41 | 42 | from .. import config as cf
|
@@ -177,6 +178,8 @@ class CAS(object):
|
177 | 178 | Base path of URL when using the REST protocol.
|
178 | 179 | ssl_ca_list : string, optional
|
179 | 180 | The path to the SSL certificates for the CAS server.
|
| 181 | + authcode : string, optional |
| 182 | + Authorization code from SASLogon used to retrieve an OAuth token. |
180 | 183 | **kwargs : any, optional
|
181 | 184 | Arbitrary keyword arguments used for internal purposes only.
|
182 | 185 |
|
@@ -346,7 +349,7 @@ def _get_connection_info(cls, hostname, port, username, password, protocol, path
|
346 | 349 | def __init__(self, hostname=None, port=None, username=None, password=None,
|
347 | 350 | session=None, locale=None, nworkers=None, name=None,
|
348 | 351 | authinfo=None, protocol=None, path=None, ssl_ca_list=None,
|
349 |
| - auth_code=None, **kwargs): |
| 352 | + authcode=None, **kwargs): |
350 | 353 |
|
351 | 354 | # Filter session options allowed as parameters
|
352 | 355 | _kwargs = {}
|
@@ -392,11 +395,11 @@ def __init__(self, hostname=None, port=None, username=None, password=None,
|
392 | 395 | soptions = a2n(getsoptions(session=session, locale=locale,
|
393 | 396 | nworkers=nworkers, protocol=protocol))
|
394 | 397 |
|
395 |
| - # Check for auth_code authentication |
396 |
| - auth_code = auth_code or cf.get_option('cas.auth_code') |
397 |
| - if protocol in ['http', 'https'] and auth_code: |
| 398 | + # Check for authcode authentication |
| 399 | + authcode = authcode or cf.get_option('cas.authcode') |
| 400 | + if protocol in ['http', 'https'] and authcode: |
398 | 401 | username = None
|
399 |
| - password = self.get_token(auth_code=auth_code, url=hostname) |
| 402 | + password = type(self)._get_token(authcode=authcode, url=hostname) |
400 | 403 |
|
401 | 404 | # Create error handler
|
402 | 405 | try:
|
@@ -529,33 +532,41 @@ def _id_generator():
|
529 | 532 | num = num + 1
|
530 | 533 | self._id_generator = _id_generator()
|
531 | 534 |
|
532 |
| - def _get_token(self, username=None, password=None, auth_code=None, |
533 |
| - client_id=None, client_secret=None, base_url=None): |
| 535 | + @classmethod |
| 536 | + def _get_token(cls, username=None, password=None, authcode=None, |
| 537 | + client_id=None, client_secret=None, url=None): |
534 | 538 | ''' Retrieve token from Viya installation '''
|
535 |
| - headers = {'Accept': 'application/vnd.sas.compute.session+json', |
536 |
| - 'Content-Type': 'application/x-www-form-urlencoded'} |
| 539 | + from .rest.connection import _print_request, _setup_ssl |
| 540 | + |
| 541 | + with requests.Session() as req_sess: |
| 542 | + |
| 543 | + _setup_ssl(req_sess) |
| 544 | + |
| 545 | + req_sess.headers.update({'Accept': 'application/vnd.sas.compute.session+json', |
| 546 | + 'Content-Type': 'application/x-www-form-urlencoded'}) |
537 | 547 |
|
538 |
| - auth_code = auth_code or cf.get_option('cas.auth_code') |
539 |
| - if auth_code: |
540 | 548 | client_id = client_id or cf.get_option('cas.client_id') or 'SWAT'
|
541 |
| - client_secret = client_secret or cf.get_option('cas.client_secret') or '' |
542 |
| - body = {'grant_type': 'authorization_code', 'code': auth_code, |
543 |
| - 'client_id': client_id, 'client_secret': client_secret} |
544 |
| - else: |
545 |
| - user = client_id or cf.get_option('cas.username') |
546 |
| - password = client_secret or cf.get_option('cas.token') |
547 |
| - body = {'grant_type': 'password', 'username': user, |
548 |
| - 'password': password} |
549 |
| - |
550 |
| - resp = requests.post(base_url + '/SASLogon/oauth/token', |
551 |
| - headers=headers, user=('sas.tkmtrb', ''), |
552 |
| - data=urlparse.quote_plus(body)) |
553 |
| - |
554 |
| - if resp.status_code >= 300: |
555 |
| - raise SWATError('Token request resulted in a status of %s' % |
556 |
| - resp.status_code) |
557 |
| - |
558 |
| - return resp['access_token'] |
| 549 | + |
| 550 | + authcode = authcode or cf.get_option('cas.authcode') |
| 551 | + if authcode: |
| 552 | + client_secret = client_secret or cf.get_option('cas.client_secret') or '' |
| 553 | + body = {'grant_type': 'authorization_code', 'code': authcode, |
| 554 | + 'client_id': client_id, 'client_secret': client_secret} |
| 555 | + else: |
| 556 | + username = username or cf.get_option('cas.username') |
| 557 | + password = password or cf.get_option('cas.token') |
| 558 | + body = {'grant_type': 'password', 'username': username, |
| 559 | + 'password': password} |
| 560 | + |
| 561 | + resp = req_sess.post(urljoin(url, '/SASLogon/oauth/token'), |
| 562 | + auth=(client_id, ''), |
| 563 | + data=urlencode(body)) |
| 564 | + |
| 565 | + if resp.status_code >= 300: |
| 566 | + raise SWATError('Token request resulted in a status of %s' % |
| 567 | + resp.status_code) |
| 568 | + |
| 569 | + return resp.json()['access_token'] |
559 | 570 |
|
560 | 571 | def _gen_id(self):
|
561 | 572 | ''' Generate an ID unique to the session '''
|
|
0 commit comments