-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsession.py
More file actions
37 lines (28 loc) · 1.15 KB
/
session.py
File metadata and controls
37 lines (28 loc) · 1.15 KB
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
""" This module provides a class that implements a remote user session.
"""
from requests import delete, post
from yaml import safe_load
class Session(object):
""" This class implements a remote user session.
"""
def __init__(self, provider, service, username, password):
payload = {'username': username,
'password': password}
result = post(provider, data=payload)
if result.status_code != 201:
raise Exception('Session Error: Acquisition of ticket granting ticket failed.')
self.service = service
self.tgt = result.headers['location']
self.user = username
def __exit__(self, exc_type, exc_value, traceback):
delete(self.tgt)
def get_ticket(self):
""" Returns a one-time ticket for a remote user action """
payload = {'service': self.service}
result = post(self.tgt, data=payload)
if result.status_code != 200:
raise Exception('Session Error: Acquisition of session ticket failed.')
return safe_load(result.text)
def get_user(self):
""" Returns the name of the user """
return self.user