Coverage for src/main/python/services/service_token.py: 100%

1 from datetime import UTC, datetime

2 from typing import Any, cast

3

4 import requests

5

6

7 def get_service_token(

8 org_name: str, team_name: str, key_id: str, key_secret: str

9) -> dict[str, Any]:

10 """

11 Get a service token from Okta API.

12

13 Args:

14 org_name: The organization name

15 team_name: The team name

16 key_id: The API key ID

17 key_secret: The API key secret

18

19 Returns:

20 dict: The API response data

21 """

22 url = f"https://{org_name}.pam.okta.com/v1/teams/{team_name}/service_token"

23

24 payload = {"key_id": key_id, "key_secret": key_secret}

25

26 headers = {"Content-Type": "application/json"}

27

28 response = requests.post(url, json=payload, headers=headers)

29 response.raise_for_status() # Raise an exception for bad status codes

30

31 data = response.json()

32

33 now = datetime.now(UTC)

34 expires_at = datetime.fromisoformat(data["expires_at"].replace("Z", "+00:00"))

35

36 time_diff = expires_at - now

37 seconds = int(time_diff.total_seconds())

38 # print(f"Token expires in: {seconds} seconds")

39 data["expires_in"] = seconds

40 return cast(dict[str, Any], data)