Esempio n. 1
0
    def get_token_v2(self, username, platform, device_id, device_name,
                     client_version, platform_version):

        if platform in DESKTOP_PLATFORMS:
            # desktop device id is the peer id, so it must be 40 chars
            if len(device_id) != 40:
                raise serializers.ValidationError('invalid device id')

        elif platform == 'android':
            # android device id is the 64bit secure id, so it must be 16 chars in hex representation
            if len(device_id) != 16:
                raise serializers.ValidationError('invalid device id')
        elif platform == 'ios':
            if len(device_id) != 36:
                raise serializers.ValidationError('invalid device id')
        else:
            raise serializers.ValidationError('invalid platform')

        request = self.context['request']
        last_login_ip = get_client_ip(request)

        return TokenV2.objects.get_or_create_token(username, platform,
                                                   device_id, device_name,
                                                   client_version,
                                                   platform_version,
                                                   last_login_ip)
Esempio n. 2
0
    def get_token_v2(self, username, platform, device_id, device_name,
                     client_version, platform_version):

        if platform in DESKTOP_PLATFORMS:
            # desktop device id is the peer id, so it must be 40 chars
            if len(device_id) != 40:
                raise serializers.ValidationError('invalid device id')

        elif platform == 'android':
            # See http://developer.android.com/reference/android/provider/Settings.Secure.html#ANDROID_ID
            # android device id is the 64bit secure id, so it must be 16 chars in hex representation
            # but some user reports their device ids are 14 or 15 chars long. So we relax the validation.
            if not _ANDROID_DEVICE_ID_PATTERN.match(device_id.lower()):
                raise serializers.ValidationError('invalid device id')
        elif platform == 'ios':
            if len(device_id) != 36:
                raise serializers.ValidationError('invalid device id')
        else:
            raise serializers.ValidationError('invalid platform')

        request = self.context['request']
        last_login_ip = get_client_ip(request)

        return TokenV2.objects.get_or_create_token(username, platform, device_id, device_name,
                                                   client_version, platform_version, last_login_ip)
Esempio n. 3
0
    def get_token_v2(self, username, platform, device_id, device_name,
                     client_version, platform_version):

        if platform in DESKTOP_PLATFORMS:
            # desktop device id is the peer id, so it must be 40 chars
            if len(device_id) != 40:
                raise serializers.ValidationError('invalid device id')

        elif platform == 'android':
            # See http://developer.android.com/reference/android/provider/Settings.Secure.html#ANDROID_ID
            # android device id is the 64bit secure id, so it must be 16 chars in hex representation
            # but some user reports their device ids are 14 or 15 chars long. So we relax the validation.
            if not _ANDROID_DEVICE_ID_PATTERN.match(device_id.lower()):
                raise serializers.ValidationError('invalid device id')
        elif platform == 'ios':
            if len(device_id) != 36:
                raise serializers.ValidationError('invalid device id')
        else:
            raise serializers.ValidationError('invalid platform')

        request = self.context['request']
        last_login_ip = get_client_ip(request)

        return TokenV2.objects.get_or_create_token(username, platform,
                                                   device_id, device_name,
                                                   client_version,
                                                   platform_version,
                                                   last_login_ip)
Esempio n. 4
0
    def authenticate_v2(self, request, key):
        try:
            token = TokenV2.objects.get(key=key)
        except TokenV2.DoesNotExist:
            try:
                token = WipedDevice.objects.get(key=key)
            except WipedDevice.DoesNotExist:
                pass
            else:
                raise DeviceRemoteWipedException('Device set to be remote wiped')

            # Continue authentication in token v1
            return None

        try:
            user = User.objects.get(email=token.user)
        except User.DoesNotExist:
            raise AuthenticationFailed('User inactive or deleted')

        if MULTI_TENANCY:
            orgs = seaserv.get_orgs_by_user(token.user)
            if orgs:
                user.org = orgs[0]

        self._populate_user_permissions(user)

        if user.is_active:
            need_save = False

            # We update the device's last_login_ip, client_version, platform_version if changed
            ip = get_client_ip(request)
            if ip and ip != token.last_login_ip:
                token.last_login_ip = ip
                need_save = True

            client_version = request.META.get(HEADER_CLIENT_VERSION, '')
            if client_version and client_version != token.client_version:
                token.client_version = client_version
                need_save = True

            platform_version = request.META.get(HEADER_PLATFORM_VERSION, '')
            if platform_version and platform_version != token.platform_version:
                token.platform_version = platform_version
                need_save = True

            if not within_time_range(token.last_accessed, datetime.datetime.now(), 10 * 60):
                # We only need 10min precision for the last_accessed field
                need_save = True

            if need_save:
                try:
                    token.save()
                except:
                    logger.exception('error when save token v2:')

            return (user, token)
Esempio n. 5
0
    def authenticate_v2(self, request, key):
        try:
            token = TokenV2.objects.get(key=key)
        except TokenV2.DoesNotExist:
            # Continue authentication in token v1
            return None

        if token.wiped_at:
            raise DeviceRemoteWipedException('Device set to be remote wiped')

        try:
            user = User.objects.get(email=token.user)
        except User.DoesNotExist:
            raise AuthenticationFailed('User inactive or deleted')

        if MULTI_TENANCY:
            orgs = ccnet_api.get_orgs_by_user(token.user)
            if orgs:
                user.org = orgs[0]

        populate_user_permissions(user)

        if user.is_active:
            need_save = False

            # We update the device's last_login_ip, client_version, platform_version if changed
            ip = get_client_ip(request)
            if ip and ip != token.last_login_ip:
                token.last_login_ip = ip
                need_save = True

            client_version = request.META.get(HEADER_CLIENT_VERSION, '')
            if client_version and client_version != token.client_version:
                token.client_version = client_version
                need_save = True

            platform_version = request.META.get(HEADER_PLATFORM_VERSION, '')
            if platform_version and platform_version != token.platform_version:
                token.platform_version = platform_version
                need_save = True

            if not within_time_range(token.last_accessed,
                                     datetime.datetime.now(), 10 * 60):
                # We only need 10min precision for the last_accessed field
                need_save = True

            if need_save:
                try:
                    token.save()
                except:
                    logger.exception('error when save token v2:')

            return (user, token)
Esempio n. 6
0
    def authenticate_v2(self, request, key):
        try:
            token = TokenV2.objects.get(key=key)
        except TokenV2.DoesNotExist:
            return None

        try:
            user = User.objects.get(email=token.user)
        except User.DoesNotExist:
            return None

        if MULTI_TENANCY:
            orgs = seaserv.get_orgs_by_user(token.user)
            if orgs:
                user.org = orgs[0]

        self._populate_user_permissions(user)

        if user.is_active:
            need_save = False

            # We update the device's last_login_ip, client_version, platform_version if changed
            ip = get_client_ip(request)
            if ip and ip != token.last_login_ip:
                token.last_login_ip = ip
                need_save = True

            client_version = request.META.get(HEADER_CLIENT_VERSION, '')
            if client_version and client_version != token.client_version:
                token.client_version = client_version
                need_save = True

            platform_version = request.META.get(HEADER_PLATFORM_VERSION, '')
            if platform_version and platform_version != token.platform_version:
                token.platform_version = platform_version
                need_save = True

            if not within_ten_min(token.last_accessed,
                                  datetime.datetime.now()):
                # We only need 10min precision for the last_accessed field
                need_save = True

            if need_save:
                try:
                    token.save()
                except:
                    logger.exception('error when save token v2:')

            return (user, token)
Esempio n. 7
0
    def authenticate_v2(self, request, key):
        try:
            token = TokenV2.objects.get(key=key)
        except TokenV2.DoesNotExist:
            return None

        try:
            user = User.objects.get(email=token.user)
        except User.DoesNotExist:
            return None

        if MULTI_TENANCY:
            orgs = seaserv.get_orgs_by_user(token.user)
            if orgs:
                user.org = orgs[0]

        if user.is_active:
            need_save = False

            # We update the device's last_login_ip, client_version, platform_version if changed
            ip = get_client_ip(request)
            if ip and ip != token.last_login_ip:
                token.last_login_ip = ip
                need_save = True

            client_version = request.META.get(HEADER_CLIENT_VERSION, '')
            if client_version and client_version != token.client_version:
                token.client_version = client_version
                need_save = True

            platform_version = request.META.get(HEADER_PLATFORM_VERSION, '')
            if platform_version and platform_version != token.platform_version:
                token.platform_version = platform_version
                need_save = True

            if not within_ten_min(token.last_accessed, datetime.datetime.now()):
                # We only need 10min precision for the last_accessed field
                need_save = True

            if need_save:
                try:
                    token.save()
                except:
                    logger.exception('error when save token v2:')

            return (user, token)
Esempio n. 8
0
    def get_token_v2(self, username, platform, device_id, device_name,
                     client_version, platform_version):

        if platform in DESKTOP_PLATFORMS:
            # desktop device id is the peer id, so it must be 40 chars
            if len(device_id) != 40:
                raise serializers.ValidationError('invalid device id')

        elif platform == 'android':
            # android device id is the 64bit secure id, so it must be 16 chars in hex representation
            if len(device_id) != 16:
                raise serializers.ValidationError('invalid device id')
        elif platform == 'ios':
            if len(device_id) != 36:
                raise serializers.ValidationError('invalid device id')
        else:
            raise serializers.ValidationError('invalid platform')

        request = self.context['request']
        last_login_ip = get_client_ip(request)

        return TokenV2.objects.get_or_create_token(username, platform, device_id, device_name,
                                                   client_version, platform_version, last_login_ip)