Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/azure/core/credentials.py: 53%

55 statements  

« prev     ^ index     » next       coverage.py v7.4.0, created at 2024-01-07 06:33 +0000

1# ------------------------------------------------------------------------- 

2# Copyright (c) Microsoft Corporation. All rights reserved. 

3# Licensed under the MIT License. See LICENSE.txt in the project root for 

4# license information. 

5# ------------------------------------------------------------------------- 

6from typing import Any, NamedTuple, Optional 

7from typing_extensions import Protocol, runtime_checkable 

8 

9 

10class AccessToken(NamedTuple): 

11 """Represents an OAuth access token.""" 

12 

13 token: str 

14 expires_on: int 

15 

16 

17AccessToken.token.__doc__ = """The token string.""" 

18AccessToken.expires_on.__doc__ = """The token's expiration time in Unix time.""" 

19 

20 

21@runtime_checkable 

22class TokenCredential(Protocol): 

23 """Protocol for classes able to provide OAuth tokens.""" 

24 

25 def get_token( 

26 self, 

27 *scopes: str, 

28 claims: Optional[str] = None, 

29 tenant_id: Optional[str] = None, 

30 enable_cae: bool = False, 

31 **kwargs: Any 

32 ) -> AccessToken: 

33 """Request an access token for `scopes`. 

34 

35 :param str scopes: The type of access needed. 

36 

37 :keyword str claims: Additional claims required in the token, such as those returned in a resource 

38 provider's claims challenge following an authorization failure. 

39 :keyword str tenant_id: Optional tenant to include in the token request. 

40 :keyword bool enable_cae: Indicates whether to enable Continuous Access Evaluation (CAE) for the requested 

41 token. Defaults to False. 

42 

43 :rtype: AccessToken 

44 :return: An AccessToken instance containing the token string and its expiration time in Unix time. 

45 """ 

46 ... 

47 

48 

49class AzureNamedKey(NamedTuple): 

50 """Represents a name and key pair.""" 

51 

52 name: str 

53 key: str 

54 

55 

56__all__ = [ 

57 "AzureKeyCredential", 

58 "AzureSasCredential", 

59 "AccessToken", 

60 "AzureNamedKeyCredential", 

61 "TokenCredential", 

62] 

63 

64 

65class AzureKeyCredential: 

66 """Credential type used for authenticating to an Azure service. 

67 It provides the ability to update the key without creating a new client. 

68 

69 :param str key: The key used to authenticate to an Azure service 

70 :raises: TypeError 

71 """ 

72 

73 def __init__(self, key: str) -> None: 

74 if not isinstance(key, str): 

75 raise TypeError("key must be a string.") 

76 self._key = key 

77 

78 @property 

79 def key(self) -> str: 

80 """The value of the configured key. 

81 

82 :rtype: str 

83 :return: The value of the configured key. 

84 """ 

85 return self._key 

86 

87 def update(self, key: str) -> None: 

88 """Update the key. 

89 

90 This can be used when you've regenerated your service key and want 

91 to update long-lived clients. 

92 

93 :param str key: The key used to authenticate to an Azure service 

94 :raises: ValueError or TypeError 

95 """ 

96 if not key: 

97 raise ValueError("The key used for updating can not be None or empty") 

98 if not isinstance(key, str): 

99 raise TypeError("The key used for updating must be a string.") 

100 self._key = key 

101 

102 

103class AzureSasCredential: 

104 """Credential type used for authenticating to an Azure service. 

105 It provides the ability to update the shared access signature without creating a new client. 

106 

107 :param str signature: The shared access signature used to authenticate to an Azure service 

108 :raises: TypeError 

109 """ 

110 

111 def __init__(self, signature: str) -> None: 

112 if not isinstance(signature, str): 

113 raise TypeError("signature must be a string.") 

114 self._signature = signature 

115 

116 @property 

117 def signature(self) -> str: 

118 """The value of the configured shared access signature. 

119 

120 :rtype: str 

121 :return: The value of the configured shared access signature. 

122 """ 

123 return self._signature 

124 

125 def update(self, signature: str) -> None: 

126 """Update the shared access signature. 

127 

128 This can be used when you've regenerated your shared access signature and want 

129 to update long-lived clients. 

130 

131 :param str signature: The shared access signature used to authenticate to an Azure service 

132 :raises: ValueError or TypeError 

133 """ 

134 if not signature: 

135 raise ValueError("The signature used for updating can not be None or empty") 

136 if not isinstance(signature, str): 

137 raise TypeError("The signature used for updating must be a string.") 

138 self._signature = signature 

139 

140 

141class AzureNamedKeyCredential: 

142 """Credential type used for working with any service needing a named key that follows patterns 

143 established by the other credential types. 

144 

145 :param str name: The name of the credential used to authenticate to an Azure service. 

146 :param str key: The key used to authenticate to an Azure service. 

147 :raises: TypeError 

148 """ 

149 

150 def __init__(self, name: str, key: str) -> None: 

151 if not isinstance(name, str) or not isinstance(key, str): 

152 raise TypeError("Both name and key must be strings.") 

153 self._credential = AzureNamedKey(name, key) 

154 

155 @property 

156 def named_key(self) -> AzureNamedKey: 

157 """The value of the configured name. 

158 

159 :rtype: AzureNamedKey 

160 :return: The value of the configured name. 

161 """ 

162 return self._credential 

163 

164 def update(self, name: str, key: str) -> None: 

165 """Update the named key credential. 

166 

167 Both name and key must be provided in order to update the named key credential. 

168 Individual attributes cannot be updated. 

169 

170 :param str name: The name of the credential used to authenticate to an Azure service. 

171 :param str key: The key used to authenticate to an Azure service. 

172 """ 

173 if not isinstance(name, str) or not isinstance(key, str): 

174 raise TypeError("Both name and key must be strings.") 

175 self._credential = AzureNamedKey(name, key)