-
Notifications
You must be signed in to change notification settings - Fork 1
/
authonticate.py
70 lines (64 loc) · 2.1 KB
/
authonticate.py
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import requests
import json
import time
'''
this class for authonticate in keyston
'''
class Authonticate():
def __init__(self,user_name,password,project_scope_name="admin",domain_id="default"):
self.user_name=user_name
self.password=password
self.project_scope_name=project_scope_name
self.expire_time=None
self.domain_id=domain_id
self.token=None
def getRequestBody(self):
body={
"auth": {
"identity": {
"methods": ["password"],
"password": {
"user": {
"name": self.user_name,
"domain": { "id": "default" },
"password": self.password
}
}
},
"scope": {
"project": {
"name": self.project_scope_name,
"domain": { "id": self.domain_id }
}
}
}
}
return json.dumps(body)
def getURL(self):
return "http://ipaddress:5000/v3/auth/tokens"
'''
this function for get token
'''
def getToken(self):
if self.expire_time==None or self.expire_time < time.time():
body=self.getRequestBody()
header= {"Content-Type":"application/json"}
r = requests.post(self.getURL(),headers=header,data = body,verify=False)
self.authResponse=r.json()
if r.status_code ==201:
self.token=r.headers["X-Subject-Token"]
else:
self.status="failed"
self.token=None
self.expire_time=time.time()+1200
return self.token
'''
this function for authonticate test
'''
class TestAuthonticate():
def testGetBody(self):
auth=Authonticate("user","pass")
print(auth.getRequestBody())
def testGetToken(self):
auth=Authonticate("user","pass")
return auth.getToken()