-
2023 - 08 - 17 Passport 로그인, Oauth의 개념과 특징. (소셜 로그인 관련)Today I Learned/TIL 08 2023. 8. 17. 21:40
OAuth.20
OAuth 2.0이란 로그인하지 않고도 제 삼자에게 서비스를 제공할 수 있도록 하는 표준 사용자 인증 프로토콜이다. 즉, 구글이나 페이스북과 같은 서비스 계정으로 제 3의 서비스에 로그인하여 등록되어 있는 정보나 해당 사이트들의 기능을 사용할 권한을 부여받는 표준 프로토콜이다. OAuth 2.0은 HTTPS 환경에서만 작동한다.
termdescriptionAuthentication 신원확인 Authorization 권한 부여 Client google, facebook 등의 아이디로 로그인이 가능한 제 3자의 서비스 Resource Owner google, facebook 등의 아이디로 로그인하는 사용자 Resource Server 회원의 정보를 저장하고 있는 서버 (google, facebook 등) Authorization Server 로그인을 통해 인증 후 권한을 부여받는 서버 (google, facebook 등) Authentication Server 실제로 로그인 서비스를 제공하는 서버 (google, facebook 등) Access Token 실제 요청을 보낼 때 사용하는 토큰 (google, facebook 등에서 로그인 시 발급) Refresh Token Access Token이 만료된 경우 재발급을 위해 사용하는 토큰 (google, facebook 등에서 로그인 시 발급) passport-google
패키지 설치
$ npm install passport-google-oauth20
Google Developers Console에서 OAuth 2.0 클라이언트 생성
https://console.developers.google.com/apis
에서 프로젝트의 clientID와 client secret을 발급받는다. 발급받은 id와 secret은 환경변수로 설정하여 외부에 노출되지 않도록 한다GOOGLE_CLIENT_ID=google_client_id GOOGLE_CLIENT_SECRET=google_client_secret
Strategy
var GoogleStrategy = require('passport-google-oauth20').Strategy; passport.use(new GoogleStrategy({ clientID: GOOGLE_CLIENT_ID, clientSecret: GOOGLE_CLIENT_SECRET, callbackURL: "http://www.example.com/auth/google/callback" }, function(accessToken, refreshToken, profile, cb) { User.findOrCreate({ googleId: profile.id }, function (err, user) { return cb(err, user); }); } ));
Authenticate Request
app.get('/auth/google', passport.authenticate('google', { scope: ['email'] })); app.get('/auth/google/callback', passport.authenticate('google', { failureRedirect: '/login' }), function(req, res) { // Successful authentication, redirect home. res.redirect('/'); });
- Web에서 google 로그인 클릭
- app.get('/auth/google) 실행됨
- passport.use 에서 new GoogleStrategy 실행됨
- clientID, clientSecret 인증 성공
- 구글 이동 및 사용자 정보 권한 승인
- 구글 사용자 정보를 callback해줌
- function(accessToken, refreshToken, profile, cb)에서 callback받은 사용자 정보(profile)를 받음.
- DB에서 사용자를 생성하거나 아이디를 찾아 로그인 하고 마지막에 cb(null, user)를 리턴한다.
https://tecoble.techcourse.co.kr/post/2021-07-10-understanding-oauth/
OAuth 개념 및 동작 방식 이해하기
1. OAuth란? image 웹 서핑을 하다 보면 Google과 Facebook 및 Twitter…
tecoble.techcourse.co.kr
'Today I Learned > TIL 08' 카테고리의 다른 글