summaryrefslogtreecommitdiffstats
path: root/testapi/testapi-client/testapiclient/utils/identity.py
blob: 2aeb87af143a85300f75f427c682e02fa7c2522b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import functools
import os
import urllib

import requests

from testapiclient.utils import user


def _authenticate(username, password):
    session = requests.Session()
    hostname = '{}{}{}'.format(os.environ.get('testapi_cas_auth_url'),
                               urllib.quote(os.environ.get('testapi_url')),
                               os.environ.get('testapi_cas_signin_return'))
    data = {
        'name': username,
        'pass': password,
        'form_id': 'user_login'
    }
    response = session.post(hostname, data)
    if "login" not in response.text:
        user.User.session = session
    return response


def authenticate(xstep):
    @functools.wraps(xstep)
    def wrapper(self, parsed_args):
        if(user.User.session is None):
            username = parsed_args.u
            password = parsed_args.p
            if(username and password):
                response = _authenticate(username, password)
                if "login" in response.text:
                    print "Authentication has failed."
                    return
        xstep(self, parsed_args)
    return wrapper