summaryrefslogtreecommitdiffstats
path: root/testapi/testapi-client/testapiclient/identity.py
blob: d62ed7aea2dc6822504936b3de6f2cb246cc338c (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
import requests
from user import User
from config import Config
import urllib
import functools


def _authenticate(username, password):
    session = requests.Session()
    hostname = '{}{}{}'.format(
        Config.config.get("cas", "auth_url"),
        urllib.quote(Config.config.get("api", "url")),
        Config.config.get("cas", "signin_return"))
    data = {'name': username, 'pass': password, 'form_id': 'user_login'}
    response = session.post(hostname, data)
    User.session = session
    return response


def authenticate(xstep):
    @functools.wraps(xstep)
    def wrapper(self, parsed_args):
        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."
            else:
                xstep(self, parsed_args)
    return wrapper