Skip to content

Commit

Permalink
Fix #3 - Usage of permanent API Key
Browse files Browse the repository at this point in the history
  • Loading branch information
elad-bar committed May 29, 2021
1 parent cac1a96 commit b3a8b7d
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 26 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 1.0.7

- Added code protection, logs and documentation for API Key usage [#3](https://github.com/elad-bar/ha-shinobi/issues/3)

## v1.0.6

- More instructions in README
Expand Down
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,14 @@ Integration with Shinobi Video NVR. Creates the following components:
## How to

#### Requirements
- Shinobi Video Server available with credentials
- Shinobi Video Server available with credentials - Dashboard user with API Key
- MQTT Integration is optional - it will allow listening to Shinobi Video event

#### How to generate permanent API Key:
In Shinobi Video Dashboard, click your username in the top left.
A menu will appear, click API.
Add new token - IP: 0.0.0.0, Permissions - Select all

#### Shinobi Video links:
- [Using MQTT to receive and trigger events](https://hub.shinobi.video/articles/view/xEMps3O4y4VEaYk)
- [How to use Motion Detection](https://hub.shinobi.video/articles/view/LKdcgcgWy9RJfUh)
Expand Down
53 changes: 30 additions & 23 deletions custom_components/shinobi/api/shinobi_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from homeassistant.core import HomeAssistant
from homeassistant.helpers.aiohttp_client import async_create_clientsession

from . import APIRequestException
from ..helpers.const import *
from ..managers.configuration_manager import ConfigManager
from ..models.camera_data import CameraData
Expand Down Expand Up @@ -93,10 +92,10 @@ async def initialize(self):
def build_url(self, endpoint):
url = f"{self.base_url}/{endpoint}"

if GROUP_ID in url:
if GROUP_ID in url and self.group_id is not None:
url = url.replace(GROUP_ID, self.group_id)

if AUTH_TOKEN in url:
if AUTH_TOKEN in url and self.api_key is not None:
url = url.replace(AUTH_TOKEN, self.api_key)

return url
Expand Down Expand Up @@ -172,37 +171,45 @@ async def login(self):
}

login_data = await self.async_post(URL_LOGIN, data)
user_data = login_data.get("$user")

if user_data.get("ok", False):
self.group_id = user_data.get("ke")
self.api_key = user_data.get("auth_token")
uid = user_data.get("uid")
if login_data is not None:
user_data = login_data.get("$user", {})

_LOGGER.debug(f"Auth token: {self.api_key}")
if user_data.get("ok", False):
self.group_id = user_data.get("ke")
temp_api_key = user_data.get("auth_token")
uid = user_data.get("uid")

api_keys_data = await self.async_get(URL_API_KEYS)
_LOGGER.debug(f"Temporary auth token: {temp_api_key}")

if api_keys_data.get("ok", False):
keys = api_keys_data.get("keys", [])
self.api_key = temp_api_key

for key in keys:
key_uid = key.get("uid")
api_keys_data = await self.async_get(URL_API_KEYS)

if key_uid == uid:
self.api_key = key.get("code")
self.api_key = None

_LOGGER.debug(f"Access token: {self.api_key}")
if api_keys_data is not None and api_keys_data.get("ok", False):
keys = api_keys_data.get("keys", [])

break
else:
self.api_key = None
raise APIRequestException(URL_API_KEYS, login_data)
for key in keys:
key_uid = key.get("uid", None)

else:
raise APIRequestException(URL_LOGIN, login_data)
if key_uid is not None and key_uid == uid:
self.api_key = key.get("code")

_LOGGER.debug(f"Permanent access token: {self.api_key}")

break

if self.api_key is None:
error_message = "Failed to get permanent API Key"
error_instructions = f"please go to Shinobi Video Dashboard and add API for `{config_data.username}`"

_LOGGER.error(f"{error_message}, {error_instructions}")

except Exception as ex:
self.api_key = None

exc_type, exc_obj, tb = sys.exc_info()
line_number = tb.tb_lineno

Expand Down
2 changes: 1 addition & 1 deletion custom_components/shinobi/helpers/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
CONF_HOST,
CONF_NAME,
CONF_PASSWORD,
CONF_PORT,
CONF_PATH,
CONF_PORT,
CONF_SSL,
CONF_USERNAME,
CONF_VERIFY_SSL,
Expand Down
7 changes: 6 additions & 1 deletion info.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,14 @@ Integration with Shinobi Video NVR. Creates the following components:
## How to

#### Requirements
- Shinobi Video Server available with credentials
- Shinobi Video Server available with credentials - Dashboard user with API Key
- MQTT Integration is optional - it will allow listening to Shinobi Video event

#### How to generate permanent API Key:
In Shinobi Video Dashboard, click your username in the top left.
A menu will appear, click API.
Add new token - IP: 0.0.0.0, Permissions - Select all

#### Shinobi Video links:
- [Using MQTT to receive and trigger events](https://hub.shinobi.video/articles/view/xEMps3O4y4VEaYk)
- [How to use Motion Detection](https://hub.shinobi.video/articles/view/LKdcgcgWy9RJfUh)
Expand Down

0 comments on commit b3a8b7d

Please sign in to comment.