Before Token Expiry Worker Code Sample

JFrog Platform Administration Documentation

Content Type
Administration / Platform
ft:sourceType
Paligo

The following section provides a sample code for a Before Token Expiry worker.

export default async (
  context: PlatformContext,
  data: BeforeTokenExpiryRequest
): Promise<BeforeTokenExpiryResponse> => {
  try {
    // The in-browser HTTP client facilitates making calls to the JFrog REST APIs
    //To call an external endpoint, use 'await context.clients.axios.get("https://foo.com")'
    const res = await context.clients.platformHttp.get(
      "/artifactory/api/v1/system/readiness"
    );

    // You should reach this part if the HTTP request status is successful (HTTP Status 399 or lower)
    if (res.status === 200) {
      console.log("Artifactory ping success");
    } else {
      console.warn(
        `Request is successful but returned status other than 200. Status code : ${res.status}`
      );
    }
  } catch (error) {
    // The platformHttp client throws PlatformHttpClientError if the HTTP request status is 400 or higher
    console.error(
      `Request failed with status code ${
        error.status || "<none>"
      } caused by : ${error.message}`
    );
  }

  return {
    message: `Acknowledged tokens that are about to expire: ${data.tokens.length}`,
  };
};

Input Parameters

context

Provides baseUrl, token, and clients to communicate with the JFrog Platform (for more information, see PlatformContext).

data

The request with details sent by Access.

export interface BeforeTokenExpiryRequest {
    /** The tokens that will expire */
    tokens:
            | Array<Token>
            | undefined;
}

export interface Token {
    /** The token id to revoke */
    id: string
    /** The subject the token belongs to */
    subject: string;
    /** The owner of the token */
    owner: string;
    /** The scope of the token*/
    scope: string;
    /** The audience (i.e. services) this token is aimed for. These services are expected to accept this token */
    audience: string;
    /** The time (epoch) this token expires (optional if it has no expiration time) */
    expirationTime: number;
    /** The time (epoch) this token was created */
    created: number;
    /** Token type. Could be session or generic */
    type: string;
    /** Optional username derived from the token subject */
    username: string;
    /** Optional free text describing the token */
    description: string;
    /** The project key associated with the token */
    projectKey: string;
} 
Response
export interface BeforeTokenExpiryResponse {
    /** Message to print to the log, in case of an error it will be printed as a warning */
    message: string;
}
{
  "status": CreateTokenStatus.CREATE_TOKEN_PROCEED,
  "message": "Overwritten by worker-service if an error occurs.",
}