-
Notifications
You must be signed in to change notification settings - Fork 7
Oauth
๊ตฌ๊ธ ํด๋ผ์ฐ๋ ํ๋ซํผ์ ๋ค์ด๊ฐ์ ํ๋ก์ ํธ ๋ฑ๋ก ํ OAuth key์ id๋ฅผ ๋ฐ๊ธ๋ฐ๋๋ค. - ์ด ๋ ๊ฐ๋ฐ์ฉ์ผ๋ก ์ฌ์ฉํ๊ธฐ์ url๊ณผ ๋ฆฌ๋ค์ด๋ ์ url์ ๋ก์ปฌํธ์คํธ๋ฅผ ์ ์ฉํจ.
๋ฑ๋ก๋ฐ์ key์ id๋ฅผ ํ๊ฒฝ๋ณ์๋ ๋ณ๋์ ํ์ผ๋ก ๋ถ๋ฆฌํ์ฌ ์ ์ฅ.
passport-google-oauth20 ๋ชจ๋ ์ค์น ํ initialize๋ฅผ ์ํ strategy ์ค์ .
import passport from "passport";
import { Strategy } from "passport-google-oauth20";
import loadConfig from "../config/configLoader";
import { createHost, findHostById } from "../../DB/queries/host";
const GoogleStrategy = Strategy;
function extractProfile(profile) {
let imageUrl = "";
if (profile.photos && profile.photos.length) {
imageUrl = profile.photos[0].value;
}
return {
id: profile.id,
displayName: profile.displayName,
image: imageUrl,
email: profile.emails[0].value,
};
}
export default (function() {
const { oAuthArgs } = loadConfig();
passport.use(
new GoogleStrategy(
{ ...oAuthArgs },
async (accessToken, refreshToken, profile, cb) => {
try {
const { id, displayName, image, email } = extractProfile(
profile
);
let host = await findHostById(id);
if (!host) host = await createHost(id, displayName, email);
return cb(null, host);
} catch (error) {
console.error(error);
}
}
)
);
})();- ๋ฐ๋ก Token์ ์์ฑํ๊ธฐ ๋๋ฌธ์ accessToken๊ณผ refreshToken์ ์ฌ์ฉํ์ง ์์, profile์ ๊ตฌ๊ธ ์ธ์ฆ ์ฑ๊ณต ํ ๊ฐ์ ธ์ฌ ์ ์๋ ์ฌ์ฉ์ ํ๋กํ์ ํด๋น.
- profile์์ ๋ฐํํ๋ id ๊ฐ์ผ๋ก DB์ ์ด๋ฏธ ๋ฑ๋ก๋์ด์๋์ง ์ฒดํฌ ํ ๋ฑ๋ก๋์ด์์ง ์์ผ๋ฉด, ์ ์ ์์ฑ.
- session์ ์ฌ์ฉํ์ง ์๊ธฐ ๋๋ฌธ์ serialize์ deserialize๋ ์ ์ธํ์ง ์์.
๊ตฌ๊ธ ๋ก๊ทธ์ธ์ ์ ์ ํ ์ ์๋ endpoint๋ฅผ ์ ์ํ๊ณ routing
import express from "express";
import passport from "passport";
import { generateAccessToken } from "../authentication/token";
const router = express.Router();
router.get(
"/login",
passport.authenticate("google", {
session: false,
scope: ["email", "profile"],
prompt: "select_account",
})
);
router.get("/logout", function(req, res, next) {
req.logOut();
res.redirect("/");
});
router.get(
"/google/callback",
passport.authenticate("google", {
session: false,
}),
(req, res) => {
const accessToken = generateAccessToken(req.user.oauthId);
res.cookie("vaagle", accessToken);
res.redirect("http://localhost:3002/");
}
);
module.exports = router;- req.user๋ก ์์ ์ ์ํ google strategy์์์ cb return ๊ฐ์ ์ฝ์ด์ฌ ์ ์์.
- passport.authenticate์ session ์ต์ ์ ๋ช ์์ ์ผ๋ก false๋ก ์ค.
- promt option์ ๋ก๊ทธ์ธ ๋ฆฌ๋ค์ด๋ ์ ์ ์๋๋ก๊ทธ์ธ์ด ์๋ ๋ก๊ทธ์ธ ๊ณ์ ์ ์ ํํ ์ ์๋ ์ฐฝ์ ๋ถ๋ฌ์ค๋๋ก ์ค์ .
๊ตฌ๊ธ ๋ก๊ทธ์ธ์ ํตํด ์ฌ์ฉ์๊ฐ ์ธ์ฆ๋๋ฉด JWT๋ฅผ ์์ฑ ํ ํด๋ผ์ด์ธํธ ์ฟ ํค์ ๋ฑ๋ก์ํจ๋ค. ์ด ํ ํ ํฐ์ ๋ฐ๊ธ๋ฐ์ ํด๋ผ์ด์ธํธ๋ค์ passport jwt ์ ๋ต์ ํตํด ํ ํฐ ๊ฐ์ ๋ณตํธ์ํค๊ณ ์ ์ ๊ฐ ์๋์ง ํ์ธํ๋ ๋ฐฉ์์ผ๋ก ์ธ์ฆ์ ์ํํ๋ค.
import passport from "passport";
import passportJwt from "passport-jwt";
import loadConfig from "../config/configLoader";
import { findHostById } from "../../DB/queries/host";
export default (function() {
const { tokenArgs } = loadConfig();
const jwtOptions = {
jwtFromRequest: passportJwt.ExtractJwt.fromAuthHeaderAsBearerToken(),
secretOrKey: tokenArgs.secret,
issuer: tokenArgs.issuer,
audience: tokenArgs.audience,
};
passport.use(
new passportJwt.Strategy(jwtOptions, async (payload, cb) => {
try {
const host = findHostById(payload.sub);
if (host) {
return cb(null, host, payload);
}
return cb();
} catch (error) {}
})
);
})();- jwtFromRequest์ ํด๋ผ์ด์ธํธ ํค๋์ ๋ด๊ธด jwt ํ ํฐ์ ์๋ฏธ.
- payload๋ ํ ํฐ์ JWT payload ๋ถ๋ถ์ ํด๋น.
๋ง์ฝ RefreshToken์ ์ฌ์ฉํ๋ค๋ฉด accessToken์ ํด๋ผ์ด์ธํธ inMemory์ ์ ์ฅํ๊ณ refreshToken์ cookie์ ์ ์ฅํ์ฌ ์ฌ์ฉํ๋๋ก ํ ์์ .

๊ธฐํ
์ค๊ณ
๊ฐ๋ฐ ๋ฐ ๋ฐฐํฌ ํ๊ฒฝ ์ค์
๋ฐํ ์๋ฃ
๋ฐ์ผ๋ฆฌ ์คํฌ๋ผ
- 2019.11.05
- 2019.11.06
- 2019.11.07
- 2019.11.08
- 2019.11.11
- 2019.11.12
- 2019.11.13
- 2019.11.14
- 2019.11.15
- 2019.11.18
- 2019.11.19
- 2019.11.20
- 2019.11.21
- 2019.11.22
- 2019.11.25
- 2019.11.26
- 2019.11.27
- 2019.11.28
- 2019.12.03
- 2019.12.04
- 2019.12.09
- 2019.12.10
- 2019.12.11
- 2019.12.12
- 2019.12.13
- 2019.12.16
- 2019.12.18
- 2019.12.19
- 2019.12.20