mirror of
https://github.com/rustfs/console.git
synced 2026-08-30 17:14:47 +08:00
c019276fcb
Signed-off-by: junxiang Mu <1948535941@qq.com>
70 lines
1.7 KiB
TypeScript
70 lines
1.7 KiB
TypeScript
import type { AwsCredentialIdentity, AwsCredentialIdentityProvider } from '@aws-sdk/types';
|
|
import { getStsToken } from '~/lib/sts';
|
|
import type { SiteConfig } from '~/types/config';
|
|
|
|
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 (!isValidCredentials(store.value)) {
|
|
return;
|
|
}
|
|
|
|
return store.value;
|
|
};
|
|
|
|
const isExpired = (expiration: string) => (expiration ? new Date(expiration) < new Date() : false);
|
|
|
|
const isValidCredentials = (credentials: Credentials) => {
|
|
return (
|
|
!!credentials?.AccessKeyId &&
|
|
!!credentials?.SecretAccessKey &&
|
|
!!credentials?.SessionToken &&
|
|
credentials?.Expiration &&
|
|
!isExpired(credentials.Expiration)
|
|
);
|
|
};
|
|
|
|
const login = async (
|
|
credentials: AwsCredentialIdentity | AwsCredentialIdentityProvider,
|
|
customConfig?: SiteConfig
|
|
) => {
|
|
const credentialsResponse = await getStsToken(credentials, 'arn:aws:iam::*:role/Admin', customConfig);
|
|
|
|
setCredentials({
|
|
...credentialsResponse,
|
|
Expiration: credentialsResponse.Expiration?.toISOString(),
|
|
});
|
|
|
|
return credentialsResponse;
|
|
};
|
|
|
|
const logout = () => {
|
|
store.value = {};
|
|
};
|
|
|
|
const logoutAndRedirect = () => {
|
|
logout();
|
|
window.location.href = '/rustfs/console/auth/login';
|
|
};
|
|
|
|
return {
|
|
login,
|
|
logout,
|
|
logoutAndRedirect,
|
|
credentials: ref<Credentials | undefined>(getCredentials()),
|
|
isAuthenticated: ref(isValidCredentials(store.value)),
|
|
};
|
|
}
|