diff --git a/plugins/proto-interface/src/main/java/org/polypheny/db/protointerface/ClientManager.java b/plugins/proto-interface/src/main/java/org/polypheny/db/protointerface/ClientManager.java index 18a8d24dde..426cdc56e3 100644 --- a/plugins/proto-interface/src/main/java/org/polypheny/db/protointerface/ClientManager.java +++ b/plugins/proto-interface/src/main/java/org/polypheny/db/protointerface/ClientManager.java @@ -76,28 +76,40 @@ public void unregisterConnection( PIClient client ) { } - public String registerConnection( ConnectionRequest connectionRequest, Transport t ) throws AuthenticationException, TransactionException, PIServiceException { - byte[] raw = new byte[32]; - new SecureRandom().nextBytes( raw ); - String uuid = Base64.getUrlEncoder().encodeToString( raw ); - - if ( log.isTraceEnabled() ) { - log.trace( "User {} tries to establish connection via proto interface.", uuid ); - } - final LogicalUser user; + public LogicalUser getUser( ConnectionRequest connectionRequest, Transport t ) throws AuthenticationException { if ( connectionRequest.hasUsername() ) { String username = connectionRequest.getUsername(); if ( !connectionRequest.hasPassword() ) { throw new AuthenticationException( "A password is required" ); } String password = connectionRequest.getPassword(); - user = authenticator.authenticate( username, password ); - } else { - user = t.getPeer() - .flatMap( u -> Catalog.getInstance().getSnapshot().getUser( u ) ) - .orElseThrow( () -> new AuthenticationException( "Peer authentication failed: No user with that name" ) ); + return authenticator.authenticate( username, password ); + } else if ( t.getPeer().isPresent() ) { + String username = t.getPeer().get(); + Optional catalogUser = Catalog.getInstance().getSnapshot().getUser( username ); + if ( catalogUser.isPresent() ) { + return catalogUser.get(); + } else { + if ( username.equals( System.getProperty( "user.name" ) ) ) { + return Catalog.getInstance().getSnapshot().getUser( Catalog.USER_NAME ).orElseThrow(); + } else { + throw new AuthenticationException( "Peer authentication failed: No user with that name" ); + } + } } + throw new AuthenticationException( "Authentication failed" ); + } + + public String registerConnection( ConnectionRequest connectionRequest, Transport t ) throws AuthenticationException, TransactionException, PIServiceException { + byte[] raw = new byte[32]; + new SecureRandom().nextBytes( raw ); + String uuid = Base64.getUrlEncoder().encodeToString( raw ); + + if ( log.isTraceEnabled() ) { + log.trace( "User {} tries to establish connection via proto interface.", uuid ); + } + final LogicalUser user = getUser( connectionRequest, t ); Transaction transaction = transactionManager.startTransaction( user.id, false, "proto-interface" ); transaction.commit(); LogicalNamespace namespace = getNamespaceOrDefault( connectionRequest ); @@ -122,6 +134,7 @@ public Stream> getClients() { return clients.entrySet().stream(); } + public int getClientCount() { return clients.size(); }