|
| 1 | +/* |
| 2 | + * Copyright (c) 2016 HERE Europe B.V. |
| 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 | + * http://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.here.account.oauth2.tutorial; |
| 17 | + |
| 18 | +import java.io.File; |
| 19 | +import java.io.IOException; |
| 20 | + |
| 21 | +import com.here.account.auth.OAuth1ClientCredentialsProvider; |
| 22 | +import com.here.account.http.apache.ApacheHttpClientProvider; |
| 23 | +import com.here.account.oauth2.AccessTokenResponse; |
| 24 | +import com.here.account.oauth2.ClientCredentialsGrantRequest; |
| 25 | +import com.here.account.oauth2.Fresh; |
| 26 | +import com.here.account.oauth2.HereAccount; |
| 27 | +import com.here.account.oauth2.TokenEndpoint; |
| 28 | + |
| 29 | +/** |
| 30 | + * A tutorial class providing example code for always obtaining a fresh |
| 31 | + * HERE Access Token, from the HERE Account authorization server, |
| 32 | + * using the client_credentials grant_type. |
| 33 | + * |
| 34 | + * @author kmccrack |
| 35 | + * |
| 36 | + */ |
| 37 | +public class GetHereClientCredentialsAccessTokenTutorial { |
| 38 | + |
| 39 | + /** |
| 40 | + * The main method includes the bulk of the code integration, |
| 41 | + * for always obtaining a fresh |
| 42 | + * HERE Access Token, from the HERE Account authorization server, |
| 43 | + * using the client_credentials grant_type. |
| 44 | + * |
| 45 | + * @param argv the arguments to main; see usage output for details. |
| 46 | + */ |
| 47 | + public static void main(String[] argv) { |
| 48 | + GetHereClientCredentialsAccessTokenTutorial tutorial = new GetHereClientCredentialsAccessTokenTutorial(argv); |
| 49 | + tutorial.getAccessToken(); |
| 50 | + } |
| 51 | + |
| 52 | + private String[] argv; |
| 53 | + private OAuth1ClientCredentialsProvider testCreds = null; |
| 54 | + |
| 55 | + public GetHereClientCredentialsAccessTokenTutorial(String[] argv) { |
| 56 | + this.argv = argv; |
| 57 | + } |
| 58 | + |
| 59 | + public String getAccessToken() { |
| 60 | + Args args = parseArgs(argv); |
| 61 | + try { |
| 62 | + OAuth1ClientCredentialsProvider credentials = getCredentials(args); |
| 63 | + TokenEndpoint tokenEndpoint = HereAccount.getTokenEndpoint( |
| 64 | + ApacheHttpClientProvider.builder().build(), |
| 65 | + credentials); |
| 66 | + Fresh<AccessTokenResponse> fresh = |
| 67 | + tokenEndpoint.requestAutoRefreshingToken(new ClientCredentialsGrantRequest()); |
| 68 | + String accessToken = fresh.get().getAccessToken(); |
| 69 | + if (args.isVerbose()) { |
| 70 | + System.out.println("HERE Access Token: " + accessToken); |
| 71 | + } else { |
| 72 | + System.out.println("HERE Access Token: " + accessToken.substring(0, 20) + "..." + accessToken.substring(accessToken.length() - 4)); |
| 73 | + } |
| 74 | + return accessToken; |
| 75 | + } catch (Exception e) { |
| 76 | + System.err.println("trouble getting Here client_credentials Access Token: " + e); |
| 77 | + e.printStackTrace(); |
| 78 | + exit(2); |
| 79 | + return null; |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + protected OAuth1ClientCredentialsProvider getCredentials(Args args) throws IOException { |
| 84 | + if (null != testCreds) { |
| 85 | + return testCreds; |
| 86 | + } |
| 87 | + |
| 88 | + File file = getCredentialsFile(args); |
| 89 | + return new OAuth1ClientCredentialsProvider.FromFile(file); |
| 90 | + } |
| 91 | + |
| 92 | + protected void exit(int status) { |
| 93 | + System.exit(status); |
| 94 | + } |
| 95 | + |
| 96 | + //////// |
| 97 | + // an approach to parsing input args |
| 98 | + //////// |
| 99 | + protected Args parseArgs(String[] argv) { |
| 100 | + if (null == argv || argv.length > 3) { |
| 101 | + printUsageAndExit(); |
| 102 | + } |
| 103 | + int i = 0; |
| 104 | + boolean verbose = false; |
| 105 | + String filePathString = null; |
| 106 | + while (i < argv.length) { |
| 107 | + String arg = argv[i++]; |
| 108 | + if (arg.equals("-v")) { |
| 109 | + System.out.println("INFO: Running in verbose mode."); |
| 110 | + verbose = true; |
| 111 | + } else if (arg.equals("-help")) { |
| 112 | + System.out.println("INFO: in help mode, will print usage and exit."); |
| 113 | + printUsageAndExit(); |
| 114 | + } else if (null == filePathString) { |
| 115 | + filePathString = arg; |
| 116 | + } else { |
| 117 | + System.out.println("unrecognized option or more than one path_to_credentials_property_file"); |
| 118 | + printUsageAndExit(); |
| 119 | + } |
| 120 | + } |
| 121 | + if (!verbose) { |
| 122 | + System.out.println("INFO: Running in quiet mode; to enable verbose mode add '-v' as your first argument."); |
| 123 | + System.out.println("WARNING: verbose mode will display an actual valid HERE Access Token to stdout."); |
| 124 | + } |
| 125 | + return new Args(verbose, filePathString); |
| 126 | + } |
| 127 | + |
| 128 | + protected class Args { |
| 129 | + private final boolean verbose; |
| 130 | + private final String filePathString; |
| 131 | + |
| 132 | + public Args(boolean verbose, String filePathString) { |
| 133 | + this.verbose = verbose; |
| 134 | + this.filePathString = filePathString; |
| 135 | + } |
| 136 | + |
| 137 | + public boolean isVerbose() { |
| 138 | + return verbose; |
| 139 | + } |
| 140 | + |
| 141 | + public String getFilePathString() { |
| 142 | + return filePathString; |
| 143 | + } |
| 144 | + |
| 145 | + } |
| 146 | + |
| 147 | + //////// |
| 148 | + // print usage and exit |
| 149 | + //////// |
| 150 | + |
| 151 | + /** |
| 152 | + * Usage is displayed to stderr, along with exiting the process with a non-zero exit code. |
| 153 | + */ |
| 154 | + private void printUsageAndExit() { |
| 155 | + System.err.println("Usage: java " |
| 156 | + + GetHereClientCredentialsAccessTokenTutorial.class.getName() |
| 157 | + + " [-help]" |
| 158 | + + " [-v]" |
| 159 | + + " [path_to_credentials_property_file]"); |
| 160 | + System.err.println("where:"); |
| 161 | + System.err.println(" -help: means print this message and exit"); |
| 162 | + System.err.println(" -v: sets verbose mode; WARNING: HERE Access Token will be displayed to stdout."); |
| 163 | + System.err.println(" path_to_credentials_property_file: optionally override the default path of "); |
| 164 | + System.err.println(" "+DEFAULT_CREDENTIALS_FILE_PATH+", to point to any file on your filesystem.");; |
| 165 | + exit(1); |
| 166 | + } |
| 167 | + |
| 168 | + //////// |
| 169 | + // get credentials file, either command-line override, or default file location |
| 170 | + //////// |
| 171 | + |
| 172 | + protected File getCredentialsFile(Args args) { |
| 173 | + File file; |
| 174 | + String filePathString = args.getFilePathString(); |
| 175 | + if (null != filePathString) { |
| 176 | + file = new File(filePathString); |
| 177 | + if (!isFileAndExists(file)) { |
| 178 | + System.err.println("WARNING: credentials properties file does not exist: " + file); |
| 179 | + printUsageAndExit(); |
| 180 | + } |
| 181 | + } else { |
| 182 | + file = getDefaultCredentialsFile(); |
| 183 | + if (null == file) { |
| 184 | + System.err.println("WARNING: " + DEFAULT_CREDENTIALS_FILE_PATH |
| 185 | + + " default credentials file location does not exist, please specify a location"); |
| 186 | + printUsageAndExit(); |
| 187 | + } |
| 188 | + } |
| 189 | + return file; |
| 190 | + } |
| 191 | + |
| 192 | + //////// |
| 193 | + // a possible default path to credentials properties file |
| 194 | + //////// |
| 195 | + |
| 196 | + private static final String USER_DOT_HOME = "user.home"; |
| 197 | + private static final String DOT_HERE_SUBDIR = ".here"; |
| 198 | + private static final String CREDENTIALS_DOT_PROPERTIES_FILENAME = "credentials.properties"; |
| 199 | + private static final String DEFAULT_CREDENTIALS_FILE_PATH = "~" + File.separatorChar + DOT_HERE_SUBDIR |
| 200 | + + File.separatorChar + CREDENTIALS_DOT_PROPERTIES_FILENAME; |
| 201 | + |
| 202 | + protected static File getDefaultCredentialsFile() { |
| 203 | + String userDotHome = System.getProperty(USER_DOT_HOME); |
| 204 | + if (userDotHome != null && userDotHome.length() > 0) { |
| 205 | + File dir = new File(userDotHome, DOT_HERE_SUBDIR); |
| 206 | + if (dir.exists() && dir.isDirectory()) { |
| 207 | + File file = new File(dir, CREDENTIALS_DOT_PROPERTIES_FILENAME); |
| 208 | + if (isFileAndExists(file)) { |
| 209 | + return file; |
| 210 | + } |
| 211 | + } |
| 212 | + } |
| 213 | + return null; |
| 214 | + } |
| 215 | + |
| 216 | + protected static boolean isFileAndExists(File file) { |
| 217 | + return file.exists() && file.isFile(); |
| 218 | + } |
| 219 | + |
| 220 | +} |
0 commit comments