Skip to content

Commit

Permalink
Web3j account login|logout|create is functional with my MVP
Browse files Browse the repository at this point in the history
  • Loading branch information
josh-richardson authored and AlexandrouR committed Mar 2, 2020
1 parent ca2aa7f commit c6f13ba
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 14 deletions.
4 changes: 4 additions & 0 deletions src/main/java/org/web3j/console/Runner.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.web3j.codegen.Console;
import org.web3j.codegen.SolidityFunctionWrapperGenerator;
import org.web3j.codegen.TruffleJsonFunctionWrapperGenerator;
import org.web3j.console.account.AccountManager;
import org.web3j.console.config.CliConfig;
import org.web3j.console.project.ProjectCreator;
import org.web3j.console.project.ProjectImporter;
Expand Down Expand Up @@ -85,6 +86,9 @@ public static void main(String[] args) throws Exception {
case "audit":
ContractAuditor.main(tail(args));
break;
case "account":
AccountManager.main(config, tail(args));
break;
case COMMAND_GENERATE_TESTS:
UnitTestCreator.main(tail(args));
break;
Expand Down
111 changes: 111 additions & 0 deletions src/main/java/org/web3j/console/account/AccountManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
* Copyright 2020 Web3 Labs Ltd.
*
* 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
*
* http://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 org.web3j.console.account;

import java.io.IOException;
import java.util.Properties;
import java.util.Scanner;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import okhttp3.*;
import org.web3j.console.config.CliConfig;

import static org.web3j.codegen.Console.exitError;

public class AccountManager {
private static final String USAGE = "account login|logout|create";
private static final String CLOUD_URL = "http://localhost:8000";

public static void main(final CliConfig config, final String[] args) {
OkHttpClient client = new OkHttpClient();

Scanner console = new Scanner(System.in);
if (args[0].equals("create")) {
if (config.getLoginToken() != null) {
exitError("You are already logged in. To create a new account, please log out first.");
}

System.out.println("Please enter your email address: ");
String email = console.nextLine().trim();

RequestBody accountBody =
new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("email", email)
.build();

Request newAccountRequest =
new okhttp3.Request.Builder()
.url(String.format("%s/api/users/create/", CLOUD_URL))
.post(accountBody)
.build();

try {
Response sendRawResponse = client.newCall(newAccountRequest).execute();
ResponseBody body;
if (sendRawResponse.code() == 200 && (body = sendRawResponse.body()) != null) {
String rawResponse = body.string();
JsonObject responseJsonObj = JsonParser.parseString(rawResponse).getAsJsonObject();
String token = responseJsonObj.get("token").getAsString();
config.setLoginToken(token);
System.out.println("Account created successfully. You can now use Web3j Cloud. Please confirm your e-mail within 24 hours to continue using all features without interruption.");
}
} catch (IOException e) {
e.printStackTrace();
}


} else if (args[0].equals("login")) {
System.out.println("Please enter your email address: ");
String email = console.nextLine().trim();
System.out.println("Please enter your password: ");
String password = console.nextLine().trim();


RequestBody loginBody =
new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("username", email)
.addFormDataPart("password", password)
.build();

Request loginRequest =
new okhttp3.Request.Builder()
.url(String.format("%s/api/api-token-auth/", CLOUD_URL))
.post(loginBody)
.build();

try {
Response sendRawResponse = client.newCall(loginRequest).execute();
ResponseBody body;
if (sendRawResponse.code() == 200 && (body = sendRawResponse.body()) != null) {
String rawResponse = body.string();
JsonObject responseJsonObj = JsonParser.parseString(rawResponse).getAsJsonObject();
String token = responseJsonObj.get("token").getAsString();
config.setLoginToken(token);
System.out.println("You have been successfully logged in to Web3j Cloud.");
}
} catch (IOException e) {
e.printStackTrace();
}


} else if (args[0].equals("logout")) {
config.setLoginToken(null);
} else {
exitError(USAGE);
}
}
}
38 changes: 25 additions & 13 deletions src/main/java/org/web3j/console/config/CliConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ private static CliConfig initializeDefaultConfig(File configFile) throws IOExcep
defaultServicesUrl,
UUID.randomUUID().toString(),
Version.getVersion(),
null,
null);
}

Expand All @@ -60,18 +61,6 @@ public static CliConfig getConfig(File configFile) throws IOException {
}
}

public String getLatestVersion() {
return latestVersion;
}

public void setLatestVersion(String latestVersion) {
this.latestVersion = latestVersion;
}

public boolean isUpdateAvailable() {
return !version.equals(latestVersion);
}

public enum OS {
DARWIN,
FREEBSD,
Expand Down Expand Up @@ -109,6 +98,26 @@ public static OS determineOS() {
}
}

public String getLatestVersion() {
return latestVersion;
}

public void setLatestVersion(String latestVersion) {
this.latestVersion = latestVersion;
}

public boolean isUpdateAvailable() {
return !version.equals(latestVersion);
}

public String getLoginToken() {
return loginToken;
}

public void setLoginToken(final String loginToken) {
this.loginToken = loginToken;
}

public void setVersion(final String version) {
this.version = version;
}
Expand All @@ -118,18 +127,21 @@ public void setVersion(final String version) {
private String clientId;
private String latestVersion;
private String updatePrompt;
private String loginToken;

public CliConfig(
String version,
String servicesUrl,
String clientId,
String latestVersion,
String updatePrompt) {
String updatePrompt,
String loginToken) {
this.version = version;
this.servicesUrl = servicesUrl;
this.clientId = clientId;
this.latestVersion = latestVersion;
this.updatePrompt = updatePrompt;
this.loginToken = loginToken;
}

public String getVersion() {
Expand Down
3 changes: 2 additions & 1 deletion src/test/java/org/web3j/console/project/UpdaterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ private void testWorksWithVersion(String version) throws IOException {
config.getServicesUrl(),
config.getClientId(),
config.getLatestVersion(),
config.getUpdatePrompt()));
config.getUpdatePrompt(),
null));
Files.write(
tempWeb3jSettingsPath,
jsonToWrite.getBytes(Charset.defaultCharset()));
Expand Down

0 comments on commit c6f13ba

Please sign in to comment.