Skip to content

Commit

Permalink
Merge pull request #393 from aXenDeveloper/improve_auth
Browse files Browse the repository at this point in the history
refactor(backend): Authorization middleware for AdminCP
  • Loading branch information
aXenDeveloper authored Jul 1, 2024
2 parents 5504f95 + 5a1b4b4 commit dec47e4
Show file tree
Hide file tree
Showing 26 changed files with 274 additions and 123 deletions.
File renamed without changes.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
<br>
<a href="https://vitnode.com/" target="_blank">
<picture>
<source media="(prefers-color-scheme: dark)" srcset="apps/docs/public/assets/vitnode_logo_dark.svg">
<source media="(prefers-color-scheme: light)" srcset="apps/docs/public/assets/vitnode_logo_light.svg">
<img alt="VitNode Logo" src="apps/docs/public/assets/vitnode_logo_light.svg" width="400">
<source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/aXenDeveloper/vitnode/canary/apps/docs/assets/logo/vitnode_logo_dark.svg">
<source media="(prefers-color-scheme: light)" srcset="https://raw.githubusercontent.com/aXenDeveloper/vitnode/canary/apps/docs/assets/logo/vitnode_logo_light.svg">
<img alt="VitNode Logo" src="https://raw.githubusercontent.com/aXenDeveloper/vitnode/canary/apps/docs/assets/logo/vitnode_logo_light.svg" width="400">
</picture>
</a>
<br>
Expand Down
4 changes: 2 additions & 2 deletions apps/backend/schema.gql
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,9 @@ type Mutation {
core_members__avatar__delete: String!
core_members__avatar__upload(file: Upload!): UploadAvatarCoreMembersObj!
core_members__delete(id: Int!): String!
core_members__sign_up(email: String!, name: String!, newsletter: Boolean, password: String!): SignUpCoreMembersObj!
core_sessions__sign_in(admin: Boolean, email: String!, password: String!, remember: Boolean): String!
core_sessions__sign_out: String!
core_sessions__sign_up(email: String!, name: String!, newsletter: Boolean, password: String!): SignUpCoreSessionsObj!
}

type NavAdminPluginsAuthorization {
Expand Down Expand Up @@ -610,7 +610,7 @@ type ShowSettingsObj {
site_short_name: String!
}

type SignUpCoreMembersObj {
type SignUpCoreSessionsObj {
email: String!
group_id: Int!
name: String!
Expand Down
16 changes: 16 additions & 0 deletions apps/docs/assets/logo/vitnode_logo_dark.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 16 additions & 0 deletions apps/docs/assets/logo/vitnode_logo_light.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 1 addition & 3 deletions apps/frontend/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { NextRequest } from 'next/server';
import createIntlMiddleware from 'next-intl/middleware';
import { createMiddleware } from 'vitnode-frontend/middleware';

export default async function middleware(request: NextRequest) {
const handleI18nRouting = createIntlMiddleware(await createMiddleware());
const response = handleI18nRouting(request);
const response = await createMiddleware(request);

return response;
}
Expand Down
13 changes: 13 additions & 0 deletions packages/backend/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
<p align="center">
<br>
<a href="https://vitnode.com/" target="_blank">
<picture>
<source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/aXenDeveloper/vitnode/canary/apps/docs/assets/logo/vitnode_logo_dark.svg">
<source media="(prefers-color-scheme: light)" srcset="https://raw.githubusercontent.com/aXenDeveloper/vitnode/canary/apps/docs/assets/logo/vitnode_logo_light.svg">
<img alt="VitNode Logo" src="https://raw.githubusercontent.com/aXenDeveloper/vitnode/canary/apps/docs/assets/logo/vitnode_logo_light.svg" width="400">
</picture>
</a>
<br>
<br>
</p>

# (Vitnode) Backend Package

This package is used to create wrapper for a backend server in NestJS for the VitNode app.
Expand Down
4 changes: 0 additions & 4 deletions packages/backend/src/core/members/members.module.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { Module } from '@nestjs/common';

import { SignUpCoreMembersResolver } from './sign_up/sign_up.resolver';
import { SignUpCoreMembersService } from './sign_up/sign_up.service';
import { ShowCoreMembersService } from './show/show.service';
import { ShowCoreMembersResolver } from './show/show.resolver';
import { AvatarCoreMembers } from './avatar/avatar-core_members.module';
Expand All @@ -12,8 +10,6 @@ import { DeleteCoreMembersService } from './delete/delete.service';
providers: [
ShowCoreMembersService,
ShowCoreMembersResolver,
SignUpCoreMembersResolver,
SignUpCoreMembersService,
DeleteCoreMembersResolver,
DeleteCoreMembersService,
],
Expand Down

This file was deleted.

20 changes: 0 additions & 20 deletions packages/backend/src/core/members/sign_up/sign_up.resolver.ts

This file was deleted.

4 changes: 4 additions & 0 deletions packages/backend/src/core/sessions/sessions.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import { InternalAuthorizationCoreSessionsService } from './authorization/intern
import { CoreSessionsCron } from './sessions.cron';
import { DeviceSignInCoreSessionsService } from './sign_in/device.service';
import { DevicesCoreSessionsModule } from './devices/devices.module';
import { SignUpCoreSessionsResolver } from './sign_up/sign_up.resolver';
import { SignUpCoreSessionsService } from './sign_up/sign_up.service';

@Module({
providers: [
Expand All @@ -20,6 +22,8 @@ import { DevicesCoreSessionsModule } from './devices/devices.module';
SignOutCoreSessionsService,
SignOutCoreSessionsResolver,
CoreSessionsCron,
SignUpCoreSessionsResolver,
SignUpCoreSessionsService,
],
imports: [DevicesCoreSessionsModule],
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { TransformString } from '../../../../utils';
export const nameRegex = /^(?!.* {2})[\p{L}\p{N}._@ -]*$/u;

@ArgsType()
export class SignUpCoreMembersArgs {
export class SignUpCoreSessionsArgs {
@Transform(TransformString)
@Field(() => String)
@IsEmail()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { IsEmail, Matches, MaxLength, MinLength } from 'class-validator';
import { nameRegex } from './sign_up.args';

@ObjectType()
export class SignUpCoreMembersObj {
export class SignUpCoreSessionsObj {
@Field(() => String)
@IsEmail()
email: string;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { Injectable } from '@nestjs/common';
import { convertColor } from 'vitnode-shared';

@Injectable()
export class AvatarColorService {
private readonly hRange = [0, 360];
private readonly sRange = [50, 75];
private readonly lRange = [25, 60];

private readonly getHashOfString = (str: string) => {
let hash = 0;
for (let i = 0; i < str.length; i++) {
hash = str.charCodeAt(i) + ((hash << 5) - hash);
}
hash = Math.abs(hash);

return hash;
};

private readonly normalizeHash = (hash: number, min: number, max: number) => {
return Math.floor((hash % (max - min)) + min);
};

private readonly generateHSLFromName = (name: string) => {
const hash = this.getHashOfString(name);
const h = this.normalizeHash(hash, this.hRange[0], this.hRange[1]);
const s = this.normalizeHash(hash, this.sRange[0], this.sRange[1]);
const l = this.normalizeHash(hash, this.lRange[0], this.lRange[1]);

return [h, s, l];
};

generateAvatarColor = (name: string) => {
const hslName = this.generateHSLFromName(name);

return convertColor.hslToRgb(hslName[0], hslName[1], hslName[2]);
};
}
20 changes: 20 additions & 0 deletions packages/backend/src/core/sessions/sign_up/sign_up.resolver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Args, Context, Mutation, Resolver } from '@nestjs/graphql';

import { SignUpCoreSessionsObj } from './dto/sign_up.obj';
import { SignUpCoreSessionsService } from './sign_up.service';
import { SignUpCoreSessionsArgs } from './dto/sign_up.args';

import { Ctx } from '../../../utils';

@Resolver()
export class SignUpCoreSessionsResolver {
constructor(private readonly service: SignUpCoreSessionsService) {}

@Mutation(() => SignUpCoreSessionsObj)
async core_sessions__sign_up(
@Args() args: SignUpCoreSessionsArgs,
@Context() ctx: Ctx,
): Promise<SignUpCoreSessionsObj> {
return this.service.signUp(args, ctx);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { count } from 'drizzle-orm';
import { ConfigService } from '@nestjs/config';
import { removeSpecialCharacters } from 'vitnode-shared';

import { SignUpCoreMembersArgs } from './dto/sign_up.args';
import { SignUpCoreMembersObj } from './dto/sign_up.obj';
import { generateAvatarColor } from './functions/generate-avatar-color';
import { SignUpCoreSessionsArgs } from './dto/sign_up.args';
import { SignUpCoreSessionsObj } from './dto/sign_up.obj';
import { AvatarColorService } from './helpers/avatar-color.service';

import { DatabaseService } from '../../../database';
import { core_users } from '../../../templates/core/admin/database/schema/users';
Expand All @@ -15,13 +15,15 @@ import { CustomError } from '../../../errors';
import { getUserIp } from '../../../functions';

@Injectable()
export class SignUpCoreMembersService {
export class SignUpCoreSessionsService extends AvatarColorService {
constructor(
private readonly databaseService: DatabaseService,
private readonly configService: ConfigService,
) {}
) {
super();
}

protected getGroupId = async (): Promise<number> => {
private readonly getGroupId = async (): Promise<number> => {
const countUsers = await this.databaseService.db
.select({ count: count() })
.from(core_users);
Expand All @@ -47,11 +49,10 @@ export class SignUpCoreMembersService {
};

async signUp(
{ email: emailRaw, name, newsletter, password }: SignUpCoreMembersArgs,
{ email: emailRaw, name, newsletter, password }: SignUpCoreSessionsArgs,
{ req }: Ctx,
): Promise<SignUpCoreMembersObj> {
): Promise<SignUpCoreSessionsObj> {
const email = emailRaw.toLowerCase();

const checkEmail = await this.databaseService.db.query.core_users.findFirst(
{
where: (table, { eq }) => eq(table.email, email),
Expand Down Expand Up @@ -91,7 +92,7 @@ export class SignUpCoreMembersService {
name_seo: convertToNameSEO,
newsletter,
password: hashPassword,
avatar_color: generateAvatarColor(name),
avatar_color: this.generateAvatarColor(name),
group_id: await this.getGroupId(),
ip_address: getUserIp(req),
})
Expand Down
13 changes: 13 additions & 0 deletions packages/create-vitnode-app/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
<p align="center">
<br>
<a href="https://vitnode.com/" target="_blank">
<picture>
<source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/aXenDeveloper/vitnode/canary/apps/docs/assets/logo/vitnode_logo_dark.svg">
<source media="(prefers-color-scheme: light)" srcset="https://raw.githubusercontent.com/aXenDeveloper/vitnode/canary/apps/docs/assets/logo/vitnode_logo_light.svg">
<img alt="VitNode Logo" src="https://raw.githubusercontent.com/aXenDeveloper/vitnode/canary/apps/docs/assets/logo/vitnode_logo_light.svg" width="400">
</picture>
</a>
<br>
<br>
</p>

# (VitNode) Create App

This package is a CLI tool to create a new VitNode app quickly.
Expand Down
13 changes: 13 additions & 0 deletions packages/eslint-config-vitnode/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
<p align="center">
<br>
<a href="https://vitnode.com/" target="_blank">
<picture>
<source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/aXenDeveloper/vitnode/canary/apps/docs/assets/logo/vitnode_logo_dark.svg">
<source media="(prefers-color-scheme: light)" srcset="https://raw.githubusercontent.com/aXenDeveloper/vitnode/canary/apps/docs/assets/logo/vitnode_logo_light.svg">
<img alt="VitNode Logo" src="https://raw.githubusercontent.com/aXenDeveloper/vitnode/canary/apps/docs/assets/logo/vitnode_logo_light.svg" width="400">
</picture>
</a>
<br>
<br>
</p>

# (VitNode) ESLint Config

Package providing ESLint & TSConfig configuration for VitNode projects.
Expand Down
Loading

0 comments on commit dec47e4

Please sign in to comment.