Skip to content

Sync-in · Sovereign platform for file storage, sharing, synchronization, and collaboration.

1.9 KB
Raw Blame
import { Controller, Get, HttpStatus, Query, Req, Res } from '@nestjs/common'
import { type FastifyReply, type FastifyRequest } from 'fastify'
import type { UserModel } from '../../../applications/users/models/user.model.js'
import { AuthManager } from '../../auth.service.js'
import { AUTH_ROUTE } from '../../constants/routes.js'
import { AuthTokenSkip } from '../../decorators/auth-token-skip.decorator.js'
import type { LoginResponseDto } from '../../dto/login-response.dto.js'
import { AUTH_SESSION } from '../auth-providers.constants.js'
import { OAuthDesktopPortParam } from './auth-oidc-desktop.constants.js'
import { oauthDesktopPortSchema, oidcCallbackQuerySchema } from './auth-oidc.schema.js'
import { AuthProviderOIDC } from './auth-provider-oidc.service.js'

@Controller(AUTH_ROUTE.BASE)
export class AuthOIDCController {
  constructor(
    private readonly authManager: AuthManager,
    private readonly authProviderOIDC: AuthProviderOIDC
  ) {}

  @Get(AUTH_ROUTE.OIDC_LOGIN)
  @AuthTokenSkip()
  async oidcLogin(
    @Query(OAuthDesktopPortParam, { schema: oauthDesktopPortSchema }) desktopPort: number | undefined,
    @Res() res: FastifyReply
  ): Promise<void> {
    const url = await this.authProviderOIDC.getAuthorizationUrl(res, desktopPort)
    // Redirect to OIDC provider
    return res.redirect(url, HttpStatus.FOUND)
  }

  @Get(AUTH_ROUTE.OIDC_CALLBACK)
  @AuthTokenSkip()
  async oidcCallback(
    @Query({ schema: oidcCallbackQuerySchema }) query: Record<string, string>,
    @Req() req: FastifyRequest,
    @Res() res: FastifyReply
  ): Promise<void> {
    const user: UserModel = await this.authProviderOIDC.handleCallback(req, res, query)
    const r: LoginResponseDto = await this.authManager.setCookies(user, res, false, AUTH_SESSION.OIDC)
    return res.redirect(this.authProviderOIDC.getRedirectCallbackUrl(r.token.access_expiration!, r.token.refresh_expiration!), HttpStatus.FOUND)
  }
}