From f79ac9297db22f7dd28d232d7b60131687b82d2e Mon Sep 17 00:00:00 2001 From: Rafa de la Torre Date: Fri, 6 Oct 2017 13:44:01 +0200 Subject: [PATCH] Add again the check for valid credentials As our check is more strict that the one provided in googlemaps library. --- .../cartodb_services/google/client_factory.py | 19 +++++++++++++++++++ .../cartodb_services/google/geocoder.py | 3 +-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/server/lib/python/cartodb_services/cartodb_services/google/client_factory.py b/server/lib/python/cartodb_services/cartodb_services/google/client_factory.py index 3e2ada6..61e5d4e 100644 --- a/server/lib/python/cartodb_services/cartodb_services/google/client_factory.py +++ b/server/lib/python/cartodb_services/cartodb_services/google/client_factory.py @@ -2,6 +2,8 @@ # -*- coding: utf-8 -*- import googlemaps +import base64 +from exceptions import InvalidGoogleCredentials class GoogleMapsClientFactory(): clients = {} @@ -10,8 +12,25 @@ class GoogleMapsClientFactory(): def get(cls, client_id, client_secret): client = cls.clients.get(client_id) if not client: + cls.assert_valid_crendentials(client_secret) client = googlemaps.Client( client_id=client_id, client_secret=client_secret) cls.clients[client_id] = 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 diff --git a/server/lib/python/cartodb_services/cartodb_services/google/geocoder.py b/server/lib/python/cartodb_services/cartodb_services/google/geocoder.py index b900921..bf14b84 100644 --- a/server/lib/python/cartodb_services/cartodb_services/google/geocoder.py +++ b/server/lib/python/cartodb_services/cartodb_services/google/geocoder.py @@ -1,10 +1,9 @@ #!/usr/local/bin/python # -*- coding: utf-8 -*- -import base64 import googlemaps -from exceptions import MalformedResult, InvalidGoogleCredentials +from exceptions import MalformedResult from client_factory import GoogleMapsClientFactory