diff --git a/google-http-client-android/src/main/java/com/google/api/client/extensions/android/util/store/FileDataStoreFactory.java b/google-http-client-android/src/main/java/com/google/api/client/extensions/android/util/store/FileDataStoreFactory.java new file mode 100644 index 000000000..b9c808b5a --- /dev/null +++ b/google-http-client-android/src/main/java/com/google/api/client/extensions/android/util/store/FileDataStoreFactory.java @@ -0,0 +1,141 @@ +/* + * Copyright 2020 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.google.api.client.extensions.android.util.store; + +import com.google.api.client.util.IOUtils; +import com.google.api.client.util.Maps; +import com.google.api.client.util.store.AbstractDataStoreFactory; +import com.google.api.client.util.store.AbstractMemoryDataStore; +import com.google.api.client.util.store.DataStore; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.Serializable; +import java.util.logging.Logger; + +/** + * Thread-safe file implementation of a credential store. + * + *
For security purposes, the file's permissions are set to be accessible only by the file's + * owner. + * + *
Note: This class was branched from the primary implementation in google-http-client to allow
+ * the mainline implementation to support Windows file permissions.
+ *
+ * @since 1.36
+ * @author Yaniv Inbar
+ */
+public class FileDataStoreFactory extends AbstractDataStoreFactory {
+
+ private static final Logger LOGGER = Logger.getLogger(FileDataStoreFactory.class.getName());
+
+ /** Directory to store data. */
+ private final File dataDirectory;
+
+ /** @param dataDirectory data directory */
+ public FileDataStoreFactory(File dataDirectory) throws IOException {
+ dataDirectory = dataDirectory.getCanonicalFile();
+ // error if it is a symbolic link
+ if (IOUtils.isSymbolicLink(dataDirectory)) {
+ throw new IOException("unable to use a symbolic link: " + dataDirectory);
+ }
+ // create parent directory (if necessary)
+ if (!dataDirectory.exists() && !dataDirectory.mkdirs()) {
+ throw new IOException("unable to create directory: " + dataDirectory);
+ }
+ this.dataDirectory = dataDirectory;
+ setPermissionsToOwnerOnly(dataDirectory);
+ }
+
+ /** Returns the data directory. */
+ public final File getDataDirectory() {
+ return dataDirectory;
+ }
+
+ @Override
+ protected For security purposes, the file's permissions are set such that the file is only accessible by
* the file's owner.
*
+ * Note: this class is not compatible with Android lower than API level 26 (Oreo). For an
+ * implementation compatible with Android < 26, please use
+ * com.google.api.client.extensions.android.util.store.FileDataStoreFactory which is provided by
+ * com.google.http-client:google-http-client-android.
+ *
* @since 1.16
* @author Yaniv Inbar
*/
@@ -61,7 +66,6 @@ public class FileDataStoreFactory extends AbstractDataStoreFactory {
/** @param dataDirectory data directory */
public FileDataStoreFactory(File dataDirectory) throws IOException {
dataDirectory = dataDirectory.getCanonicalFile();
- this.dataDirectory = dataDirectory;
// error if it is a symbolic link
if (IOUtils.isSymbolicLink(dataDirectory)) {
throw new IOException("unable to use a symbolic link: " + dataDirectory);
@@ -70,6 +74,7 @@ public FileDataStoreFactory(File dataDirectory) throws IOException {
if (!dataDirectory.exists() && !dataDirectory.mkdirs()) {
throw new IOException("unable to create directory: " + dataDirectory);
}
+ this.dataDirectory = dataDirectory;
if (IS_WINDOWS) {
setPermissionsToOwnerOnlyWindows(dataDirectory);
diff --git a/pom.xml b/pom.xml
index 3d6505d19..4cff5a308 100644
--- a/pom.xml
+++ b/pom.xml
@@ -499,18 +499,32 @@