Skip to content

AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents

2.8 KB
Raw Blame
import { AppConnectionValueForAuthProperty, PieceAuth } from '@activepieces/pieces-framework';
import { AppConnectionType } from '@activepieces/pieces-framework';
import { httpClient, HttpMethod, AuthenticationType } from '@activepieces/pieces-common';
import { SAVVYCAL_BASE_URL } from './common';

export const savvyCalAuth = [
  PieceAuth.OAuth2({
    description: 'Connect your SavvyCal account using OAuth2.',
    authUrl: 'https://savvycal.com/oauth/authorize',
    tokenUrl: 'https://savvycal.com/oauth/token',
    required: true,
    scope: [],
    getConnectionIdentifier: async ({ auth }) => fetchUserLabel(auth.access_token),
  }),
  PieceAuth.CustomAuth({
    displayName: 'Personal Access Token',
    description: `To get your SavvyCal API token:
1. Log in to your SavvyCal account at https://savvycal.com
2. Go to **Settings > Developers**
3. Under **Personal Tokens**, click **Create a token**
4. Give it a name, then click the **...** menu next to it to view the token
5. Copy the **Private Key** (starts with \`pt_secret_\`) — not the Public Key

**Note:** Keep this token secret — it gives full access to your SavvyCal account.`,
    required: true,
    props: {
      token: PieceAuth.SecretText({
        displayName: 'Personal Access Token',
        description: 'Your SavvyCal Personal Access Token (Private Key).',
        required: true,
      }),
    },
    validate: async ({ auth }) => {
      try {
        await httpClient.sendRequest({
          method: HttpMethod.GET,
          url: `${SAVVYCAL_BASE_URL}/me`,
          authentication: {
            type: AuthenticationType.BEARER_TOKEN,
            token: auth.token,
          },
        });
        return { valid: true };
      } catch {
        return { valid: false, error: 'Invalid token. Please check your Personal Access Token and try again.' };
      }
    },
    getConnectionIdentifier: async ({ auth }) => fetchUserLabel(auth.token),
  }),
];

export type SavvyCalAuthValue = AppConnectionValueForAuthProperty<typeof savvyCalAuth>

export function getToken(auth: SavvyCalAuthValue): string {
  if (auth.type === AppConnectionType.CUSTOM_AUTH) {
    return auth.props.token;
  }
  return auth.access_token;
}

async function fetchUserLabel(token: string): Promise<string | undefined> {
  try {
    const response = await httpClient.sendRequest<SavvyCalUser>({
      method: HttpMethod.GET,
      url: `${SAVVYCAL_BASE_URL}/me`,
      headers: { Authorization: `Bearer ${token}` },
      timeout: 5000,
    });
    const user = response.body;
    const fullName = [user.first_name, user.last_name].filter(Boolean).join(' ');
    return user.email || user.display_name || fullName || undefined;
  } catch {
    return undefined;
  }
}

type SavvyCalUser = {
  email?: string;
  display_name?: string;
  first_name?: string;
  last_name?: string;
};