You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@guacamole.apache.org by GitBox <gi...@apache.org> on 2020/11/29 23:36:31 UTC

[GitHub] [guacamole-client] mike-jumper commented on a change in pull request #576: GUACAMOLE-1218: Include "guacamole-auth-json" within Apache Guacamole.

mike-jumper commented on a change in pull request #576:
URL: https://github.com/apache/guacamole-client/pull/576#discussion_r532286680



##########
File path: extensions/guacamole-auth-json/src/main/java/org/apache/guacamole/auth/json/AuthenticationProviderService.java
##########
@@ -0,0 +1,116 @@
+/*
+ * 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.auth.json;
+
+import com.google.inject.Inject;
+import com.google.inject.Provider;
+import org.apache.guacamole.GuacamoleException;
+import org.apache.guacamole.auth.json.user.AuthenticatedUser;
+import org.apache.guacamole.auth.json.user.UserContext;
+import org.apache.guacamole.auth.json.user.UserData;
+import org.apache.guacamole.auth.json.user.UserDataService;
+import org.apache.guacamole.net.auth.Credentials;
+import org.apache.guacamole.net.auth.credentials.CredentialsInfo;
+import org.apache.guacamole.net.auth.credentials.GuacamoleInvalidCredentialsException;
+
+/**
+ * Service providing convenience functions for the JSONAuthenticationProvider.
+ */
+public class AuthenticationProviderService {
+
+    /**
+     * Service for deriving Guacamole extension API data from UserData objects.
+     */
+    @Inject
+    private UserDataService userDataService;
+
+    /**
+     * Provider for AuthenticatedUser objects.
+     */
+    @Inject
+    private Provider<AuthenticatedUser> authenticatedUserProvider;
+
+    /**
+     * Provider for UserContext objects.
+     */
+    @Inject
+    private Provider<UserContext> userContextProvider;
+
+    /**
+     * Returns an AuthenticatedUser representing the user authenticated by the
+     * given credentials.
+     *
+     * @param credentials
+     *     The credentials to use for authentication.
+     *
+     * @return
+     *     An AuthenticatedUser representing the user authenticated by the
+     *     given credentials.
+     *
+     * @throws GuacamoleException
+     *     If an error occurs while authenticating the user, or if access is
+     *     denied.
+     */
+    public AuthenticatedUser authenticateUser(Credentials credentials)
+            throws GuacamoleException {
+
+        // Pull UserData from credentials, if possible
+        UserData userData = userDataService.fromCredentials(credentials);
+        if (userData == null)
+            throw new GuacamoleInvalidCredentialsException("Permission denied.", CredentialsInfo.EMPTY);
+
+        // Produce AuthenticatedUser associated with derived UserData
+        AuthenticatedUser authenticatedUser = authenticatedUserProvider.get();
+        authenticatedUser.init(credentials, userData);
+        return authenticatedUser;
+
+    }
+
+    /**
+     * Returns a UserContext object initialized with data accessible to the
+     * given AuthenticatedUser.
+     *
+     * @param authenticatedUser
+     *     The AuthenticatedUser to retrieve data for.
+     *
+     * @return
+     *     A UserContext object initialized with data accessible to the given
+     *     AuthenticatedUser.
+     *
+     * @throws GuacamoleException
+     *     If the UserContext cannot be created due to an error.
+     */
+    public UserContext getUserContext(org.apache.guacamole.net.auth.AuthenticatedUser authenticatedUser)
+            throws GuacamoleException {
+
+        // The JSONAuthenticationProvider only provides data for users it has
+        // authenticated itself
+        if (!(authenticatedUser instanceof AuthenticatedUser))
+            return null;

Review comment:
       It can stack. For example, if you submit JSON like:
   
   ```json
   {
       "username" : "guacadmin"
   }
   ```
   
   that would be sufficient for the database authentication to accept the user as "guacadmin".
   
   The JSON extension simply won't attempt to provide _data_ (a `UserContext`) for a user that it hasn't authenticated, as its data comes from the JSON. If there isn't any JSON, then the JSON extension has no data to provide.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org