|
| 1 | +/* |
| 2 | + * Copyright 2020 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.google.api.client.extensions.android.util.store; |
| 17 | + |
| 18 | +import com.google.api.client.util.IOUtils; |
| 19 | +import com.google.api.client.util.Maps; |
| 20 | +import com.google.api.client.util.store.AbstractDataStoreFactory; |
| 21 | +import com.google.api.client.util.store.AbstractMemoryDataStore; |
| 22 | +import com.google.api.client.util.store.DataStore; |
| 23 | +import java.io.File; |
| 24 | +import java.io.FileInputStream; |
| 25 | +import java.io.FileOutputStream; |
| 26 | +import java.io.IOException; |
| 27 | +import java.io.Serializable; |
| 28 | +import java.util.logging.Logger; |
| 29 | + |
| 30 | +/** |
| 31 | + * Thread-safe file implementation of a credential store. |
| 32 | + * |
| 33 | + * <p>For security purposes, the file's permissions are set to be accessible only by the file's |
| 34 | + * owner. |
| 35 | + * |
| 36 | + * <p>Note: This class was branched from the primary implementation in google-http-client to allow |
| 37 | + * the mainline implementation to support Windows file permissions. |
| 38 | + * |
| 39 | + * @since 1.36 |
| 40 | + * @author Yaniv Inbar |
| 41 | + */ |
| 42 | +public class FileDataStoreFactory extends AbstractDataStoreFactory { |
| 43 | + |
| 44 | + private static final Logger LOGGER = Logger.getLogger(FileDataStoreFactory.class.getName()); |
| 45 | + |
| 46 | + /** Directory to store data. */ |
| 47 | + private final File dataDirectory; |
| 48 | + |
| 49 | + /** @param dataDirectory data directory */ |
| 50 | + public FileDataStoreFactory(File dataDirectory) throws IOException { |
| 51 | + dataDirectory = dataDirectory.getCanonicalFile(); |
| 52 | + // error if it is a symbolic link |
| 53 | + if (IOUtils.isSymbolicLink(dataDirectory)) { |
| 54 | + throw new IOException("unable to use a symbolic link: " + dataDirectory); |
| 55 | + } |
| 56 | + // create parent directory (if necessary) |
| 57 | + if (!dataDirectory.exists() && !dataDirectory.mkdirs()) { |
| 58 | + throw new IOException("unable to create directory: " + dataDirectory); |
| 59 | + } |
| 60 | + this.dataDirectory = dataDirectory; |
| 61 | + setPermissionsToOwnerOnly(dataDirectory); |
| 62 | + } |
| 63 | + |
| 64 | + /** Returns the data directory. */ |
| 65 | + public final File getDataDirectory() { |
| 66 | + return dataDirectory; |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + protected <V extends Serializable> DataStore<V> createDataStore(String id) throws IOException { |
| 71 | + return new FileDataStore<V>(this, dataDirectory, id); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * File data store that inherits from the abstract memory data store because the key-value pairs |
| 76 | + * are stored in a memory cache, and saved in the file (see {@link #save()} when changing values. |
| 77 | + * |
| 78 | + * @param <V> serializable type of the mapped value |
| 79 | + */ |
| 80 | + static class FileDataStore<V extends Serializable> extends AbstractMemoryDataStore<V> { |
| 81 | + |
| 82 | + /** File to store data. */ |
| 83 | + private final File dataFile; |
| 84 | + |
| 85 | + FileDataStore(FileDataStoreFactory dataStore, File dataDirectory, String id) |
| 86 | + throws IOException { |
| 87 | + super(dataStore, id); |
| 88 | + this.dataFile = new File(dataDirectory, id); |
| 89 | + // error if it is a symbolic link |
| 90 | + if (IOUtils.isSymbolicLink(dataFile)) { |
| 91 | + throw new IOException("unable to use a symbolic link: " + dataFile); |
| 92 | + } |
| 93 | + // create new file (if necessary) |
| 94 | + if (dataFile.createNewFile()) { |
| 95 | + keyValueMap = Maps.newHashMap(); |
| 96 | + // save the credentials to create a new file |
| 97 | + save(); |
| 98 | + } else { |
| 99 | + // load credentials from existing file |
| 100 | + keyValueMap = IOUtils.deserialize(new FileInputStream(dataFile)); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public void save() throws IOException { |
| 106 | + IOUtils.serialize(keyValueMap, new FileOutputStream(dataFile)); |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + public FileDataStoreFactory getDataStoreFactory() { |
| 111 | + return (FileDataStoreFactory) super.getDataStoreFactory(); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Attempts to set the given file's permissions such that it can only be read, written, and |
| 117 | + * executed by the file's owner. |
| 118 | + * |
| 119 | + * @param file the file's permissions to modify |
| 120 | + */ |
| 121 | + static void setPermissionsToOwnerOnly(File file) { |
| 122 | + // Disable access by other users if O/S allows it and set file permissions to readable and |
| 123 | + // writable by user. |
| 124 | + try { |
| 125 | + if (!file.setReadable(false, false) |
| 126 | + || !file.setWritable(false, false) |
| 127 | + || !file.setExecutable(false, false)) { |
| 128 | + LOGGER.warning("unable to change permissions for everybody: " + file); |
| 129 | + } |
| 130 | + if (!file.setReadable(true, true) |
| 131 | + || !file.setWritable(true, true) |
| 132 | + || !file.setExecutable(true, true)) { |
| 133 | + LOGGER.warning("unable to change permissions for owner: " + file); |
| 134 | + } |
| 135 | + } catch (SecurityException exception) { |
| 136 | + // ignored |
| 137 | + } catch (IllegalArgumentException exception) { |
| 138 | + // ignored |
| 139 | + } |
| 140 | + } |
| 141 | +} |
0 commit comments