Add OAuth 2.0 Bearer Token authentication to send_file for telegram_bot (#46567)

This commit is contained in:
Farzad Noorian 2021-06-29 04:43:23 +10:00 committed by GitHub
parent d4211c4a66
commit fbf85fd86b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 98 additions and 40 deletions

View file

@ -26,6 +26,7 @@ from homeassistant.const import (
CONF_API_KEY,
CONF_PLATFORM,
CONF_URL,
HTTP_BEARER_AUTHENTICATION,
HTTP_DIGEST_AUTHENTICATION,
)
from homeassistant.exceptions import TemplateError
@ -255,7 +256,9 @@ def load_data(
if url is not None:
# Load data from URL
params = {"timeout": 15}
if username is not None and password is not None:
if authentication == HTTP_BEARER_AUTHENTICATION and password is not None:
params["headers"] = {"Authorization": f"Bearer {password}"}
elif username is not None and password is not None:
if authentication == HTTP_DIGEST_AUTHENTICATION:
params["auth"] = HTTPDigestAuth(username, password)
else:

View file

@ -29,9 +29,9 @@ send_message:
selector:
select:
options:
- 'html'
- 'markdown'
- 'markdown2'
- "html"
- "markdown"
- "markdown2"
disable_notification:
name: Disable notification
description: Sends the message silently. iOS users and Web users will not receive a notification, Android users will receive a notification with no sound.
@ -65,7 +65,7 @@ send_message:
object:
message_tag:
name: Message tag
description: 'Tag for sent message. In telegram_sent event data: {{trigger.event.data.message_tag}}'
description: "Tag for sent message. In telegram_sent event data: {{trigger.event.data.message_tag}}"
example: "msg_to_edit"
selector:
text:
@ -94,16 +94,25 @@ send_photo:
text:
username:
name: Username
description: Username for a URL which require HTTP basic authentication.
description: Username for a URL which require HTTP authentication.
example: myuser
selector:
text:
password:
name: Password
description: Password for a URL which require HTTP basic authentication.
description: Password (or bearer token) for a URL which require HTTP authentication.
example: myuser_pwd
selector:
text:
authentication:
name: Authentication method
description: Define which authentication method to use. Set to `digest` to use HTTP digest authentication, or `bearer_token` for OAuth 2.0 bearer token authentication. Defaults to `basic`.
default: digest
selector:
select:
options:
- "digest"
- "bearer_token"
target:
name: Target
description: An array of pre-authorized chat_ids to send the document to. If not present, first allowed chat_id is the default.
@ -116,9 +125,9 @@ send_photo:
selector:
select:
options:
- 'html'
- 'markdown'
- 'markdown2'
- "html"
- "markdown"
- "markdown2"
disable_notification:
name: Disable notification
description: Sends the message silently. iOS users and Web users will not receive a notification, Android users will receive a notification with no sound.
@ -151,7 +160,7 @@ send_photo:
object:
message_tag:
name: Message tag
description: 'Tag for sent message. In telegram_sent event data: {{trigger.event.data.message_tag}}'
description: "Tag for sent message. In telegram_sent event data: {{trigger.event.data.message_tag}}"
example: "msg_to_edit"
selector:
text:
@ -174,16 +183,25 @@ send_sticker:
text:
username:
name: Username
description: Username for a URL which require HTTP basic authentication.
description: Username for a URL which require HTTP authentication.
example: myuser
selector:
text:
password:
name: Password
description: Password for a URL which require HTTP basic authentication.
description: Password (or bearer token) for a URL which require HTTP authentication.
example: myuser_pwd
selector:
text:
authentication:
name: Authentication method
description: Define which authentication method to use. Set to `digest` to use HTTP digest authentication, or `bearer_token` for OAuth 2.0 bearer token authentication. Defaults to `basic`.
default: digest
selector:
select:
options:
- "digest"
- "bearer_token"
target:
name: Target
description: An array of pre-authorized chat_ids to send the document to. If not present, first allowed chat_id is the default.
@ -222,7 +240,7 @@ send_sticker:
object:
message_tag:
name: Message tag
description: 'Tag for sent message. In telegram_sent event data: {{trigger.event.data.message_tag}}'
description: "Tag for sent message. In telegram_sent event data: {{trigger.event.data.message_tag}}"
example: "msg_to_edit"
selector:
text:
@ -251,16 +269,25 @@ send_animation:
text:
username:
name: Username
description: Username for a URL which require HTTP basic authentication.
description: Username for a URL which require HTTP authentication.
example: myuser
selector:
text:
password:
name: Password
description: Password for a URL which require HTTP basic authentication.
description: Password (or bearer token) for a URL which require HTTP authentication.
example: myuser_pwd
selector:
text:
authentication:
name: Authentication method
description: Define which authentication method to use. Set to `digest` to use HTTP digest authentication, or `bearer_token` for OAuth 2.0 bearer token authentication. Defaults to `basic`.
default: digest
selector:
select:
options:
- "digest"
- "bearer_token"
target:
name: Target
description: An array of pre-authorized chat_ids to send the document to. If not present, first allowed chat_id is the default.
@ -273,9 +300,9 @@ send_animation:
selector:
select:
options:
- 'html'
- 'markdown'
- 'markdown2'
- "html"
- "markdown"
- "markdown2"
disable_notification:
name: Disable notification
description: Sends the message silently. iOS users and Web users will not receive a notification, Android users will receive a notification with no sound.
@ -331,16 +358,25 @@ send_video:
text:
username:
name: Username
description: Username for a URL which require HTTP basic authentication.
description: Username for a URL which require HTTP authentication.
example: myuser
selector:
text:
password:
name: Password
description: Password for a URL which require HTTP basic authentication.
description: Password (or bearer token) for a URL which require HTTP authentication.
example: myuser_pwd
selector:
text:
authentication:
name: Authentication method
description: Define which authentication method to use. Set to `digest` to use HTTP digest authentication, or `bearer_token` for OAuth 2.0 bearer token authentication. Defaults to `basic`.
default: digest
selector:
select:
options:
- "digest"
- "bearer_token"
target:
name: Target
description: An array of pre-authorized chat_ids to send the document to. If not present, first allowed chat_id is the default.
@ -353,9 +389,9 @@ send_video:
selector:
select:
options:
- 'html'
- 'markdown'
- 'markdown2'
- "html"
- "markdown"
- "markdown2"
disable_notification:
name: Disable notification
description: Sends the message silently. iOS users and Web users will not receive a notification, Android users will receive a notification with no sound.
@ -388,7 +424,7 @@ send_video:
object:
message_tag:
name: Message tag
description: 'Tag for sent message. In telegram_sent event data: {{trigger.event.data.message_tag}}'
description: "Tag for sent message. In telegram_sent event data: {{trigger.event.data.message_tag}}"
example: "msg_to_edit"
selector:
text:
@ -417,16 +453,25 @@ send_voice:
text:
username:
name: Username
description: Username for a URL which require HTTP basic authentication.
description: Username for a URL which require HTTP authentication.
example: myuser
selector:
text:
password:
name: Password
description: Password for a URL which require HTTP basic authentication.
description: Password (or bearer token) for a URL which require HTTP authentication.
example: myuser_pwd
selector:
text:
authentication:
name: Authentication method
description: Define which authentication method to use. Set to `digest` to use HTTP digest authentication, or `bearer_token` for OAuth 2.0 bearer token authentication. Defaults to `basic`.
default: digest
selector:
select:
options:
- "digest"
- "bearer_token"
target:
name: Target
description: An array of pre-authorized chat_ids to send the document to. If not present, first allowed chat_id is the default.
@ -465,7 +510,7 @@ send_voice:
object:
message_tag:
name: Message tag
description: 'Tag for sent message. In telegram_sent event data: {{trigger.event.data.message_tag}}'
description: "Tag for sent message. In telegram_sent event data: {{trigger.event.data.message_tag}}"
example: "msg_to_edit"
selector:
text:
@ -494,16 +539,25 @@ send_document:
text:
username:
name: Username
description: Username for a URL which require HTTP basic authentication.
description: Username for a URL which require HTTP authentication.
example: myuser
selector:
text:
password:
name: Password
description: Password for a URL which require HTTP basic authentication.
description: Password (or bearer token) for a URL which require HTTP authentication.
example: myuser_pwd
selector:
text:
authentication:
name: Authentication method
description: Define which authentication method to use. Set to `digest` to use HTTP digest authentication, or `bearer_token` for OAuth 2.0 bearer token authentication. Defaults to `basic`.
default: digest
selector:
select:
options:
- "digest"
- "bearer_token"
target:
name: Target
description: An array of pre-authorized chat_ids to send the document to. If not present, first allowed chat_id is the default.
@ -516,9 +570,9 @@ send_document:
selector:
select:
options:
- 'html'
- 'markdown'
- 'markdown2'
- "html"
- "markdown"
- "markdown2"
disable_notification:
name: Disable notification
description: Sends the message silently. iOS users and Web users will not receive a notification, Android users will receive a notification with no sound.
@ -551,7 +605,7 @@ send_document:
object:
message_tag:
name: Message tag
description: 'Tag for sent message. In telegram_sent event data: {{trigger.event.data.message_tag}}'
description: "Tag for sent message. In telegram_sent event data: {{trigger.event.data.message_tag}}"
example: "msg_to_edit"
selector:
text:
@ -569,7 +623,7 @@ send_location:
min: -90
max: 90
step: 0.001
unit_of_measurement: '°'
unit_of_measurement: "°"
longitude:
name: Longitude
description: The longitude to send.
@ -579,7 +633,7 @@ send_location:
min: -180
max: 180
step: 0.001
unit_of_measurement: '°'
unit_of_measurement: "°"
target:
name: Target
description: An array of pre-authorized chat_ids to send the location to. If not present, first allowed chat_id is the default.
@ -613,7 +667,7 @@ send_location:
object:
message_tag:
name: Message tag
description: 'Tag for sent message. In telegram_sent event data: {{trigger.event.data.message_tag}}'
description: "Tag for sent message. In telegram_sent event data: {{trigger.event.data.message_tag}}"
example: "msg_to_edit"
selector:
text:
@ -654,9 +708,9 @@ edit_message:
selector:
select:
options:
- 'html'
- 'markdown'
- 'markdown2'
- "html"
- "markdown"
- "markdown2"
disable_web_page_preview:
name: Disable web page preview
description: Disables link previews for links in the message.

View file

@ -637,6 +637,7 @@ HTTP_BAD_GATEWAY: Final = 502
HTTP_SERVICE_UNAVAILABLE: Final = 503
HTTP_BASIC_AUTHENTICATION: Final = "basic"
HTTP_BEARER_AUTHENTICATION: Final = "bearer_token"
HTTP_DIGEST_AUTHENTICATION: Final = "digest"
HTTP_HEADER_X_REQUESTED_WITH: Final = "X-Requested-With"