The vulnerability existed because the /users/login/sso/code-exchange endpoint did not validate the type of token being used for authentication. The loginSSOCodeExchange function would consume any provided token as long as it was valid, without checking if it was intended for the SSO code exchange flow. The core of the vulnerability was in the ConsumeTokenOnce function, which was called down to the database layer (SqlTokenStore.ConsumeOnce), where the token was deleted from the database based only on its value, not its type. This allowed an attacker to generate a token from another authentication process (e.g., SAML authentication) and use it to take over a user's account through the SSO code exchange. The patch addresses this by introducing a tokenType parameter to the ConsumeTokenOnce function and its callers, ensuring that the token type is checked at every level, from the API handler down to the database query. Specifically, loginSSOCodeExchange now requires a token of type TokenTypeSSOCodeExchange.