2021-06-14 16:09:40 +02:00
|
|
|
import { WebAuthService } from '@ghostfolio/api/app/auth/web-auth.service';
|
2021-04-21 20:27:39 +02:00
|
|
|
import { ConfigurationService } from '@ghostfolio/api/services/configuration.service';
|
2021-04-13 21:53:58 +02:00
|
|
|
import {
|
2021-06-14 16:09:40 +02:00
|
|
|
Body,
|
2021-04-13 21:53:58 +02:00
|
|
|
Controller,
|
|
|
|
Get,
|
|
|
|
HttpException,
|
|
|
|
Param,
|
2021-06-14 16:09:40 +02:00
|
|
|
Post,
|
2021-04-13 21:53:58 +02:00
|
|
|
Req,
|
|
|
|
Res,
|
|
|
|
UseGuards
|
|
|
|
} from '@nestjs/common';
|
|
|
|
import { AuthGuard } from '@nestjs/passport';
|
|
|
|
import { StatusCodes, getReasonPhrase } from 'http-status-codes';
|
|
|
|
|
|
|
|
import { AuthService } from './auth.service';
|
2021-06-14 16:09:40 +02:00
|
|
|
import {
|
|
|
|
AssertionCredentialJSON,
|
|
|
|
AttestationCredentialJSON
|
|
|
|
} from './interfaces/simplewebauthn';
|
2021-04-13 21:53:58 +02:00
|
|
|
|
|
|
|
@Controller('auth')
|
|
|
|
export class AuthController {
|
2021-04-18 19:06:54 +02:00
|
|
|
public constructor(
|
|
|
|
private readonly authService: AuthService,
|
2021-06-14 16:09:40 +02:00
|
|
|
private readonly configurationService: ConfigurationService,
|
|
|
|
private readonly webAuthService: WebAuthService
|
2021-04-18 19:06:54 +02:00
|
|
|
) {}
|
2021-04-13 21:53:58 +02:00
|
|
|
|
|
|
|
@Get('anonymous/:accessToken')
|
|
|
|
public async accessTokenLogin(@Param('accessToken') accessToken: string) {
|
|
|
|
try {
|
|
|
|
const authToken = await this.authService.validateAnonymousLogin(
|
|
|
|
accessToken
|
|
|
|
);
|
|
|
|
return { authToken };
|
|
|
|
} catch {
|
|
|
|
throw new HttpException(
|
|
|
|
getReasonPhrase(StatusCodes.FORBIDDEN),
|
|
|
|
StatusCodes.FORBIDDEN
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Get('google')
|
|
|
|
@UseGuards(AuthGuard('google'))
|
|
|
|
public googleLogin() {
|
|
|
|
// Initiates the Google OAuth2 login flow
|
|
|
|
}
|
|
|
|
|
|
|
|
@Get('google/callback')
|
|
|
|
@UseGuards(AuthGuard('google'))
|
|
|
|
public googleLoginCallback(@Req() req, @Res() res) {
|
|
|
|
// Handles the Google OAuth2 callback
|
|
|
|
const jwt: string = req.user.jwt;
|
|
|
|
|
|
|
|
if (jwt) {
|
2021-04-18 19:06:54 +02:00
|
|
|
res.redirect(`${this.configurationService.get('ROOT_URL')}/auth/${jwt}`);
|
2021-04-13 21:53:58 +02:00
|
|
|
} else {
|
2021-04-18 19:06:54 +02:00
|
|
|
res.redirect(`${this.configurationService.get('ROOT_URL')}/auth`);
|
2021-04-13 21:53:58 +02:00
|
|
|
}
|
|
|
|
}
|
2021-06-14 16:09:40 +02:00
|
|
|
|
|
|
|
@Get('webauthn/generate-attestation-options')
|
|
|
|
@UseGuards(AuthGuard('jwt'))
|
|
|
|
public async generateAttestationOptions() {
|
|
|
|
return this.webAuthService.generateAttestationOptions();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Post('webauthn/verify-attestation')
|
|
|
|
@UseGuards(AuthGuard('jwt'))
|
|
|
|
public async verifyAttestation(
|
|
|
|
@Body() body: { deviceName: string; credential: AttestationCredentialJSON }
|
|
|
|
) {
|
|
|
|
return this.webAuthService.verifyAttestation(
|
|
|
|
body.deviceName,
|
|
|
|
body.credential
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Post('webauthn/generate-assertion-options')
|
|
|
|
public async generateAssertionOptions(@Body() body: { deviceId: string }) {
|
|
|
|
return this.webAuthService.generateAssertionOptions(body.deviceId);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Post('webauthn/verify-assertion')
|
|
|
|
public async verifyAssertion(
|
|
|
|
@Body() body: { deviceId: string; credential: AssertionCredentialJSON }
|
|
|
|
) {
|
|
|
|
try {
|
|
|
|
const authToken = await this.webAuthService.verifyAssertion(
|
|
|
|
body.deviceId,
|
|
|
|
body.credential
|
|
|
|
);
|
|
|
|
return { authToken };
|
|
|
|
} catch {
|
|
|
|
throw new HttpException(
|
|
|
|
getReasonPhrase(StatusCodes.FORBIDDEN),
|
|
|
|
StatusCodes.FORBIDDEN
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2021-04-13 21:53:58 +02:00
|
|
|
}
|