네이버, 카카오 등에서 제공하는 OAuth 2.0 관련 api는 이렇게 저렇게 하면 무리 없이 사용할 수 있어서, 공식문서 조금 둘러보면서 정리해봤다. 그렇게.. 쓸모있는 지식이었을까?라는 생각은 들지만 정리했다 ㅋㅋㅋ.. 알게 된건 내가 알던 방식 (Authorization Code Grant)가 OAuth 2.0의 유일한 방법이 아니었다는 것, 그리고 당연하지만 Access token은 세션을 위한 쿠키로 쓰면 절대 절대 안된다는 것을 한번 더 상기하는 정도?!
OAuth라고 하지만, OAuth2.0을 다룰 것이다.
사용법은 각종 OAuth server api 문서에 잘 정리되어있으니 그걸 따르면 된다.
Service Provider / Auth|Resource server
Consumer / Client
User / Resource Owner
Request Token
Access Token
Refresh Token
Scope
출처: https://www.rfc-editor.org/rfc/rfc6749 Figure 1
사실 승인 방식이 몇 가지 된다. 위는 모든 방식을 추상화한, 일반적인 protocol flow를 보여준다.
A, B
공식문서에서 설명하는 Authorization Grant. 유저에게 유저 정보를 열람할 수 있는 허가를 받았다는 증명서 정도로 생각하면 된다. 이걸 이용해 나중에 access token을 받는다.
An authorization grant is a credential representing the resource owner's authorization (to access its protected resources) used by the client to obtain an access token. This specification defines four grant types -- authorization code, implicit, resource owner password credentials, and client credentials -- as well as an extensibility mechanism for defining additional types.
C, D
E, F
결론적으로, 다음과 같이 축약된다.
위에서 A, B 과정에 몇 가지 방법이 있다고 했다. 그 중 하나인 Authorization code grant만 설명하려고 한다. 이 방법은 Refresh token의 사용이 가능한 방식이다. refresh token에 대한 자세한 설명은 뒤에서 다룬다. 또한 이 방법 외에 다른 방법에 대해 알 필요가 있으면 그 때 그 때 해당 문서를 참고하거나 구글링하자
출처 https://www.rfc-editor.org/rfc/rfc6749 Figure 3
https://www.rfc-editor.org/rfc/rfc6749에서 설명하는 Authorization Code Grant. Redirection-based flow를 사용하며, 그렇다보니 당연히 user-agent(보통 web browser)와 의사소통이 가능해야한다.
The authorization code grant type is used to obtain both access tokens and refresh tokens and is optimized for confidential clients. Since this is a redirection-based flow, the client must be capable of interacting with the resource owner's user-agent (typically a web browser) and capable of receiving incoming requests (via redirection) from the authorization server.
내가 서비스 S를 운영중이며, naver의 OAuth 기능을 사용중이라고 하자. naver는 아무 서비스에게나 막 권한을 주면 안된다. 당연히 나는 사전에 naver에서 OAuth2.0(이하 OAuth로 축약)기능을 사용할 수 있도록, 일종의 회원가입/등록을 해놔야한다. 나도 결국 naver 입장에서는 client이다.
과정을 살펴보자
출처 https://hudi.blog/oauth-2.0/
훨씬 더 명료하다. 사실 직접 해보는게 이해하기 더 쉬울 것이다.
출처 https://www.rfc-editor.org/rfc/rfc6749 Figure 2
RFC 6749: The OAuth 2.0 Authorization Framework