All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.molgenis.data.security.auth.Token Maven / Gradle / Ivy

There is a newer version: 8.4.5
Show newest version
package org.molgenis.data.security.auth;

import static java.time.Instant.now;
import static org.molgenis.data.security.auth.TokenMetadata.CREATIONDATE;
import static org.molgenis.data.security.auth.TokenMetadata.DESCRIPTION;
import static org.molgenis.data.security.auth.TokenMetadata.EXPIRATIONDATE;
import static org.molgenis.data.security.auth.TokenMetadata.ID;
import static org.molgenis.data.security.auth.TokenMetadata.TOKEN_ATTR;
import static org.molgenis.data.security.auth.TokenMetadata.USER;

import java.time.Instant;
import java.util.Optional;
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
import org.molgenis.data.Entity;
import org.molgenis.data.meta.model.EntityType;
import org.molgenis.data.support.StaticEntity;

public class Token extends StaticEntity {
  public Token(Entity entity) {
    super(entity);
  }

  public Token(EntityType entityType) {
    super(entityType);
  }

  public Token(String id, EntityType entityType) {
    super(entityType);
    setId(id);
  }

  public String getId() {
    return getString(ID);
  }

  public void setId(String id) {
    set(ID, id);
  }

  public User getUser() {
    return getEntity(USER, User.class);
  }

  public void setUser(User user) {
    set(USER, user);
  }

  public String getToken() {
    return getString(TOKEN_ATTR);
  }

  public void setToken(String token) {
    set(TOKEN_ATTR, token);
  }

  public Optional getExpirationDate() {
    return Optional.ofNullable(getInstant(EXPIRATIONDATE));
  }

  public void setExpirationDate(Instant expirationDate) {
    set(EXPIRATIONDATE, expirationDate);
  }

  public Instant getCreationDate() {
    return getInstant(CREATIONDATE);
  }

  public void setCreationDate(Instant creationDate) {
    set(CREATIONDATE, creationDate);
  }

  @Nullable
  @CheckForNull
  public String getDescription() {
    return getString(DESCRIPTION);
  }

  public void setDescription(String description) {
    set(DESCRIPTION, description);
  }

  public boolean isExpired() {
    Optional expirationDate = getExpirationDate();
    return expirationDate.isPresent() && expirationDate.get().isBefore(now());
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy