8000 Add typings to auth stack · openvelora/botbuilder-python@d3c0499 · GitHub
[go: up one dir, main page]

Skip to content

Commit d3c0499

Browse files
committed
Add typings to auth stack
1 parent 935a85f commit d3c0499

File tree

7 files changed

+35
-27
lines changed

7 files changed

+35
-27
lines changed

libraries/botframework-connector/microsoft/botframework/connector/auth/channel_validation.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
from .verify_options import VerifyOptions
44
from .constants import Constants
55
from .jwt_token_extractor import JwtTokenExtractor
6+
from .claims_identity import ClaimsIdentity
7+
from .credential_provider import CredentialProvider
68

79
class ChannelValidation:
810
# This claim is ONLY used in the Channel Validation, and not in the emulator validation
@@ -20,7 +22,7 @@ class ChannelValidation:
2022
)
2123

2224
@staticmethod
23-
async def authenticate_token_service_url(auth_header, credentials, service_url):
25+
async def authenticate_token_service_url(auth_header: str, credentials: CredentialProvider, service_url: str) -> ClaimsIdentity:
2426
""" V 8000 alidate the incoming Auth Header
2527
2628
Validate the incoming Auth Header as a token sent from the Bot Framework Service.
@@ -47,7 +49,7 @@ async def authenticate_token_service_url(auth_header, credentials, service_url):
4749
return identity
4850

4951
@staticmethod
50-
async def authenticate_token(auth_header, credentials):
52+
async def authenticate_token(auth_header: str, credentials: CredentialProvider) -> ClaimsIdentity:
5153
""" Validate the incoming Auth Header
5254
5355
Validate the incoming Auth Header as a token sent from the Bot Framework Service.
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
class Claim:
2-
def __init__(self, claim_type, value):
2+
def __init__(self, claim_type: str, value):
33
self.type = claim_type
44
self.value = value
55

66
class ClaimsIdentity:
7-
def __init__(self, claims, isAuthenticated):
7+
def __init__(self, claims: dict, isAuthenticated: bool):
88
self.claims = claims
99
self.isAuthenticated = isAuthenticated
1010

11-
def get_claim_value(self, claim_type):
11+
def get_claim_value(self, claim_type: str):
1212
return self.claims.get(claim_type)

libraries/botframework-connector/microsoft/botframework/connector/auth/credential_provider.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class CredentialProvider:
77
appid/password pair is valid.
88
"""
99

10-
async def is_valid_appid(self, app_id):
10+
async def is_valid_appid(self, app_id: str) -> bool:
1111
"""Validate AppId.
1212
1313
This method is async to enable custom implementations
@@ -18,7 +18,7 @@ async def is_valid_appid(self, app_id):
1818
"""
1919
raise NotImplementedError
2020

21-
async def get_app_password(self, app_id):
21+
async def get_app_password(self, app_id: str) -> str:
2222
"""Get the app password for a given bot appId, if it is not a valid appId, return Null
2323
2424
This method is async to enable custom implementations
@@ -29,7 +29,7 @@ async def get_app_password(self, app_id):
2929
"""
3030
raise NotImplementedError
3131

32-
async def is_authentication_disabled(self):
32+
async def is_authentication_disabled(self) -> bool:
3333
"""Checks if bot authentication is disabled.
3434
3535
Return true if bot authentication is disabled.
@@ -41,15 +41,15 @@ async def is_authentication_disabled(self):
4141
raise NotImplementedError
4242

4343
class SimpleCredentialProvider(CredentialProvider):
44-
def __init__(self, app_id, password):
44+
def __init__(self, app_id: str, password: str):
4545
self.app_id = app_id
4646
self.password = password
4747

48-
async def is_valid_appid(self, app_id):
48+
async def is_valid_appid(self, app_id: str) -> bool:
4949
return self.app_id == app_id
5050

51-
async def get_app_password(self, app_id):
51+
async def get_app_password(self, app_id: str) -> str:
5252
return self.password if self.app_id == app_id else None
5353

54-
async def is_authentication_disabled(self):
54+
async def is_authentication_disabled(self) -> bool:
5555
return not self.app_id

libraries/botframework-connector/microsoft/botframework/connector/auth/emulator_validation.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
from .jwt_token_extractor import JwtTokenExtractor
55
from .verify_options import VerifyOptions
66
from .constants import Constants
7+
from .credential_provider import CredentialProvider
8+
from .claims_identity import ClaimsIdentity
79

810
class EmulatorValidation:
911
APP_ID_CLAIM = "appid"
@@ -28,7 +30,7 @@ class EmulatorValidation:
2830
)
2931

3032
@staticmethod
31-
def is_token_from_emulator(auth_header):
33+
def is_token_from_emulator(auth_header: str) -> bool:
3234
""" Determines if a given Auth header is from the Bot Framework Emulator
3335
3436
:param auth_header: Bearer Token, in the 'Bearer [Long String]' Format.
@@ -79,7 +81,7 @@ def is_token_from_emulator(auth_header):
7981
return True
8082

8183
@staticmethod
82-
async def authenticate_emulator_token(auth_header, credentials):
84+
async def authenticate_emulator_token(auth_header: str, credentials: CredentialProvider) -> ClaimsIdentity:
8385
""" Validate the incoming Auth Header
8486
8587
Validate the incoming Auth Header as a token sent from the Bot Framework Service.

libraries/botframework-connector/microsoft/botframework/connector/auth/jwt_token_extractor.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,34 @@
55
from jwt.algorithms import RSAAlgorithm
66
import jwt
77
from .claims_identity import ClaimsIdentity
8+
from .verify_options import VerifyOptions
89

910
class JwtTokenExtractor:
1011
metadataCache = {}
1112

12-
def __init__(self, validationParams, metadata_url, allowedAlgorithms, validator=None):
13+
def __init__(self, validationParams: VerifyOptions, metadata_url: str, allowedAlgorithms: list, validator=None):
1314
self.validation_parameters = validationParams
1415
self.validation_parameters.algorithms = allowedAlgorithms
1516
self.open_id_metadata = JwtTokenExtractor.get_open_id_metadata(metadata_url)
1617
self.validator = validator if validator is not None else lambda x: True
1718

1819
@staticmethod
19-
def get_open_id_metadata(metadata_url):
20+
def get_open_id_metadata(metadata_url: str):
2021
metadata = JwtTokenExtractor.metadataCache.get(metadata_url, None)
2122
if metadata is None:
2223
metadata = _OpenIdMetadata(metadata_url)
2324
JwtTokenExtractor.metadataCache.setdefault(metadata_url, metadata)
2425
return metadata
2526

26-
async def get_identity_from_auth_header(self, auth_header):
27+
async def get_identity_from_auth_header(self, auth_header: str) -> ClaimsIdentity:
2728
if not auth_header:
2829
return None
2930
parts = auth_header.split(" ")
3031
if len(parts) == 2:
3132
return await self.get_identity(parts[0], parts[1])
3233
return None
3334

34-
async def get_identity(self, schema, parameter):
35+
async def get_identity(self, schema: str, parameter: str) -> ClaimsIdentity:
3536
# No header in correct scheme or no token
3637
if schema != "Bearer" or not parameter:
3738
return None
@@ -45,15 +46,15 @@ async def get_identity(self, schema, parameter):
4546
except:
4647
raise
4748

48-
def _has_allowed_issuer(self, jwt_token):
49+
def _has_allowed_issuer(self, jwt_token: str) -> bool:
4950
decoded = jwt.decode(jwt_token, verify=False)
5051
issuer = decoded.get("iss", None)
5152
if issuer in self.validation_parameters.issuer:
5253
return True
5354

5455
return issuer is self.validation_parameters.issuer
5556

56-
async def _validate_token(self, jwt_token):
57+
async def _validate_token(self, jwt_token: str) -> ClaimsIdentity:
5758
headers = jwt.get_unverified_header(jwt_token)
5859

5960
# Update the signing tokens from the last refresh
@@ -82,7 +83,7 @@ def __init__(self, url):
8283
self.keys = []
8384
self.last_updated = datetime.min
8485

85-
async def get(self, key_id):
86+
async def get(self, key_id: str):
8687
# If keys are more than 5 days old, refresh them
8788
if self.last_updated < (datetime.now() + timedelta(days=5)):
8889
await self._refresh()
@@ -97,7 +98,7 @@ async def _refresh(self):
9798
self.last_updated = datetime.now()
9899
self.keys = response_keys.json()["keys"]
99100

100-
def _find(self, key_id):
101+
def _find(self, key_id: str):
101102
if not self.keys:
102103
return None
103104
key = next(x for x in self.keys if x["kid"] == key_id)

libraries/botframework-connector/microsoft/botframework/connector/auth/jwt_token_validation.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1+
from microsoft.botbuilder.schema import Activity
2+
13
from .emulator_validation import EmulatorValidation
24
from .channel_validation import ChannelValidation
35
from .microsoft_app_credentials import MicrosoftAppCredentials
6+
from .credential_provider import CredentialProvider
47

58
class JwtTokenValidation:
69

710
@staticmethod
8-
async def assert_valid_activity(activity, auth_header, credentials):
11+
async def assert_valid_activity(activity: Activity, auth_header: str, credentials: CredentialProvider):
912
"""Validates the security tokens required by the Bot Framework Protocol. Throws on any exceptions.
1013
1114
:param activity: The incoming Activity from the Bot Framework or the Emulator

libraries/botframework-connector/microsoft/botframework/connector/auth/microsoft_app_credentials.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class MicrosoftAppCredentials(Authentication):
4848
trustedHostNames = {}
4949
cache = {}
5050

51-
def __init__(self, appId, password):
51+
def __init__(self, appId: str, password: str):
5252
self.microsoft_app_id = appId
5353
self.microsoft_app_password = password
5454
self.token_cache_key = appId + '-cache'
@@ -90,21 +90,21 @@ def refresh_token(self):
9090
return oauth_response
9191

9292
@staticmethod
93-
def trust_service_url(service_url, expiration=None):
93+
def trust_service_url(service_url: str, expiration=None):
9494
if expiration is None:
9595
expiration = datetime.now() + timedelta(days=1)
9696
host = urlparse(service_url).hostname
9797
if host is not None:
9898
MicrosoftAppCredentials.trustedHostNames[host] = expiration
9999

100100
@staticmethod
101-
def is_trusted_service(service_url):
101+
def is_trusted_service(service_url: str) -> bool:
102102
host = urlparse(service_url).hostname
103103
if host is not None:
104104
return MicrosoftAppCredentials.is_trusted_url(host)
105105
return False
106106

107107
@staticmethod
108-
def is_trusted_url(host):
108+
def is_trusted_url(host: str) -> bool:
109109
expiration = MicrosoftAppCredentials.trustedHostNames.get(host, datetime.min)
110110
return expiration > (datetime.now() - timedelta(minutes=5))

0 commit comments

Comments
 (0)
0