cupping-note / cupping-note-server

0 stars 0 forks source link

oauth flow ์„ค๊ณ„ #5

Closed white-gyu closed 1 year ago

white-gyu commented 1 year ago

๐Ÿ“• ๊ฐœ์š”

oauth flow๋ฅผ ๊ธฐ์ˆ ํ•ฉ๋‹ˆ๋‹ค.

๐Ÿ“ Sequence Diagram

sequenceDiagram
autonumber
actor user
participant client(= cupping-note-expo)
participant server(= cupping-note-server)
participant sns-auth-server

user ->> client(= cupping-note-expo): ์นด์นด์˜ค/์• ํ”Œ ๋กœ๊ทธ์ธ ์š”์ฒญ
client(= cupping-note-expo) ->>+sns-auth-server: authorization code ์š”์ฒญ
sns-auth-server ->> user: ๋™์˜ ๋ฐ ๋กœ๊ทธ์ธ
user ->> sns-auth-server: authorization code ๋ฐœ๊ธ‰ ์š”์ฒญ
sns-auth-server ->>- client(= cupping-note-expo): authorization code ๋ฐœ๊ธ‰
client(= cupping-note-expo) ->> server(= cupping-note-server): authorization code๋กœ ํšŒ์›๊ฐ€์ž…/๋กœ๊ทธ์ธ ์š”์ฒญ
server(= cupping-note-server) ->> sns-auth-server: authorization code๋กœ access/refresh token ๋ฐœ๊ธ‰ ์š”์ฒญ
sns-auth-server -->> server(= cupping-note-server): access/refresh token ๋ฐœ๊ธ‰
server(= cupping-note-server) ->> server(= cupping-note-server): ํ† ํฐ ์ •๋ณด ์กฐํšŒ/๊ฒ€์ฆ ๋ฐ ๋กœ๊ทธ์ธ ์™„๋ฃŒ
server(= cupping-note-server) ->> sns-auth-server: ํ† ํฐ์œผ๋กœ ์‚ฌ์šฉ์ž ์ •๋ณด ์กฐํšŒ ์š”์ฒญ
sns-auth-server -->> server(= cupping-note-server): ์š”์ฒญ ๊ฒ€์ฆ ๋ฐ ์ฒ˜๋ฆฌ
alt ์‹ ๊ทœ ์‚ฌ์šฉ์ž์ธ ๊ฒฝ์šฐ
server(= cupping-note-server) -->> client(= cupping-note-expo): ํšŒ์› ๊ฐ€์ž… ์ฒ˜๋ฆฌ
else ๊ธฐ์กด ํšŒ์›์ธ ๊ฒฝ์šฐ
server(= cupping-note-server) -->> client(= cupping-note-expo): ๋กœ๊ทธ์ธ ์ฒ˜๋ฆฌ
end
client(= cupping-note-expo) ->> user: ๋กœ๊ทธ์ธ ์™„๋ฃŒ

1. ์นด์นด์˜ค/์• ํ”Œ ๋กœ๊ทธ์ธ ์š”์ฒญ

์‚ฌ์šฉ์ž๊ฐ€ ์นด์นด์˜ค, ์• ํ”Œ ๋กœ๊ทธ์ธ์„ ์š”์ฒญํ•ฉ๋‹ˆ๋‹ค.

2. authorization code ์š”์ฒญ

access/refresh token ๋ฐœ๊ธ‰์„ ์œ„ํ•œ authorization code ๋ฐœ๊ธ‰์„ ์š”์ฒญํ•ฉ๋‹ˆ๋‹ค.

3. ๋™์˜ ๋ฐ ๋กœ๊ทธ์ธ

์นด์นด์˜ค๋‚˜ ์• ํ”Œ ๋กœ๊ทธ์ธ ๋™์˜ ํ™”๋ฉด์„ ํ˜ธ์ถœํ•˜๊ณ  ์‚ฌ์šฉ์ž ๋™์˜๋ฅผ ๊ตฌํ•ฉ๋‹ˆ๋‹ค. ๋™์˜ ํ™”๋ฉด์€ ์•ฑ์— ์„ค์ •๋œ ๋™์˜ ํ•ญ๋ชฉ์— ๋Œ€ํ•ด ์‚ฌ์šฉ์ž์—๊ฒŒ ๋ณธ ์„œ๋น„์Šค์— ์ •๋ณด๋ฅผ ์ œ๊ณตํ•˜๋Š” ๋“ฑ์˜ ๋™์˜๋ฅผ ๊ตฌํ•ฉ๋‹ˆ๋‹ค.

4. authorization code ๋ฐœ๊ธ‰ ์š”์ฒญ

์‚ฌ์šฉ์ž ๋™์˜ ์ดํ›„ authorization code ๋ฐœ๊ธ‰ ์š”์ฒญํ•ฉ๋‹ˆ๋‹ค.

5. authorization code ๋ฐœ๊ธ‰

์œ„ ์š”์ฒญํ•œ API ์‘๋‹ต๊ฐ’์„ ํ†ตํ•ด authorization code๋ฅผ ๋ฐœ๊ธ‰ ๋ฐ›์Šต๋‹ˆ๋‹ค.

6. authorization code๋กœ access/refresh token ๋ฐœ๊ธ‰ ์š”์ฒญ

authorization code๋กœ access/refresh token ๋ฐœ๊ธ‰ ์š”์ฒญํ•ฉ๋‹ˆ๋‹ค.

7. access/refresh token ๋ฐœ๊ธ‰

์œ„ ์š”์ฒญํ•œ API ์‘๋‹ต๊ฐ’์„ ํ†ตํ•ด access/refresh token์„ ๋ฐœ๊ธ‰ ๋ฐ›์Šต๋‹ˆ๋‹ค.

8. ํ† ํฐ ์ •๋ณด ์กฐํšŒ/๊ฒ€์ฆ ๋ฐ ๋กœ๊ทธ์ธ ์™„๋ฃŒ

ํ† ํฐ์„ ๊ฒ€์ฆํ•˜๊ณ  ์ดํ›„ ๋กœ๊ทธ์ธ์„ ์™„๋ฃŒํ•ฉ๋‹ˆ๋‹ค.

9. ํ† ํฐ์œผ๋กœ ์‚ฌ์šฉ์ž ์ •๋ณด ์กฐํšŒ ์š”์ฒญ

access token์„ ํ†ตํ•ด ์ด๋ฉ”์ผ, ๋‹‰๋„ค์ž„๊ณผ ๊ฐ™์€ ์‚ฌ์šฉ์ž ์ •๋ณด๋ฅผ ์š”์ฒญํ•ฉ๋‹ˆ๋‹ค.

10. ์š”์ฒญ ๊ฒ€์ฆ ๋ฐ ์ฒ˜๋ฆฌ

์นด์นด์˜ค ์„œ๋ฒ„์—์„œ ์š”์ฒญ์„ ๊ฒ€์ฆํ•˜๊ณ  ์ฒ˜๋ฆฌํ•˜์—ฌ ์‚ฌ์šฉ์ž ์ •๋ณด๋ฅผ ์‘๋‹ตํ•ฉ๋‹ˆ๋‹ค.

11. ํšŒ์› ๊ฐ€์ž… ์ฒ˜๋ฆฌ

๋ณธ ์„œ๋น„์Šค์— ๊ฐ€์ž…๋˜์ง€ ์•Š์€ ์‹ ๊ทœ ์‚ฌ์šฉ์ž์ธ ๊ฒฝ์šฐ ํšŒ์› ๊ฐ€์ž…์œผ๋กœ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค.

12. ๋กœ๊ทธ์ธ ์ฒ˜๋ฆฌ

๋ณธ ์„œ๋น„์Šค์— ๊ฐ€์ž…๋œ ๊ธฐ์กด ์‚ฌ์šฉ์ž์ธ ๊ฒฝ์šฐ ๋กœ๊ทธ์ธ์œผ๋กœ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค.

13. ๋กœ๊ทธ์ธ ์™„๋ฃŒ

๋กœ๊ทธ์ธ์„ ์™„๋ฃŒํ•˜์—ฌ ์‚ฌ์šฉ์ž์—๊ฒŒ ์•Œ๋ฆฝ๋‹ˆ๋‹ค. ์ด ๋•Œ userId์™€ email์„ Jwt์— ๋‹ด์•„ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค.

๐Ÿ“Œ ์ฐธ๊ณ  ์ž๋ฃŒ

white-gyu commented 1 year ago

13. ๋กœ๊ทธ์ธ ์™„๋ฃŒ

๋กœ๊ทธ์ธ์„ ์™„๋ฃŒํ•˜์—ฌ ์‚ฌ์šฉ์ž์—๊ฒŒ ์•Œ๋ฆฝ๋‹ˆ๋‹ค. ์ด ๋•Œ userId์™€ email์„ Jwt์— ๋‹ด์•„ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค.

jwt ์œ ํšจ ๊ธฐ๊ฐ„ = 1๋‹ฌ ๋กœ ์ฒ˜๋ฆฌํ•˜์˜€์Šต๋‹ˆ๋‹ค. @JooJaeSeong @bhyoo99 @lcw729 @bahar-j ์ด์— ๋Œ€ํ•œ ์˜๊ฒฌ ๋ถ€ํƒ๋“œ๋ ค์š” ๐Ÿ™


์ฐธ๊ณ )

jwt๋ฅผ header๋กœ ๋‹ด๋Š” API๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ๋ณด๋‚ด์ฃผ์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค..!

curl --location --request GET 'http://localhost:8080/api/me' \
--header 'Authorization: eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJ1c2VyX2lkIjoyLCJyb2xlcyI6WyJVU0VSIl0sImlzcyI6ImN1cHBpbmdfbm90ZSIsImV4cCI6MTY3OTE1MjExNiwiaWF0IjoxNjc2NTYwMTE2LCJlbWFpbCI6ImhhbWEyMjI5NjY4QG5hdmVyLmNvbSJ9.GG2aWZ3c5s5I3LZRHbt1qNybPFAO5a7UcPMkw7imr2xjfyj3x2F5mR8uBne0hiZytf_hYml2gU3dhEzWwgI9uw'
white-gyu commented 1 year ago

https://github.com/cupping-note/cupping-note-server/pull/14/commits/8f13f5615c2e35cc8b727864f7c496b49b613bef ์ปค๋ฐ‹์„ ํ†ตํ•ด 5๋ฒˆ authorization code ๋ฐ›๋Š” ๋Œ€์ƒ์ด server(= cupping-note-server)client(= cupping-note-expo)๋กœ ๋ณ€๊ฒฝ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

white-gyu commented 1 year ago

์„œ๋ฒ„์—์„œ userId ๊ฐ€์ ธ์˜ค๋Š” ๋ฐฉ๋ฒ•

controller ๋‚ด ์•„๋ž˜์™€ ๊ฐ™์ด ํ‘œํ˜„

@GetMapping
public ResponseEntity<String> getUser(@AuthenticationPrincipal final JwtAuthentication authentication) {
   return ResponseEntity.ok(authentication.userId);
}