Пример #1
0
 def validate_host(self, field, value):
     if not value:
         return value
     url = PING_URL % {'host': value, 'apikey': 1234}
     response = utils.http_request(url, timeout=2).get('response')
     if not response:
         raise ValidationError('Host not reachable.')
     if response.status != 200:
         raise ValidationError('Invalid response from server: %s' %
                               response.status)
     return value
Пример #2
0
 def validate_apikey(self, field, value):
     if not value:
         return value
     url = UPDATE_URL % {'apikey':value, 'location':'autoip'}
     response = utils.http_request(url, timeout=2).get('response')
     if not response:
         raise ValidationError('No response from Weather Underground.')
     content = json.loads(response.read().decode('utf-8'))
     if 'response' not in content:
         raise ValidationError('Invalid response from Weather Underground.')
     if 'current_observation' not in content:
         raise ValidationError('Invalid API key specified.')
     return value
Пример #3
0
 def validate_apikey(self, field, value):
     if not value:
         return value
     host = self.fields.host.value
     if host is None:
         host = self.pi_config.get(self.namespace, 'host')
     url = UPDATE_URL % {'host': host, 'apikey': value, 'end': '2000-01-01'}
     response = utils.http_request(url, timeout=2).get('response')
     if utils.rget(response, 'error.code') == 401:
         raise ValidationError('Invalid API key specified.')
     content = json.loads(response.read().decode('utf-8'))
     if not isinstance(content, list):
         raise ValidationError('Invalid response from server.')
     return value
Пример #4
0
 def validate_host(self, field, value):
     if not value:
         return value
     try:
         username = self.fields.username.value
         password = self.fields.password.value
         fetch_plex_instance(self.pi_dash, username, password, value)
         return value
     except Unauthorized:
         raise ValidationError('Invalid username or password.')
     except NotFound:
         raise ValidationError('Server host or name not found.')
     except:
         raise ValidationError('Invalid server.')
Пример #5
0
 def validate_query(self, field, value):
     if not value or value == 'autoip':
         self.fields.location.value = 'autoip'
         field.help.setText(field.help_default)
         return value
     url = QUERY_URL % {'query':value}
     response = utils.http_request(url, timeout=2).get('response')
     if not response:
         raise ValidationError('No response from Weather Underground.')
     content = json.loads(response.read().decode('utf-8'))
     if not content.get('RESULTS'):
         raise ValidationError('Unable to determine specified location.')
     result = content['RESULTS'][0]
     self.fields.location.value = result['ll'].replace(' ',',')
     field.help.setText(result['name'])
     return value
Пример #6
0
 def validate_interval(self, field, value):
     try:
         interval = int(value)
         assert 1 <= interval <= 3600, 'Value out of bounds.'
         return interval
     except:
         raise ValidationError('Interval seconds must be a number 1-3600.')
Пример #7
0
 def validate_apikey(self, field, value):
     if not value:
         return value
     host = self.fields.host.value
     if host is None:
         host = self.pi_config.get(self.namespace, 'host')
     url = PING_URL % {'host': host, 'apikey': value}
     response = utils.http_request(url, timeout=2).get('response')
     if not response:
         raise ValidationError('Host not reachable.')
     if response.status != 200:
         raise ValidationError('Invalid response from server: %s' %
                               response.status)
     content = json.loads(response.read().decode('utf-8'))
     if content.get('result') != 'success':
         raise ValidationError('Invalid API key specified.')
     return value
Пример #8
0
 def validate_host(self, field, value):
     if not value:
         return value
     url = UPDATE_URL % {'host': value, 'apikey': 1234, 'end': '2000-01-01'}
     response = utils.http_request(url, timeout=2)
     if utils.rget(response, 'error.code') != 401:
         raise ValidationError('Host not reachable.')
     return value
Пример #9
0
 def validate_password(self, field, value):
     if not value:
         return value
     try:
         MyPlexAccount.signin(self.fields.username.value, value)
         return value
     except Unauthorized:
         raise ValidationError('Invalid username or password.')
Пример #10
0
def get_access_token_from_header(headers):
    auth = headers.get('Authorization', None)
    if not auth:
        raise ValidationError(status_code=401,
                              message='Missing Authorization header')

    parts = auth.split()

    if parts[0].lower() != 'bearer':
        raise ValidationError(
            status_code=401,
            message='Authorization header must start with Bearer')

    elif len(parts) == 1:
        raise ValidationError(status_code=401, message='Token not found')

    elif len(parts) > 2:
        raise ValidationError(
            status_code=401,
            message='Authorization header must be Bearer token')

    return parts[1]