OAuth 2.0 - password Grant

웹 표준 인증 프레임 워크중 access token의 사용으로 여러 디바이스의 인증을 손쉽게 해준 OAuth 2.0 에서 access token를 얻는 여러 방법 중 하나인 password Grant를 보자.

password Grant

이 유형은 외부의 프로그램이나 타사 서비스에서 접근할 때가 아니라, 본인의 서비스의 어플리케이션에서 사용하는 인증이다.

OAuth 2.0에서 가장 간단한 인증 중 하나이고 전통적으로 사용자의 이름과 비밀번호로 access token을 얻는다.

인증 과정

공식 문서의 인증 과정

  1. 사용자의 username과 password를 OAuth 서버에 제공한다.
  2. OAuth 서버에서 확인해 유효하면 access token을 발급해준다.(선택적으로 refresh token)

사용할 때

타사 개발자나 타 시스템에서 접근할 때 이 유형을 쓰면 안될 것이다. 유저 고유한 비밀번호를 타 시스템이 알면 안될테니까.

하지만 자사 시스템에서라면 username과 password를 묻는 건 당연하다. 즉, 이 유형은 본인의 시스템에 속해있는 어플리케이션에서 사용하는 인증이다.

Request & Response Example

1. username, password와 함께 OAuth 서버에 요청

POST /oauth/token HTTP/1.1
Host: authorization-server.com
Content-type: application/x-www-form-urlencoded

grant_type=password
&username=exampleuser
&password=1234luggage
&client_id=xxxxxxxxxx
  • grant_type=password : password 유형을 사용하는 서버에 알려줍니다.
  • username= : 응용 프로그램에 입력 한 사용자 이름
  • password= : 응용 프로그램에 입력 한 사용자의 암호
  • client_id= : 개발자가 등록 과정에서 얻은 응용 프로그램의 공용 식별자
  • scope= (선택 사항) : 응용 프로그램이 범위가 제한된 토큰을 요청하는 경우 여기에 요청 된 범위를 제공해야합니다.

2. 어플리케이션에 돌아오는 응답

HTTP/1.1 200 OK
Content-Type: application/json
Cache-Control: no-store
Pragma: no-cache

{
  "access_token":"MTQ0NjJkZmQ5OTM2NDE1ZTZjNGZmZjI3",
  "token_type":"bearer",
  "expires_in":3600,
  "refresh_token":"IwOGYzYTlmM2YxOTQ5MGE3YmNmMDFkNTVk",
  "scope":"create delete"
}

refresh token은 추가적으로 구현할 수 있다.

참조

  • https://tools.ietf.org/html/rfc6749#section-4.3
  • https://developer.okta.com/blog/2018/06/29/what-is-the-oauth2-password-grant
김땡땡's blog

김땡땡's blog

김땡땡