Skip to content

Commit

Permalink
Address review comment
Browse files Browse the repository at this point in the history
  • Loading branch information
awarecan committed Jul 20, 2018
1 parent e8e2ab5 commit 94dde04
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions homeassistant/components/auth/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,35 +271,32 @@ async def post(self, request):
grant_type = data.get('grant_type')

if grant_type == 'authorization_code':
resp = await self._async_handle_auth_code(hass, client_id, data)
if resp.get('status_code', 200) != 200:
await process_wrong_login(request)
return resp
return await self._async_handle_auth_code(
hass, request, client_id, data)

elif grant_type == 'refresh_token':
resp = await self._async_handle_refresh_token(
hass, client_id, data)
if resp.get('status_code', 200) != 200:
await process_wrong_login(request)
return resp
return await self._async_handle_refresh_token(
hass, request, client_id, data)

await process_wrong_login(request)
return self.json({
'error': 'unsupported_grant_type',
}, status_code=400)

async def _async_handle_auth_code(self, hass, client_id, data):
async def _async_handle_auth_code(self, hass, request, client_id, data):
"""Handle authorization code request."""
code = data.get('code')

if code is None:
await process_wrong_login(request)
return self.json({
'error': 'invalid_request',
}, status_code=400)

credentials = self._retrieve_credentials(client_id, code)

if credentials is None:
await process_wrong_login(request)
return self.json({
'error': 'invalid_request',
'error_description': 'Invalid code',
Expand All @@ -308,6 +305,7 @@ async def _async_handle_auth_code(self, hass, client_id, data):
user = await hass.auth.async_get_or_create_user(credentials)

if not user.is_active:
await process_wrong_login(request)
return self.json({
'error': 'access_denied',
'error_description': 'User is not active',
Expand All @@ -325,18 +323,21 @@ async def _async_handle_auth_code(self, hass, client_id, data):
int(refresh_token.access_token_expiration.total_seconds()),
})

async def _async_handle_refresh_token(self, hass, client_id, data):
async def _async_handle_refresh_token(
self, hass, request, client_id, data):
"""Handle authorization code request."""
token = data.get('refresh_token')

if token is None:
await process_wrong_login(request)
return self.json({
'error': 'invalid_request',
}, status_code=400)

refresh_token = await hass.auth.async_get_refresh_token(token)

if refresh_token is None or refresh_token.client_id != client_id:
await process_wrong_login(request)
return self.json({
'error': 'invalid_grant',
}, status_code=400)
Expand Down

0 comments on commit 94dde04

Please sign in to comment.