Skip to content
This repository has been archived by the owner on Oct 31, 2022. It is now read-only.

Commit

Permalink
Fix: OAuth 토큰 중복발급 제한
Browse files Browse the repository at this point in the history
  • Loading branch information
leehj050211 committed May 19, 2022
1 parent 000d01e commit 8a21e7b
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/api/oauth/oauth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import loginCheck from '@src/util/loginCheck';


router.get('/authentication', loginCheck, async (req: express.Request, res: express.Response, next: express.NextFunction) => {
const user = new User(jwt.verify(req.cookies.token).value);
try {
res.send(JSON.stringify(
await service.authentication(
user,
String(req.query.clientId),
String(req.query.redirectURI)
)
Expand Down
23 changes: 19 additions & 4 deletions src/api/oauth/oauth.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import express from 'express';
import { BadRequestException, InternalServerException, NotFoundException, UnAuthorizedException } from '@src/util/exceptions';
import { BadRequestException, NotFoundException, UnAuthorizedException } from '@src/util/exceptions';
import * as oauthClientReposiroty from '@src/api/oauth/repository/client.repository';
import * as oauthScopeReposiroty from '@src/api/oauth/repository/scope.repository';
import * as oauthScopeInfoReposiroty from '@src/api/oauth/repository/scopeInfo.repository';
Expand All @@ -25,6 +25,7 @@ const getScopeInfoList = async () => {
getScopeInfoList();

const authentication = async (
user: User,
clientId: string,
redirectURI: string
) => {
Expand All @@ -41,8 +42,14 @@ const authentication = async (
if (scopeInfo === null) {
throw new NotFoundException('Failed to load scope info');
}
if (await oauthTokenReposiroty.getByUsercodeAndClientId(user.getUser().code, clientId)) {
return {
authorized: true
}
}

return {
authorized: false,
domain,
serviceName,
scope: scopeInfoList.filter(e => scopeInfo.some(scope => scope.info == e.info))
Expand Down Expand Up @@ -91,6 +98,14 @@ const getToken = async (
}

await oauthAuthcodeReposiroty.expireCode(authcode);

const authorizedInfo = await oauthTokenReposiroty.getByUsercodeAndClientId(authcodeInfo.usercode, clientId);
if (authorizedInfo !== null) {
return {
token: authorizedInfo.token
}
}

const newToken = crypto.randomBytes(16).toString('hex');
await oauthTokenReposiroty.createToken(newToken, clientId, authcodeInfo.usercode);

Expand Down Expand Up @@ -171,15 +186,15 @@ const createClient = async (
try {
scopeList = (typeof scope == 'string')? JSON.parse(scope): scope;
if (typeof scopeList != 'object' || !scopeList.length) {
throw new BadRequestException('Scope is invalid1');
throw new BadRequestException('Scope is invalid');
}
} catch (err) {
throw new BadRequestException('Scope is invalid2');
throw new BadRequestException('Scope is invalid');
}

const scopeListCheck = scopeList.filter((e: string) => scopeInfoList.some(scopeInfo => e == scopeInfo.info));
if (scopeListCheck.length != scopeList.length) {
throw new BadRequestException('Scope is invalid3');
throw new BadRequestException('Scope is invalid');
}

const newClientId = crypto.randomBytes(4).toString('hex');
Expand Down
26 changes: 26 additions & 0 deletions src/api/oauth/repository/token.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,31 @@ const getByToken = async (
}
}

const getByUsercodeAndClientId = async (
usercode: number,
clientId: string
): Promise<{
token: string
} | null> => {
const getQuery='SELECT token FROM oauth_token WHERE usercode=? AND client_id=?';
// SELECT
// token
// FROM oauth_token
// WHERE
// usercode=? AND
// client_id=?
try {
const [rows] = await pool.query(getQuery, [usercode, clientId]);
if (rows.length)
return rows[0];
else
return null;
} catch(err) {
console.error(err);
throw new InternalServerException();
}
}

const createToken = async (
token: string,
clientId: string,
Expand Down Expand Up @@ -62,6 +87,7 @@ const expireCode = async (

export {
getByToken,
getByUsercodeAndClientId,
createToken,
expireCode
}

0 comments on commit 8a21e7b

Please sign in to comment.