Skip to content

Commit

Permalink
Add example totps
Browse files Browse the repository at this point in the history
Rename TestExampleWeb3 to ExampleWeb3
  • Loading branch information
eozores-telefonica committed Nov 25, 2024
1 parent 1a1cac1 commit 22d3a4f
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ You need this additional parameters:
- WEB3SIGNATURE: A proof-of-ownership signature of a constant, in order to verify that the user owns the private key of the wallet. You can use https://etherscan.io/verifiedSignatures# to sign the following message:
- MESSAGE TO SIGN : **"Latch-Web3"**

Example of using it [java example](src/test/java/TestExampleWeb3.java)
Example of using it [java example](src/test/java/ExampleWeb3.java)


#### TROUBLESHOOTING ####
Expand Down
72 changes: 72 additions & 0 deletions src/test/java/ExampleTotp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*Latch Java SDK - Set of reusable classes to allow developers integrate Latch on their applications.
Copyright (C) 2024 Telefonica Innovación Digital
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA*/

import com.elevenpaths.latch.LatchApp;
import com.elevenpaths.latch.LatchResponse;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class ExampleTotp {

public static String appId = "<Your appId>";
public static String secretId = "<your secret id>";

public static String readInput(String msg) {
System.out.print(msg);
BufferedReader reader = new BufferedReader(
new InputStreamReader(System.in));
try {
return reader.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
}

public static void createTotp() {
LatchApp latchApp = new LatchApp(appId, secretId);
String id = readInput("Enter the name displayed for the totp: ");
String commonName = readInput("Enter name ");
LatchResponse latchResponse = latchApp.createTotp(id,commonName);
if (latchResponse.hasErrors()) {
System.out.printf("Error creating the TOTP: %s%n", latchResponse.getError().getMessage());
} else {
System.out.println("Successful totp creating");
System.out.printf("Totp Id (Save it, you'll need it later): %s%n", latchResponse.getData().get("totpId"));
System.out.printf("QR (Scan the QR with the app, you can open it with any browse): %s%n", latchResponse.getData().get("qr"));
}
}

public static void validateTotp() {
LatchApp latchApp = new LatchApp(appId, secretId);
String totpId = readInput("Enter the identifier for the totp: ");
String code = readInput("Enter the code generated ");
LatchResponse latchResponse = latchApp.validateTotp(totpId,code);
if (latchResponse.hasErrors()) {
System.out.printf("Error validating the TOTP: %s%n", latchResponse.getError().getMessage());
} else {
System.out.println("Successful totp validating");
}
}


public static void main(String[] args) {
createTotp();
validateTotp();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import java.io.InputStreamReader;


public class TestExampleWeb3 {
public class ExampleWeb3 {

public static String appId = "<Your appId>";

Expand Down

0 comments on commit 22d3a4f

Please sign in to comment.