Skip to content

Commit 3bf03af

Browse files
committed
docs: enable refresh feature in swagger
1 parent 4f3efb0 commit 3bf03af

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

src/idp/idp.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ export class IdpService {
9191
const tokenResponse = await firstValueFrom(
9292
this.httpService
9393
.post<IdpJwtResponse>(
94-
this.idpUrl + '/token',
94+
this.idpUrl + '/oauth/token',
9595
{
9696
grant_type: 'refresh_token',
9797
refresh_token: refreshToken,

src/main.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { ValidationPipe } from '@nestjs/common';
44

55
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
66
import expressBasicAuth from 'express-basic-auth';
7+
import cookieParser from 'cookie-parser';
78

89
async function bootstrap() {
910
const app = await NestFactory.create(AppModule);
@@ -17,6 +18,7 @@ async function bootstrap() {
1718
},
1819
}),
1920
);
21+
app.use(cookieParser());
2022

2123
app.useGlobalPipes(
2224
new ValidationPipe({
@@ -31,6 +33,20 @@ async function bootstrap() {
3133
)
3234
.setVersion('1.0')
3335
.addTag('Fint-it')
36+
.addOAuth2({
37+
type: 'oauth2',
38+
flows: {
39+
authorizationCode: {
40+
authorizationUrl: `${process.env.IDP_WEB_URL}/authorize?prompt=consent`,
41+
tokenUrl: `${process.env.IDP_URL}/oauth/token`,
42+
scopes: {
43+
openid: '',
44+
profile: '',
45+
offline_access: '',
46+
},
47+
},
48+
},
49+
})
3450
.addBearerAuth(
3551
{
3652
type: 'http',
@@ -40,13 +56,21 @@ async function bootstrap() {
4056
},
4157
'access-token',
4258
)
59+
.addCookieAuth('refresh_token')
4360
.build();
4461

4562
const document = SwaggerModule.createDocument(app, config);
4663

4764
SwaggerModule.setup('api/docs', app, document, {
4865
swaggerOptions: {
66+
oauth2RedirectUrl: `${process.env.IDP_CALLBACK_URL}`,
67+
persistAuthorization: true,
4968
displayRequestDuration: true,
69+
initOAuth: {
70+
clientId: process.env.IDP_CLIENT_ID,
71+
clientSecret: process.env.IDP_CLIENT_SECRET,
72+
usePkceWithAuthorizationCodeGrant: true,
73+
},
5074
},
5175
});
5276

src/user/user.controller.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import { LoginCallbackDto } from './dto/req/callBack.dto';
1919
import { JwtTokenResDto } from './dto/res/jwtTokenRes.dto';
2020
import {
2121
ApiBearerAuth,
22+
ApiCookieAuth,
23+
ApiCreatedResponse,
2224
ApiInternalServerErrorResponse,
2325
ApiOkResponse,
2426
ApiOperation,
@@ -70,12 +72,21 @@ export class UserController {
7072
return { access_token };
7173
}
7274

75+
@ApiOperation({
76+
summary: 'Refresh token',
77+
description: 'Refresh the access token from idp',
78+
})
79+
@ApiCookieAuth('refresh_token')
80+
@ApiCreatedResponse({ type: JwtTokenResDto, description: 'Return jwt token' })
81+
@ApiUnauthorizedResponse({ description: 'Unauthorized' })
82+
@ApiInternalServerErrorResponse({ description: 'Internal Server Error' })
7383
@Post('refresh')
7484
async refreshToken(
7585
@Req() req: Request,
7686
@Res({ passthrough: true }) res: Response,
7787
): Promise<JwtTokenResDto> {
7888
const refreshToken = req.cookies['refresh_token'];
89+
7990
if (!refreshToken) throw new UnauthorizedException();
8091

8192
const { access_token, refresh_token } =

0 commit comments

Comments
 (0)