Skip to content

Instantly share code, notes, and snippets.

@alej0varas
Created October 5, 2017 13:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alej0varas/82768a2ed4ad62330b3d8f58c7104dfb to your computer and use it in GitHub Desktop.
Save alej0varas/82768a2ed4ad62330b3d8f58c7104dfb to your computer and use it in GitHub Desktop.
(FTR Digits was retired on 30/09/2017) Digits verification
class DigitsVerification:
DIGITS_API_URL = 'https://api.digits.com'
AUTH_PROVIDER_KEY = 'apiUrl'
CREDENTIALS_KEY = 'credentials'
def __call__(self, credentials):
auth_provider = self.validate_auth_provider(credentials)
if auth_provider is None:
return None
credentials = self.validate_credentials(credentials)
if credentials is None:
return None
headers = self.make_headers(credentials)
response = requests.get(auth_provider, headers=headers)
if response.status_code == 200:
return True
@staticmethod
def make_headers(credentials):
return {'Authorization': credentials}
@staticmethod
def validate_auth_provider(credentials):
auth_provider = credentials.get(DigitsVerification.AUTH_PROVIDER_KEY, '')
if auth_provider.startswith(DigitsVerification.DIGITS_API_URL):
return auth_provider
@staticmethod
def validate_credentials(credentials):
credentials = credentials.get(DigitsVerification.CREDENTIALS_KEY, '')
key = 'oauth_consumer_key="'
start = credentials.index(key) + len(key)
end = start + len(settings.DIGITS_CONSUMER_KEY)
if credentials[start:end] != settings.DIGITS_CONSUMER_KEY:
return None
return credentials
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment