Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adds private api adds AuthTokenDto and AuthTokenWithSecretDto adds necessary methods in the users service adds RandomnessError Signed-off-by: Philip Molares <philip.molares@udo.edu>
- Loading branch information
Showing
10 changed files
with
236 additions
and
12 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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/* | ||
* SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file) | ||
* | ||
* SPDX-License-Identifier: AGPL-3.0-only | ||
*/ | ||
|
||
import { Module } from '@nestjs/common'; | ||
import { UsersModule } from '../../users/users.module'; | ||
import { TokensController } from './tokens/tokens.controller'; | ||
|
||
@Module({ | ||
imports: [UsersModule], | ||
controllers: [TokensController], | ||
}) | ||
export class PrivateApiModule {} |
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,32 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { TokensController } from './tokens.controller'; | ||
import { LoggerModule } from '../../../logger/logger.module'; | ||
import { UsersModule } from '../../../users/users.module'; | ||
import { getRepositoryToken } from '@nestjs/typeorm'; | ||
import { Identity } from '../../../users/identity.entity'; | ||
import { User } from '../../../users/user.entity'; | ||
import { AuthToken } from '../../../users/auth-token.entity'; | ||
|
||
describe('TokensController', () => { | ||
let controller: TokensController; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
controllers: [TokensController], | ||
imports: [LoggerModule, UsersModule], | ||
}) | ||
.overrideProvider(getRepositoryToken(User)) | ||
.useValue({}) | ||
.overrideProvider(getRepositoryToken(AuthToken)) | ||
.useValue({}) | ||
.overrideProvider(getRepositoryToken(Identity)) | ||
.useValue({}) | ||
.compile(); | ||
|
||
controller = module.get<TokensController>(TokensController); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(controller).toBeDefined(); | ||
}); | ||
}); |
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,48 @@ | ||
import { | ||
Body, | ||
Controller, | ||
Delete, | ||
Get, | ||
HttpCode, | ||
Param, | ||
Post, | ||
} from '@nestjs/common'; | ||
import { ConsoleLoggerService } from '../../../logger/console-logger.service'; | ||
import { UsersService } from '../../../users/users.service'; | ||
import { AuthTokenDto } from '../../../users/auth-token.dto'; | ||
import { AuthTokenWithSecretDto } from '../../../users/auth-token-with-secret.dto'; | ||
|
||
@Controller('tokens') | ||
export class TokensController { | ||
constructor( | ||
private readonly logger: ConsoleLoggerService, | ||
private usersService: UsersService, | ||
) { | ||
this.logger.setContext(TokensController.name); | ||
} | ||
|
||
@Get() | ||
async getUserTokens(): Promise<AuthTokenDto[]> { | ||
// ToDo: Get real userName | ||
return (await this.usersService.getTokensByUsername('molly')).map((token) => | ||
this.usersService.toAuthTokenDto(token), | ||
); | ||
} | ||
|
||
@Post() | ||
async postToken(@Body() label: string): Promise<AuthTokenWithSecretDto> { | ||
// ToDo: Get real userName | ||
const authToken = await this.usersService.createTokenForUser( | ||
'hardcoded', | ||
label, | ||
); | ||
return this.usersService.toAuthTokenWithSecretDto(authToken); | ||
} | ||
|
||
@Delete('/:timestamp') | ||
@HttpCode(204) | ||
async deleteToken(@Param('timestamp') timestamp: number) { | ||
// ToDo: Get real userName | ||
return this.usersService.removeToken('hardcoded', timestamp); | ||
} | ||
} |
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,13 @@ | ||
/* | ||
* SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file) | ||
* | ||
* SPDX-License-Identifier: AGPL-3.0-only | ||
*/ | ||
|
||
import { IsString } from 'class-validator'; | ||
import { AuthTokenDto } from './auth-token.dto'; | ||
|
||
export class AuthTokenWithSecretDto extends AuthTokenDto { | ||
@IsString() | ||
secret: string; | ||
} |
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,14 @@ | ||
/* | ||
* SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file) | ||
* | ||
* SPDX-License-Identifier: AGPL-3.0-only | ||
*/ | ||
|
||
import { IsNumber, IsString } from 'class-validator'; | ||
|
||
export class AuthTokenDto { | ||
@IsString() | ||
label: string; | ||
@IsNumber() | ||
created: number; | ||
} |
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