-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathlogin.py
More file actions
74 lines (57 loc) · 1.87 KB
/
login.py
File metadata and controls
74 lines (57 loc) · 1.87 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
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
71
72
73
74
"""
This module performs client credentials grant authentication
by sending HTTP requests with TLS and with required environment
variables.
"""
import os
import json
import time
import requests
from urllib.parse import urljoin
from config import get_config
class LoginError(Exception):
"""Raised when EIC login fails"""
def login():
"""
Get bearer token for accessing platform REST APIs:
https://developer.intelligentautomationplatform.ericsson.net/#tutorials/app-authentication
"""
config = get_config()
login_path = "/auth/realms/master/protocol/openid-connect/token"
# Envoy outbound listener
envoy_base_url = "http://127.0.0.1:9000"
login_url = urljoin(envoy_base_url, login_path)
headers = {
"Content-Type": "application/x-www-form-urlencoded"
}
form_data = {
"grant_type": "client_credentials",
"tenant_id": "master",
}
client_id_path = os.path.join(
"/", config.get("client_creds_file_path"), config.get("client_id_file_name")
)
try:
with open(client_id_path, "r", encoding="utf-8") as f:
form_data["client_id"] = f.read().strip()
except OSError as e:
raise LoginError(f"Error while reading client id: {e}") from e
try:
response = requests.post(
login_url,
data=form_data,
headers=headers,
timeout=5,
)
if response.status_code != 200:
raise LoginError(
f"Login failed ({response.status_code}): {response.text}"
)
except Exception as exception:
raise LoginError(f"Login failed ({exception})") from exception
resp = response.json()
token = resp["access_token"]
time_until_expiry = resp["expires_in"]
# buffer to avoid expiry mid-request
time_until_expiry -= 10
return token, time.time() + time_until_expiry