CI: Use latest actions and reusable workflows
[releng/lftools.git] / lftools / oauth2_helper.py
1 # SPDX-License-Identifier: EPL-1.0
2 ##############################################################################
3 # Copyright (c) 2019, 2023 The Linux Foundation and others.
4 #
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Eclipse Public License v1.0
7 # which accompanies this distribution, and is available at
8 # http://www.eclipse.org/legal/epl-v10.html
9 ##############################################################################
10 """Helper script to get access_token for lfid api."""
11 from __future__ import annotations
12
13 import logging
14 from typing import Tuple
15
16 import httplib2
17 from oauth2client import client
18
19 from lftools import config
20
21
22 def oauth_helper() -> Tuple[str, str]:
23     """Helper script to get access_token for lfid api."""
24     logging.getLogger("oauth2client").setLevel(logging.ERROR)
25     client_id: str = str(config.get_setting("lfid", "clientid"))
26     client_secret: str = str(config.get_setting("lfid", "client_secret"))
27     refresh_token: str = str(config.get_setting("lfid", "refresh_token"))
28     token_uri: str = str(config.get_setting("lfid", "token_uri"))
29     url: str = str(config.get_setting("lfid", "url"))
30
31     credentials: client.Oauth2Credentials = client.OAuth2Credentials(
32         access_token=None,  # set access_token to None since we use a refresh token
33         client_id=client_id,
34         client_secret=client_secret,
35         refresh_token=refresh_token,
36         token_expiry=None,
37         token_uri=token_uri,
38         user_agent=None,
39     )
40     credentials.refresh(httplib2.Http())
41     access_token: str = credentials.access_token
42     return access_token, url