-
Notifications
You must be signed in to change notification settings - Fork 331
/
Copy pathmulti_factor_config_mgt.py
222 lines (185 loc) · 7.52 KB
/
multi_factor_config_mgt.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# Copyright 2024 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Firebase multifactor configuration management module.
This module contains functions for managing multifactor auth configuration at
the project and tenant level.
"""
from enum import Enum
from typing import List
__all__ = [
'validate_keys',
'MultiFactorServerConfig',
'TOTPProviderConfig',
'ProviderConfig',
'MultiFactorConfig',
]
def validate_keys(keys, valid_keys, config_name):
for key in keys:
if key not in valid_keys:
raise ValueError(
'"{0}" is not a valid "{1}" parameter.'.format(
key, config_name))
class MultiFactorServerConfig:
"""Represents the multi-factor configuration response received from the server.
"""
def __init__(self, data):
if not isinstance(data, dict):
raise ValueError(
'Invalid data argument in MultiFactorServerConfig constructor: {0}, must be a valid'
' dict'.format(data))
self._data = data
@property
def provider_configs(self):
data = self._data.get('providerConfigs', None)
if data is not None:
return [self.ProviderServerConfig(d) for d in data]
return None
class ProviderServerConfig:
"""Represents the provider configuration response received from the server.
"""
def __init__(self, data):
if not isinstance(data, dict):
raise ValueError(
'Invalid data argument in ProviderServerConfig constructor: {0}'.format(data))
self._data = data
@property
def state(self):
return self._data.get('state', None)
@property
def totp_provider_config(self):
data = self._data.get('totpProviderConfig', None)
if data is not None:
return self.TOTPProviderServerConfig(data)
return None
class TOTPProviderServerConfig:
"""Represents the TOTP provider configuration response received from the server.
"""
def __init__(self, data):
if not isinstance(data, dict):
raise ValueError(
'Invalid data argument in TOTPProviderServerConfig'
' constructor: {0}'.format(data))
self._data = data
@property
def adjacent_intervals(self):
return self._data.get('adjacentIntervals', None)
class TOTPProviderConfig:
"""A tenant or project's TOTP provider configuration."""
def __init__(self, adjacent_intervals: int = None):
self.adjacent_intervals: int = adjacent_intervals
def to_dict(self) -> dict:
data = {}
if self.adjacent_intervals is not None:
data['adjacentIntervals'] = self.adjacent_intervals
return data
def validate(self):
"""Validates the configuration.
Raises:
ValueError: In case of an unsuccessful validation.
"""
validate_keys(
keys=vars(self).keys(),
valid_keys={'adjacent_intervals'},
config_name='TOTPProviderConfig')
if self.adjacent_intervals is not None:
# Because bool types get converted to int here
# pylint: disable=C0123
if type(self.adjacent_intervals) is not int:
raise ValueError(
'totp_provider_config.adjacent_intervals must be an integer between'
' 1 and 10 (inclusive).')
if not 1 <= self.adjacent_intervals <= 10:
raise ValueError(
'totp_provider_config.adjacent_intervals must be an integer between'
' 1 and 10 (inclusive).')
def build_server_request(self):
self.validate()
return self.to_dict()
class ProviderConfig:
"""A tenant or project's multifactor provider configuration.
Currently, only TOTP can be configured."""
class State(Enum):
ENABLED = 'ENABLED'
DISABLED = 'DISABLED'
def __init__(self,
state: State = None,
totp_provider_config: TOTPProviderConfig = None):
self.state: self.State = state
self.totp_provider_config: TOTPProviderConfig = totp_provider_config
def to_dict(self) -> dict:
data = {}
if self.state:
data['state'] = self.state.value
if self.totp_provider_config:
data['totpProviderConfig'] = self.totp_provider_config.to_dict()
return data
def validate(self):
"""Validates the provider configuration.
Raises:
ValueError: In case of an unsuccessful validation.
"""
validate_keys(
keys=vars(self).keys(),
valid_keys={
'state',
'totp_provider_config'},
config_name='ProviderConfig')
if self.state is None:
raise ValueError('ProviderConfig.state must be defined.')
if not isinstance(self.state, ProviderConfig.State):
raise ValueError(
'ProviderConfig.state must be of type ProviderConfig.State.')
if self.totp_provider_config is None:
raise ValueError(
'ProviderConfig.totp_provider_config must be defined.')
if not isinstance(self.totp_provider_config, TOTPProviderConfig):
raise ValueError(
'ProviderConfig.totp_provider_config must be of type TOTPProviderConfig.')
def build_server_request(self):
self.validate()
return self.to_dict()
class MultiFactorConfig:
"""A tenant or project's multi factor configuration."""
def __init__(self,
provider_configs: List[ProviderConfig] = None):
self.provider_configs: List[ProviderConfig] = provider_configs
def to_dict(self) -> dict:
data = {}
if self.provider_configs is not None:
data['providerConfigs'] = [d.to_dict()
for d in self.provider_configs]
return data
def validate(self):
"""Validates the configuration.
Raises:
ValueError: In case of an unsuccessful validation.
"""
validate_keys(
keys=vars(self).keys(),
valid_keys={'provider_configs'},
config_name='MultiFactorConfig')
if self.provider_configs is None:
raise ValueError(
'multi_factor_config.provider_configs must be specified')
if not isinstance(self.provider_configs, list) or not self.provider_configs:
raise ValueError(
'provider_configs must be an array of type ProviderConfig.')
for provider_config in self.provider_configs:
if not isinstance(provider_config, ProviderConfig):
raise ValueError(
'provider_configs must be an array of type ProviderConfig.')
provider_config.validate()
def build_server_request(self):
self.validate()
return self.to_dict()