code: public String authenticate(Credentials credentials, String token)
          throws GuacamoleException {
  
          // Pull existing session if token provided
          GuacamoleSession existingSession;
          if (token != null)
              existingSession = tokenSessionMap.get(token);
          else
              existingSession = null;
  
          // Get up-to-date AuthenticatedUser and associated UserContexts
          AuthenticatedUser authenticatedUser = getAuthenticatedUser(existingSession, credentials);
          List<DecoratedUserContext> userContexts = getUserContexts(existingSession, authenticatedUser, credentials);
  
          // Update existing session, if it exists
          String authToken;
          if (existingSession != null) {
              authToken = token;
              existingSession.setAuthenticatedUser(authenticatedUser);
              existingSession.setUserContexts(userContexts);
          }
  
          // If no existing session, generate a new token/session pair
          else {
              authToken = authTokenGenerator.getToken();
              tokenSessionMap.put(authToken, new GuacamoleSession(environment, authenticatedUser, userContexts));
              logger.debug("Login was successful for user \"{}\".", authenticatedUser.getIdentifier());
          }
  
          return authToken;
  
      }
docs: Authenticates a user using the given credentials and optional
  authentication token, returning the authentication token associated with
  the user's Guacamole session, which may be newly generated. If an
  existing token is provided, the authentication procedure will attempt to
  update or reuse the provided token, but it is possible that a new token
  will be returned. Note that this function CANNOT return null.
  
  @param credentials
  The credentials to use when authenticating the user.
  
  @param token
  The authentication token to use if attempting to re-authenticate an
  existing session, or null to request a new token.
  
  @return
  The authentication token associated with the newly created or
  existing session.
  
  @throws GuacamoleException
  If the authentication or re-authentication attempt fails.