Discovering external account name#

A bind procedure allows you to discover the name of a user external account and update the relevant parameter in the database each time the user logs in though an external identity provider. To do so, use the updateFederatedAccountName function.

Here’s an example of how to use the function in a procedure:

package com.identityblitz.idp.federation.matching.dynamic;

import java.lang.*;
import java.util.*;
import java.text.*;
import java.time.*;
import java.math.*;
import java.security.*;
import javax.crypto.*;
import org.slf4j.LoggerFactory;
import org.slf4j.Logger;
import com.identityblitz.idp.federation.*;
import com.identityblitz.idp.federation.matching.*;
import com.identityblitz.idp.flow.common.api.*;
import com.identityblitz.idp.flow.common.model.*;
import com.identityblitz.idp.federation.matching.dynamic.*;
import java.util.function.Consumer;
import java.util.stream.Stream;
import java.util.stream.Collectors;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;
import com.identityblitz.idp.extensions.types.JsObject;

import com.identityblitz.idp.federation.matching.*;
import com.identityblitz.idp.flow.common.api.HttpFactory;

public class Esia_1Esia extends MatchingBlock {

  private final Logger logger = LoggerFactory.getLogger("com.identityblitz.idp.federation.matching.dynamic");

  @Override public MatchResult match(MatchingContext ctx, List<MatchingUser> users){

    if (ctx.iteration() == 1) {
      //return matchError(ctx, new MatchingError("bad_err_code","bad_err_msg"));
      return tryToSearch(ctx);
    }

    if (users.isEmpty()) {
      return matchError(ctx, new MatchingError("error","error"));
    }

    if (users.size() == 1) {
      return matched(ctx, users.get(0));
    }

    return refine(ctx, users, ctx.extAttrs());
  };

  private MatchResult tryToSearch(MatchingContext ctx) {
    return find(ctx, filter().eq("uid", "test@test.ru"));
  }

  @Override public Tuple2<JsObj, Set<String>> update(JsObj extAttrs, MatchingUser user, Boolean justMatched, HttpFactory httpFactory){
    return change(JsObj.empty(), Collections.<String>emptySet());
  };

  @Override  public boolean isAllowMultiBind() {
    return true;
  }

  @Override public String updateFederatedAccountName(JsObj extAttrs){
    if (extAttrs.contains("firstName") && extAttrs.contains("lastName")){
      String name = extAttrs.asString("firstName") + " " + extAttrs.asString("lastName");
      if (extAttrs.contains("middleName")) {
        name = name + " " + extAttrs.asString("middleName");
      }
      return name;
   } else {
     // don't update federated account name
     return super.updateFederatedAccountName(extAttrs);
   }
  };
}