-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
credentials.py
152 lines (116 loc) · 5.18 KB
/
credentials.py
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See LICENSE.txt in the project root for
# license information.
# -------------------------------------------------------------------------
from collections import namedtuple
from typing import TYPE_CHECKING
import six
if TYPE_CHECKING:
from typing import Any, NamedTuple
from typing_extensions import Protocol
AccessToken = NamedTuple("AccessToken", [("token", str), ("expires_on", int)])
class TokenCredential(Protocol):
"""Protocol for classes able to provide OAuth tokens.
:param str scopes: Lets you specify the type of access needed.
"""
# pylint:disable=too-few-public-methods
def get_token(self, *scopes, **kwargs):
# type: (*str, **Any) -> AccessToken
pass
else:
AccessToken = namedtuple("AccessToken", ["token", "expires_on"])
AzureNamedKey = namedtuple("AzureNamedKey", ["name", "key"])
__all__ = ["AzureKeyCredential", "AzureSasCredential", "AccessToken", "AzureNamedKeyCredential"]
class AzureKeyCredential(object):
"""Credential type used for authenticating to an Azure service.
It provides the ability to update the key without creating a new client.
:param str key: The key used to authenticate to an Azure service
:raises: TypeError
"""
def __init__(self, key):
# type: (str) -> None
if not isinstance(key, six.string_types):
raise TypeError("key must be a string.")
self._key = key # type: str
@property
def key(self):
# type () -> str
"""The value of the configured key.
:rtype: str
"""
return self._key
def update(self, key):
# type: (str) -> None
"""Update the key.
This can be used when you've regenerated your service key and want
to update long-lived clients.
:param str key: The key used to authenticate to an Azure service
:raises: ValueError or TypeError
"""
if not key:
raise ValueError("The key used for updating can not be None or empty")
if not isinstance(key, six.string_types):
raise TypeError("The key used for updating must be a string.")
self._key = key
class AzureSasCredential(object):
"""Credential type used for authenticating to an Azure service.
It provides the ability to update the shared access signature without creating a new client.
:param str signature: The shared access signature used to authenticate to an Azure service
:raises: TypeError
"""
def __init__(self, signature):
# type: (str) -> None
if not isinstance(signature, six.string_types):
raise TypeError("signature must be a string.")
self._signature = signature # type: str
@property
def signature(self):
# type () -> str
"""The value of the configured shared access signature.
:rtype: str
"""
return self._signature
def update(self, signature):
# type: (str) -> None
"""Update the shared access signature.
This can be used when you've regenerated your shared access signature and want
to update long-lived clients.
:param str signature: The shared access signature used to authenticate to an Azure service
:raises: ValueError or TypeError
"""
if not signature:
raise ValueError("The signature used for updating can not be None or empty")
if not isinstance(signature, six.string_types):
raise TypeError("The signature used for updating must be a string.")
self._signature = signature
class AzureNamedKeyCredential(object):
"""Credential type used for working with any service needing a named key that follows patterns
established by the other credential types.
:param str name: The name of the credential used to authenticate to an Azure service.
:param str key: The key used to authenticate to an Azure service.
:raises: TypeError
"""
def __init__(self, name, key):
# type: (str, str) -> None
if not isinstance(name, six.string_types) or not isinstance(key, six.string_types):
raise TypeError("Both name and key must be strings.")
self._credential = AzureNamedKey(name, key)
@property
def named_key(self):
# type () -> AzureNamedKey
"""The value of the configured name.
:rtype: AzureNamedKey
"""
return self._credential
def update(self, name, key):
# type: (str, str) -> None
"""Update the named key credential.
Both name and key must be provided in order to update the named key credential.
Individual attributes cannot be updated.
:param str name: The name of the credential used to authenticate to an Azure service.
:param str key: The key used to authenticate to an Azure service.
"""
if not isinstance(name, six.string_types) or not isinstance(key, six.string_types):
raise TypeError("Both name and key must be strings.")
self._credential = AzureNamedKey(name, key)