forked from home-assistant/core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig_flow.py
More file actions
74 lines (54 loc) · 2.31 KB
/
config_flow.py
File metadata and controls
74 lines (54 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
"""Config flow for the Eve Online integration."""
from __future__ import annotations
import logging
from typing import Any
import jwt
from homeassistant.config_entries import ConfigFlowResult
from homeassistant.helpers.config_entry_oauth2_flow import AbstractOAuth2FlowHandler
from .const import CONF_CHARACTER_ID, CONF_CHARACTER_NAME, DOMAIN, SCOPES
_LOGGER = logging.getLogger(__name__)
class OAuth2FlowHandler(AbstractOAuth2FlowHandler, domain=DOMAIN):
"""Handle OAuth2 config flow for Eve Online.
Each config entry represents one authenticated character.
Multiple characters can be added as separate entries.
"""
DOMAIN = DOMAIN
@property
def logger(self) -> logging.Logger:
"""Return logger."""
return _LOGGER
@property
def extra_authorize_data(self) -> dict[str, Any]:
"""Extra data to include in the authorize URL."""
return {"scope": " ".join(SCOPES)}
async def async_oauth_create_entry(self, data: dict[str, Any]) -> ConfigFlowResult:
"""Create an entry for the flow.
Decode the Eve SSO JWT access token to extract character_id and
character_name, then create a config entry for that character.
"""
try:
token = data["token"]["access_token"]
character_info = _decode_eve_jwt(token)
except ValueError, KeyError, jwt.DecodeError:
return self.async_abort(reason="oauth_error")
character_id = character_info[CONF_CHARACTER_ID]
character_name = character_info[CONF_CHARACTER_NAME]
await self.async_set_unique_id(str(character_id))
self._abort_if_unique_id_configured()
data[CONF_CHARACTER_ID] = character_id
data[CONF_CHARACTER_NAME] = character_name
return self.async_create_entry(
title=character_name,
data=data,
)
def _decode_eve_jwt(token: str) -> dict[str, Any]:
"""Decode an Eve SSO JWT to extract character info."""
decoded = jwt.decode(token, options={"verify_signature": False})
sub = decoded.get("sub", "")
sub_parts = sub.split(":")
if len(sub_parts) != 3 or sub_parts[0] != "CHARACTER" or sub_parts[1] != "EVE":
raise ValueError(sub)
return {
CONF_CHARACTER_ID: int(sub_parts[2]),
CONF_CHARACTER_NAME: decoded.get("name", "Unknown"),
}