def test_constructor_explicit(self): dictionary = {} key = 'test-key' storage = dictionary_storage.DictionaryStorage(dictionary, key) lock = object() storage = dictionary_storage.DictionaryStorage( dictionary, key, lock=lock) self.assertEqual(storage._lock, lock)
def get_storage(request): """ Gets a Credentials storage object provided by the Django OAuth2 Helper object. Args: request: Reference to the current request object. Returns: An :class:`oauth2.client.Storage` object. """ storage_model = oauth2_settings.storage_model user_property = oauth2_settings.storage_model_user_property credentials_property = oauth2_settings.storage_model_credentials_property if storage_model: module_name, class_name = storage_model.rsplit('.', 1) module = importlib.import_module(module_name) storage_model_class = getattr(module, class_name) return storage.DjangoORMStorage(storage_model_class, user_property, request.user, credentials_property) else: # use session return dictionary_storage.DictionaryStorage( request.session, key=_CREDENTIALS_KEY)
def test_constructor_defaults(self): dictionary = {} key = 'test-key' storage = dictionary_storage.DictionaryStorage(dictionary, key) self.assertEqual(dictionary, storage._dictionary) self.assertEqual(key, storage._key) self.assertIsNone(storage._lock)
def test_delete(self): credentials = _generate_credentials() dictionary = {} key = 'credentials' storage = dictionary_storage.DictionaryStorage(dictionary, key) storage.put(credentials) self.assertIn(key, dictionary) storage.delete() self.assertNotIn(key, dictionary) self.assertIsNone(storage.get())
def test_put(self): credentials = _generate_credentials() dictionary = {} key = 'credentials' storage = dictionary_storage.DictionaryStorage(dictionary, key) storage.put(credentials) returned = storage.get() self.assertIn(key, dictionary) self.assertIsNotNone(returned) self.assertEqual(returned.access_token, credentials.access_token) self.assertEqual(returned.id_token, credentials.id_token) self.assertEqual(returned.refresh_token, credentials.refresh_token) self.assertEqual(returned.client_id, credentials.client_id)
def init_app(self, app, scopes=None, client_secrets_file=None, client_id=None, client_secret=None, authorize_callback=None, storage=None, **kwargs): """Initialize this extension for the given app. Arguments: app: A Flask application. scopes: Optional list of scopes to authorize. client_secrets_file: Path to a file containing client secrets. You can also specify the GOOGLE_OAUTH2_CLIENT_SECRETS_FILE config value. client_id: If not specifying a client secrets file, specify the OAuth2 client id. You can also specify the GOOGLE_OAUTH2_CLIENT_ID config value. You must also provide a client secret. client_secret: The OAuth2 client secret. You can also specify the GOOGLE_OAUTH2_CLIENT_SECRET config value. authorize_callback: A function that is executed after successful user authorization. storage: A oauth2client.client.Storage subclass for storing the credentials. By default, this is a Flask session based storage. kwargs: Any additional args are passed along to the Flow constructor. """ self.app = app self.authorize_callback = authorize_callback self.flow_kwargs = kwargs if storage is None: storage = dictionary_storage.DictionaryStorage( session, key=_CREDENTIALS_KEY) self.storage = storage if scopes is None: scopes = app.config.get('GOOGLE_OAUTH2_SCOPES', _DEFAULT_SCOPES) self.scopes = scopes self._load_config(client_secrets_file, client_id, client_secret) app.register_blueprint(self._create_blueprint())
def authenticate(scope, args): """ Performs the authentication flow. Handles the credential management :param scope: The scope to authenticate with :param args: The arguments passed in to the program. :return: (The drive api service, The http requests object) """ if args.cache: store = file.Storage(folder + '/token.json') else: store = dictionary_storage.DictionaryStorage({}, 'token') creds = store.get() if not creds or creds.invalid: flow = client.flow_from_clientsecrets(folder + '/credentials.json', scope) creds = tools.run_flow(flow, store, args) service = build('drive', 'v2', http=creds.authorize(Http())) if not args.cache and os.path.exists(folder + '/token.json'): os.remove(folder + '/token.json') return service