• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2023, Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  *    * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *    * Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following disclaimer
12  * in the documentation and/or other materials provided with the
13  * distribution.
14  *
15  *    * Neither the name of Google Inc. nor the names of its
16  * contributors may be used to endorse or promote products derived from
17  * this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 package com.google.auth.oauth2;
33 
34 import java.io.File;
35 
36 /**
37  * This public class provides shared utilities for common OAuth2 utils or ADC. It also exposes
38  * convenience methods such as a getter for well-known Application Default Credentials file path
39  */
40 public class GoogleAuthUtils {
41 
42   /**
43    * Gets the path to the well-known Application Default Credentials file location
44    *
45    * @return the path to the well-known Application Default Credentials file location
46    */
getWellKnownCredentialsPath()47   public static final String getWellKnownCredentialsPath() {
48     return getWellKnownCredentialsFile(DefaultCredentialsProvider.DEFAULT).getAbsolutePath();
49   }
50 
51   /**
52    * Testing version of getWellKnownCredentialsPath() that uses a custom provider
53    *
54    * @return the path to the well-known Application Default Credentials file location
55    */
getWellKnownCredentialsPath(DefaultCredentialsProvider provider)56   static final String getWellKnownCredentialsPath(DefaultCredentialsProvider provider) {
57     return getWellKnownCredentialsFile(provider).getAbsolutePath();
58   }
59 
60   /**
61    * Platform-independent logic to obtain the well-known Application Default Credentials file
62    *
63    * @param provider the provider used to resolve env and system properties (exposed for testing
64    *     purposes)
65    * @return the well-known Application Default Credentials file
66    */
getWellKnownCredentialsFile(DefaultCredentialsProvider provider)67   static final File getWellKnownCredentialsFile(DefaultCredentialsProvider provider) {
68     File cloudConfigPath;
69     String envPath = provider.getEnv("CLOUDSDK_CONFIG");
70     if (envPath != null) {
71       cloudConfigPath = new File(envPath);
72     } else if (provider.getOsName().indexOf("windows") >= 0) {
73       File appDataPath = new File(provider.getEnv("APPDATA"));
74       cloudConfigPath = new File(appDataPath, provider.CLOUDSDK_CONFIG_DIRECTORY);
75     } else {
76       File configPath = new File(provider.getProperty("user.home", ""), ".config");
77       cloudConfigPath = new File(configPath, provider.CLOUDSDK_CONFIG_DIRECTORY);
78     }
79     return new File(cloudConfigPath, provider.WELL_KNOWN_CREDENTIALS_FILE);
80   }
81 }
82