import { md5 } from './tools' import { fetch } from './http-request' import { getMacAddress } from './platform' import { authStore } from './store/auth' import { ConfigItems } from '../constant' import { Credential, AuthSecret } from '../types' import { CloudBaseError } from '../error' const refreshTokenUrl = 'https://iaas.cloud.tencent.com/tcb_refresh' // 临时密钥过期后,进行续期 export async function refreshTmpToken( metaData: Credential & { isLogout?: boolean } ): Promise { const mac = await getMacAddress() const hash = md5(mac) metaData.hash = hash const res = await fetch(refreshTokenUrl, { method: 'POST', body: JSON.stringify(metaData), headers: { 'Content-Type': 'application/json' } }) if (res.code !== 0) { throw new CloudBaseError(res.message, { code: res.code }) } const { data: credential } = res return credential } // 获取 credentail 数据 export async function getCredentialData(): Promise { // @ts-ignore return authStore.get(ConfigItems.credentail) as Credential } // 获取身份认证信息并校验、自动刷新 export async function getCredentialWithoutCheck(): Promise { const credential = await getCredentialData() if (!credential) { return null } // 存在永久密钥 if (credential.secretId && credential.secretKey) { const { secretId, secretKey } = credential return { secretId, secretKey } } // 存在临时密钥信息 if (credential.refreshToken) { // 临时密钥在 2 小时有效期内,可以直接使用 if (Date.now() < Number(credential.tmpExpired)) { const { tmpSecretId, tmpSecretKey, tmpToken } = credential return { secretId: tmpSecretId, secretKey: tmpSecretKey, token: tmpToken } } else if (Date.now() < Number(credential.expired)) { // 临时密钥超过两小时有效期,但在 1 个月 refresh 有效期内,刷新临时密钥 const refreshCredential = await refreshTmpToken(credential) // 存储 await authStore.set(ConfigItems.credentail, refreshCredential) const { tmpSecretId, tmpSecretKey, tmpToken } = refreshCredential return { secretId: tmpSecretId, secretKey: tmpSecretKey, token: tmpToken } } } return null }