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

com.rt.storage.auth.oauth2.IdTokenCredentials Maven / Gradle / Ivy

package com.rt.storage.auth.oauth2;

import com.rt.storage.api.client.util.Preconditions;
import com.google.common.annotations.Beta;
import com.google.common.base.MoreObjects;
import java.io.IOException;
import java.util.List;
import java.util.Objects;

@Beta
public class IdTokenCredentials extends OAuth2Credentials {

  private static final long serialVersionUID = -2133257318957588431L;

  private IdTokenProvider idTokenProvider;
  private String targetAudience;
  private List options;

  private IdTokenCredentials(Builder builder) {
    this.idTokenProvider = Preconditions.checkNotNull(builder.getIdTokenProvider());
    this.targetAudience = Preconditions.checkNotNull(builder.getTargetAudience());
    this.options = builder.getOptions();
  }

  @Override
  public AccessToken refreshAccessToken() throws IOException {
    return this.idTokenProvider.idTokenWithAudience(targetAudience, options);
  }

  public IdToken getIdToken() {
    return (IdToken) getAccessToken();
  }

  @Override
  public int hashCode() {
    return Objects.hash(options, targetAudience);
  }

  @Override
  public String toString() {
    return MoreObjects.toStringHelper(this).toString();
  }

  @Override
  public boolean equals(Object obj) {
    if (!(obj instanceof IdTokenCredentials)) {
      return false;
    }
    IdTokenCredentials other = (IdTokenCredentials) obj;
    return Objects.equals(this.idTokenProvider, other.idTokenProvider)
        && Objects.equals(this.targetAudience, other.targetAudience);
  }

  public Builder toBuilder() {
    return new Builder()
        .setIdTokenProvider(this.idTokenProvider)
        .setTargetAudience(this.targetAudience)
        .setOptions(this.options);
  }

  public static Builder newBuilder() {
    return new Builder();
  }

  public static class Builder extends OAuth2Credentials.Builder {

    private IdTokenProvider idTokenProvider;
    private String targetAudience;
    private List options;

    protected Builder() {}

    public Builder setIdTokenProvider(IdTokenProvider idTokenProvider) {
      this.idTokenProvider = idTokenProvider;
      return this;
    }

    public IdTokenProvider getIdTokenProvider() {
      return this.idTokenProvider;
    }

    public Builder setTargetAudience(String targetAudience) {
      this.targetAudience = targetAudience;
      return this;
    }

    public String getTargetAudience() {
      return this.targetAudience;
    }

    public Builder setOptions(List options) {
      this.options = options;
      return this;
    }

    public List getOptions() {
      return this.options;
    }

    public IdTokenCredentials build() {
      return new IdTokenCredentials(this);
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy