Compare commits
No commits in common. "c8d733dfac56b3f409671bda716d4b39cf3f5457" and "c4048c20b63109441b02747329f5dd067c3355cf" have entirely different histories.
c8d733dfac
...
c4048c20b6
@ -9,20 +9,20 @@ import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import djmil.cordacheckers.cordaclient.CordaClient;
|
||||
import djmil.cordacheckers.cordaclient.dao.VirtualNode;
|
||||
import djmil.cordacheckers.cordaclient.pojo.virtualNodes;
|
||||
|
||||
@RestController
|
||||
public class ApiController {
|
||||
|
||||
@Autowired
|
||||
CordaClient cordaClient;
|
||||
CordaClient cordaclient;
|
||||
|
||||
@GetMapping("/api/badjokes")
|
||||
public ResponseEntity<Joke> badJokes() {
|
||||
|
||||
List<VirtualNode> vNodeList = cordaClient.getVirtualNodeList();
|
||||
List<virtualNodes> vNodes = cordaclient.getVirtualnode();
|
||||
|
||||
Joke joke = new Joke("What do you call a fly without wings? A walk! " + vNodeList.get(1).holdingIdentity().x500Name());
|
||||
Joke joke = new Joke("What do you call a fly without wings? A walk! " + vNodes.get(1).holdingIdentity().x500Name());
|
||||
|
||||
return ResponseEntity.ok(joke);
|
||||
}
|
||||
@ -31,13 +31,13 @@ public class ApiController {
|
||||
*
|
||||
* @return a Json list of active games
|
||||
*/
|
||||
@GetMapping("/api/gameproposals")
|
||||
@GetMapping("/api/activegames")
|
||||
public ResponseEntity<String> dashboard(@AuthenticationPrincipal ApiUserDetails user) {
|
||||
System.out.println("List of active games for "
|
||||
+ "user: " + user.getUsername()
|
||||
+ " with HoldingIdentity ShortHash: " + user.getHoldingIdentity().shortHash());
|
||||
|
||||
return ResponseEntity.ok("{ \"UnconsumedGameProposals\" : [\"id_game1\", \"id_game2\"] }" );
|
||||
+ " with shortIdentityHash: " + user.getShortHash());
|
||||
|
||||
return ResponseEntity.ok("{ \"ActiveGames\" : [\"id_game1\", \"id_game2\"] }" );
|
||||
}
|
||||
|
||||
}
|
@ -3,19 +3,17 @@ package djmil.cordacheckers;
|
||||
import org.springframework.security.core.userdetails.User;
|
||||
import org.springframework.security.core.userdetails.UserDetails;
|
||||
|
||||
import djmil.cordacheckers.cordaclient.dao.HoldingIdentity;
|
||||
|
||||
public class ApiUserDetails extends User {
|
||||
private final HoldingIdentity holdingIdentity;
|
||||
private final String shortHash;
|
||||
|
||||
public ApiUserDetails(UserDetails user, HoldingIdentity holdingIdentity) {
|
||||
public ApiUserDetails(UserDetails user, String shortHash) {
|
||||
super(user.getUsername(), user.getPassword(), user.isEnabled(), user.isAccountNonExpired(), user.isCredentialsNonExpired(), user.isAccountNonLocked(), user.getAuthorities());
|
||||
|
||||
this.holdingIdentity = holdingIdentity;
|
||||
this.shortHash = shortHash;
|
||||
}
|
||||
|
||||
public HoldingIdentity getHoldingIdentity() {
|
||||
return this.holdingIdentity;
|
||||
public String getShortHash() {
|
||||
return this.shortHash;
|
||||
}
|
||||
|
||||
}
|
@ -7,27 +7,25 @@ import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import djmil.cordacheckers.cordaclient.HoldingIdentityResolver;
|
||||
import djmil.cordacheckers.cordaclient.dao.HoldingIdentity;
|
||||
|
||||
@Service
|
||||
public class ApiUserDetailsService implements UserDetailsService {
|
||||
|
||||
private final PasswordEncoder encoder;
|
||||
private final HoldingIdentityResolver holdingIdentityResolver;
|
||||
private final ShortHashManager shortHashManager;
|
||||
|
||||
public ApiUserDetailsService(
|
||||
PasswordEncoder encoder,
|
||||
HoldingIdentityResolver holdingIdentityNameResolver) {
|
||||
ShortHashManager shortHashManager) {
|
||||
this.encoder = encoder;
|
||||
this.holdingIdentityResolver = holdingIdentityNameResolver;
|
||||
this.shortHashManager = shortHashManager;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ApiUserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
|
||||
HoldingIdentity holdingIdentity = holdingIdentityResolver.getByCommonName(username);
|
||||
if (holdingIdentity == null) {
|
||||
throw new UsernameNotFoundException("Can't find HoldingIdentity for the user '"+username+ "'");
|
||||
String shortHash = shortHashManager.getShortHashBy(username);
|
||||
if (shortHash == null) {
|
||||
throw new UsernameNotFoundException("ShortHash for user '"
|
||||
+username+ "' not found");
|
||||
}
|
||||
|
||||
System.out.println("Load user "+username);
|
||||
@ -38,6 +36,6 @@ public class ApiUserDetailsService implements UserDetailsService {
|
||||
.password(encoder.encode("qaz123"))
|
||||
.build();
|
||||
|
||||
return new ApiUserDetails(user, holdingIdentity);
|
||||
return new ApiUserDetails(user, shortHash);
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,51 @@
|
||||
package djmil.cordacheckers;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.naming.InvalidNameException;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import djmil.cordacheckers.cordaclient.CordaClient;
|
||||
import djmil.cordacheckers.cordaclient.pojo.virtualNodes;
|
||||
|
||||
@Service
|
||||
public class ShortHashManager {
|
||||
static final Locale locale = Locale.getDefault();
|
||||
|
||||
Map<String, String> cnName2shortHash;
|
||||
|
||||
ShortHashManager(CordaClient client) {
|
||||
this.cnName2shortHash = setCnName2shortHash(client);
|
||||
}
|
||||
|
||||
private static Map<String, String> setCnName2shortHash(CordaClient client) {
|
||||
Map<String, String> map = new HashMap<>();
|
||||
|
||||
List<virtualNodes> vNodesList = client.getVirtualnode();
|
||||
|
||||
try {
|
||||
for (virtualNodes vNode : vNodesList) {
|
||||
var identity = vNode.holdingIdentity();
|
||||
|
||||
if (identity.isPlayer()) {
|
||||
map.put(identity.getName().toLowerCase(locale), identity.shortHash());
|
||||
}
|
||||
}
|
||||
} catch (InvalidNameException e) {
|
||||
// TODO: logs
|
||||
System.out.println("Unable to get ShorHash map for Corda virtual nodes: "+e.getExplanation());
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
System.out.println("ApiUserShortHashMap " + map);
|
||||
return map;
|
||||
}
|
||||
|
||||
String getShortHashBy(String apiUserName) {
|
||||
return this.cnName2shortHash.get(apiUserName.toLowerCase(locale));
|
||||
}
|
||||
}
|
@ -10,10 +10,8 @@ import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import djmil.cordacheckers.cordaclient.dao.VirtualNode;
|
||||
import djmil.cordacheckers.cordaclient.dao.VirtualNodeList;
|
||||
|
||||
import static java.util.Objects.requireNonNull;
|
||||
import djmil.cordacheckers.cordaclient.pojo.virtualNodes;
|
||||
import djmil.cordacheckers.cordaclient.pojo.virtualnode;
|
||||
|
||||
@Service
|
||||
public class CordaClient {
|
||||
@ -27,41 +25,26 @@ public class CordaClient {
|
||||
this.restTemplate = restTemplate;
|
||||
}
|
||||
|
||||
public List<VirtualNode> getVirtualNodeList() {
|
||||
public List<virtualNodes> getVirtualnode() {
|
||||
|
||||
// Request authorization header
|
||||
HttpHeaders headers = basicAuthorizationHeader();
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.setBasicAuth("admin", "admin");
|
||||
|
||||
// Request
|
||||
final HttpEntity<String> request = new HttpEntity<>(headers);
|
||||
|
||||
ResponseEntity<VirtualNodeList> resp = this.restTemplate.exchange(
|
||||
ResponseEntity<virtualnode> resp = this.restTemplate.exchange(
|
||||
"https://localhost:8888/api/v1/virtualnode",
|
||||
HttpMethod.GET,
|
||||
request,
|
||||
VirtualNodeList.class );
|
||||
virtualnode.class );
|
||||
|
||||
if (resp.getStatusCode() != HttpStatus.OK) {
|
||||
throw new RuntimeException("CordaClient.getVirtualNodeList: unexpected request status "+resp.getStatusCode()) ;
|
||||
// TODO: throw exeption instead
|
||||
if (resp.getStatusCode() != HttpStatus.OK || !resp.hasBody()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return requireNonNull(resp.getBody(), "CordaClient.getVirtualNodeList: empty getBody()")
|
||||
.virtualNodes();
|
||||
}
|
||||
|
||||
// public String getGemeProposals(String ) {
|
||||
// // Request authorization header
|
||||
// HttpHeaders headers = basicAuthorizationHeader();
|
||||
|
||||
// // Request
|
||||
// final HttpEntity<String> request = new HttpEntity<>(headers);
|
||||
|
||||
// }
|
||||
|
||||
private HttpHeaders basicAuthorizationHeader() {
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.setBasicAuth("admin", "admin");
|
||||
|
||||
return headers;
|
||||
return resp.getBody().virtualNodes();
|
||||
}
|
||||
}
|
||||
|
@ -1,63 +0,0 @@
|
||||
package djmil.cordacheckers.cordaclient;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.naming.InvalidNameException;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import djmil.cordacheckers.cordaclient.dao.HoldingIdentity;
|
||||
import djmil.cordacheckers.cordaclient.dao.VirtualNode;
|
||||
|
||||
@Service
|
||||
public class HoldingIdentityResolver {
|
||||
static final Locale locale = Locale.getDefault();
|
||||
|
||||
/*
|
||||
* NOTE: Proof-of-Concept impementation
|
||||
* Ideally, we want to be able to update HoldingIdentity cache, had the
|
||||
* corda cluster configuration changed (aka in case of a cache miss)
|
||||
*/
|
||||
final Map<String, HoldingIdentity> cache;
|
||||
|
||||
HoldingIdentityResolver(CordaClient cordaClient) {
|
||||
this.cache = setCache(cordaClient);
|
||||
}
|
||||
|
||||
private static Map<String, HoldingIdentity> setCache(CordaClient cordaClient) {
|
||||
Map<String, HoldingIdentity> map = new HashMap<>();
|
||||
|
||||
List<VirtualNode> vNodeList = cordaClient.getVirtualNodeList();
|
||||
|
||||
try {
|
||||
for (VirtualNode vNode : vNodeList) {
|
||||
var identity = vNode.holdingIdentity();
|
||||
|
||||
if (identity.isPlayer()) {
|
||||
map.put(identity.getName().toLowerCase(locale), identity);
|
||||
}
|
||||
}
|
||||
} catch (InvalidNameException e) {
|
||||
// TODO: logs
|
||||
System.out.println("Unable to get ShorHash map for Corda virtual nodes: "+e.getExplanation());
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return map;
|
||||
}
|
||||
|
||||
/*
|
||||
* @param apiUserName
|
||||
* HoldingIdentity x500 name typically looks like
|
||||
* "CN=Bob, OU=Player, O=Checkers, L=Kviv, C=UA"
|
||||
* CN - is a common name, expected to be unique for CordaCheckers setup.
|
||||
*
|
||||
* @return HoldingIdentity
|
||||
*/
|
||||
public HoldingIdentity getByCommonName(String apiUserName) {
|
||||
return this.cache.get(apiUserName.toLowerCase(locale));
|
||||
}
|
||||
}
|
@ -1,6 +0,0 @@
|
||||
package djmil.cordacheckers.cordaclient.dao;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public record VirtualNode(HoldingIdentity holdingIdentity) { }
|
@ -1,6 +1,4 @@
|
||||
package djmil.cordacheckers.cordaclient.dao;
|
||||
|
||||
import java.io.Serializable;
|
||||
package djmil.cordacheckers.cordaclient.pojo;
|
||||
|
||||
import javax.naming.InvalidNameException;
|
||||
import javax.naming.ldap.LdapName;
|
||||
@ -9,7 +7,7 @@ import javax.naming.ldap.Rdn;
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public record HoldingIdentity(String x500Name, String shortHash) implements Serializable {
|
||||
public record holdingIdentity(String x500Name, String shortHash) {
|
||||
|
||||
public String getName() throws InvalidNameException {
|
||||
LdapName ln = new LdapName(x500Name);
|
||||
@ -20,7 +18,7 @@ public record HoldingIdentity(String x500Name, String shortHash) implements Seri
|
||||
}
|
||||
}
|
||||
|
||||
throw new IllegalArgumentException("CN not found: "+ x500Name);
|
||||
throw new IllegalArgumentException("CN was not found");
|
||||
}
|
||||
|
||||
public boolean isPlayer() throws InvalidNameException {
|
@ -0,0 +1,6 @@
|
||||
package djmil.cordacheckers.cordaclient.pojo;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public record virtualNodes(holdingIdentity holdingIdentity) { }
|
@ -1,8 +1,8 @@
|
||||
package djmil.cordacheckers.cordaclient.dao;
|
||||
package djmil.cordacheckers.cordaclient.pojo;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public record VirtualNodeList(List<VirtualNode> virtualNodes) { }
|
||||
public record virtualnode(List<virtualNodes> virtualNodes) { }
|
@ -6,8 +6,8 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
import djmil.cordacheckers.cordaclient.CordaClient;
|
||||
import djmil.cordacheckers.cordaclient.dao.HoldingIdentity;
|
||||
import djmil.cordacheckers.cordaclient.dao.VirtualNode;
|
||||
import djmil.cordacheckers.cordaclient.pojo.holdingIdentity;
|
||||
import djmil.cordacheckers.cordaclient.pojo.virtualNodes;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@ -24,10 +24,10 @@ public class CordaClientTest {
|
||||
|
||||
@Test
|
||||
void whenGetVirtualnode_thenListHoldingIdentity() throws GeneralSecurityException, IOException, InvalidNameException {
|
||||
List<VirtualNode> vNodes = cordaclient.getVirtualNodeList();
|
||||
List<virtualNodes> vNodes = cordaclient.getVirtualnode();
|
||||
|
||||
HoldingIdentity identity = vNodes.get(0).holdingIdentity();
|
||||
assertThat(identity.getName()).isEqualTo("Bob");
|
||||
holdingIdentity identity = vNodes.get(0).holdingIdentity();
|
||||
assertThat(identity.getName()).isEqualTo("NotaryRep1");
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -27,8 +27,8 @@ import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import djmil.cordacheckers.cordaclient.dao.HoldingIdentity;
|
||||
import djmil.cordacheckers.cordaclient.dao.VirtualNodeList;
|
||||
import djmil.cordacheckers.cordaclient.pojo.holdingIdentity;
|
||||
import djmil.cordacheckers.cordaclient.pojo.virtualnode;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@ -80,19 +80,19 @@ class CordacheckersApplicationTests {
|
||||
// Request
|
||||
final HttpEntity<String> request = new HttpEntity<>(headers);
|
||||
|
||||
final ResponseEntity<VirtualNodeList> response = new RestTemplate(requestFactory)
|
||||
.exchange("https://localhost:8888/api/v1/virtualnode", HttpMethod.GET, request, VirtualNodeList.class);
|
||||
final ResponseEntity<virtualnode> response = new RestTemplate(requestFactory)
|
||||
.exchange("https://localhost:8888/api/v1/virtualnode", HttpMethod.GET, request, virtualnode.class);
|
||||
|
||||
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
|
||||
assertThat(response.hasBody());
|
||||
|
||||
VirtualNodeList vNodeList = response.getBody();
|
||||
assertThat(vNodeList).isNotNull();
|
||||
if (vNodeList != null) {
|
||||
assertThat(vNodeList.virtualNodes().size() == 5);
|
||||
virtualnode vNode = response.getBody();
|
||||
assertThat(vNode).isNotNull();
|
||||
if (vNode != null) {
|
||||
assertThat(vNode.virtualNodes().size() == 5);
|
||||
}
|
||||
|
||||
HoldingIdentity identity = vNodeList.virtualNodes().get(0).holdingIdentity();
|
||||
holdingIdentity identity = vNode.virtualNodes().get(0).holdingIdentity();
|
||||
assertThat(identity.x500Name().contains("NotaryRep1"));
|
||||
}
|
||||
}
|
||||
|
@ -2,7 +2,7 @@ package djmil.cordacheckers;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import djmil.cordacheckers.cordaclient.dao.HoldingIdentity;
|
||||
import djmil.cordacheckers.cordaclient.pojo.holdingIdentity;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@ -14,8 +14,8 @@ public class HoldingIdentityTest {
|
||||
@Test
|
||||
public void isPlayerTest() throws InvalidNameException {
|
||||
|
||||
HoldingIdentity alice = new HoldingIdentity("CN=Alice, OU=Player, O=Checkers, L=Zug, C=CH", "HHHDDD");
|
||||
HoldingIdentity bob = new HoldingIdentity("CN=Bob, OU=Other, O=Checkers, L=Zug, C=CH", "HHHDDD");
|
||||
holdingIdentity alice = new holdingIdentity("CN=Alice, OU=Player, O=Checkers, L=Zug, C=CH", "HHHDDD");
|
||||
holdingIdentity bob = new holdingIdentity("CN=Bob, OU=Other, O=Checkers, L=Zug, C=CH", "HHHDDD");
|
||||
|
||||
assertThat(alice.getName()).isEqualTo("Alice");
|
||||
assertThat(alice.isPlayer()).isEqualTo(true);
|
||||
|
Loading…
Reference in New Issue
Block a user