Wecode - Project 2 (부트캠프)/Project 2 과정

Project 2: "소셜 로그인" + 깃허브 캡쳐 ** (수요일)

JBS 12 2023. 10. 3. 14:14

구글 지메일 회원가입 

1. client가 google server로 회원가입 요청 

2. google server가 idToken 전송 

3. client 가 service server로 idToken 전송 

4. service server가 idToken이 유효한지 goolge server에 검증/ user data 요청 

5. 유효한 idToken이면 user Data 전송 

6. service server가 받은 user data 저장 , 가공 -> client 에 응답 

 

https://developers.google.com/identity/sign-in/web/backend-auth?hl=ko 

 

백엔드 서버로 인증  |  Authentication  |  Google for Developers

이 페이지는 Cloud Translation API를 통해 번역되었습니다. Switch to English 의견 보내기 백엔드 서버로 인증 컬렉션을 사용해 정리하기 내 환경설정을 기준으로 콘텐츠를 저장하고 분류하세요. 백엔드

developers.google.com

 

https://developers.facebook.com/docs/facebook-login/web

 

웹 - Facebook 로그인 - 문서 - Meta for Developers

Basic Automatic Login Example The following code sample shows you how to add the Facebook SDK for Javascript to your webpage, initialize the SDK, and, if you are logged in to Facebook, will display your name and email. If you are not logged into Facebook,

developers.facebook.com

 

OAuth (Open Authorization)

- 인터넷 사용자들이 비밀번호를 제공하지 않고, 다른 웹사이트 상의 자신들의 정보에 대해 웹사이트/어플리케이션 접근 권한을 부여할 수 있는 공통적 수단 (접근 위임을 위한 개방형 표준)

- 다양한 플랫폼에서 권한 부여를 위한 산업 표준 프로토콜 

 

- resource server: OAuth 2.0 서비스 제공, 자원 관리하는 서버(Facebook, Google, Twitter)

- resource owner: resource server 의 계정을 소유한 사용자 

- Client:  resource server의 API를 사용해, 데이터를 가져오려는 사이트 

- Authorization Server:  client 가 resource server의 서비스를 사용할 수 있게 인증, 토큰 발행하는 서버

 

 

1. client 에서 resource owner에게 승인 요청

2. resource owner가 client에게 인증 권한 부여 

3. client는 부여 받은 인증 권한으로 authorization server에 access token 요청 

4. authorization server에서 client와 부여 받은 인증 권한에 대한 유효성 검사 후 통과하면 access token 부여 

5. client는 받아온 access token 이용해, resource owner의 resource에 접근 요청

6. resource server는 해당 access token 유효성 검사 후, 통과시 resource를 client에 넘겨줌. 

 

 

const {OAuth2Client} = require('google-auth-library');
const client = new OAuth2Client(CLIENT_ID);
async function verify() {
  const ticket = await client.verifyIdToken({
      idToken: token,
      audience: CLIENT_ID,  // Specify the CLIENT_ID of the app that accesses the backend
      // Or, if multiple clients access the backend:
      //[CLIENT_ID_1, CLIENT_ID_2, CLIENT_ID_3]
  });
  const payload = ticket.getPayload();
  const userid = payload['sub'];
  // If request specified a G Suite domain:
  // const domain = payload['hd'];
}
verify().catch(console.error);

 

const env = process.env.NODE_ENV || 'local'; const { OAuth2Client } = require('google-auth-library'); let googleAccountOauthClient; if (env !== 'ci') { if (!process.env.SECRETS) { throw Error('`SECRETS` 환경변수가 설정되어 있지 않습니다'); } googleAccountOauthClient = JSON.parse(process.env.SECRETS).oauth.google; if (!googleAccountOauthClient.clientId) { throw Error('`SECRETS`에 `clientId`가 없습니다.'); } } /** * @param {string} token * @returns {Promise<TokenPayload|undefined|true>} * @throws Error */ const verifyGoogle = (() => { if (env === 'ci') return async () => true; const client = new OAuth2Client(googleAccountOauthClient.clientId); return async (token) => { const ticket = await client.verifyIdToken({ idToken: token, audience: googleAccountOauthClient.clientId, }); return ticket.getPayload(); }; })(); module.exports = { verifyGoogle, };