Skip to content

Commit

Permalink
Authenticate users as Catalog.USER_NAME if their peer name matches th…
Browse files Browse the repository at this point in the history
…e property user.name
  • Loading branch information
gartens committed Apr 9, 2024
1 parent 7ead805 commit 3fa7758
Showing 1 changed file with 27 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<LogicalUser> 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 );
Expand All @@ -122,6 +134,7 @@ public Stream<Entry<String, PIClient>> getClients() {
return clients.entrySet().stream();
}


public int getClientCount() {
return clients.size();
}
Expand Down

0 comments on commit 3fa7758

Please sign in to comment.