(self, params, **kwargs)
| 92 | return TfaAddCommand.parser |
| 93 | |
| 94 | def execute(self, params, **kwargs): |
| 95 | all_tfa_restrictions = {'require_security_key_pin', 'restrict_two_factor_channel_text', |
| 96 | 'restrict_two_factor_channel_google', 'restrict_two_factor_channel_duo', |
| 97 | 'restrict_two_factor_channel_security_key', 'restrict_two_factor_channel_rsa', |
| 98 | 'restrict_two_factor_channel_dna'} |
| 99 | tfa_restrictions = set() |
| 100 | if params.enforcements and 'booleans' in params.enforcements: |
| 101 | for x in params.enforcements['booleans']: |
| 102 | key = (x.get('key') or '').lower() |
| 103 | if key in all_tfa_restrictions: |
| 104 | tfa_restrictions.add(key) |
| 105 | |
| 106 | method = kwargs.get('method') |
| 107 | rq = APIRequest_pb2.TwoFactorAddRequest() |
| 108 | rq.channel_uid = utils.base64_url_decode(utils.generate_uid()) |
| 109 | rq.channelName = kwargs.get('name') or '' |
| 110 | |
| 111 | if method == 'totp': |
| 112 | if 'restrict_two_factor_channel_google' in tfa_restrictions: |
| 113 | raise CommandError('', 'Authenticator App (TOTP) 2FA method is disabled by the Administrator') |
| 114 | rq.channelType = APIRequest_pb2.TWO_FA_CT_TOTP |
| 115 | elif method == 'sms': |
| 116 | if 'restrict_two_factor_channel_text' in tfa_restrictions: |
| 117 | raise CommandError('', 'Text Message (SMS) 2FA method is disabled by the Administrator') |
| 118 | rq.channelType = APIRequest_pb2.TWO_FA_CT_SMS |
| 119 | elif method == 'key': |
| 120 | if 'restrict_two_factor_channel_security_key' in tfa_restrictions: |
| 121 | raise CommandError('', 'Security Key 2FA method is disabled by the Administrator') |
| 122 | rq.channelType = APIRequest_pb2.TWO_FA_CT_WEBAUTHN |
| 123 | elif method == 'duo': |
| 124 | rq.channelType = APIRequest_pb2.TWO_FA_CT_DUO |
| 125 | elif method == 'backup': |
| 126 | rq.channelType = APIRequest_pb2.TWO_FA_CT_BACKUP |
| 127 | else: |
| 128 | raise CommandError('2fa add', f'2FA method "{method}" is not supported') |
| 129 | |
| 130 | # Input |
| 131 | if rq.channelType == APIRequest_pb2.TWO_FA_CT_SMS: |
| 132 | try: |
| 133 | phone_number = input('\nEnter your phone number for text messages: ') |
| 134 | if not phone_number: |
| 135 | return |
| 136 | rq.phoneNumber = phone_number |
| 137 | except KeyboardInterrupt: |
| 138 | return |
| 139 | |
| 140 | elif rq.channelType == APIRequest_pb2.TWO_FA_CT_DUO: |
| 141 | duo_rs = api.communicate_rest(params, None, 'authentication/2fa_duo_status', |
| 142 | rs_type=APIRequest_pb2.TwoFactorDuoStatus) |
| 143 | if duo_rs.enroll_url: |
| 144 | logging.warning(duo_rs.message) |
| 145 | logging.warning("Enroll URL") |
| 146 | print(duo_rs.enroll_url) |
| 147 | return |
| 148 | capabilities = [x for x in duo_rs.capabilities if x in ('mobile_otp', 'sms', 'voice')] |
| 149 | print(f'Device Phone Number: {duo_rs.phoneNumber}') |
| 150 | if len(capabilities) == 0: |
| 151 | return |
nothing calls this directly
no test coverage detected