트러블 슈팅

oauth에서 파라미터를 넘길 때 (state)

이째형 2024. 2. 18. 20:40

- 상황

사내 b2b 형태의 프로젝트에서, 어떤 트리거가 발생 했을 때 notification을 해당 기업에게 보낼 수 있는 슬랙 앱을 개발하고 있었습니다.

슬랙에서 공식적으로 승인을 받지 못한 승인 이전의 app의 경우

위와 같이 url또는 버튼을 제공하여 

url 입력 시 슬랙 설치 화면으로 리다이렉트,

허용 버튼을 누르면 
https://api.slack.com/methods/oauth.v2.access

 

oauth.v2.access API method

Exchanges a temporary OAuth verifier code for an access token.

api.slack.com

- slack oatuh api를 호출

- 우리가 설정한 페이지로 리다이렉트 

와 같은 플로우를 진행하게 됩니다.

 

{
    "ok": true,
    "access_token": "xoxb-17653672481-19874698323-pdFZKVeTuE8sk7oOcBrzbqgy",
    "token_type": "bot",
    "scope": "commands,incoming-webhook",
    "bot_user_id": "U0KRQLJ9H",
    "app_id": "A0KRD7HC3",
    "team": {
        "name": "Slack Softball Team",
        "id": "T9TK3CUKW"
    },
    "enterprise": {
        "name": "slack-sports",
        "id": "E12345678"
    },
    "authed_user": {
        "id": "U1234",
        "scope": "chat:write",
        "access_token": "xoxp-1234",
        "token_type": "user"
    }
}

 slack oauth api의 응답은 다음과 같습니다.

 

응답을 통해서 슬랙 앱을 설치한 team의 정보 / web_hook url등을 얻을 수 있어 해당 채널에 메세지를 보낼 수 있게 됩니다.

 

- 문제

슬랙에서 리다이렉트를 시키는 환경 상, 프로덕트를 이용하여 슬랙 앱을 설치한 

기업 - 슬랙 채널 간 연결이 해당 정보만으로는 불가능합니다. 

 

slack에서는 rediect url에 쿼리 스트링을 추가 할 경우 보안처리가 되어 있어 바로 아래와 같은 화면이 노출됩니다.

 

 

- 해결

Authorization protocols provide a state parameter 
that allows you to restore the previous state of your application. 
The state parameter preserves some state objects 
set by the client in the Authorization request and makes it available 
to the client in the response

 

oauth2.0에서는 이전 상태의 파라미터를 넘길 수 있는 수단으로써, state라는 파라미터를 제공합니다.

해당 파라미터는 필수 사항은 아니지만 클라이언트가 요청 시 state를 포함 시켰다면 provider는 동일값을 클라이언트에게 보내야 합니다.

 

 

- 주의 

state를 이용하여 리다이렉트가 되는 상황에서도 파라미터를 넘길 수 있게 되었지만, 보안에 유의하여야 합니다.

https://auth0.com/docs/secure/attack-protection/state-parameters