This commit is contained in:
batche
2022-10-31 18:10:33 +01:00
parent 6879ddaac8
commit 3b24582ebc
98 changed files with 5734 additions and 14 deletions

View File

@@ -5,6 +5,7 @@ import { UsersModule } from './users/users.module';
import { TypeOrmModule } from '@nestjs/typeorm';
import { ConfigModule } from '@nestjs/config';
import { FriendshipsModule } from './friendship/friendships.module';
import { AuthenticationModule } from './auth/42/authentication.module';
@Module({
imports: [UsersModule,

View File

@@ -0,0 +1,45 @@
import { Controller, Get, Res } from '@nestjs/common';
import { Response } from 'express';
@Controller('auth')
export class AuthenticationController {
/**
* GET /api/v2/auth/login
* Route pour l'autentification des utilisateurs
*/
@Get('login')
login() {
return 'login';
}
/**
* GET /api/v2/auth/redirect
* C'est la route que nous devons spécifier à l'Oauth de 42.
* L'api de 42 redirige vers cette route après l'autentification.
*/
@Get('redirect')
redirect(@Res() res: Response) {
res.send(200);
}
/**
* GET /api/v2/auth/status
* Route pour vérifier si l'utilisateur est connecté
* ou non.
*/
@Get('status')
status() {
return 'status';
}
/**
* GET /api/v2/auth/logout
* Route pour déconnecter l'utilisateur
*/
@Get('logout')
logout() {
return 'logout';
}
}

View File

@@ -1,9 +1,10 @@
import { Module } from '@nestjs/common';
import { UsersModule } from 'src/users/users.module';
import { AuthenticationService } from './authentication.service';
import { FortyTwoStrategy } from './strategy/strategy';
@Module({
imports: [UsersModule],
providers: [AuthenticationService],
providers: [AuthenticationService, FortyTwoStrategy],
})
export class AuthenticationModule {}

View File

@@ -0,0 +1,14 @@
import { CanActivate, ExecutionContext, Injectable } from "@nestjs/common";
import { AuthGuard } from "@nestjs/passport";
@Injectable()
export class FortyTwoAuthGuard extends AuthGuard('42') {
async canActivate(context: ExecutionContext): Promise<any> {
const activate = (await super.canActivate(context)) as boolean;
const request = context.switchToHttp().getRequest();
console.log(request.user);
await super.logIn(request);
return activate;
}
}

View File

@@ -0,0 +1,20 @@
import { Strategy, Profile } from "passport-42/lib";
import { PassportStrategy } from "@nestjs/passport";
import { Injectable } from "@nestjs/common";
import { AuthenticationService } from "../authentication.service";
@Injectable()
export class FortyTwoStrategy extends PassportStrategy(Strategy, "42") {
constructor(private readonly authenticationService: AuthenticationService) {
super({
clientID: process.env.FORTYTWO_CLIENT_ID,
clientSecret: process.env.FORTYTWO_CLIENT_SECRET,
callbackURL: process.env.FORTYTWO_CALLBACK_URL,
scope: ["public"],
});
}
async validate(accessToken: string, refreshToken: string, profile: Profile, callbackURL: string) {
}
}

View File

@@ -1,7 +0,0 @@
import { Controller } from '@nestjs/common';
@Controller('/')
export class AuthenticationController {
}

View File

@@ -26,7 +26,6 @@ export class Friendship {
@ManyToOne(type => User, user => user.addresseeId)
addresseeId: string;
@Column({ type: 'enum', enum: FriendshipStatus, default: FriendshipStatus.REQUESTED})
status: FriendshipStatus;
}

View File

@@ -20,6 +20,7 @@ async function bootstrap() {
},
}),
);
app.setGlobalPrefix('api/v2');
await app.listen(3000);
}
bootstrap();

View File

@@ -11,6 +11,9 @@ export class User {
@PrimaryGeneratedColumn()
id: number;
@Column({ name: 'fourty_two_id' })
fourtyTwoId: string;
@Column()
username: string;
@@ -21,6 +24,9 @@ export class User {
@Column()
password: string;
@Column({ nullable: true })
avatar: string;
@Column('json', { nullable: true })
status: [string];