Skip to content

Commit 05fcfdd

Browse files
authored
Merge pull request #7 from wang-bam-bbang/docs
Docs
2 parents 1e02abc + 3bf03af commit 05fcfdd

File tree

6 files changed

+61
-2
lines changed

6 files changed

+61
-2
lines changed

package-lock.json

Lines changed: 23 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"axios": "^1.7.7",
3535
"class-transformer": "^0.5.1",
3636
"class-validator": "^0.14.1",
37+
"cookie-parser": "^1.4.7",
3738
"express-basic-auth": "^1.2.1",
3839
"passport": "^0.7.0",
3940
"passport-http-bearer": "^1.0.1",

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 } =

src/user/user.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export class UserService {
1818
) {}
1919

2020
async getIdpLoginUrl(): Promise<string> {
21-
const idpWebUrl = 'https://idp.gistory.me';
21+
const idpWebUrl = this.configService.get<string>('IDP_WEB_URL');
2222

2323
const params = new URLSearchParams({
2424
response_type: 'code',

0 commit comments

Comments
 (0)