-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: support plugins * fix: add test plugin * fix: ServiceRegister -> register_service * doc: plugin * feat: auth * Update add.py * fix: login * feat: oauth * fix: oauth * Update oauth.py * fix: auth login * feat: logout * Update configure.py * fix: py3 compatibility * Update login.py * Update login.py * Update login.py
- Loading branch information
Showing
13 changed files
with
652 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
import json | ||
import os | ||
import time | ||
|
||
import requests | ||
import uuid | ||
|
||
_API_ENDPOINT = "https://cli.cloud.tencent.com" | ||
_CRED_REFRESH_SAFE_DUR = 60 * 5 | ||
_ACCESS_REFRESH_SAFE_DUR = 60 * 5 | ||
|
||
|
||
def maybe_refresh_credential(profile): | ||
cred_path = cred_path_of_profile(profile) | ||
try: | ||
with open(cred_path, "r") as cred_file: | ||
cred = json.load(cred_file) | ||
except IOError: | ||
# file not found, don't check | ||
return | ||
|
||
if cred.get("type") != "oauth": | ||
return | ||
|
||
try: | ||
now = time.time() | ||
|
||
expires_at = cred["expiresAt"] | ||
if expires_at - now > _CRED_REFRESH_SAFE_DUR: | ||
return | ||
|
||
token_info = cred["oauth"] | ||
site = token_info["site"] | ||
access_expires = token_info["expiresAt"] | ||
if access_expires - now < _ACCESS_REFRESH_SAFE_DUR: | ||
refresh_token = token_info["refreshToken"] | ||
open_id = token_info["openId"] | ||
new_token = refresh_user_token(refresh_token, open_id, site) | ||
token_info.update(new_token) | ||
|
||
access_token = token_info["accessToken"] | ||
new_cred = get_temp_cred(access_token, site) | ||
save_credential(token_info, new_cred, profile) | ||
|
||
except KeyError as e: | ||
print("failed to refresh credential, your credential file(%s) is corrupted, %s" % (cred_path, e)) | ||
|
||
except Exception as e: | ||
print("failed to refresh credential, %s" % e) | ||
|
||
|
||
def refresh_user_token(ref_token, open_id, site): | ||
api_endpoint = _API_ENDPOINT + "/refresh_user_token" | ||
body = { | ||
"TraceId": str(uuid.uuid4()), | ||
"RefreshToken": ref_token, | ||
"OpenId": open_id, | ||
"Site": site, | ||
} | ||
http_response = requests.post(api_endpoint, json=body, verify=False) | ||
resp = http_response.json() | ||
|
||
if "Error" in resp: | ||
raise ValueError("refresh_user_token: %s" % json.dumps(resp)) | ||
|
||
return { | ||
"accessToken": resp["AccessToken"], | ||
"expiresAt": resp["ExpiresAt"], | ||
} | ||
|
||
|
||
def get_temp_cred(access_token, site): | ||
api_endpoint = _API_ENDPOINT + "/get_temp_cred" | ||
body = { | ||
"TraceId": str(uuid.uuid4()), | ||
"AccessToken": access_token, | ||
"Site": site, | ||
} | ||
http_response = requests.post(api_endpoint, json=body, verify=False) | ||
resp = http_response.json() | ||
|
||
if "Error" in resp: | ||
raise ValueError("get_temp_key: %s" % json.dumps(resp)) | ||
|
||
return { | ||
"secretId": resp["SecretId"], | ||
"secretKey": resp["SecretKey"], | ||
"token": resp["Token"], | ||
"expiresAt": resp["ExpiresAt"], | ||
} | ||
|
||
|
||
def cred_path_of_profile(profile): | ||
return os.path.join(os.path.expanduser("~"), ".tccli", profile + ".credential") | ||
|
||
|
||
def save_credential(token, new_cred, profile): | ||
cred_path = cred_path_of_profile(profile) | ||
|
||
cred = { | ||
"type": "oauth", | ||
"secretId": new_cred["secretId"], | ||
"secretKey": new_cred["secretKey"], | ||
"token": new_cred["token"], | ||
"expiresAt": new_cred["expiresAt"], | ||
"oauth": { | ||
"openId": token["openId"], | ||
"accessToken": token["accessToken"], | ||
"expiresAt": token["expiresAt"], | ||
"refreshToken": token["refreshToken"], | ||
"site": token["site"], | ||
}, | ||
} | ||
with open(cred_path, "w") as cred_file: | ||
json.dump(cred, cred_file, indent=4) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import importlib | ||
import logging | ||
import pkgutil | ||
|
||
import tccli.plugins as plugins | ||
|
||
_plugins = {} | ||
_imported = False | ||
|
||
|
||
def import_plugins(): | ||
global _imported | ||
|
||
if not _imported: | ||
_reimport_plugins() | ||
_imported = True | ||
|
||
return _plugins | ||
|
||
|
||
def _reimport_plugins(): | ||
for _, name, _ in pkgutil.iter_modules(plugins.__path__, plugins.__name__ + "."): | ||
mod = importlib.import_module(name) | ||
register_service = getattr(mod, "register_service", None) | ||
if not register_service: | ||
logging.warning("invalid module %s" % name) | ||
continue | ||
register_service(_plugins) | ||
|
||
return _plugins |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# encoding: utf-8 | ||
from tccli.plugins.auth.login import login_command_entrypoint | ||
from tccli.plugins.auth.logout import logout_command_entrypoint | ||
|
||
service_name = "auth" | ||
service_version = "2024-08-20" | ||
|
||
_spec = { | ||
"metadata": { | ||
"serviceShortName": service_name, | ||
"apiVersion": service_version, | ||
"description": "auth related commands", | ||
}, | ||
"actions": { | ||
"login": { | ||
"name": "登陆", | ||
"document": "login through sso", | ||
"input": "loginRequest", | ||
"output": "loginResponse", | ||
"action_caller": login_command_entrypoint, | ||
}, | ||
"logout": { | ||
"name": "登出", | ||
"document": "remove local credential file", | ||
"input": "logoutRequest", | ||
"output": "logoutResponse", | ||
"action_caller": logout_command_entrypoint, | ||
}, | ||
}, | ||
"objects": { | ||
"loginRequest": { | ||
"members": [ | ||
{ | ||
"name": "browser", | ||
"member": "string", | ||
"type": "string", | ||
"required": False, | ||
"document": "use browser=no to indicate no browser login mode", | ||
}, | ||
], | ||
}, | ||
"loginResponse": { | ||
"members": [], | ||
}, | ||
"logoutRequest": { | ||
"members": [], | ||
}, | ||
"logoutResponse": { | ||
"members": [], | ||
}, | ||
}, | ||
"version": "1.0", | ||
} | ||
|
||
|
||
def register_service(specs): | ||
specs[service_name] = { | ||
service_version: _spec, | ||
} |
Oops, something went wrong.