public async getClientAccessToken()

in sdk/webpubsub-socketio-extension/src/common/rest-service-client.ts [73:110]


  public async getClientAccessToken(options: GenerateClientTokenOptions = {}): Promise<ClientTokenResponse> {
    return tracingClient.withSpan("getClientAccessToken", options, async (updatedOptions) => {
      const endpoint = this.endpoint.endsWith("/") ? this.endpoint : this.endpoint + "/";
      const baseUrl = `${endpoint}clients/socketio/hubs/${this.hubName}`;
      const credential = this["credential"];
      const innerClient = this["client"];
      let token: string;
      if (isTokenCredential(credential)) {
        const response = await innerClient.webPubSub.generateClientToken(this.hubName, updatedOptions);
        token = response.token;
        if (token === null || token === undefined) {
          throw new Error(`Response from "generateClientToken" doesn't contain valid property "token"`);
        }
      } else {
        const key = credential.key;
        const payload = {
          role: options?.roles,
          "webpubsub.group": options?.groups,
          userId: options?.userId,
        };
        const signOptions: jwt.SignOptions = {
          audience: baseUrl,
          expiresIn: options?.expirationTimeInMinutes === undefined ? "1h" : `${options.expirationTimeInMinutes}m`,
          algorithm: "HS256",
        };
        if (options?.userId) {
          signOptions.subject = options?.userId;
        }
        token = jwt.sign(payload, key, signOptions);
      }

      return {
        token,
        baseUrl,
        url: `${baseUrl}?access_token=${token}`,
      };
    });
  }