Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use user's email as ID as a fallback for Vercel auth. #25

Merged
merged 3 commits into from
Oct 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 14 additions & 12 deletions src/main/java/vercel/VercelMPIdentityProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@

public class VercelMPIdentityProvider extends OIDCIdentityProvider implements SocialIdentityProvider<OIDCIdentityProviderConfig> {
private static final String BROKER_NONCE_PARAM = "BROKER_NONCE";
private static final String EMAIL_FALLBACK_TEMPLATE = "%[email protected]";

private static final Logger logger = Logger.getLogger(VercelMPIdentityProvider.class);
//private static final String AUTH_URL = "https://api.vercel.com/oauth/authorize";
Expand Down Expand Up @@ -147,32 +148,33 @@ public BrokeredIdentityContext getFederatedIdentity(String response) {

// Extract user's identity from JWT.
protected BrokeredIdentityContext extractIdentity(AccessTokenResponse tokenResponse, JsonWebToken idToken) {
String name = (String) idToken.getOtherClaims().get("user_name");
String email = (String) idToken.getOtherClaims().get("user_email");
String userIdPerInstallation = (String) idToken.getOtherClaims().get("user_id");

if (email == null || email.isEmpty()) {
email = EMAIL_FALLBACK_TEMPLATE.formatted(userIdPerInstallation);
kszafran marked this conversation as resolved.
Show resolved Hide resolved
}
// Global user ID is provided by Vercel only for Neon integrations!
// For other marketplace integrations it provides only user ID per each integration installation.
// I.e. the same Vercel user will have different ID in different Vercel teams.
//
// In case global_user_id is not set we will fall back to user ID per installation.
// In case global_user_id is not set we will fall back to user's Email and ID per installation eventually.
//
// NB! User will be able to login using Vercel SSO only into his first integration installation in case
// of userID per installation fallback! Because Keycloak will fail inserting second Federal ID for the same
// user and Identity Provider.
String id = (String) idToken.getOtherClaims().get("global_user_id");
if (id == null || id.isEmpty()) {
id = (String) idToken.getOtherClaims().get("user_id");
id = email;
}

BrokeredIdentityContext identity = new BrokeredIdentityContext(id, getConfig());

String name = (String) idToken.getOtherClaims().get("user_name");
String email = (String) idToken.getOtherClaims().get("user_email");

if (email == null || email.isEmpty()) {
email = id + "@vercel-marketplace.com";
}

identity.getContextData().put(VALIDATED_ID_TOKEN, idToken);

identity.setId(id);
identity.setEmail(email);
identity.setName(name);
identity.setUsername((name == null || name.isEmpty()) ? email : name);

identity.setBrokerUserId(getConfig().getAlias() + "." + id);

if (tokenResponse != null && tokenResponse.getSessionState() != null) {
Expand Down