com.google.firebase.auth.UserRecord Java Examples

The following examples show how to use com.google.firebase.auth.UserRecord. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: FirebaseAuthSnippets.java    From firebase-admin-java with Apache License 2.0 7 votes vote down vote up
public static void revokeIdTokens(
    String idToken) throws FirebaseAuthException {
  String uid = "someUid";
  // [START revoke_tokens]
  FirebaseAuth.getInstance().revokeRefreshTokens(uid);
  UserRecord user = FirebaseAuth.getInstance().getUser(uid);
  // Convert to seconds as the auth_time in the token claims is in seconds too.
  long revocationSecond = user.getTokensValidAfterTimestamp() / 1000;
  System.out.println("Tokens revoked at: " + revocationSecond);
  // [END revoke_tokens]

  // [START save_revocation_in_db]
  DatabaseReference ref = FirebaseDatabase.getInstance().getReference("metadata/" + uid);
  Map<String, Object> userData = new HashMap<>();
  userData.put("revokeTime", revocationSecond);
  ref.setValueAsync(userData);
  // [END save_revocation_in_db]
}
 
Example #2
Source File: AuthSnippets.java    From quickstart-java with Apache License 2.0 6 votes vote down vote up
public static void revokeIdTokens(String idToken) throws InterruptedException, ExecutionException { 
  String uid="someUid";
  // [START revoke_tokens]
  FirebaseAuth.getInstance().revokeRefreshTokensAsync(uid).get();
  UserRecord user = FirebaseAuth.getInstance().getUserAsync(uid).get();
  // Convert to seconds as the auth_time in the token claims is in seconds too. 
  long revocationSecond = user.getTokensValidAfterTimestamp() / 1000;
  System.out.println("Tokens revoked at: " + revocationSecond);
  // [END revoke_tokens]

  // [START save_revocation_in_db]
  DatabaseReference ref = FirebaseDatabase.getInstance().getReference("metadata/" + uid);
  Map<String, Object> userData = new HashMap<>();
  userData.put("revokeTime", revocationSecond);
  ref.setValueAsync(userData).get();
  // [END save_revocation_in_db]
  
}
 
Example #3
Source File: AuthSnippets.java    From quickstart-java with Apache License 2.0 6 votes vote down vote up
public static void setCustomUserClaims(
    String uid) throws InterruptedException, ExecutionException {
  // [START set_custom_user_claims]
  // Set admin privilege on the user corresponding to uid.
  Map<String, Object> claims = new HashMap<>();
  claims.put("admin", true);
  FirebaseAuth.getInstance().setCustomUserClaimsAsync(uid, claims).get();
  // The new custom claims will propagate to the user's ID token the
  // next time a new one is issued.
  // [END set_custom_user_claims]

  String idToken = "id_token";
  // [START verify_custom_claims]
  // Verify the ID token first.
  FirebaseToken decoded = FirebaseAuth.getInstance().verifyIdTokenAsync(idToken).get();
  if (Boolean.TRUE.equals(decoded.getClaims().get("admin"))) {
    // Allow access to requested admin resource.
  }
  // [END verify_custom_claims]

  // [START read_custom_user_claims]
  // Lookup the user associated with the specified uid.
  UserRecord user = FirebaseAuth.getInstance().getUserAsync(uid).get();
  System.out.println(user.getCustomClaims().get("admin"));
  // [END read_custom_user_claims]
}
 
Example #4
Source File: FirebaseAuthSnippets.java    From firebase-admin-java with Apache License 2.0 6 votes vote down vote up
public static void setCustomUserClaims(
    String uid) throws FirebaseAuthException {
  // [START set_custom_user_claims]
  // Set admin privilege on the user corresponding to uid.
  Map<String, Object> claims = new HashMap<>();
  claims.put("admin", true);
  FirebaseAuth.getInstance().setCustomUserClaims(uid, claims);
  // The new custom claims will propagate to the user's ID token the
  // next time a new one is issued.
  // [END set_custom_user_claims]

  String idToken = "id_token";
  // [START verify_custom_claims]
  // Verify the ID token first.
  FirebaseToken decoded = FirebaseAuth.getInstance().verifyIdToken(idToken);
  if (Boolean.TRUE.equals(decoded.getClaims().get("admin"))) {
    // Allow access to requested admin resource.
  }
  // [END verify_custom_claims]

  // [START read_custom_user_claims]
  // Lookup the user associated with the specified uid.
  UserRecord user = FirebaseAuth.getInstance().getUser(uid);
  System.out.println(user.getCustomClaims().get("admin"));
  // [END read_custom_user_claims]
}
 
Example #5
Source File: FirebaseAuthSnippets.java    From firebase-admin-java with Apache License 2.0 5 votes vote down vote up
public static void getUserByEmail(String email) throws FirebaseAuthException {
  // [START get_user_by_email]
  UserRecord userRecord = FirebaseAuth.getInstance().getUserByEmail(email);
  // See the UserRecord reference doc for the contents of userRecord.
  System.out.println("Successfully fetched user data: " + userRecord.getEmail());
  // [END get_user_by_email]
}
 
Example #6
Source File: AuthSnippets.java    From quickstart-java with Apache License 2.0 5 votes vote down vote up
public static void setCustomUserClaimsInc() throws InterruptedException, ExecutionException {
  // [START set_custom_user_claims_incremental]
  UserRecord user = FirebaseAuth.getInstance()
      .getUserByEmailAsync("[email protected]").get();
  // Add incremental custom claim without overwriting the existing claims.
  Map<String, Object> currentClaims = user.getCustomClaims();
  if (Boolean.TRUE.equals(currentClaims.get("admin"))) {
    // Add level.
    currentClaims.put("level", 10);
    // Add custom claims for additional privileges.
    FirebaseAuth.getInstance().setCustomUserClaimsAsync(user.getUid(), currentClaims).get();
  }
  // [END set_custom_user_claims_incremental]
}
 
Example #7
Source File: AuthSnippets.java    From quickstart-java with Apache License 2.0 5 votes vote down vote up
public static void setCustomUserClaimsScript() throws InterruptedException, ExecutionException {
  // [START set_custom_user_claims_script]
  UserRecord user = FirebaseAuth.getInstance()
      .getUserByEmailAsync("[email protected]").get();
  // Confirm user is verified.
  if (user.isEmailVerified()) {
    Map<String, Object> claims = new HashMap<>();
    claims.put("admin", true);
    FirebaseAuth.getInstance().setCustomUserClaimsAsync(user.getUid(), claims).get();
  }
  // [END set_custom_user_claims_script]
}
 
Example #8
Source File: AuthSnippets.java    From quickstart-java with Apache License 2.0 5 votes vote down vote up
public static void updateUser(String uid) throws InterruptedException, ExecutionException {
  // [START update_user]
  UpdateRequest request = new UpdateRequest(uid)
      .setEmail("[email protected]")
      .setPhoneNumber("+11234567890")
      .setEmailVerified(true)
      .setPassword("newPassword")
      .setDisplayName("Jane Doe")
      .setPhotoUrl("http://www.example.com/12345678/photo.png")
      .setDisabled(true);

  UserRecord userRecord = FirebaseAuth.getInstance().updateUserAsync(request).get();
  System.out.println("Successfully updated user: " + userRecord.getUid());
  // [END update_user]
}
 
Example #9
Source File: AuthSnippets.java    From quickstart-java with Apache License 2.0 5 votes vote down vote up
public static void createUserWithUid() throws InterruptedException, ExecutionException {
  // [START create_user_with_uid]
  CreateRequest request = new CreateRequest()
      .setUid("some-uid")
      .setEmail("[email protected]")
      .setPhoneNumber("+11234567890");

  UserRecord userRecord = FirebaseAuth.getInstance().createUserAsync(request).get();
  System.out.println("Successfully created new user: " + userRecord.getUid());
  // [END create_user_with_uid]
}
 
Example #10
Source File: AuthSnippets.java    From quickstart-java with Apache License 2.0 5 votes vote down vote up
public static void createUser() throws InterruptedException, ExecutionException {
  // [START create_user]
  CreateRequest request = new CreateRequest()
      .setEmail("[email protected]")
      .setEmailVerified(false)
      .setPassword("secretPassword")
      .setPhoneNumber("+11234567890")
      .setDisplayName("John Doe")
      .setPhotoUrl("http://www.example.com/12345678/photo.png")
      .setDisabled(false);

  UserRecord userRecord = FirebaseAuth.getInstance().createUserAsync(request).get();
  System.out.println("Successfully created new user: " + userRecord.getUid());
  // [END create_user]
}
 
Example #11
Source File: AuthSnippets.java    From quickstart-java with Apache License 2.0 5 votes vote down vote up
public static void getUserByPhoneNumber(
    String phoneNumber) throws InterruptedException, ExecutionException {
  // [START get_user_by_phone]
  UserRecord userRecord = FirebaseAuth.getInstance().getUserByPhoneNumberAsync(phoneNumber).get();
  // See the UserRecord reference doc for the contents of userRecord.
  System.out.println("Successfully fetched user data: " + userRecord.getPhoneNumber());
  // [END get_user_by_phone]
}
 
Example #12
Source File: AuthSnippets.java    From quickstart-java with Apache License 2.0 5 votes vote down vote up
public static void getUserByEmail(String email) throws InterruptedException, ExecutionException {
  // [START get_user_by_email]
  UserRecord userRecord = FirebaseAuth.getInstance().getUserByEmailAsync(email).get();
  // See the UserRecord reference doc for the contents of userRecord.
  System.out.println("Successfully fetched user data: " + userRecord.getEmail());
  // [END get_user_by_email]
}
 
Example #13
Source File: AuthSnippets.java    From quickstart-java with Apache License 2.0 5 votes vote down vote up
public static void getUserById(String uid) throws InterruptedException, ExecutionException {
  // [START get_user_by_id]
  UserRecord userRecord = FirebaseAuth.getInstance().getUserAsync(uid).get();
  // See the UserRecord reference doc for the contents of userRecord.
  System.out.println("Successfully fetched user data: " + userRecord.getUid());
  // [END get_user_by_id]
}
 
Example #14
Source File: GoogleFirebaseProvider.java    From mxisd with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public List<ThreePidMapping> populate(List<ThreePidMapping> mappings) {
    List<ThreePidMapping> results = new ArrayList<>();
    mappings.parallelStream().forEach(o -> {
        Optional<UserRecord> urOpt = findInternal(o.getMedium(), o.getValue());
        if (urOpt.isPresent()) {
            ThreePidMapping result = new ThreePidMapping();
            result.setMedium(o.getMedium());
            result.setValue(o.getValue());
            result.setMxid(getMxid(urOpt.get()));
            results.add(result);
        }
    });
    return results;
}
 
Example #15
Source File: GoogleFirebaseProvider.java    From mxisd with GNU Affero General Public License v3.0 5 votes vote down vote up
private Optional<UserRecord> findInternal(String medium, String address) {
    final UserRecord[] r = new UserRecord[1];
    CountDownLatch l = new CountDownLatch(1);

    OnSuccessListener<UserRecord> success = result -> {
        log.info("Found 3PID match for {}:{} - UID is {}", medium, address, result.getUid());
        r[0] = result;
        l.countDown();
    };

    OnFailureListener failure = e -> {
        log.info("No 3PID match for {}:{} - {}", medium, address, e.getMessage());
        r[0] = null;
        l.countDown();
    };

    if (ThreePidMedium.Email.is(medium)) {
        log.info("Performing E-mail 3PID lookup for {}", address);
        getFirebase().getUserByEmail(address)
                .addOnSuccessListener(success)
                .addOnFailureListener(failure);
        waitOnLatch(l);
    } else if (ThreePidMedium.PhoneNumber.is(medium)) {
        log.info("Performing msisdn 3PID lookup for {}", address);
        getFirebase().getUserByPhoneNumber(address)
                .addOnSuccessListener(success)
                .addOnFailureListener(failure);
        waitOnLatch(l);
    } else {
        log.info("{} is not a supported 3PID medium", medium);
        r[0] = null;
    }

    return Optional.ofNullable(r[0]);
}
 
Example #16
Source File: FirebaseAuthSnippets.java    From firebase-admin-java with Apache License 2.0 5 votes vote down vote up
public static void setCustomUserClaimsInc() throws FirebaseAuthException {
  // [START set_custom_user_claims_incremental]
  UserRecord user = FirebaseAuth.getInstance()
      .getUserByEmail("[email protected]");
  // Add incremental custom claim without overwriting the existing claims.
  Map<String, Object> currentClaims = user.getCustomClaims();
  if (Boolean.TRUE.equals(currentClaims.get("admin"))) {
    // Add level.
    currentClaims.put("level", 10);
    // Add custom claims for additional privileges.
    FirebaseAuth.getInstance().setCustomUserClaims(user.getUid(), currentClaims);
  }
  // [END set_custom_user_claims_incremental]
}
 
Example #17
Source File: FirebaseAuthSnippets.java    From firebase-admin-java with Apache License 2.0 5 votes vote down vote up
public static void setCustomUserClaimsScript() throws FirebaseAuthException {
  // [START set_custom_user_claims_script]
  UserRecord user = FirebaseAuth.getInstance()
      .getUserByEmail("[email protected]");
  // Confirm user is verified.
  if (user.isEmailVerified()) {
    Map<String, Object> claims = new HashMap<>();
    claims.put("admin", true);
    FirebaseAuth.getInstance().setCustomUserClaims(user.getUid(), claims);
  }
  // [END set_custom_user_claims_script]
}
 
Example #18
Source File: FirebaseAuthSnippets.java    From firebase-admin-java with Apache License 2.0 5 votes vote down vote up
public static void updateUser(String uid) throws FirebaseAuthException {
  // [START update_user]
  UpdateRequest request = new UpdateRequest(uid)
      .setEmail("[email protected]")
      .setPhoneNumber("+11234567890")
      .setEmailVerified(true)
      .setPassword("newPassword")
      .setDisplayName("Jane Doe")
      .setPhotoUrl("http://www.example.com/12345678/photo.png")
      .setDisabled(true);

  UserRecord userRecord = FirebaseAuth.getInstance().updateUser(request);
  System.out.println("Successfully updated user: " + userRecord.getUid());
  // [END update_user]
}
 
Example #19
Source File: FirebaseAuthSnippets.java    From firebase-admin-java with Apache License 2.0 5 votes vote down vote up
public static void createUserWithUid() throws FirebaseAuthException {
  // [START create_user_with_uid]
  CreateRequest request = new CreateRequest()
      .setUid("some-uid")
      .setEmail("[email protected]")
      .setPhoneNumber("+11234567890");

  UserRecord userRecord = FirebaseAuth.getInstance().createUser(request);
  System.out.println("Successfully created new user: " + userRecord.getUid());
  // [END create_user_with_uid]
}
 
Example #20
Source File: FirebaseAuthSnippets.java    From firebase-admin-java with Apache License 2.0 5 votes vote down vote up
public static void createUser() throws FirebaseAuthException {
  // [START create_user]
  CreateRequest request = new CreateRequest()
      .setEmail("[email protected]")
      .setEmailVerified(false)
      .setPassword("secretPassword")
      .setPhoneNumber("+11234567890")
      .setDisplayName("John Doe")
      .setPhotoUrl("http://www.example.com/12345678/photo.png")
      .setDisabled(false);

  UserRecord userRecord = FirebaseAuth.getInstance().createUser(request);
  System.out.println("Successfully created new user: " + userRecord.getUid());
  // [END create_user]
}
 
Example #21
Source File: FirebaseAuthSnippets.java    From firebase-admin-java with Apache License 2.0 5 votes vote down vote up
public static void getUserByPhoneNumber(
    String phoneNumber) throws FirebaseAuthException {
  // [START get_user_by_phone]
  UserRecord userRecord = FirebaseAuth.getInstance().getUserByPhoneNumber(phoneNumber);
  // See the UserRecord reference doc for the contents of userRecord.
  System.out.println("Successfully fetched user data: " + userRecord.getPhoneNumber());
  // [END get_user_by_phone]
}
 
Example #22
Source File: FirebaseAuthSnippets.java    From firebase-admin-java with Apache License 2.0 5 votes vote down vote up
public static void getUserById(String uid) throws FirebaseAuthException {
  // [START get_user_by_id]
  UserRecord userRecord = FirebaseAuth.getInstance().getUser(uid);
  // See the UserRecord reference doc for the contents of userRecord.
  System.out.println("Successfully fetched user data: " + userRecord.getUid());
  // [END get_user_by_id]
}
 
Example #23
Source File: GoogleFirebaseProvider.java    From mxisd with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public Optional<SingleLookupReply> find(SingleLookupRequest request) {
    Optional<UserRecord> urOpt = findInternal(request.getType(), request.getThreePid());
    return urOpt.map(userRecord -> new SingleLookupReply(request, getMxid(userRecord)));

}
 
Example #24
Source File: GoogleFirebaseProvider.java    From mxisd with GNU Affero General Public License v3.0 4 votes vote down vote up
private String getMxid(UserRecord record) {
    return MatrixID.asAcceptable(record.getUid(), domain).getId();
}