Bibi's DevLog ๐Ÿค“๐ŸŽ

OAuth Github ๋กœ๊ทธ์ธ ๋ณธ๋ฌธ

ํ”„๋กœ๊ทธ๋ž˜๋ฐ/Git & GIthub ๊นƒ๊ณผ ๊นƒํ—ˆ๋ธŒ

OAuth Github ๋กœ๊ทธ์ธ

๋น„๋น„ bibi 2022. 6. 15. 00:08

OAuth Github ๋กœ๊ทธ์ธ

OAuth๋ž€

https://www.youtube.com/watch?v=hm2r6LtUbk8

OAuth์˜ ์„ธ ์—ญํ• ๋“ค

  • ๋‚ด ์„œ๋น„์Šค
  • ์‚ฌ์šฉ์ž
  • ์œ ๋ช… ์„œ๋น„์Šค (๊ตฌ๊ธ€, ๋„ค์ด๋ฒ„ ๋“ฑ)

OAuth์˜ ์ธ์ฆ ์ ˆ์ฐจ

  1. ์œ ๋ช… ์„œ๋น„์Šค๊ฐ€ Access Token์„ ๋ฐœ๊ธ‰ํ•œ๋‹ค

    AccessToken์ด๋ž€?

    ์œ ๋ช… ์„œ๋น„์Šค๊ฐ€ ๊ฐ€์ง„ ์‚ฌ์šฉ์ž์ •๋ณด์— ์ ‘๊ทผํ•˜๊ธฐ ์œ„ํ•ด, ์‚ฌ์šฉ์ž์˜ ์•„์ด๋”” ๋น„๋ฐ€๋ฒˆํ˜ธ ๋Œ€์‹  ์‚ฌ์šฉํ•˜๋Š” ํ† ํฐ

    ์œ ๋ช… ์„œ๋น„์Šค๊ฐ€ ๊ฐ€์ง„ ์‚ฌ์šฉ์ž ์ •๋ณด๋“ค ์ค‘์—์„œ ๋‚ด ์„œ๋น„์Šค๊ฐ€ ๊ผญ ํ•„์š”ํ•œ ๊ธฐ๋Šฅ๋งŒ ๋ถ€๋ถ„์ ์œผ๋กœ ์ ‘๊ทผ์„ ํ—ˆ์šฉํ•œ๋‹ค.

  2. ๋‚ด ์„œ๋น„์Šค๋Š” OAuth๋ฅผ ํ†ตํ•ด AccessToken์„ ๊ฐ€์ ธ์˜จ๋‹ค

  3. ๋‚ด ์„œ๋น„์Šค๋Š” AccessToken์„ ํ†ตํ•ด ์œ ๋ช… ์„œ๋น„์Šค๊ฐ€ ๊ฐ€์ง„ ์‚ฌ์šฉ์ž ์ •๋ณด์— ์ ‘๊ทผํ•˜๊ณ , ๊ฐ€์ ธ์˜ค๊ณ , ์ˆ˜์ •ํ•œ๋‹ค.

GitHub OAuth - Authorizing OAuth Apps(Web Application Flow)

https://docs.github.com/en/enterprise-server@2.21/developers/apps/building-oauth-apps/authorizing-oauth-apps#non-web-application-flow

https://zeddios.tistory.com/1102

0. Github OAuth App์„ ๋งŒ๋“ ๋‹ค.

  • https://docs.github.com/en/enterprise-server@2.21/developers/apps/building-oauth-apps/creating-an-oauth-app ๋ฅผ ์ฐธ๊ณ .
  • Authorization callback URL์ด๋ž€? OAuth ์ธ์ฆ์ด ๋๋‚˜๊ณ  ๋˜๋Œ์•„์˜ฌ URL. ์ธ์ฆ์ด ๋๋‚˜๋ฉด ๋‚ด ์„œ๋น„์Šค(์•ฑ)์œผ๋กœ ๋˜๋Œ์•„์™€์•ผ ํ•˜๋ฏ€๋กœ ์•ฑ ์ฃผ์†Œ๋ฅผ ๋„ฃ์–ด์ฃผ์–ด์•ผ ํ•œ๋‹ค.
  • https://zeddios.tistory.com/1102๋ฅผ ์ฐธ๊ณ ํ•ด URL Schemes๋ฅผ ๋งŒ๋“  ํ›„, ๋’ค์— ://์„ ๋ถ™์—ฌ ๋„ฃ์–ด์ค€๋‹ค
  • OAuth ์•ฑ ์ƒ์„ฑ ์‹œ ์•Œ๋ ค์ฃผ๋Š” Client ID, Client Secret์„ ๊ธฐ์–ตํ•ด ๋‘”๋‹ค.
  • ํŠนํžˆ Client Secret์€ ์ฒซ ์ƒ์„ฑ ํ›„์— ๋‹ค์‹œ ๋ณผ ์ˆ˜ ์—†์œผ๋‹ˆ ์•ˆ์ „ํ•œ ๊ณณ์— ์ž˜ ๋ณด๊ด€ํ•ด ๋‘”๋‹ค.

1. ์‚ฌ์šฉ์ž์˜ ๊นƒํ—™ ์•„์ด๋ดํ‹ฐํ‹ฐ๋ฅผ ์š”์ฒญํ•˜๋„๋ก ์‚ฌ์šฉ์ž๋ฅผ ๋ฆฌ๋‹ค์ด๋ ‰ํŠธ ์‹œํ‚จ๋‹ค.

  • ์˜ˆ๋ฅผ ๋“ค์–ด "GitHub์œผ๋กœ ๋กœ๊ทธ์ธ" ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ์‚ฌ์šฉ์ž์˜ ๊นƒํ—™ ์ •๋ณด๋ฅผ ์š”์ฒญํ•œ๋‹ค.
  • *GET https://github.com/login/oauth/authorize *
    • [hostname]์— github.com์„ ๋„ฃ์œผ๋ฉด ๋œ๋‹ค.
  • ๋’ค์— ์›ํ•˜๋Š” ์ •๋ณด๋ฅผ ์ฟผ๋ฆฌ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋ถ™์—ฌ ์š”์ฒญํ•œ๋‹ค.
  • Parameters (์ฟผ๋ฆฌ ํŒŒ๋ผ๋ฏธํ„ฐ)
    • (ํ•„์ˆ˜) client_id : String. ๊นƒํ—™์œผ๋กœ๋ถ€ํ„ฐ ๋ฐ›์€ Client ID์ด๋‹ค
    • redirect_uri : String. ์‚ฌ์šฉ์ž๊ฐ€ ์ธ์ฆ ํ›„์— ๋ณด๋‚ด์งˆ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ URL. redirect urls ์ฐธ๊ณ .
    • login : String. ์•ฑ ํšŒ์›๊ฐ€์ž… ๋ฐ ์ธ์ฆ์— ์‚ฌ์šฉํ•  ํŠน์ • ๊ณ„์ •์„ ์ œ์‹œํ•จ.
    • scope : String. ๋ฒ”์œ„(scope) ๋ชฉ๋ก. ๊ณต๋ฐฑ์œผ๋กœ ๊ตฌ๋ถ„๋จ.
      • ์ด ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ ์ œ๊ณต๋˜์ง€ ์•Š์œผ๋ฉด ๋นˆ ๋ฐฐ์—ด์ด๊ธฐ ๋•Œ๋ฌธ์—, ์•ฑ์ด ์‚ฌ์šฉ์ž ์ •๋ณด์— ์–ด๋–ค ์ ‘๊ทผ ๋ฒ”์œ„๋„ ๊ฐ–์ง€ ๋ชปํ•จ.
      • ์•ฑ์—์„œ ์ด๋ฏธ ์ธ์ฆ๋œ ๋ฒ”์œ„๋ฅผ ๊ฐ€์ง„ ์‚ฌ์šฉ์ž์— ๋Œ€ํ•ด์„œ๋Š”, ๋ฒ”์œ„๋ฅผ ๋ณด์—ฌ์ฃผ๋Š” ์ธ์ฆ ํŽ˜์ด์ง€๊ฐ€ ๋ณด์—ฌ์ง€์ง€ ์•Š์Œ. ์‚ฌ์šฉ์ž๊ฐ€ ์ด๋ฏธ ์ธ์ฆํ•œ ๋ฒ”์œ„๋ฅผ ๊ฐ€์ง€๊ณ  ์ด ๋ถ€๋ถ„์„ ์ฑ„์šด๋‹ค.
      • ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ๋ฒ”์œ„ ์ •๋ณด๋Š” ์—ฌ๊ธฐ์— ์„ค๋ช…๋˜์–ด ์žˆ๋‹ค. - ๋ฒˆ์—ญ
    • state : String. ์ถ”์ธกํ•  ์ˆ˜ ์—†๋Š” ๋žœ๋ค ๋ฌธ์ž์—ด. ์‚ฌ์ดํŠธ ๊ฐ„ ์š”์ฒญ ์œ„์กฐ ๊ณต๊ฒฉ์œผ๋กœ๋ถ€ํ„ฐ ๋ณดํ˜ธํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ.
    • allow_signup : String. OAuth ํ๋ฆ„ ๋™์•ˆ ๊นƒํ—™ ํšŒ์›๊ฐ€์ž…์ด ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ• ์ง€์— ๋Œ€ํ•œ ์˜ต์…˜. ๊ธฐ๋ณธ๊ฐ’์€ true.

2. ์‚ฌ์šฉ์ž๊ฐ€ ๊นƒํ—™์— ์˜ํ•ด ๋‚ด ์„œ๋น„์Šค๋กœ ๋ฆฌ๋‹ค์ด๋ ‰ํŠธ๋˜์–ด ๋˜๋Œ์•„์˜จ๋‹ค.

  • ์‚ฌ์šฉ์ž๊ฐ€ ๋‹น์‹ (๋‚ด ์„œ๋น„์Šค)์˜ ์š”์ฒญ์„ ํ—ˆ์šฉํ•˜๋ฉด, ๊นƒํ—™ ์„œ๋ฒ„๊ฐ€ codeํŒŒ๋ผ๋ฏธํ„ฐ์— ์ž„์‹œ ์ฝ”๋“œ๋ฅผ, stateํŒŒ๋ผ๋ฏธํ„ฐ์— 1.์—์„œ ์ œ๊ณตํ•œ state๋ฅผ ๋„ฃ์–ด์„œ ๋‚ด ์„œ๋น„์Šค๋กœ ๋‹ค์‹œ ๋ฆฌ๋‹ค์ด๋ ‰ํŠธ ํ•ด์ค€๋‹ค.

  • code๋Š” 10๋ถ„ ํ›„์— ๋งŒ๋ฃŒ๋œ๋‹ค.

  • ๋งŒ์•ฝ state๊ฐ€ ๋‚ด๊ฐ€ ๋ณด๋‚ธ ๊ฒƒ๊ณผ ์ผ์น˜ํ•˜์ง€ ์•Š์œผ๋ฉด, ์ œ3์ž๊ฐ€ ์š”์ฒญ์„ ์ƒ์„ฑํ•œ ๊ฒƒ์ด๋ฏ€๋กœ ์ธ์ฆ ์ ˆ์ฐจ๋ฅผ ์ค‘๋‹จํ•ด์•ผ ํ•œ๋‹ค.

  • code๋Š” ์•ก์„ธ์Šค ํ† ํฐ์„ ์˜๋ฏธํ•˜๋ฉฐ, ์•„๋ž˜ URL๋กœ ์š”์ฒญ์„ ๋ณด๋‚ผ ๋•Œ access_token์ž๋ฆฌ์— ๋„ฃ๋Š”๋‹ค.

  • POST https://github.com/login/oauth/{access_token}

  • ๋’ค์— ํ•„์ˆ˜ ์ •๋ณด๋ฅผ ํฌํ•จํ•œ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๋ถ™์—ฌ ์š”์ฒญํ•œ๋‹ค.

  • Parameters (์ฟผ๋ฆฌ ํŒŒ๋ผ๋ฏธํ„ฐ)

    • (ํ•„์ˆ˜) client_id : String. ๋‚ด OAuth ์•ฑ์— ๋Œ€ํ•ด ๊นƒํ—™์œผ๋กœ๋ถ€ํ„ฐ ๋ฐ›์€ Client ID
    • (ํ•„์ˆ˜) client_secret : String. ๋‚ด OAuth ์•ฑ์— ๋Œ€ํ•ด ๊นƒํ—™์œผ๋กœ๋ถ€ํ„ฐ ๋ฐ›์€ Client Secret
    • (ํ•„์ˆ˜) code : String.1.์˜ ๊ฒฐ๊ณผ๋กœ ๋ฐ›์€ ์ฝ”๋“œ.
    • redirect_uri : String. ์‚ฌ์šฉ์ž๊ฐ€ ์ธ์ฆ ํ›„์— ๋ณด๋‚ด์งˆ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ URL.
  • Response

    • ๊ธฐ๋ณธ์ ์œผ๋กœ๋Š” ์•„๋ž˜์˜ ํ˜•ํƒœ๋กœ ์‘๋‹ต์ด ์˜จ๋‹ค.

      • access_token=e72e16c7e42f292c6912e7710c838347ae178b4a&token_type=bearer
    • ๋˜ํ•œ Accept ํ—ค๋”์— ๋”ฐ๋ผ ์„œ๋กœ ๋‹ค๋ฅธ ํฌ๋งท์œผ๋กœ ๋‚ด์šฉ์„ ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค.

      • Accept: application/json
        {"access_token":"e72e16c7e42f292c6912e7710c838347ae178b4a", "scope":"repo,gist", "token_type":"bearer"}
        
        Accept: application/xml
        <OAuth>
          <token_type>bearer</token_type>
          <scope>repo,gist</scope>
          <access_token>e72e16c7e42f292c6912e7710c838347ae178b4a</access_token>
        </OAuth>

3. ๋‚ด ์„œ๋น„์Šค๊ฐ€ ์‚ฌ์šฉ์ž์˜ ์•ก์„ธ์Šค ํ† ํฐ์„ ๊ฐ€์ง€๊ณ  API์— ์ ‘๊ทผํ•œ๋‹ค.

  • AccessToken (์•ก์„ธ์Šคํ† ํฐ) ์„ ๊ฐ€์ง€๊ณ , ์‚ฌ์šฉ์ž๋ฅผ ๋Œ€์‹ ํ•ด API์— ์š”์ฒญ์„ ๋ณด๋‚ผ ์ˆ˜ ์žˆ๋‹ค.

    • Authorization: token OAUTH-TOKEN
      GET http(s)://[hostname]/api/v3/user