def activate(self, request): validator = Validator(request) token = validator.token() check_validator(validator) with self.create_storage() as storage: user = storage.get_user_by_activate_token(token) if not user: raise servicesexceptions.bad_request('Invalid activation token') if user.active: raise servicesexceptions.bad_request('User is active already') user.active = True return True
def domain_update(self, request, request_ip=None): validator = Validator(request) token = validator.token() ip = validator.ip(request_ip) local_ip = validator.local_ip() map_local_address = validator.boolean('map_local_address', required=False) platform_version = validator.string('platform_version', required=False) web_protocol = validator.web_protocol(required=True) web_local_port = validator.port('web_local_port', required=True) web_port = validator.port('web_port', required=False) check_validator(validator) if map_local_address is None: map_local_address = False with self.create_storage() as storage: domain = storage.get_domain_by_update_token(token) if not domain or not domain.user.active: raise servicesexceptions.bad_request('Unknown domain update token') update_ip = (domain.map_local_address != map_local_address) or (domain.ip != ip) or (domain.local_ip != local_ip) domain.ip = ip domain.local_ip = local_ip domain.map_local_address = map_local_address domain.platform_version = platform_version domain.web_protocol = web_protocol domain.web_local_port = web_local_port domain.web_port = web_port if update_ip: self.dns.update_domain(self.main_domain, domain) domain.last_update = datetime.now() return domain
def port_probe(self, request, request_ip): validator = Validator(request) token = validator.token() port = validator.port('port', True) protocol = validator.string('protocol', False) ip = validator.string('ip', False) check_validator(validator) domain = None with self.create_storage() as storage: domain = storage.get_domain_by_update_token(token) if not domain or not domain.user.active: raise servicesexceptions.bad_request('Unknown domain update token') try: if ip: request_ip = ip response = requests.get('{0}://{1}:{2}/ping'.format(protocol, request_ip, port), timeout=1, verify=False, allow_redirects=False) if response.status_code == 200: return {'message': response.text, 'device_ip': request_ip}, 200 # TODO: Remove after release 18.07 reaches wider adoption elif response.status_code == 301 and protocol == 'http': return {'message': "OK", 'device_ip': request_ip}, 200 except Exception, e: pass
def port_probe(self, request, request_ip): validator = Validator(request) token = validator.token() port = validator.port('port', True) protocol = validator.string('protocol', False) ip = validator.string('ip', False) check_validator(validator) domain = None with self.create_storage() as storage: domain = storage.get_domain_by_update_token(token) if not domain or not domain.user.active: raise servicesexceptions.bad_request('Unknown domain update token') try: if ip: request_ip = ip response = requests.get('{0}://{1}:{2}/ping'.format( protocol, request_ip, port), timeout=1, verify=False, allow_redirects=False) if response.status_code == 200: return {'message': response.text, 'device_ip': request_ip}, 200 # TODO: Remove after release 18.07 reaches wider adoption elif response.status_code == 301 and protocol == 'http': return {'message': "OK", 'device_ip': request_ip}, 200 except Exception, e: pass
def get_domain(self, request): validator = Validator(request) token = validator.token() with self.create_storage() as storage: domain = storage.get_domain_by_update_token(token) if not domain or not domain.user.active: raise servicesexceptions.bad_request('Unknown domain update token') return domain
def activate(self, request): validator = Validator(request) token = validator.token() check_validator(validator) with self.create_storage() as storage: user = storage.get_user_by_activate_token(token) if not user: raise servicesexceptions.bad_request( 'Invalid activation token') if user.active: raise servicesexceptions.bad_request('User is active already') user.active = True return True
def get_domain(self, request): validator = Validator(request) token = validator.token() with self.create_storage() as storage: domain = storage.get_domain_by_update_token(token) if not domain or not domain.user.active: raise servicesexceptions.bad_request( 'Unknown domain update token') return domain
def user_log(self, request): validator = Validator(request) token = validator.token() data = validator.string('data') include_support = validator.boolean('include_support', False, True) with self.create_storage() as storage: user = storage.get_user_by_update_token(token) if not user: raise servicesexceptions.bad_request('Invalid update token') self.mail.send_logs(user.email, data, include_support)
def do_user_domain_delete(self, user_domain): with self.create_storage() as storage: domain = storage.get_domain_by_name(user_domain) if not domain: raise servicesexceptions.bad_request('Unknown domain') self.dns.delete_domain(self.main_domain, domain) storage.delete_domain(domain)
def user_set_subscribed(self, request, user_email): validator = Validator(request) subscribed = validator.boolean('subscribed', required=True) check_validator(validator) with self.create_storage() as storage: user = storage.get_user_by_email(user_email) if not user: raise servicesexceptions.bad_request('Unknown user') user.unsubscribed = not subscribed
def do_delete_user(self, email): with self.create_storage() as storage: user = storage.get_user_by_email(email) if not user: raise servicesexceptions.bad_request('Authentication failed') for domain in user.domains: self.dns.delete_domain(self.main_domain, domain) storage.delete_user(user)
def activate(self, request): validator = Validator(request) token = validator.token() errors = validator.errors if errors: message = ", ".join(errors) raise servicesexceptions.bad_request(message) with self.create_storage() as storage: user = storage.get_user_by_activate_token(token) if not user: raise servicesexceptions.bad_request('Invalid activation token') if user.active: raise servicesexceptions.conflict('User is active already') user.active = True return True
def authenticate(self, request): validator = Validator(request) email = validator.email() password = validator.password() check_validator(validator) user = self.get_user(email) if not user or not user.active or not util.hash(password) == user.password_hash: raise servicesexceptions.bad_request('Authentication failed') return user
def domain_update(self, request, request_ip=None): validator = Validator(request) token = validator.token() ip = validator.ip(request_ip) errors = validator.errors if errors: message = ", ".join(errors) raise servicesexceptions.bad_request(message) with self.create_storage() as storage: domain = storage.get_domain_by_update_token(token) if not domain or not domain.user.active: raise servicesexceptions.bad_request('Unknown domain update token') map(self.validate_service, request['services']) request_services = [new_service_from_dict(s) for s in request['services']] added_services = self.get_missing(request_services, domain.services) removed_services = self.get_missing(domain.services, request_services) storage.delete(removed_services) for s in added_services: s.domain = domain domain.services.append(s) storage.add(added_services) is_new_dmain = domain.ip is None update_ip = domain.ip != ip domain.ip = ip if is_new_dmain: self.dns.new_domain(self.main_domain, domain) else: self.dns.update_domain(self.main_domain, domain, update_ip=update_ip, added=added_services, removed=removed_services) domain.last_update = datetime.now() return domain
def authenticate(self, request): validator = Validator(request) email = validator.email() password = validator.password() check_validator(validator) user = self.get_user(email) if not user or not user.active or not util.hash( password) == user.password_hash: raise servicesexceptions.bad_request('Authentication failed') return user
def redirect_url(self, request_url): user_domain = util.get_second_level_domain(request_url, self.main_domain) if not user_domain: raise servicesexceptions.bad_request('Second level domain should be specified') user = self.storage.get_user_by_domain(user_domain) if not user: raise servicesexceptions.not_found('The second level domain is not registered') return 'http://device.{0}.{1}:{2}/owncloud'.format(user_domain, self.main_domain, user.port)
def user_domain_delete(self, request, user): validator = Validator(request) user_domain = validator.user_domain() check_validator(validator) with self.create_storage() as storage: domain = storage.get_domain_by_name(user_domain) if not domain or domain.user.email != user.email: raise servicesexceptions.bad_request('Unknown domain') self.dns.delete_domain(self.main_domain, domain) storage.delete_domain(domain)
def authenticate(self, request): validator = Validator(request) email = validator.email() password = validator.password() errors = validator.errors if errors: message = ", ".join(errors) raise servicesexceptions.bad_request(message) user = self.get_user(email) if not user or not user.active or not util.hash(password) == user.password_hash: raise servicesexceptions.forbidden('Authentication failed') return user
def delete_user(self, request): validator = Validator(request) email = validator.email() password = validator.password() check_validator(validator) with self.create_storage() as storage: user = storage.get_user_by_email(email) if not user or not user.active or not util.hash(password) == user.password_hash: raise servicesexceptions.bad_request('Authentication failed') for domain in user.domains: self.dns.delete_domain(self.main_domain, domain) storage.delete_user(user)
def user_reset_password(self, request): validator = Validator(request) email = validator.email() errors = validator.errors if errors: message = ", ".join(errors) raise servicesexceptions.bad_request(message) with self.create_storage() as storage: user = storage.get_user_by_email(email) if user and user.active: action = user.enable_action(ActionType.PASSWORD) self.mail.send_reset_password(user.email, action.token)
def delete_user(self, request): validator = Validator(request) email = validator.email() password = validator.password() check_validator(validator) with self.create_storage() as storage: user = storage.get_user_by_email(email) if not user or not user.active or not util.hash( password) == user.password_hash: raise servicesexceptions.bad_request('Authentication failed') for domain in user.domains: self.dns.delete_domain(self.main_domain, domain) storage.delete_user(user)
def user_set_password(self, request): validator = Validator(request) token = validator.token() password = validator.new_password() check_validator(validator) with self.create_storage() as storage: user = storage.get_user_by_token(ActionType.PASSWORD, token) if not user: raise servicesexceptions.bad_request('Invalid password token') user.password_hash = util.hash(password) self.mail.send_set_password(user.email) action = storage.get_action(token) storage.delete(action)
def create_new_user(self, request): validator = Validator(request) email = validator.email() password = validator.password() user_domain = validator.new_user_domain(error_if_missing=False) errors = validator.errors if errors: message = ", ".join(errors) raise servicesexceptions.bad_request(message) user = None action = None with self.create_storage() as storage: by_email = storage.get_user_by_email(email) if by_email and by_email.email == email: raise servicesexceptions.conflict('Email is already registered') if user_domain: by_domain = storage.get_domain_by_name(user_domain) if by_domain and by_domain.user_domain == user_domain: raise servicesexceptions.conflict('User domain name is already in use') update_token = util.create_token() user = User(email, util.hash(password), not self.activate_by_email) if user_domain: domain = Domain(user_domain, None, update_token) domain.user = user user.domains.append(domain) storage.add(domain) if self.activate_by_email: action = user.enable_action(ActionType.ACTIVATE) storage.add(user) if self.activate_by_email: self.mail.send_activate(self.main_domain, user.email, action.token) return user
def drop_device(self, request): user = self.authenticate(request) validator = Validator(request) user_domain = validator.new_user_domain() check_validator(validator) with self.create_storage() as storage: domain = storage.get_domain_by_name(user_domain) if not domain or not domain.user.active: raise servicesexceptions.bad_request('Unknown domain') domain.update_token = None domain.device_mac_address = None domain.device_name = None domain.device_title = None domain.ip = None domain.local_ip = None self.dns.delete_domain(self.main_domain, domain) return domain
def user_set_password(self, request): validator = Validator(request) token = validator.token() password = validator.new_password() errors = validator.errors if errors: message = ", ".join(errors) raise servicesexceptions.bad_request(message) with self.create_storage() as storage: user = storage.get_user_by_token(ActionType.PASSWORD, token) if not user: raise servicesexceptions.forbidden('Invalid password token') user.password_hash = util.hash(password) self.mail.send_set_password(user.email) action = storage.get_action(token) storage.delete(action)
def domain_update(self, request, request_ip=None): validator = Validator(request) token = validator.token() ip = validator.ip(request_ip) local_ip = validator.local_ip() map_local_address = validator.boolean('map_local_address', required=False) platform_version = validator.string('platform_version', required=False) web_protocol = validator.web_protocol(required=True) web_local_port = validator.port('web_local_port', required=True) web_port = validator.port('web_port', required=False) check_validator(validator) if map_local_address is None: map_local_address = False with self.create_storage() as storage: domain = storage.get_domain_by_update_token(token) if not domain or not domain.user.active: raise servicesexceptions.bad_request( 'Unknown domain update token') update_ip = (domain.map_local_address != map_local_address) or ( domain.ip != ip) or (domain.local_ip != local_ip) domain.ip = ip domain.local_ip = local_ip domain.map_local_address = map_local_address domain.platform_version = platform_version domain.web_protocol = web_protocol domain.web_local_port = web_local_port domain.web_port = web_port if update_ip: self.dns.update_domain(self.main_domain, domain) domain.last_update = datetime.now() return domain
def validate_service(self, data): validator = Validator(data) validator.port() if validator.errors: message = ", ".join(validator.errors) raise servicesexceptions.bad_request(message)