mirror of
https://github.com/balena-io/balena-cli.git
synced 2025-02-21 09:51:58 +00:00
Cache username for a given auth to avoid fetching it every time
Change-type: patch
This commit is contained in:
parent
ab9dabaf77
commit
2f7dd54e37
@ -29,6 +29,11 @@ const getMixpanel = _.once((balenaUrl: string) => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
interface CachedUsername {
|
||||||
|
token: string;
|
||||||
|
username: string;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Mixpanel.com analytics tracking (information on balena CLI usage).
|
* Mixpanel.com analytics tracking (information on balena CLI usage).
|
||||||
*
|
*
|
||||||
@ -48,14 +53,44 @@ export async function trackCommand(commandSignature: string) {
|
|||||||
Sentry.configureScope((scope) => {
|
Sentry.configureScope((scope) => {
|
||||||
scope.setExtra('command', commandSignature);
|
scope.setExtra('command', commandSignature);
|
||||||
});
|
});
|
||||||
const balena = getBalenaSdk();
|
const settings = await import('balena-settings-client');
|
||||||
const balenaUrlPromise = balena.settings.get('balenaUrl');
|
const balenaUrl = settings.get('balenaUrl') as string;
|
||||||
|
|
||||||
return Bluebird.props({
|
return Bluebird.props({
|
||||||
balenaUrl: balenaUrlPromise,
|
username: (async () => {
|
||||||
username: balena.auth.whoami().catchReturn(undefined),
|
const getStorage = await import('balena-settings-storage');
|
||||||
mixpanel: balenaUrlPromise.then(getMixpanel),
|
const dataDirectory = settings.get('dataDirectory') as string;
|
||||||
|
const storage = getStorage({ dataDirectory });
|
||||||
|
let token;
|
||||||
|
try {
|
||||||
|
token = await storage.get('token');
|
||||||
|
} catch {
|
||||||
|
// If we can't get a token then we can't get a username
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
const result = (await storage.get('cachedUsername')) as CachedUsername;
|
||||||
|
if (result.token === token) {
|
||||||
|
return result.username;
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
// ignore
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
const balena = getBalenaSdk();
|
||||||
|
const username = await balena.auth.whoami();
|
||||||
|
storage.set('cachedUsername', {
|
||||||
|
token,
|
||||||
|
username,
|
||||||
|
} as CachedUsername);
|
||||||
|
return username;
|
||||||
|
} catch {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
})(),
|
||||||
|
mixpanel: getMixpanel(balenaUrl),
|
||||||
})
|
})
|
||||||
.then(({ username, balenaUrl, mixpanel }) => {
|
.then(({ username, mixpanel }) => {
|
||||||
Sentry.configureScope((scope) => {
|
Sentry.configureScope((scope) => {
|
||||||
scope.setUser({
|
scope.setUser({
|
||||||
id: username,
|
id: username,
|
||||||
|
@ -336,7 +336,7 @@ export class BalenaAPIMock extends NockMock {
|
|||||||
|
|
||||||
// User details are cached in the SDK
|
// User details are cached in the SDK
|
||||||
// so often we don't know if we can expect the whoami request
|
// so often we don't know if we can expect the whoami request
|
||||||
public expectGetWhoAmI(opts: ScopeOpts = {}) {
|
public expectGetWhoAmI(opts: ScopeOpts = { optional: true }) {
|
||||||
this.optGet('/user/v1/whoami', opts).reply(200, {
|
this.optGet('/user/v1/whoami', opts).reply(200, {
|
||||||
id: 99999,
|
id: 99999,
|
||||||
username: 'gh_user',
|
username: 'gh_user',
|
||||||
|
Loading…
x
Reference in New Issue
Block a user