Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
let auth = (await globalConfig.get(AUTH)) as AuthResponse | null;
// if old AUTH config is not found, we trigger a new login flow
if (auth === null) {
auth = await retryLogin(auth);
} else {
const creds = auth.credentials as DeviceTokenCredentials;
const { clientId, domain, username, tokenAudience, environment } = creds;
// if old AUTH config was found, we extract and check if the required fields are valid
if (creds && clientId && domain && username && tokenAudience && environment) {
const cache = new MemoryCache();
cache.add(creds.tokenCache._entries, () => {});
// we need to regenerate a proper object from the saved credentials
auth.credentials = new DeviceTokenCredentials(
clientId,
domain,
username,
tokenAudience,
new Environment(environment),
cache
);
const token = await auth.credentials.getToken();
// if extracted token has expired, we request a new login flow
if (new Date(token.expiresOn).getTime() < Date.now()) {
logger.info(`Your stored credentials have expired; you'll have to log in again`);
auth = await retryLogin(auth);
}
} else {
public async interactiveLogin(options?: InteractiveLoginOptions): Promise {
let authResp: AuthResponse = { credentials: undefined, subscriptions: [] };
const fileTokenCache = new SimpleFileTokenCache();
if (fileTokenCache.isEmpty()) {
await open("https://microsoft.com/devicelogin");
authResp = await interactiveLoginWithAuthResponse({ ...options, tokenCache: fileTokenCache });
fileTokenCache.addSubs(authResp.subscriptions);
} else {
authResp.credentials = new DeviceTokenCredentials(undefined, undefined, fileTokenCache.first().userId, undefined, undefined, fileTokenCache);
authResp.subscriptions = fileTokenCache.listSubscriptions();
}
return authResp;
}