-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(api): ✨ allow non-admin users to access and modify their own cal…
…endars
- Loading branch information
Showing
5 changed files
with
144 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,45 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { ConfigService } from '@nestjs/config'; | ||
import * as bcrypt from 'bcrypt'; | ||
import { isValidObjectId } from 'mongoose'; | ||
import { Types } from 'mongoose'; | ||
import { Role } from 'src/users/enum/roles.enum'; | ||
import { IUser } from 'src/users/interface/user.interface'; | ||
import { UsersService } from 'src/users/users.service'; | ||
import { isValidObjectId } from 'src/utils'; | ||
|
||
@Injectable() | ||
export class AuthService { | ||
constructor(private usersService: UsersService) {} | ||
constructor( | ||
private usersService: UsersService, | ||
private configService: ConfigService, | ||
) {} | ||
|
||
async validateApiKey(apiKey: string): Promise<IUser | null> { | ||
if (apiKey === this.configService.get('ADMIN_API_KEY')) | ||
return { | ||
_id: new Types.ObjectId(0), | ||
role: Role.Admin, | ||
username: '', | ||
password_hash: '', | ||
}; | ||
|
||
async validateApiKey(apiKey: string): Promise<any> { | ||
const userId = apiKey.split(':')[0]; | ||
|
||
if (!isValidObjectId(userId)) return null; | ||
const user = await this.usersService.getUser(userId, ''); | ||
try { | ||
const user = await this.usersService.getUser(userId, ''); | ||
|
||
if ( | ||
user && | ||
user.api_key_hash && | ||
(await bcrypt.compare(apiKey, user.api_key_hash)) | ||
) { | ||
return user; | ||
if ( | ||
user && | ||
user.api_key_hash && | ||
(await bcrypt.compare(apiKey, user.api_key_hash)) | ||
) { | ||
return user; | ||
} | ||
} catch (e) { | ||
return null; | ||
} | ||
|
||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { NotFoundException, UnauthorizedException } from '@nestjs/common'; | ||
import { Model } from 'mongoose'; | ||
import { Role } from 'src/users/enum/roles.enum'; | ||
import { IUser } from 'src/users/interface/user.interface'; | ||
import { ICalendar } from './interface/calendar.interface'; | ||
|
||
export function asCalendarAccess(user: IUser, calendar: ICalendar): void { | ||
if ( | ||
user.role != Role.Admin && | ||
calendar.user.toString() != user._id.toString() | ||
) | ||
throw new UnauthorizedException( | ||
'you are not allowed to access this calendar', | ||
); | ||
} | ||
|
||
export async function checkCalendarAccess( | ||
user: IUser, | ||
calendarId: string, | ||
CalendarModel: Model<ICalendar>, | ||
): Promise<void> { | ||
const existingCalendar = await CalendarModel.findById(calendarId, 'user'); | ||
if (!existingCalendar) throw new NotFoundException('Calendar not found'); | ||
|
||
asCalendarAccess(user, existingCalendar); | ||
} |