Add current entitlements to whoami response

This commit is contained in:
Ravi Khadiwala
2024-12-11 11:51:38 -06:00
committed by ravi-signal
parent d5b39cd496
commit 33c0a27b85
6 changed files with 165 additions and 3 deletions

View File

@@ -4,7 +4,11 @@
*/
package org.whispersystems.textsecuregcm.controllers;
import java.time.Clock;
import java.util.List;
import java.util.Optional;
import org.whispersystems.textsecuregcm.entities.AccountIdentityResponse;
import org.whispersystems.textsecuregcm.entities.Entitlements;
import org.whispersystems.textsecuregcm.storage.Account;
import org.whispersystems.textsecuregcm.storage.DeviceCapability;
@@ -12,10 +16,12 @@ public class AccountIdentityResponseBuilder {
private final Account account;
private boolean storageCapable;
private Clock clock;
public AccountIdentityResponseBuilder(Account account) {
this.account = account;
this.storageCapable = account.hasCapability(DeviceCapability.STORAGE);
this.clock = Clock.systemUTC();
}
public AccountIdentityResponseBuilder storageCapable(boolean storageCapable) {
@@ -23,13 +29,31 @@ public class AccountIdentityResponseBuilder {
return this;
}
public AccountIdentityResponseBuilder clock(Clock clock) {
this.clock = clock;
return this;
}
public AccountIdentityResponse build() {
final List<Entitlements.BadgeEntitlement> badges = account.getBadges()
.stream()
.filter(bv -> bv.expiration().isAfter(clock.instant()))
.map(badge -> new Entitlements.BadgeEntitlement(badge.id(), badge.expiration(), badge.visible()))
.toList();
final Entitlements.BackupEntitlement backupEntitlement = Optional
.ofNullable(account.getBackupVoucher())
.filter(bv -> bv.expiration().isAfter(clock.instant()))
.map(bv -> new Entitlements.BackupEntitlement(bv.receiptLevel(), bv.expiration()))
.orElse(null);
return new AccountIdentityResponse(account.getUuid(),
account.getNumber(),
account.getPhoneNumberIdentifier(),
account.getUsernameHash().filter(h -> h.length > 0).orElse(null),
account.getUsernameLinkHandle(),
storageCapable);
storageCapable,
new Entitlements(badges, backupEntitlement));
}
public static AccountIdentityResponse fromAccount(final Account account) {

View File

@@ -31,5 +31,8 @@ public record AccountIdentityResponse(
@Nullable UUID usernameLinkHandle,
@Schema(description="whether any of this account's devices support storage")
boolean storageCapable) {
boolean storageCapable,
@Schema(description = "entitlements for this account and their current expirations")
Entitlements entitlements) {
}

View File

@@ -0,0 +1,43 @@
package org.whispersystems.textsecuregcm.entities;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import io.swagger.v3.oas.annotations.media.Schema;
import org.whispersystems.textsecuregcm.util.InstantAdapter;
import javax.annotation.Nullable;
import java.time.Instant;
import java.util.List;
public record Entitlements(
@Schema(description = "Active badges added via /v1/donation/redeem-receipt")
List<BadgeEntitlement> badges,
@Schema(description = "If present, the backup level set via /v1/archives/redeem-receipt")
@Nullable BackupEntitlement backup) {
public record BadgeEntitlement(
@Schema(description = "The badge id")
String id,
@Schema(description = "When the badge expires, in number of seconds since epoch", implementation = Long.class)
@JsonSerialize(using = InstantAdapter.EpochSecondSerializer.class)
@JsonFormat(shape = JsonFormat.Shape.NUMBER_INT)
@JsonProperty("expirationSeconds")
Instant expiration,
@Schema(description = "Whether the badge is currently configured to be visible")
boolean visible) {
}
public record BackupEntitlement(
@Schema(description = "The backup level of the account")
long backupLevel,
@Schema(description = "When the backup entitlement expires, in number of seconds since epoch", implementation = Long.class)
@JsonSerialize(using = InstantAdapter.EpochSecondSerializer.class)
@JsonFormat(shape = JsonFormat.Shape.NUMBER_INT)
@JsonProperty("expirationSeconds")
Instant expiration) {
}
}