|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +from django.conf import settings |
| 3 | +from authy.api import AuthyApiClient |
| 4 | + |
| 5 | +AUTHY_KEY = getattr(settings, 'AUTHY_KEY', None) |
| 6 | +AUTHY_FORCE_VERIFICATION = getattr(settings, 'AUTHY_FORCE_VERIFICATION', True) |
| 7 | + |
| 8 | +assert AUTHY_KEY, 'You must define a settings.AUTHY_KEY' |
| 9 | + |
| 10 | +from .signals import authy_event |
| 11 | + |
| 12 | +import logging |
| 13 | +logger = logging.getLogger('django.request') |
| 14 | + |
| 15 | + |
| 16 | +class AuthyService(object): |
| 17 | + """ |
| 18 | + Service for interacting with Authy |
| 19 | + """ |
| 20 | + user = None |
| 21 | + authy_profile = None |
| 22 | + |
| 23 | + client = None |
| 24 | + force_verification = False |
| 25 | + |
| 26 | + def __init__(self, user, *args, **kwargs): |
| 27 | + # Allow overrides |
| 28 | + self.user = user |
| 29 | + self.authy_profile = kwargs.get('authy_profile', user.authy_profile) # hope its passed in otherwise get it, efficent |
| 30 | + |
| 31 | + self.key = kwargs.get('key', AUTHY_KEY) |
| 32 | + self.force_verification = kwargs.get('force_verification', AUTHY_FORCE_VERIFICATION) |
| 33 | + |
| 34 | + self.client = AuthyApiClient(self.key) |
| 35 | + logger.info('Initialized authy.Client with key: %s' % self.key) |
| 36 | + |
| 37 | + self.ensure_user_registered() |
| 38 | + |
| 39 | + @property |
| 40 | + def authy_id(self): |
| 41 | + return int(self.authy_profile.authy_id) |
| 42 | + |
| 43 | + def ensure_user_registered(self): |
| 44 | + if self.authy_profile.authy_id is None: |
| 45 | + |
| 46 | + authy_user = self.client.users.create(self.user.email, |
| 47 | + int(self.authy_profile.cellphone.national_number), # phonenumberfield stores as long |
| 48 | + self.authy_profile.cellphone.country_code) #email, cellphone, area_code |
| 49 | + |
| 50 | + if authy_user.ok(): |
| 51 | + self.authy_profile.authy_id = authy_user.id |
| 52 | + self.authy_profile.save(update_fields=['authy_id']) |
| 53 | + |
| 54 | + else: |
| 55 | + errors = authy_user.errors() |
| 56 | + msg = 'Could not register Auth.user: %s' % errors |
| 57 | + logger.error(msg) |
| 58 | + raise Exception(msg) |
| 59 | + logger.info('Authy user: %s %s' % (self.user, self.authy_profile.authy_id)) |
| 60 | + return True |
| 61 | + |
| 62 | + def request_sms_token(self): |
| 63 | + if self.authy_profile.is_smartphone is True: |
| 64 | + sms = self.client.users.request_sms(self.authy_id) |
| 65 | + else: |
| 66 | + sms = self.client.users.request_sms(self.authy_id, {"force": True}) # force as the user is on an older phone with no app installed |
| 67 | + |
| 68 | + ok = sms.ok() |
| 69 | + return ok if ok is True else sms.errors() |
| 70 | + |
| 71 | + def verify_token(self, token): |
| 72 | + if type(token) not in [int]: |
| 73 | + raise Exception('Authy token must be an integer') |
| 74 | + |
| 75 | + verification = self.client.tokens.verify(self.authy_id, token, {"force": self.force_verification}) |
| 76 | + verified = verification.ok() |
| 77 | + errors = verification.errors() |
| 78 | + if not verified: |
| 79 | + logger.error('User: %s could not be verified using the token: %s due to: %s' % (self.user, token, errors)) |
| 80 | + return verified |
0 commit comments