ABOUT-ME-APP / about_me_server

앱구동을 위한 서버환경 설정 , api 서버 구축
0 stars 1 forks source link

apple 로그인 개발 #1

Open ABOUT-ME-APP opened 3 years ago

ABOUT-ME-APP commented 3 years ago

참고 자료 https://hwannny.tistory.com/71 https://whitepaek.tistory.com/61 https://darkstart.tistory.com/117?category=871909

jwt인증 https://tansfil.tistory.com/59?category=255594

rest template https://advenoh.tistory.com/46

junsix commented 3 years ago

API 설계

use 케이스

  1. iOS앱에서 로그인 성공시 전달받는 identityToken(id token)과 authorizationCode를 앱서버로 보낸다.

  2. 앱서버에선 identityToken의 유효성을 검증하고 identityToken에 담긴 sub(사용자 아이디)를 사용자의 고유 아이디로 인식하여 정보들을 저장하면 된다.

  3. identityToken이 유효하면 애플 서버(https://appleid.apple.com/auth/token)에 필요한 정보를 담아 request를 보내 access_token과 refresh_token을 얻는다.

  4. 이 access_token과 refresh_token은 앱서버의 db에 사용자 아이디와 같은 row에 저장하여 보관하며, iOS앱으로는 access_token과 refresh_token를 모두 보낸다.

access_token을 갱신할 시점을 알기 위해 등록 시간도 함께 저장한다.

테이블 설계

appleUserId appleAccessToken appleRefreshToken appleAccessTokenIssueDate

Request : appleUserId, appleAccessToken

https://darkstart.tistory.com/117

junsix commented 3 years ago

개발 고민 사항

https://bcho.tistory.com/955

junsix commented 3 years ago

문제가 되는 상황

apple에서 access token 발급 받기

apple이 하루에 1번 넘게 refresh_token 을 검증하지 말라고 가이드 'Apple says they may throttle your server if you attempt to validate the refresh token more than once per day.'

accessToken을 통해서 apple에서 사용자 정보를 받아오기 힘듬.

junsix commented 3 years ago

User access token 로그인 > 유저 정보 없음 > IOS에서 회원가입 UI > NINE에서 맞는 정보를 기입 + accesstoken 회원가입 > 가입완료

로그인

Request

GET /auth/signin
Token: {naver_access_token}

Response

200 OK
Content-Type: application/json
{
    "jwt token": 
}
Access token이 잘못된 경우: 401
User를 찾을 수 없는 경우: 404 -> 회원가입으로

회원가입

Request

POST /auth/signup
Token: {naverAccessToken}
Content-Type: application/json
{
    "name": String,
    "email": String,
    "profileImage": String,
}

Response

200 OK
Content-Type: application/json

{
    "token": 
}
401 : Access token이 잘못된 경우
409 : 이미 존재하는 유저인 경우
500 : 회원가입 실패

내 정보 조회

Request

GET /v1/users/me
Authorization: Bearer {jwtToken}

Response

200 OK
Content-Type: application/json
{
    "id": 2,
    "name": "",
    "email": "a@naver.com",
    "profileImage": "https://ssl.pstatic.net/static/pwe/address/img_profile.png"
}
{jwtToken} 이 잘못된 경우 / 만료됐을 경우: 401

특정 유저 정보 조회

Request

GET /v1/users/{id}
Authorization: Bearer {jwtToken}

Response 200 OK

Content-Type: application/json
{
    "id": 2,
    "name": "",
    "email": "a@naver.com",
    "profileImage": "https://ssl.pstatic.net/static/pwe/address/img_profile.png"
}

{jwtToken} 이 잘못된 경우 / 만료됐을 경우: 401

사용자 정보 리스트

Request

GET /v1/users
Authorization: Bearer {jwtToken}

Response 200 OK

Content-Type application/json
{
    "users": [
        {
            "id": 2,
            "name": "",
            "email": "a@naver.com",
            "profileImage": "https://ssl.pstatic.net/static/pwe/address/img_profile.png"
        }
    ]
}

{jwtToken} 이 잘못된 경우 / 만료됐을 경우: 401

회원 탈퇴

Request

DELETE /v1/users/me
Authorization: Bearer {jwtToken}

Response

200 OK

토큰 refresh

Request

GET /auth/refresh
Authorization: Bearer {jwtToken}

Response

200 OK
Content-Type: application/json
{
    "token": "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiLsnbTshKDrqoUiLCJpYXQiOjE2MTM0NjI0NDMsImV4cCI6MTYxMzQ2NjA0M30.93PETTkc-e49YnbH8ZpXCNO0ycDhZguiuw3xcUD2ffU"
}
junsix commented 3 years ago

JWT 발급 및 사용 순서

  1. 사용자는 토큰을 서버에 전송한다.
  2. 서버는 토큰 인증 성공 후, JWT 토큰을 아래와 같이 생성한다.
    • 헤더 생성
    • 페이로드 생성( 토큰 인증 후 사용할 정보 또는 토큰 인증 때 사용할 정보)
    • 대칭키 또는 키쌍(개인키)을 통해 Signature 생성(헤더+페이로드 값으로)
  3. 사용자에게 토큰 반환
  4. 사용자는 해당 토큰을 사용할 때 HTTP Header에 Base64로 인코딩된 JWT(Header.Payload.Signature) 토큰을 적재하여 서버에 전송한다.
  5. 서버는 토큰에 대해 무결성 검증을 실시한다. Signature 값 검증, 토큰 유효기간 검증 등의 작업을 실시한다.
  6. 토큰의 무결성/유효성 검증 성공 후 사용자에게 리소스를 반환한다.