mirror of
https://github.com/rustfs/console.git
synced 2026-09-19 01:47:44 +08:00
54 lines
1.4 KiB
TypeScript
54 lines
1.4 KiB
TypeScript
import type { AwsCredentialIdentity, AwsCredentialIdentityProvider } from "@aws-sdk/types";
|
|
import { getStsToken } from "~/lib/sts";
|
|
|
|
interface Credentials {
|
|
AccessKeyId?: string;
|
|
SecretAccessKey?: string;
|
|
SessionToken?: string;
|
|
Expiration?: string;
|
|
}
|
|
|
|
export function useAuth() {
|
|
const store = useLocalStorage('auth.credentials', {})
|
|
|
|
const setCredentials = (credentials: Credentials) => {
|
|
store.value = credentials
|
|
}
|
|
|
|
const getCredentials = () => {
|
|
if (!isValiedCredentials(store.value)) {
|
|
return
|
|
}
|
|
|
|
return store.value
|
|
}
|
|
|
|
const isExpired = (expiration: string) => expiration ? new Date(expiration) < new Date() : false
|
|
|
|
const isValiedCredentials = (credentials: Credentials) => {
|
|
return !!credentials?.AccessKeyId && !!credentials?.SecretAccessKey && !!credentials?.SessionToken && credentials?.Expiration && !isExpired(credentials.Expiration)
|
|
}
|
|
|
|
const login = async (credentials: AwsCredentialIdentity | AwsCredentialIdentityProvider) => {
|
|
const credentialsResponse = await getStsToken(credentials, 'arn:aws:iam::*:role/Admin')
|
|
|
|
setCredentials({
|
|
...credentialsResponse,
|
|
Expiration: credentialsResponse.Expiration?.toISOString()
|
|
})
|
|
|
|
return credentialsResponse
|
|
}
|
|
|
|
const logout = () => {
|
|
store.value = {}
|
|
}
|
|
|
|
return {
|
|
login,
|
|
logout,
|
|
credentials: ref<Credentials | undefined>(getCredentials()),
|
|
isAuthenticated: ref(isValiedCredentials(store.value)),
|
|
}
|
|
}
|