Skip to content

The micro-VM for AI agents — light enough to embed on your laptop, elastic enough to power an agentic cloud.

4.2 KB
Raw Blame
/*
 * Copyright 2025 Daytona Platforms Inc.
 * Modified by BoxLite AI, 2025-2026
 * SPDX-License-Identifier: AGPL-3.0
 */

import { Controller, Get, UseGuards } from '@nestjs/common'
import { ApiBearerAuth, ApiTags, ApiExcludeController } from '@nestjs/swagger'
import { CombinedAuthGuard } from '../auth/combined-auth.guard'
import { AuthContext } from '../common/decorators/auth-context.decorator'
import { AuthContext as AuthCtx } from '../common/interfaces/auth-context.interface'
import { OrganizationResourcePermission as Permission } from '../organization/enums/organization-resource-permission.enum'
import { hasOrganizationResourcePermissions } from '../organization/guards/organization-resource-action.guard'
import { OrganizationService } from '../organization/services/organization.service'
import { OrganizationUserService } from '../organization/services/organization-user.service'
import { SystemRole } from '../user/enums/system-role.enum'
import { PrincipalDto } from './dto/principal.dto'

const ORGANIZATION_SCOPE_PERMISSIONS: Record<string, Permission[]> = {
  'box:read': [],
  'box:write': [Permission.WRITE_BOXES],
  'box:exec': [Permission.WRITE_BOXES],
  'box:delete': [Permission.DELETE_BOXES],
  'image:read': [Permission.READ_IMAGES],
  'image:write': [Permission.WRITE_IMAGES],
  'volume:read': [Permission.READ_VOLUMES],
  'volume:write': [Permission.WRITE_VOLUMES],
  'volume:delete': [Permission.DELETE_VOLUMES],
}

/**
 * `GET /v1/me` — identity for the calling credential.
 *
 * Returns a [`PrincipalDto`] regardless of how the Bearer token was issued
 * (API key, OAuth device-flow access_token, or future federated source).
 * The CLI uses this to validate freshly-pasted keys and to render the
 * `Logged in as` banner.
 *
 * Spec: `openapi/box.openapi.yaml` § GET /me.
 */
@ApiExcludeController()
@ApiTags('BoxLite REST')
@Controller('v1')
@UseGuards(CombinedAuthGuard)
@ApiBearerAuth()
export class BoxliteMeController {
  constructor(
    private readonly organizationService: OrganizationService,
    private readonly organizationUserService: OrganizationUserService,
  ) {}

  @Get('me')
  async getMe(@AuthContext() ctx: AuthCtx): Promise<PrincipalDto> {
    const pathPrefix = await this.resolvePathPrefix(ctx)
    const organizationUser =
      pathPrefix && ctx.role !== SystemRole.ADMIN
        ? ((await this.organizationUserService.findOneCached(pathPrefix, ctx.userId)) ?? undefined)
        : undefined
    const subject = { role: ctx.role, apiKey: ctx.apiKey, organizationUser }
    const scopes = Object.entries(ORGANIZATION_SCOPE_PERMISSIONS)
      .filter(([, permissions]) => hasOrganizationResourcePermissions(subject, permissions))
      .map(([scope]) => scope)

    const principalType: 'user' | 'service_account' = ctx.apiKey ? 'service_account' : 'user'

    return {
      sub: ctx.userId,
      principal_type: principalType,
      email: ctx.email || undefined,
      display_name: undefined,
      path_prefix: pathPrefix,
      scopes: [...scopes, 'me:read'],
      // Source of truth for key expiry is ApiKey.expiresAt — the same column the
      // dashboard's `/api-keys` list renders. Returning a hardcoded null here let
      // clients believe a soon-to-expire key was permanent (P1-2). `null` stays
      // correct for non-expiring keys and for interactive user sessions (no apiKey).
      expires_at: ctx.apiKey?.expiresAt ? ctx.apiKey.expiresAt.toISOString() : null,
    }
  }

  /**
   * Resolve the routing-slot value for the calling credential.
   *
   * API keys carry their org binding directly. OIDC tokens carry no
   * org claim, so we look it up: prefer the user's default org;
   * otherwise the first membership; otherwise `null` (no scope yet —
   * the field stays present in the response envelope with explicit
   * `null` per the OpenAPI contract).
   */
  private async resolvePathPrefix(ctx: AuthCtx): Promise<string | null> {
    if (ctx.apiKey?.organizationId) {
      return ctx.apiKey.organizationId
    }
    const memberships = await this.organizationService.findByUserWithDefaultFlag(ctx.userId)
    if (memberships.length === 0) {
      return null
    }
    return (memberships.find((membership) => membership.isDefaultForAuthenticatedUser) ?? memberships[0]).organization
      .id
  }
}