1
0
mirror of https://github.com/desktop/desktop synced 2024-07-05 00:58:57 +00:00

Memoize getGenericPassword to avoid double TokenStore lookups

Co-Authored-By: Sergio Padrino <1083228+sergiou87@users.noreply.github.com>
This commit is contained in:
Markus Olsson 2024-05-20 14:15:57 +02:00
parent a8b6ee7c72
commit 51fd0b7518

View File

@ -29,6 +29,7 @@ import {
setMostRecentGenericGitCredential,
} from './trampoline-environment'
import { IGitAccount } from '../../models/git-account'
import memoizeOne from 'memoize-one'
async function handleSSHHostAuthenticity(
prompt: string
@ -248,6 +249,18 @@ const handleAskPassUserPassword = async (
return kind === 'Username' ? username : password
}
/**
* When we're asked for credentials we're typically first asked for the username
* immediately followed by the password. We memoize the getGenericPassword call
* such that we only call it once per endpoint/login pair. Since we include the
* trampoline token in the invalidation key we'll only call it once per
* trampoline session.
*/
const memoizedGetGenericPassword = memoizeOne(
(_trampolineToken: string, endpoint: string, login: string) =>
getGenericPassword(endpoint, login)
)
async function findAccount(
trampolineToken: string,
accountsStore: AccountsStore,
@ -255,6 +268,7 @@ async function findAccount(
): Promise<IGitAccount | undefined> {
const accounts = await accountsStore.getAll()
const parsedUrl = new URL(remoteUrl)
const endpoint = urlWithoutCredentials(remoteUrl)
const account = accounts.find(
a => new URL(getHTMLURL(a.endpoint)).origin === parsedUrl.origin
)
@ -265,15 +279,18 @@ async function findAccount(
const login =
parsedUrl.username === ''
? getGenericUsername(urlWithoutCredentials(remoteUrl))
? getGenericUsername(endpoint)
: parsedUrl.username
if (!login) {
return undefined
}
const endpoint = urlWithoutCredentials(remoteUrl)
const token = await getGenericPassword(endpoint, login)
const token = await memoizedGetGenericPassword(
trampolineToken,
endpoint,
login
)
if (!token) {
// We have a username but no password, that warrants a warning