Skip to content

Commit

Permalink
Add common name for pairing methods
Browse files Browse the repository at this point in the history
Add "manual test" for the pairing
  • Loading branch information
eozores-telefonica committed Nov 26, 2024
1 parent 22d3a4f commit 4777c49
Show file tree
Hide file tree
Showing 2 changed files with 257 additions and 3 deletions.
66 changes: 63 additions & 3 deletions src/main/java/com/elevenpaths/latch/LatchApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public LatchApp(String appId, String secretKey) {
}

/**
* Pairs the origin provider with a user account (mail).
* @param id The email for the pairing account - only useful in staging
* @return LatchResponse for the operation
*/
Expand All @@ -45,14 +46,19 @@ public LatchResponse pairWithId(String id) {
}

/**
* @param token The token for pairing the app, generated by the Latch mobile app
* Pairs the origin provider with a user account (mail).
* @param id The email for the pairing account - only useful in staging
* @param commonName Name attached to this pairing. Showed in admin panels.
* @return LatchResponse for the operation
*/
public LatchResponse pair(String token) {
return HTTP_GET_proxy(API_PAIR_URL+"/"+token);
public LatchResponse pairWithId(String id, String commonName) {
HashMap<String, String> data = new HashMap();
data.put("commonName", commonName);
return HTTP_POST_proxy(API_PAIR_WITH_ID_URL+"/"+id, data);
}

/**
* Pairs the origin provider with a user account (mail).
* @param id The email for the pairing account - only useful in staging
* @param web3Wallet The Ethereum-based account address to pairing the app
* @param web3Signature A proof-of-ownership signature with the account address
Expand All @@ -66,6 +72,44 @@ public LatchResponse pairWithId(String id, String web3Wallet, String web3Signatu
}

/**
* Pairs the origin provider with a user account (mail).
* @param id The email for the pairing account - only useful in staging
* @param commonName Name attached to this pairing. Showed in admin panels.
* @param web3Wallet The Ethereum-based account address to pairing the app
* @param web3Signature A proof-of-ownership signature with the account address
* @return LatchResponse for the operation
*/
public LatchResponse pairWithId(String id, String commonName, String web3Wallet, String web3Signature) {
HashMap<String, String> data = new HashMap();
data.put("commonName", commonName);
data.put("wallet", web3Wallet);
data.put("signature", web3Signature);
return HTTP_POST_proxy(API_PAIR_WITH_ID_URL+"/"+id, data);
}

/**
* Pairs the token provider with a user account.
* @param token The token for pairing the app, generated by the Latch mobile app
* @return LatchResponse for the operation
*/
public LatchResponse pair(String token) {
return HTTP_GET_proxy(API_PAIR_URL+"/"+token);
}

/**
* Pairs the token provider with a user account.
* @param token The token for pairing the app, generated by the Latch mobile app
* @param commonName Name attached to this pairing. Showed in admin panels.
* @return LatchResponse for the operation
*/
public LatchResponse pair(String token, String commonName) {
HashMap<String, String> data = new HashMap();
data.put("commonName", commonName);
return HTTP_POST_proxy(API_PAIR_URL+"/"+token, data);
}

/**
* Pairs the token provider with a user account.
* @param token The token for pairing the app, generated by the Latch mobile app
* @param web3Wallet The Ethereum-based account address to pairing the app
* @param web3Signature A proof-of-ownership signature with the account address
Expand All @@ -78,6 +122,22 @@ public LatchResponse pair(String token, String web3Wallet, String web3Signature)
return HTTP_POST_proxy(API_PAIR_URL+"/"+token, data);
}

/**
* Pairs the token provider with a user account.
* @param token The token for pairing the app, generated by the Latch mobile app
* @param commonName Name attached to this pairing. Showed in admin panels.
* @param web3Wallet The Ethereum-based account address to pairing the app
* @param web3Signature A proof-of-ownership signature with the account address
* @return LatchResponse for the operation
*/
public LatchResponse pair(String token, String commonName, String web3Wallet, String web3Signature) {
HashMap<String, String> data = new HashMap();
data.put("commonName", commonName);
data.put("wallet", web3Wallet);
data.put("signature", web3Signature);
return HTTP_POST_proxy(API_PAIR_URL+"/"+token, data);
}

/**
* Return application status for a given accountId
* @param accountId The accountId which status is going to be retrieved
Expand Down
194 changes: 194 additions & 0 deletions src/test/java/PairingManualTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
/*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 PairingManualTest {

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

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 String pair() {
LatchApp latchApp = new LatchApp(appId, secretId);
String token = readInput("Enter the pairing code (generated from mobile app): ");

String accountId = null;
LatchResponse latchResponse = latchApp.pair(token);
if (latchResponse.hasErrors()) {
System.out.println(String.format("Error pairing: %s", latchResponse.getError().getMessage()));
} else {
accountId = latchResponse.getData().get("accountId").getAsString();
}
return accountId;
}

public static String pairWithCommonName() {
LatchApp latchApp = new LatchApp(appId, secretId);
String token = readInput("Enter the pairing code (generated from mobile app): ");
String commonName = readInput("Enter the common name: ");

String accountId = null;
LatchResponse latchResponse = latchApp.pair(token, commonName);
if (latchResponse.hasErrors()) {
System.out.println(String.format("Error pairing: %s", latchResponse.getError().getMessage()));
} else {
accountId = latchResponse.getData().get("accountId").getAsString();
}
return accountId;
}

public static String pairWithWeb3() {
LatchApp latchApp = new LatchApp(appId, secretId);
String token = readInput("Enter the pairing code (generated from mobile app): ");
String web3Wallet = readInput("Enter the wallet for web3: ");
String web3Signature = readInput("Enter the signature for web3: ");

String accountId = null;
LatchResponse latchResponse = latchApp.pair(token, web3Wallet, web3Signature);
if (latchResponse.hasErrors()) {
System.out.println(String.format("Error pairing: %s", latchResponse.getError().getMessage()));
} else {
accountId = latchResponse.getData().get("accountId").getAsString();
}
return accountId;
}

public static String pairWithWeb3CommonName() {
LatchApp latchApp = new LatchApp(appId, secretId);
String token = readInput("Enter the pairing code (generated from mobile app): ");
String commonName = readInput("Enter the common name: ");
String web3Wallet = readInput("Enter the wallet for web3: ");
String web3Signature = readInput("Enter the signature for web3: ");

String accountId = null;
LatchResponse latchResponse = latchApp.pair(token, commonName, web3Wallet, web3Signature);
if (latchResponse.hasErrors()) {
System.out.println(String.format("Error pairing: %s", latchResponse.getError().getMessage()));
} else {
accountId = latchResponse.getData().get("accountId").getAsString();
}
return accountId;
}

public static void unpair(String accountId) {
LatchApp latchApp = new LatchApp(appId, secretId);
LatchResponse latchResponse = latchApp.unpair(accountId);
if (latchResponse.hasErrors()) {
System.out.println(String.format("Error unpairing: %s", latchResponse.getError().getMessage()));
} else {
System.out.println("Succesfull Unpairing");
}
}

public static String pairWithId() {
LatchApp latchApp = new LatchApp(appId, secretId);

String accountId = null;
LatchResponse latchResponse = latchApp.pairWithId(accountEmail);
if (latchResponse.hasErrors()) {
System.out.println(String.format("Error pairing: %s", latchResponse.getError().getMessage()));
} else {
accountId = latchResponse.getData().get("accountId").getAsString();
}
return accountId;
}

public static String pairWithIdWithCommonName() {
LatchApp latchApp = new LatchApp(appId, secretId);
String commonName = readInput("Enter the common name: ");

String accountId = null;
LatchResponse latchResponse = latchApp.pairWithId(accountEmail, commonName);
if (latchResponse.hasErrors()) {
System.out.println(String.format("Error pairing: %s", latchResponse.getError().getMessage()));
} else {
accountId = latchResponse.getData().get("accountId").getAsString();
}
return accountId;
}

public static String pairWithIdWithWeb3() {
LatchApp latchApp = new LatchApp(appId, secretId);
String web3Wallet = readInput("Enter the wallet for web3: ");
String web3Signature = readInput("Enter the signature for web3: ");

String accountId = null;
LatchResponse latchResponse = latchApp.pairWithId(accountEmail, web3Wallet, web3Signature);
if (latchResponse.hasErrors()) {
System.out.println(String.format("Error pairing: %s", latchResponse.getError().getMessage()));
} else {
accountId = latchResponse.getData().get("accountId").getAsString();
}
return accountId;
}

public static String pairWithIdWithWeb3CommonName() {
LatchApp latchApp = new LatchApp(appId, secretId);
String commonName = readInput("Enter the common name: ");
String web3Wallet = readInput("Enter the wallet for web3: ");
String web3Signature = readInput("Enter the signature for web3: ");

String accountId = null;
LatchResponse latchResponse = latchApp.pairWithId(accountEmail, commonName, web3Wallet, web3Signature);
if (latchResponse.hasErrors()) {
System.out.println(String.format("Error pairing: %s", latchResponse.getError().getMessage()));
} else {
accountId = latchResponse.getData().get("accountId").getAsString();
}
return accountId;
}


public static void main(String[] args) {
String accountId;
accountId = pair();
unpair(accountId);
accountId = pairWithCommonName();
unpair(accountId);
accountId = pairWithWeb3();
unpair(accountId);
accountId = pairWithWeb3CommonName();
unpair(accountId);

//Only development enviroments
// accountId = pairWithId();
// unpair(accountId);
// accountId = pairWithIdWithCommonName();
// unpair(accountId);
// accountId = pairWithIdWithWeb3();
// unpair(accountId);
// accountId = pairWithIdWithWeb3CommonName();
// unpair(accountId);
}
}

0 comments on commit 4777c49

Please sign in to comment.