// Path: src/main/java/com/google/fleetengine/auth/token/FleetEngineToken.java
// @AutoValue
// public abstract class FleetEngineToken {
//   /** Token creation timestamp. */
//   public abstract Date creationTimestamp();
// 
//   /** Token expiration timestamp. */
//   public abstract Date expirationTimestamp();
// 
//   /** Audience (aud claim) of the JWT. */
//   public abstract String audience();
// 
//   /** Signed and base64 encoded JWT. */
//   @Nullable
//   public abstract String jwt();
// 
//   /** Scope of the authorization token. */
//   public abstract FleetEngineTokenType tokenType();
// 
//   /** Custom authorization claims written to JWT. */
//   public abstract FleetEngineTokenClaims authorizationClaims();
// 
//   /** Provides a builder for {@link FleetEngineToken}. */
//   public static Builder builder() {
//     return new AutoValue_FleetEngineToken.Builder()
//         .setAudience(CommonConstants.DEFAULT_JWT_AUDIENCE);
//   }
// 
//   public abstract FleetEngineToken.Builder toBuilder();
// 
//   /** Builder class for FleetEngineToken. */
//   @AutoValue.Builder
//   public abstract static class Builder {
// 
//     /** Setter for token creation timestamp. */
//     public abstract Builder setCreationTimestamp(Date creationTimestamp);
// 
//     /** Setter for token expiration timestamp. */
//     public abstract Builder setExpirationTimestamp(Date expirationTimestamp);
// 
//     /** Setter for a signed JWT to be associated with the token object. */
//     public abstract Builder setJwt(String token);
// 
//     /** Setter for the token type defining the scope of the token. */
//     public abstract Builder setTokenType(FleetEngineTokenType type);
// 
//     /** Setter for token audience (aud claim). */
//     public abstract Builder setAudience(String audience);
// 
//     /** Sets custom authorization claims written to JWT. */
//     public abstract Builder setAuthorizationClaims(FleetEngineTokenClaims claims);
// 
//     /** Builds the token object. */
//     public abstract FleetEngineToken build();
//   }
// }
// 
// Path: src/main/java/com/google/fleetengine/auth/token/factory/signer/Signer.java
// public interface Signer {
// 
//   /**
//    * Signs a provided Fleet Engine token.
//    *
//    * @param token Fleet Engine token to sign
//    * @return same value as the {@code token} parameter except with the JWT property set to a valid
//    *     JWT
//    */
//   FleetEngineToken sign(FleetEngineToken token) throws SigningTokenException;
// }
// 
// Path: src/main/java/com/google/fleetengine/auth/token/factory/signer/SigningTokenException.java
// public class SigningTokenException extends Exception {
//   /**
//    * Constructs a {@code SigningTokenException} with the specified detail message.
//    *
//    * @param message The detail message (which is saved for later retrieval by the {@link
//    *     #getMessage()} method)
//    */
//   public SigningTokenException(String message) {
//     this(message, null);
//   }
// 
//   /**
//    * Constructs a {@code SigningTokenException} with the specified detail message.
//    *
//    * @param message The detail message (which is saved for later retrieval by the {@link
//    *     #getMessage()} method)
//    * @param cause The cause (which is saved for later retrieval by the {@link #getCause()} method).
//    *     (A null value is permitted, and indicates that the cause is nonexistent or unknown.)
//    */
//   public SigningTokenException(String message, Throwable cause) {
//     super(message, cause);
//   }
// }


import com.google.fleetengine.auth.token.FleetEngineToken;
import com.google.fleetengine.auth.token.factory.signer.Signer;
import com.google.fleetengine.auth.token.factory.signer.SigningTokenException;
import java.time.Duration;
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.fleetengine.auth;


/**
 * Provides non-expired Fleet Engine tokens.
 *
 * <p>Tokens can be freshly minted each time or reused.
 *
 * <p>Tokens are guaranteed to not expire for a duration of {@link
 * FleetEngineAuthTokenStateManager#EXPIRATION_WINDOW_DURATION} (currently 5 minutes.)
 */
public interface FleetEngineAuthTokenStateManager {

  /**
   * Tokens returned from any of the {@code findOrCreateToken} methods are guaranteed to be valid
   * for the given expiration window (currently 5 minutes.)
   */
  Duration EXPIRATION_WINDOW_DURATION = Duration.ofMinutes(5);

  /**
   * Either signs {@code token} or reuses an existing non-expired token.
   *
   * @param signer signer used to sign the token
   * @param token unsigned token
   * @return Signed Fleet Engine token that is guaranteed not to expire for the next {@link
   *     FleetEngineAuthTokenStateManager#EXPIRATION_WINDOW_DURATION}
   */
// Path: src/main/java/com/google/fleetengine/auth/token/FleetEngineToken.java
// @AutoValue
// public abstract class FleetEngineToken {
//   /** Token creation timestamp. */
//   public abstract Date creationTimestamp();
// 
//   /** Token expiration timestamp. */
//   public abstract Date expirationTimestamp();
// 
//   /** Audience (aud claim) of the JWT. */
//   public abstract String audience();
// 
//   /** Signed and base64 encoded JWT. */
//   @Nullable
//   public abstract String jwt();
// 
//   /** Scope of the authorization token. */
//   public abstract FleetEngineTokenType tokenType();
// 
//   /** Custom authorization claims written to JWT. */
//   public abstract FleetEngineTokenClaims authorizationClaims();
// 
//   /** Provides a builder for {@link FleetEngineToken}. */
//   public static Builder builder() {
//     return new AutoValue_FleetEngineToken.Builder()
//         .setAudience(CommonConstants.DEFAULT_JWT_AUDIENCE);
//   }
// 
//   public abstract FleetEngineToken.Builder toBuilder();
// 
//   /** Builder class for FleetEngineToken. */
//   @AutoValue.Builder
//   public abstract static class Builder {
// 
//     /** Setter for token creation timestamp. */
//     public abstract Builder setCreationTimestamp(Date creationTimestamp);
// 
//     /** Setter for token expiration timestamp. */
//     public abstract Builder setExpirationTimestamp(Date expirationTimestamp);
// 
//     /** Setter for a signed JWT to be associated with the token object. */
//     public abstract Builder setJwt(String token);
// 
//     /** Setter for the token type defining the scope of the token. */
//     public abstract Builder setTokenType(FleetEngineTokenType type);
// 
//     /** Setter for token audience (aud claim). */
//     public abstract Builder setAudience(String audience);
// 
//     /** Sets custom authorization claims written to JWT. */
//     public abstract Builder setAuthorizationClaims(FleetEngineTokenClaims claims);
// 
//     /** Builds the token object. */
//     public abstract FleetEngineToken build();
//   }
// }
// 
// Path: src/main/java/com/google/fleetengine/auth/token/factory/signer/Signer.java
// public interface Signer {
// 
//   /**
//    * Signs a provided Fleet Engine token.
//    *
//    * @param token Fleet Engine token to sign
//    * @return same value as the {@code token} parameter except with the JWT property set to a valid
//    *     JWT
//    */
//   FleetEngineToken sign(FleetEngineToken token) throws SigningTokenException;
// }
// 
// Path: src/main/java/com/google/fleetengine/auth/token/factory/signer/SigningTokenException.java
// public class SigningTokenException extends Exception {
//   /**
//    * Constructs a {@code SigningTokenException} with the specified detail message.
//    *
//    * @param message The detail message (which is saved for later retrieval by the {@link
//    *     #getMessage()} method)
//    */
//   public SigningTokenException(String message) {
//     this(message, null);
//   }
// 
//   /**
//    * Constructs a {@code SigningTokenException} with the specified detail message.
//    *
//    * @param message The detail message (which is saved for later retrieval by the {@link
//    *     #getMessage()} method)
//    * @param cause The cause (which is saved for later retrieval by the {@link #getCause()} method).
//    *     (A null value is permitted, and indicates that the cause is nonexistent or unknown.)
//    */
//   public SigningTokenException(String message, Throwable cause) {
//     super(message, cause);
//   }
// }


// Path: src/main/java/com/google/fleetengine/auth/FleetEngineAuthTokenStateManager.java
import com.google.fleetengine.auth.token.FleetEngineToken;
import com.google.fleetengine.auth.token.factory.signer.Signer;
import com.google.fleetengine.auth.token.factory.signer.SigningTokenException;
import java.time.Duration;

// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.fleetengine.auth;


/**
 * Provides non-expired Fleet Engine tokens.
 *
 * <p>Tokens can be freshly minted each time or reused.
 *
 * <p>Tokens are guaranteed to not expire for a duration of {@link
 * FleetEngineAuthTokenStateManager#EXPIRATION_WINDOW_DURATION} (currently 5 minutes.)
 */
public interface FleetEngineAuthTokenStateManager {

  /**
   * Tokens returned from any of the {@code findOrCreateToken} methods are guaranteed to be valid
   * for the given expiration window (currently 5 minutes.)
   */
  Duration EXPIRATION_WINDOW_DURATION = Duration.ofMinutes(5);

  /**
   * Either signs {@code token} or reuses an existing non-expired token.
   *
   * @param signer signer used to sign the token
   * @param token unsigned token
   * @return Signed Fleet Engine token that is guaranteed not to expire for the next {@link
   *     FleetEngineAuthTokenStateManager#EXPIRATION_WINDOW_DURATION}
   */
  FleetEngineToken signToken(Signer signer, FleetEngineToken token) throws SigningTokenException;