This repository has been archived by the owner on Jun 10, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
63315b2
commit 3157929
Showing
13 changed files
with
258 additions
and
183 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
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,9 +1,10 @@ | ||
import { HttpModule, Module } from '@nestjs/common'; | ||
import { AstraService } from './astra.service'; | ||
import { KeyspaceService } from './keyspace.service'; | ||
|
||
@Module({ | ||
imports: [HttpModule], | ||
providers: [KeyspaceService], | ||
exports: [KeyspaceService], | ||
providers: [KeyspaceService, AstraService], | ||
exports: [KeyspaceService, AstraService], | ||
}) | ||
export class AstraApiModule {} |
This file was deleted.
Oops, something went wrong.
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 was deleted.
Oops, something went wrong.
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
12 changes: 6 additions & 6 deletions
12
src/auth/auth.controller.spec.ts → src/auth/tokens.controller.spec.ts
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,50 @@ | ||
import { | ||
Body, | ||
Controller, | ||
Delete, | ||
Get, | ||
HttpCode, | ||
Param, | ||
Post, | ||
UseGuards, | ||
} from '@nestjs/common'; | ||
import { ApiSecurity, ApiTags } from '@nestjs/swagger'; | ||
import { TokensService } from './tokens.service'; | ||
import { AuthDTO } from './dto/auth.dto'; | ||
import { TokenGuard } from './token.strategy'; | ||
|
||
@ApiTags('Tokens') | ||
@Controller('auth/tokens') | ||
export class TokensController { | ||
constructor(private readonly tokensService: TokensService) {} | ||
|
||
@Get(':keyspace') | ||
@UseGuards(TokenGuard) | ||
@ApiSecurity('token') | ||
getTokens(@Param('keyspace') keyspace: string) { | ||
return this.tokensService.findAll(keyspace); | ||
} | ||
|
||
@Post() | ||
@UseGuards(TokenGuard) | ||
@ApiSecurity('token') | ||
register(@Body() body: AuthDTO) { | ||
return this.tokensService.register(body); | ||
} | ||
|
||
@Post('validate') | ||
@UseGuards(TokenGuard) | ||
@ApiSecurity('token') | ||
@HttpCode(200) | ||
validateClient(@Body() body) { | ||
return this.tokensService.validateClient(body); | ||
} | ||
|
||
@Delete(':token') | ||
@UseGuards(TokenGuard) | ||
@ApiSecurity('token') | ||
@HttpCode(204) | ||
deleteClient(@Param('token') token: string) { | ||
return this.tokensService.removeClient(token); | ||
} | ||
} |
8 changes: 4 additions & 4 deletions
8
src/auth/auth.service.spec.ts → src/auth/tokens.service.spec.ts
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,79 @@ | ||
import { HttpException, HttpStatus, Injectable } from '@nestjs/common'; | ||
import { TokenPayload } from './interfaces/token-payload.interface'; | ||
import { JwtService } from '@nestjs/jwt'; | ||
import { v4 as uuidv4 } from 'uuid'; | ||
import { AuthDTO } from './dto/auth.dto'; | ||
import { AstraService } from '../astra/astra.service'; | ||
import { catchError } from 'rxjs/operators'; | ||
import { from } from 'rxjs'; | ||
|
||
@Injectable() | ||
export class TokensService { | ||
constructor( | ||
private readonly jwtService: JwtService, | ||
private readonly astraService: AstraService, | ||
) {} | ||
|
||
public async findAll(keyspace: string) { | ||
return this.astraService | ||
.find<AuthDTO>(keyspace, 'tokens') | ||
.pipe(catchError(() => from([{}]))); | ||
} | ||
|
||
public register(body: AuthDTO) { | ||
const clientId = uuidv4(); | ||
const { serverId, scopes } = body; | ||
|
||
const payload: TokenPayload = { | ||
clientId, | ||
keyspace: serverId, | ||
scopes, | ||
}; | ||
|
||
//TODO token-expiry | ||
const signedToken = this.jwtService.sign(payload, { expiresIn: '1y' }); | ||
const decoded: any = this.jwtService.decode(signedToken); | ||
const expiresIn: number = decoded.exp - Math.round(Date.now() / 1000); | ||
const response = { ...payload, accessToken: signedToken, expiresIn }; | ||
|
||
this.astraService.create(response, serverId, 'tokens'); | ||
|
||
return response; | ||
} | ||
|
||
public async validateClient(payload: TokenPayload): Promise<boolean> { | ||
const { keyspace, clientId } = payload; | ||
|
||
try { | ||
const token = await this.astraService.findOne( | ||
{ clientId: { $eq: clientId } }, | ||
keyspace, | ||
'tokens', | ||
); | ||
if (token) { | ||
return true; | ||
} | ||
} catch (e) { | ||
return false; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
public async removeClient(token: string) { | ||
if (!token) | ||
throw new HttpException('Please provide token', HttpStatus.BAD_REQUEST); | ||
|
||
const { clientId, keyspace } = this.jwtService.decode( | ||
token, | ||
) as TokenPayload; | ||
|
||
try { | ||
await this.astraService.delete(clientId, keyspace, 'tokens'); | ||
} catch (e) { | ||
throw new HttpException('Invalid client id', HttpStatus.BAD_REQUEST); | ||
} | ||
|
||
return; | ||
} | ||
} |
Oops, something went wrong.