-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #97 from CartoonIsArt/development
라이브러리 최신화 및 JWT 기반 인증 구현
- Loading branch information
Showing
35 changed files
with
1,425 additions
and
984 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 |
---|---|---|
|
@@ -4,4 +4,5 @@ node_modules | |
.vscode | ||
yarn-error.log | ||
.idea | ||
db | ||
db/ | ||
*.sql |
This file was deleted.
Oops, something went wrong.
Binary file not shown.
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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { Connection, getConnection } from "typeorm" | ||
import * as crypto from "crypto" | ||
import User from "../entities/user" | ||
|
||
export async function Authenticate (username: string, password: string): Promise<User> { | ||
const conn: Connection = getConnection() | ||
const users: User[] = await conn | ||
.getRepository(User) | ||
.find({ | ||
where: { | ||
username, | ||
}, | ||
relations: ['profileImage'] | ||
}) | ||
const user = users[0] | ||
|
||
const [encryptedKey, salt] = user.password.split("@") | ||
const derivedKey = crypto.pbkdf2Sync(password, salt, 131071, 64, 'sha512') | ||
|
||
if (derivedKey.toString('hex') !== encryptedKey) | ||
throw new Error('password mismatch') | ||
return user | ||
} |
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,60 @@ | ||
import { Connection, getConnection } from "typeorm" | ||
import AuthenticationToken from "../entities/authenticationToken" | ||
import { ipToInt } from "../lib/ip2int" | ||
import { cookieExpirationDate } from "../lib/date" | ||
import { Authenticate } from "../auth" | ||
|
||
const jwt = require('jsonwebtoken') | ||
|
||
/* 로그인 */ | ||
export const Login = async (ctx, next) => { | ||
const authenticationToken: AuthenticationToken = new AuthenticationToken() | ||
const conn: Connection = getConnection() | ||
|
||
try { | ||
// 1. User authentication | ||
const { | ||
username, | ||
password, | ||
} = ctx.request.body | ||
|
||
const user = await Authenticate(username, password) | ||
|
||
// 2. Issue access token and refresh token | ||
const accessToken = jwt.sign({ user }, 'secretKey', { expiresIn: '1h' }) | ||
const refreshToken = jwt.sign({ user }, 'secretKey', { expiresIn: '14d' }) | ||
|
||
// 3. Set authentication token cookie | ||
ctx.cookies.set('accessToken', accessToken, { expires: cookieExpirationDate() }) | ||
|
||
// 4. Save refresh token to database | ||
authenticationToken.accessToken = accessToken | ||
authenticationToken.refreshToken = refreshToken | ||
authenticationToken.accessIp = ipToInt(ctx.ip) | ||
|
||
await conn.manager.save(authenticationToken) | ||
} | ||
catch (e){ | ||
ctx.throw(400, e) | ||
} | ||
|
||
/* 로그인 완료 응답 */ | ||
ctx.response.status = 200 | ||
} | ||
|
||
/* 로그아웃 */ | ||
export const Logout = async (ctx, next) => { | ||
try { | ||
const conn: Connection = getConnection() | ||
await conn.manager.delete(AuthenticationToken, ctx.authenticationToken.id) | ||
|
||
ctx.status = 204 | ||
ctx.redirect("/") | ||
} | ||
catch (e) { | ||
ctx.throw(400, e) | ||
} | ||
|
||
/* 로그아웃 완료 응답 */ | ||
ctx.response.status = 204 | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.