feat(api): add optional spotify user whitelist

This commit is contained in:
Julian Tölle 2020-05-03 20:18:57 +02:00
parent a7c5c68540
commit a27fcce03b
3 changed files with 41 additions and 4 deletions

View file

@ -1,21 +1,30 @@
import { Injectable } from "@nestjs/common";
import { Injectable, ForbiddenException } from "@nestjs/common";
import { ConfigService } from "@nestjs/config";
import { JwtService } from "@nestjs/jwt";
import { User } from "../users/user.entity";
import { UsersService } from "../users/users.service";
import { LoginDto } from "./dto/login.dto";
import { JwtService } from "@nestjs/jwt";
@Injectable()
export class AuthService {
private readonly userFilter: null | string;
constructor(
private readonly config: ConfigService,
private readonly usersService: UsersService,
private readonly jwtService: JwtService
) {}
) {
this.userFilter = this.config.get<string>("SPOTIFY_USER_FILTER");
}
async spotifyLogin({
accessToken,
refreshToken,
profile,
}: LoginDto): Promise<User> {
if (!this.allowedByUserFilter(profile.id)) {
throw new ForbiddenException("UserNotWhitelisted");
}
const user = await this.usersService.createOrUpdate({
displayName: profile.displayName,
photo: profile.photos.length > 0 ? profile.photos[0] : null,
@ -44,4 +53,20 @@ export class AuthService {
async findUser(id: string): Promise<User> {
return this.usersService.findById(id);
}
allowedByUserFilter(spotifyID: string) {
if (!this.userFilter) {
return true;
}
const whitelistedIDs = this.userFilter.split(",");
console.log("whitelisted ids", {
whitelistedIDs,
uf: this.userFilter,
spotifyID,
});
return whitelistedIDs.includes(spotifyID);
}
}