-
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