|
| 1 | +package com.bunq.tinker; |
| 2 | + |
| 3 | +import com.bunq.sdk.context.ApiContext; |
| 4 | +import com.bunq.sdk.context.ApiEnvironmentType; |
| 5 | +import com.bunq.sdk.context.BunqContext; |
| 6 | +import com.bunq.sdk.exception.BunqException; |
| 7 | +import com.bunq.sdk.json.BunqGsonBuilder; |
| 8 | +import com.bunq.sdk.model.core.OauthAuthorizationUri; |
| 9 | +import com.bunq.sdk.model.core.OauthResponseType; |
| 10 | +import com.bunq.sdk.model.generated.endpoint.OauthCallbackUrl; |
| 11 | +import com.bunq.sdk.model.generated.endpoint.OauthClient; |
| 12 | +import com.bunq.sdk.model.generated.object.Certificate; |
| 13 | +import com.bunq.sdk.security.SecurityUtils; |
| 14 | +import com.bunq.tinker.utils.ITinker; |
| 15 | +import com.google.gson.stream.JsonReader; |
| 16 | +import org.apache.commons.cli.*; |
| 17 | + |
| 18 | +import java.io.*; |
| 19 | +import java.nio.file.Files; |
| 20 | +import java.util.ArrayList; |
| 21 | + |
| 22 | +public class CreateOauthClient implements ITinker { |
| 23 | + /** |
| 24 | + * API constants. |
| 25 | + */ |
| 26 | + protected static final String OPTION_CONTEXT = "context"; |
| 27 | + protected static final String OPTION_REDIRECT_URI = "redirect"; |
| 28 | + |
| 29 | + /** |
| 30 | + * File constants. |
| 31 | + */ |
| 32 | + protected static final String FILE_OAUTH_CONFIGURATION = "oauth.conf"; |
| 33 | + |
| 34 | + /** |
| 35 | + * Error constants. |
| 36 | + */ |
| 37 | + protected static final String ERROR_MISSING_MANDATORY_OPTION = "Missing mandatory option."; |
| 38 | + |
| 39 | + @Override |
| 40 | + public void run(String[] args) throws Exception { |
| 41 | + Options options = new Options(); |
| 42 | + options.addOption(new Option("", OPTION_CONTEXT, true, "")); |
| 43 | + options.addOption(new Option("", OPTION_REDIRECT_URI, true, "")); |
| 44 | + |
| 45 | + CommandLineParser parser = new BasicParser(); |
| 46 | + CommandLine allOption = parser.parse(options, args); |
| 47 | + |
| 48 | + assertMandatoryOptions(allOption); |
| 49 | + |
| 50 | + BunqContext.loadApiContext( |
| 51 | + ApiContext.restore(allOption.getOptionValue(OPTION_CONTEXT)) |
| 52 | + ); |
| 53 | + |
| 54 | + OauthClient oauthClient; |
| 55 | + File oauthFile = new File(FILE_OAUTH_CONFIGURATION); |
| 56 | + if (oauthFile.exists()) { |
| 57 | + oauthClient = createOauthClientFromFile(oauthFile.getPath()); |
| 58 | + } else { |
| 59 | + Integer oauthClientId = OauthClient.create().getValue(); |
| 60 | + |
| 61 | + OauthCallbackUrl.create(oauthClientId, allOption.getOptionValue(OPTION_REDIRECT_URI)); |
| 62 | + oauthClient = OauthClient.get(oauthClientId).getValue(); |
| 63 | + |
| 64 | + String serializedClient = BunqGsonBuilder.buildDefault().create().toJson(oauthClient); |
| 65 | + FileWriter fileWriter = new FileWriter(FILE_OAUTH_CONFIGURATION); |
| 66 | + |
| 67 | + fileWriter.write(serializedClient); |
| 68 | + fileWriter.close(); |
| 69 | + } |
| 70 | + |
| 71 | + OauthAuthorizationUri authorizationUri = OauthAuthorizationUri.create( |
| 72 | + OauthResponseType.CODE, |
| 73 | + allOption.getOptionValue(OPTION_REDIRECT_URI), |
| 74 | + oauthClient |
| 75 | + ); |
| 76 | + |
| 77 | + System.out.println(" | Created OAuth client!"); |
| 78 | + System.out.println(" | Point your user to the following URL to obtain an auth code:"); |
| 79 | + System.out.println(" | " + authorizationUri.getAuthorizationUri()); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + */ |
| 84 | + private void assertMandatoryOptions(CommandLine allOption) |
| 85 | + { |
| 86 | + if (allOption.hasOption(OPTION_CONTEXT) && |
| 87 | + allOption.hasOption(OPTION_REDIRECT_URI)) { |
| 88 | + return; |
| 89 | + } |
| 90 | + throw new BunqException(ERROR_MISSING_MANDATORY_OPTION); |
| 91 | + } |
| 92 | + |
| 93 | + |
| 94 | + private OauthClient createOauthClientFromFile(String path) throws IOException |
| 95 | + { |
| 96 | + return OauthClient.fromJsonReader( |
| 97 | + new JsonReader(new InputStreamReader(new FileInputStream(path))) |
| 98 | + ); |
| 99 | + } |
| 100 | +} |
0 commit comments