Add again the check for valid credentials
As our check is more strict that the one provided in googlemaps library.
This commit is contained in:
@@ -2,6 +2,8 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
import googlemaps
|
import googlemaps
|
||||||
|
import base64
|
||||||
|
from exceptions import InvalidGoogleCredentials
|
||||||
|
|
||||||
class GoogleMapsClientFactory():
|
class GoogleMapsClientFactory():
|
||||||
clients = {}
|
clients = {}
|
||||||
@@ -10,8 +12,25 @@ class GoogleMapsClientFactory():
|
|||||||
def get(cls, client_id, client_secret):
|
def get(cls, client_id, client_secret):
|
||||||
client = cls.clients.get(client_id)
|
client = cls.clients.get(client_id)
|
||||||
if not client:
|
if not client:
|
||||||
|
cls.assert_valid_crendentials(client_secret)
|
||||||
client = googlemaps.Client(
|
client = googlemaps.Client(
|
||||||
client_id=client_id,
|
client_id=client_id,
|
||||||
client_secret=client_secret)
|
client_secret=client_secret)
|
||||||
cls.clients[client_id] = client
|
cls.clients[client_id] = client
|
||||||
return client
|
return client
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def assert_valid_crendentials(cls, client_secret):
|
||||||
|
if not cls.valid_credentials(client_secret):
|
||||||
|
raise InvalidGoogleCredentials
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def valid_credentials(client_secret):
|
||||||
|
try:
|
||||||
|
# Only fails if the string dont have a correct padding for b64
|
||||||
|
# but this way we could provide a more clear error than
|
||||||
|
# TypeError: Incorrect padding
|
||||||
|
base64.b64decode(client_secret)
|
||||||
|
return True
|
||||||
|
except TypeError:
|
||||||
|
return False
|
||||||
|
|||||||
@@ -1,10 +1,9 @@
|
|||||||
#!/usr/local/bin/python
|
#!/usr/local/bin/python
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
import base64
|
|
||||||
import googlemaps
|
import googlemaps
|
||||||
|
|
||||||
from exceptions import MalformedResult, InvalidGoogleCredentials
|
from exceptions import MalformedResult
|
||||||
from client_factory import GoogleMapsClientFactory
|
from client_factory import GoogleMapsClientFactory
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user