GUACAMOLE-542: Migrate to simpler AbstractAuthenticationProvider / AbstractUserContext base classes.

This commit is contained in:
Michael Jumper
2018-04-11 17:03:39 -07:00
parent 18136d5146
commit 9b7ef0dfcf
15 changed files with 290 additions and 725 deletions

View File

@@ -0,0 +1,81 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.guacamole.net.auth;
import org.apache.guacamole.GuacamoleException;
/**
* Base implementation of AuthenticationProvider which provides default
* implementations of most functions. Implementations must provide their
* own {@link getIdentifier()}, but otherwise need only override an implemented
* function if they wish to actually implement the functionality defined for
* that function by the AuthenticationProvider interface.
*/
public abstract class AbstractAuthenticationProvider implements AuthenticationProvider {
@Override
public Object getResource() throws GuacamoleException {
return null;
}
@Override
public AuthenticatedUser authenticateUser(Credentials credentials)
throws GuacamoleException {
return null;
}
@Override
public AuthenticatedUser updateAuthenticatedUser(AuthenticatedUser authenticatedUser,
Credentials credentials) throws GuacamoleException {
return authenticatedUser;
}
@Override
public UserContext getUserContext(AuthenticatedUser authenticatedUser)
throws GuacamoleException {
return null;
}
@Override
public UserContext updateUserContext(UserContext context,
AuthenticatedUser authenticatedUser,
Credentials credentials) throws GuacamoleException {
return context;
}
@Override
public UserContext decorate(UserContext context,
AuthenticatedUser authenticatedUser,
Credentials credentials) throws GuacamoleException {
return context;
}
@Override
public UserContext redecorate(UserContext decorated, UserContext context,
AuthenticatedUser authenticatedUser,
Credentials credentials) throws GuacamoleException {
return decorate(context, authenticatedUser, credentials);
}
@Override
public void shutdown() {
}
}

View File

@@ -0,0 +1,126 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.guacamole.net.auth;
import java.util.Collection;
import java.util.Collections;
import org.apache.guacamole.GuacamoleException;
import org.apache.guacamole.form.Form;
import org.apache.guacamole.net.auth.simple.SimpleActivityRecordSet;
import org.apache.guacamole.net.auth.simple.SimpleConnectionGroup;
import org.apache.guacamole.net.auth.simple.SimpleDirectory;
/**
* Base implementation of UserContext which provides default implementations of
* most functions. Implementations must provide their own {@link self()} and
* {@link getAuthenticationProvider()}, but otherwise need only override an
* implemented function if they wish to actually implement the functionality
* defined for that function by the UserContext interface.
*/
public abstract class AbstractUserContext implements UserContext {
/**
* The unique identifier that will be used for the root connection group if
* {@link #getRootConnectionGroup()} is not overridden.
*/
protected static final String DEFAULT_ROOT_CONNECTION_GROUP = "ROOT";
@Override
public Object getResource() throws GuacamoleException {
return null;
}
@Override
public Directory<User> getUserDirectory() throws GuacamoleException {
return new SimpleDirectory<User>(self());
}
@Override
public Directory<Connection> getConnectionDirectory()
throws GuacamoleException {
return new SimpleDirectory<Connection>();
}
@Override
public Directory<ConnectionGroup> getConnectionGroupDirectory()
throws GuacamoleException {
return new SimpleDirectory<ConnectionGroup>(getRootConnectionGroup());
}
@Override
public Directory<ActiveConnection> getActiveConnectionDirectory()
throws GuacamoleException {
return new SimpleDirectory<ActiveConnection>();
}
@Override
public Directory<SharingProfile> getSharingProfileDirectory()
throws GuacamoleException {
return new SimpleDirectory<SharingProfile>();
}
@Override
public ActivityRecordSet<ConnectionRecord> getConnectionHistory()
throws GuacamoleException {
return new SimpleActivityRecordSet<ConnectionRecord>();
}
@Override
public ActivityRecordSet<ActivityRecord> getUserHistory()
throws GuacamoleException {
return new SimpleActivityRecordSet<ActivityRecord>();
}
@Override
public ConnectionGroup getRootConnectionGroup()
throws GuacamoleException {
return new SimpleConnectionGroup(
DEFAULT_ROOT_CONNECTION_GROUP,
DEFAULT_ROOT_CONNECTION_GROUP,
getConnectionDirectory().getIdentifiers(),
Collections.<String>emptySet()
);
}
@Override
public Collection<Form> getUserAttributes() {
return Collections.<Form>emptyList();
}
@Override
public Collection<Form> getConnectionAttributes() {
return Collections.<Form>emptyList();
}
@Override
public Collection<Form> getConnectionGroupAttributes() {
return Collections.<Form>emptyList();
}
@Override
public Collection<Form> getSharingProfileAttributes() {
return Collections.<Form>emptyList();
}
@Override
public void invalidate() {
}
}

View File

@@ -23,6 +23,7 @@ import java.util.Map;
import java.util.UUID;
import org.apache.guacamole.GuacamoleException;
import org.apache.guacamole.net.auth.AbstractAuthenticatedUser;
import org.apache.guacamole.net.auth.AbstractAuthenticationProvider;
import org.apache.guacamole.net.auth.AuthenticationProvider;
import org.apache.guacamole.net.auth.AuthenticatedUser;
import org.apache.guacamole.net.auth.Credentials;
@@ -42,7 +43,7 @@ import org.apache.guacamole.token.TokenFilter;
* the AuthenticationProvider interface of older Guacamole releases.
*/
public abstract class SimpleAuthenticationProvider
implements AuthenticationProvider {
extends AbstractAuthenticationProvider {
/**
* Given an arbitrary credentials object, returns a Map containing all
@@ -203,11 +204,6 @@ public abstract class SimpleAuthenticationProvider
}
@Override
public Object getResource() throws GuacamoleException {
return null;
}
@Override
public AuthenticatedUser authenticateUser(final Credentials credentials)
throws GuacamoleException {
@@ -241,45 +237,4 @@ public abstract class SimpleAuthenticationProvider
}
@Override
public AuthenticatedUser updateAuthenticatedUser(AuthenticatedUser authenticatedUser,
Credentials credentials) throws GuacamoleException {
// Simply return the given user, updating nothing
return authenticatedUser;
}
@Override
public UserContext updateUserContext(UserContext context,
AuthenticatedUser authorizedUser, Credentials credentials)
throws GuacamoleException {
// Simply return the given context, updating nothing
return context;
}
@Override
public UserContext decorate(UserContext context,
AuthenticatedUser authenticatedUser, Credentials credentials)
throws GuacamoleException {
// Simply return the given context, decorating nothing
return context;
}
@Override
public UserContext redecorate(UserContext decorated, UserContext context,
AuthenticatedUser authenticatedUser, Credentials credentials)
throws GuacamoleException {
return decorate(context, authenticatedUser, credentials);
}
@Override
public void shutdown() {
// Do nothing
}
}

View File

@@ -20,10 +20,12 @@
package org.apache.guacamole.net.auth.simple;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.guacamole.GuacamoleException;
import org.apache.guacamole.GuacamoleSecurityException;
import org.apache.guacamole.net.auth.Directory;
@@ -54,7 +56,8 @@ public class SimpleDirectory<ObjectType extends Identifiable>
/**
* Creates a new SimpleDirectory which provides access to the objects
* contained within the given Map.
* contained within the given Map. The given Map will be used to back all
* operations on the SimpleDirectory, and must be threadsafe.
*
* @param objects
* The Map of objects to provide access to.
@@ -63,6 +66,30 @@ public class SimpleDirectory<ObjectType extends Identifiable>
this.objects = objects;
}
public SimpleDirectory(ObjectType object) {
this(Collections.singletonMap(object.getIdentifier(), object));
}
public SimpleDirectory(ObjectType... objects) {
this(Arrays.asList(objects));
}
/**
* Creates a new SimpleDirectory which provides access to the
* objects contained within the Collection. Note that a new Map will be
* created to store the given objects. If the objects are already available
* in Map form, it is more efficient to use the
* {@link #SimpleDirectory(java.util.Map)} constructor.
*
* @param objects
* A Collection of all objects that should be present in this directory.
*/
public SimpleDirectory(Collection<ObjectType> objects) {
this.objects = new ConcurrentHashMap<String, ObjectType>(objects.size());
for (ObjectType object : objects)
this.objects.put(object.getIdentifier(), object);
}
/**
* Sets the Map which backs this SimpleDirectory. Future function calls
* which retrieve objects from this SimpleDirectory will use the provided

View File

@@ -19,24 +19,16 @@
package org.apache.guacamole.net.auth.simple;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.guacamole.GuacamoleException;
import org.apache.guacamole.form.Form;
import org.apache.guacamole.net.auth.ActiveConnection;
import org.apache.guacamole.net.auth.ActivityRecord;
import org.apache.guacamole.net.auth.ActivityRecordSet;
import org.apache.guacamole.net.auth.AbstractUserContext;
import org.apache.guacamole.net.auth.AuthenticatedUser;
import org.apache.guacamole.net.auth.AuthenticationProvider;
import org.apache.guacamole.net.auth.Connection;
import org.apache.guacamole.net.auth.ConnectionGroup;
import org.apache.guacamole.net.auth.ConnectionRecord;
import org.apache.guacamole.net.auth.Directory;
import org.apache.guacamole.net.auth.SharingProfile;
import org.apache.guacamole.net.auth.User;
import org.apache.guacamole.net.auth.UserContext;
import org.apache.guacamole.protocol.GuacamoleConfiguration;
/**
@@ -44,12 +36,7 @@ import org.apache.guacamole.protocol.GuacamoleConfiguration;
* a defined and restricted set of GuacamoleConfigurations. Access to
* querying or modifying either users or permissions is denied.
*/
public class SimpleUserContext implements UserContext {
/**
* The unique identifier of the root connection group.
*/
private static final String ROOT_IDENTIFIER = "ROOT";
public class SimpleUserContext extends AbstractUserContext {
/**
* The AuthenticationProvider that created this UserContext.
@@ -57,22 +44,10 @@ public class SimpleUserContext implements UserContext {
private final AuthenticationProvider authProvider;
/**
* Reference to the user whose permissions dictate the configurations
* accessible within this UserContext.
* The unique identifier (username) of the user whose permissions dictate
* the configurations accessible within this UserContext.
*/
private final User self;
/**
* The Directory with access only to the User associated with this
* UserContext.
*/
private final Directory<User> userDirectory;
/**
* The Directory with access only to the root group associated with this
* UserContext.
*/
private final Directory<ConnectionGroup> connectionGroupDirectory;
private final String username;
/**
* The Directory with access to all connections within the root group
@@ -80,15 +55,11 @@ public class SimpleUserContext implements UserContext {
*/
private final Directory<Connection> connectionDirectory;
/**
* The root connection group.
*/
private final ConnectionGroup rootGroup;
/**
* Creates a new SimpleUserContext which provides access to only those
* configurations within the given Map. The username is assigned
* arbitrarily.
* configurations within the given Map. The username is set to the
* ANONYMOUS_IDENTIFIER defined by AuthenticatedUser, effectively declaring
* the current user as anonymous.
*
* @param authProvider
* The AuthenticationProvider creating this UserContext.
@@ -99,7 +70,7 @@ public class SimpleUserContext implements UserContext {
*/
public SimpleUserContext(AuthenticationProvider authProvider,
Map<String, GuacamoleConfiguration> configs) {
this(authProvider, UUID.randomUUID().toString(), configs);
this(authProvider, AuthenticatedUser.ANONYMOUS_IDENTIFIER, configs);
}
/**
@@ -119,11 +90,8 @@ public class SimpleUserContext implements UserContext {
public SimpleUserContext(AuthenticationProvider authProvider,
String username, Map<String, GuacamoleConfiguration> configs) {
Collection<String> connectionIdentifiers = new ArrayList<String>(configs.size());
Collection<String> connectionGroupIdentifiers = Collections.singleton(ROOT_IDENTIFIER);
// Produce collection of connections from given configs
Collection<Connection> connections = new ArrayList<Connection>(configs.size());
// Produce map of connections from given configs
Map<String, Connection> connections = new ConcurrentHashMap<String, Connection>(configs.size());
for (Map.Entry<String, GuacamoleConfiguration> configEntry : configs.entrySet()) {
// Get connection identifier and configuration
@@ -132,37 +100,33 @@ public class SimpleUserContext implements UserContext {
// Add as simple connection
Connection connection = new SimpleConnection(identifier, identifier, config);
connection.setParentIdentifier(ROOT_IDENTIFIER);
connections.add(connection);
connection.setParentIdentifier(DEFAULT_ROOT_CONNECTION_GROUP);
connections.put(identifier, connection);
// Add identifier to overall set of identifiers
connectionIdentifiers.add(identifier);
}
// Add root group that contains only the given configurations
this.rootGroup = new SimpleConnectionGroup(
ROOT_IDENTIFIER, ROOT_IDENTIFIER,
connectionIdentifiers, Collections.<String>emptyList()
);
// Build new user from credentials
this.self = new SimpleUser(username, connectionIdentifiers,
connectionGroupIdentifiers);
// Create directories for new user
this.userDirectory = new SimpleUserDirectory(self);
this.connectionDirectory = new SimpleConnectionDirectory(connections);
this.connectionGroupDirectory = new SimpleConnectionGroupDirectory(Collections.singleton(this.rootGroup));
// Associate provided AuthenticationProvider
this.username = username;
this.authProvider = authProvider;
this.connectionDirectory = new SimpleDirectory<Connection>(connections);
}
@Override
public User self() {
return self;
try {
return new SimpleUser(username,
getConnectionDirectory().getIdentifiers(),
getConnectionGroupDirectory().getIdentifiers()
);
}
catch (GuacamoleException e) {
return new SimpleUser(username,
Collections.<String>emptySet(),
Collections.<String>emptySet());
}
}
@Override
@@ -175,76 +139,10 @@ public class SimpleUserContext implements UserContext {
return authProvider;
}
@Override
public Directory<User> getUserDirectory()
throws GuacamoleException {
return userDirectory;
}
@Override
public Directory<Connection> getConnectionDirectory()
throws GuacamoleException {
return connectionDirectory;
}
@Override
public Directory<ConnectionGroup> getConnectionGroupDirectory()
throws GuacamoleException {
return connectionGroupDirectory;
}
@Override
public ConnectionGroup getRootConnectionGroup() throws GuacamoleException {
return rootGroup;
}
@Override
public Directory<SharingProfile> getSharingProfileDirectory()
throws GuacamoleException {
return new SimpleDirectory<SharingProfile>();
}
@Override
public Directory<ActiveConnection> getActiveConnectionDirectory()
throws GuacamoleException {
return new SimpleDirectory<ActiveConnection>();
}
@Override
public ActivityRecordSet<ConnectionRecord> getConnectionHistory()
throws GuacamoleException {
return new SimpleActivityRecordSet<ConnectionRecord>();
}
@Override
public ActivityRecordSet<ActivityRecord> getUserHistory()
throws GuacamoleException {
return new SimpleActivityRecordSet<ActivityRecord>();
}
@Override
public Collection<Form> getUserAttributes() {
return Collections.<Form>emptyList();
}
@Override
public Collection<Form> getConnectionAttributes() {
return Collections.<Form>emptyList();
}
@Override
public Collection<Form> getConnectionGroupAttributes() {
return Collections.<Form>emptyList();
}
@Override
public Collection<Form> getSharingProfileAttributes() {
return Collections.<Form>emptyList();
}
@Override
public void invalidate() {
// Nothing to invalidate
}
}