Skip to content

Re-org the public open core #38

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,13 @@ Here is the list of environment variables:

#### For the api

- **GITHUB_ID** (required): the Github ID for the created Github OAuth App
- **GITHUB_SECRET** (required): the Github Secret for the created Github OAuth App
- **GIT_PROVIDER_NAME**: The name of the git provider to connect to (github or gitlab)
- **GIT_PROVIDER_TOKEN**: The personal access token of the git provider to connect to
- **REDIS_HOST** (required): the host for the Redis server
- **REDIS_PORT** (required): the port for the Redis server
- **REDIS_PASSWORD** (optional): the password for the Redis server
- **JWT_SECRET** (required): the secret used to decrypt JWT tokens

#### For the client

- **GITHUB_ID** (required): the Github ID for the created Github OAuth App
- **GITHUB_SECRET** (required): the Github Secret for the created Github OAuth App
- **NEXT_PUBLIC_API_URL** (required): the public URL for the API
- **SERVER_API_URL** (required): the private URL for the API for server to server calls
- **NEXTAUTH_SECRET** (required): the secret used to encrypt JWT tokens (must be the same as the `JWT_SECRET` env variable for the api)
7 changes: 2 additions & 5 deletions api/.env
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
JWT_SECRET=SECRET
GITHUB_ID=GITHUB_ID
GITHUB_SECRET=GITHUB_SECRET
GITLAB_ID=GITLAB_ID
GITLAB_SECRET=GITLAB_SECRET
GIT_PROVIDER_NAME=github
GIT_PROVIDER_TOKEN=GIT_PROVIDER_TOKEN
REDIS_PORT=6379
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=REDIS_PASSWORD
11 changes: 10 additions & 1 deletion api/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { MiddlewareConsumer, Module, NestModule } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { TypeOrmModule } from '@nestjs/typeorm';
import { BullModule } from '@nestjs/bull';
import { AuthMiddleware } from './auth.middleware';
import { User } from './entities/user.entity';
import { UsersModule } from './modules/users/users.module';
import { Repo } from './entities/repo.entity';
Expand All @@ -12,6 +11,9 @@ import { Issue } from './entities/issue.entity';
import { PullRequest } from './entities/pullrequest.entity';
import { GitProviderModule } from './modules/git-provider/gitprovider.module';
import { RepoModule } from './modules/repo/repo.module';
import { NotificationsModule } from './modules/notifications/notifications.module';
import { InitService } from './init.service';
import { AuthMiddleware } from './auth.middleware';

@Module({
imports: [
Expand All @@ -36,9 +38,16 @@ import { RepoModule } from './modules/repo/repo.module';
GitProviderModule,
RepoModule,
SynchronizeModule,
NotificationsModule,
],
providers: [InitService],
})
export class AppModule implements NestModule {
constructor(private readonly initService: InitService) {}
onModuleInit() {
console.log(`Initialization...`);
this.initService.createUser().then(() => console.log('Initialized !'));
}
configure(consumer: MiddlewareConsumer) {
consumer.apply(AuthMiddleware).forRoutes('*');
}
Expand Down
23 changes: 2 additions & 21 deletions api/src/auth.middleware.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,21 @@
import { Injectable, NestMiddleware } from '@nestjs/common';
import { Request, Response, NextFunction } from 'express';
import { verify } from 'jsonwebtoken';
import { SESSION_COOKIE_NAME } from './common/constants';
import { GitProviderService } from './modules/git-provider/gitprovider.service';
import { RepoService } from './modules/repo/repo.service';
import { UsersService } from './modules/users/users.service';

@Injectable()
export class AuthMiddleware implements NestMiddleware {
constructor(
private readonly usersService: UsersService,
private readonly gitProviderService: GitProviderService,
private readonly repoService: RepoService,
) {}

async use(req: Request, res: Response, next: NextFunction) {
let token = req.cookies[SESSION_COOKIE_NAME];
if (!token) {
const authorization = req.header('authorization');
token = authorization?.replace('Bearer ', '');
}

if (!token) {
res.sendStatus(401);
return;
}

const jwt = verify(token, process.env.JWT_SECRET);

const user = await this.usersService.findOne(jwt.sub as string);
req['token'] = jwt;
async use(req: Request, _res: Response, next: NextFunction) {
const user = await this.usersService.findOne('user');
req['user'] = user;

if (user) {
this.gitProviderService.auth(user.gitProviderName, user.gitProviderToken);
this.repoService.auth(user.gitProviderName, user.gitProviderToken);
}
next();
}
Expand Down
1 change: 0 additions & 1 deletion api/src/common/constants.ts

This file was deleted.

5 changes: 5 additions & 0 deletions api/src/common/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export interface Organization {
id: string;
login: string;
full_path?: string;
}
6 changes: 6 additions & 0 deletions api/src/entities/repo.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ export class Repo {
})
organization?: string;

@Column({
type: String,
nullable: true,
})
owner: string;

@OneToMany(() => Commit, (commit) => commit.repo, {
cascade: ['insert', 'update'],
})
Expand Down
14 changes: 9 additions & 5 deletions api/src/entities/user.entity.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import { Entity, Column, PrimaryColumn, ManyToMany, JoinTable } from 'typeorm';
import {
Entity,
Column,
PrimaryColumn,
ManyToMany,
JoinTable,
BaseEntity,
} from 'typeorm';
import { Repo } from './repo.entity';

@Entity()
export class User {
export class User extends BaseEntity {
@PrimaryColumn()
id: string;

Expand Down Expand Up @@ -48,9 +55,6 @@ export class User {
})
gitProviderName: string | null;

@Column({ default: true })
isActive: boolean;

@ManyToMany(() => Repo, (repo) => repo.users, {
cascade: ['insert', 'update'],
})
Expand Down
Loading