protected void verifyProviderCredentialsForUser(WritableSession session, User user) {
    List<UserAuthData> authData = user.getAuthData();
    UserProviderCredentialsMap credentials = session.getUserProviderCredentials();

    if (credentials != null) {

      if (authData != null) {
        Map<AuthProviderType, UserProviderCredentials> validCreds =
            new LinkedHashMap<AuthProviderType, UserProviderCredentials>();
        for (UserAuthData userAuthData : authData) {
          UserProviderCredentials creds = credentials.get(userAuthData.getAuthProviderType());
          if (creds != null) {
            validCreds.put(userAuthData.getAuthProviderType(), creds);
          }
        }

        // Clear and reset
        credentials.removeAll();

        Set<Entry<AuthProviderType, UserProviderCredentials>> entrySet = validCreds.entrySet();

        for (Entry<AuthProviderType, UserProviderCredentials> entry : entrySet) {
          credentials.put(entry.getKey(), entry.getValue());
        }
      } else {
        credentials.removeAll();
      }

      // Set back to session
      session.setUserProviderCredentials(credentials);
    }
  }
  protected WritableSession setProviderCredentialsForUser(
      AuthProviderData data, WritableSession session) {
    AuthProviderInfo info = data.getAuthProviderInfo();

    if (info != null) {
      DefaultUserProviderCredentials userProviderCredentials = new DefaultUserProviderCredentials();
      userProviderCredentials.setAccessToken(data.getToken3rdParty());
      userProviderCredentials.setTokenSecret(data.getSecret3rdParty());
      userProviderCredentials.setUserId(data.getUserId3rdParty());
      userProviderCredentials.setAuthProviderInfo(data.getAuthProviderInfo());

      session.getUserProviderCredentials().put(info.getType(), userProviderCredentials);
    } else {
      // Legacy
      session = null;
    }

    return session;
  }