Repository: spotify/backstage
File: plugins/scaffolder-backend-module-bitbucket-cloud/src/actions/helpers.ts (TypeScript)

Code before:

import { Bitbucket } from 'bitbucket';

export const getBitbucketClient = (config: {
  token?: string;
  username?: string;
  appPassword?: string;
}) => {
  if (config.token) {
    return new Bitbucket({
      auth: {
        token: config.token,
      },
    });
  } else if (config.username && config.appPassword) {
    // TODO: appPassword can be removed once fully
    // deprecated by BitBucket on 9th June 2026.
    return new Bitbucket({
      auth: {
        username: config.username,
        password: config.appPassword,
      },
    });
  }
  throw new Error(
    `Authorization has not been provided for Bitbucket Cloud. Please add either provide a username and token or  username and appPassword to the Integrations config`,
  );
};

export const getAuthorizationHeader = (config: {
  username?: string;
  appPassword?: string;
  token?: string;
}) => {
  // TODO: appPassword can be removed once fully
  // deprecated by BitBucket on 9th June 2026.
  if (config.username && (config.token ?? config.appPassword)) {
    const buffer = Buffer.from(
      `${config.username}:${config.token ?? config.appPassword}`,
      'utf8',
    );
    return `Basic ${buffer.toString('base64')}`;
  }

  if (config.token) {
    return `Bearer ${config.token}`;
  }

  throw new Error(
    `Authorization has not been provided for Bitbucket Cloud. Please add either provide a username and token or  username and appPassword to the Integrations config`,
  );

Review comment (security):
```suggestion
    `Authorization has not been provided for Bitbucket Cloud. Please add either provide a username and token or username and appPassword to the Integrations config`,
```

Code after:

import { Bitbucket } from 'bitbucket';

export const getBitbucketClient = (config: {
  token?: string;
  username?: string;
  appPassword?: string;
}) => {
  if (config.token) {
    return new Bitbucket({
      auth: {
        token: config.token,
      },
    });
  } else if (config.username && config.appPassword) {
    // TODO: appPassword can be removed once fully
    // deprecated by BitBucket on 9th June 2026.
    return new Bitbucket({
      auth: {
        username: config.username,
        password: config.appPassword,
      },
    });
  }
  throw new Error(
    `Authorization has not been provided for Bitbucket Cloud. Please add either provide a username and token or username and appPassword to the Integrations config`,
  );
};

export const getAuthorizationHeader = (config: {
  username?: string;
  appPassword?: string;
  token?: string;
}) => {
  // TODO: appPassword can be removed once fully
  // deprecated by BitBucket on 9th June 2026.
  if (config.username && (config.token ?? config.appPassword)) {
    const buffer = Buffer.from(
      `${config.username}:${config.token ?? config.appPassword}`,
      'utf8',
    );
    return `Basic ${buffer.toString('base64')}`;
  }

  if (config.token) {
    return `Bearer ${config.token}`;
  }

  throw new Error(
    `Authorization has not been provided for Bitbucket Cloud. Please add either provide a username and token or username and appPassword to the Integrations config`,
  );