You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@james.apache.org by GitBox <gi...@apache.org> on 2022/05/30 09:49:12 UTC

[GitHub] [james-project] vttranlina commented on a diff in pull request #1032: JAMES-3756 Implement DelegationStore based Authorizator

vttranlina commented on code in PR #1032:
URL: https://github.com/apache/james-project/pull/1032#discussion_r884640461


##########
protocols/imap/src/main/java/org/apache/james/imap/processor/AbstractAuthProcessor.java:
##########
@@ -89,17 +89,19 @@ protected void doAuth(AuthenticationAttempt authenticationAttempt, ImapSession s
 
     protected void doAuthWithDelegation(AuthenticationAttempt authenticationAttempt, ImapSession session, ImapRequest request, Responder responder, HumanReadableText failed) {
         Preconditions.checkArgument(authenticationAttempt.isDelegation());
+        Username givenUser = authenticationAttempt.getAuthenticationId();
+        Optional<Username> otherUser = authenticationAttempt.getDelegateUserName();
         try {
             boolean authFailure = false;
-            if (authenticationAttempt.getAuthenticationId() == null) {
+            if (givenUser == null) {
                 authFailure = true;
             }
             if (!authFailure) {
                 final MailboxManager mailboxManager = getMailboxManager();
                 try {
-                    final MailboxSession mailboxSession = mailboxManager.loginAsOtherUser(authenticationAttempt.getAuthenticationId(),
+                    final MailboxSession mailboxSession = mailboxManager.loginAsOtherUser(givenUser,
                         authenticationAttempt.getPassword(),
-                        authenticationAttempt.getDelegateUserName().get());
+                        otherUser.get());

Review Comment:
   Why `otherUser` is optional, but here we get it, without empty checking?



##########
server/container/mailbox-adapter/src/main/java/org/apache/james/adapter/mailbox/DelegationStoreAuthorizator.java:
##########
@@ -0,0 +1,61 @@
+/****************************************************************
+ * 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.james.adapter.mailbox;
+
+import javax.inject.Inject;
+
+import org.apache.james.core.Username;
+import org.apache.james.mailbox.Authorizator;
+import org.apache.james.mailbox.exception.MailboxException;
+import org.apache.james.user.api.DelegationStore;
+import org.apache.james.user.api.UsersRepository;
+import org.apache.james.user.api.UsersRepositoryException;
+
+import reactor.core.publisher.Flux;
+
+/**
+ * Authorizator which use an DelegationStore to check if the delegation is allowed
+ */
+public class DelegationStoreAuthorizator implements Authorizator {
+    private final DelegationStore delegationStore;
+    private final UsersRepository usersRepository;
+
+    @Inject
+    public DelegationStoreAuthorizator(DelegationStore delegationStore, UsersRepository usersRepository) {
+        this.delegationStore = delegationStore;
+        this.usersRepository = usersRepository;
+    }
+
+    @Override
+    public AuthorizationState canLoginAsOtherUser(Username userId, Username otherUserId) throws MailboxException {
+        boolean isAuthorized = Flux.from(delegationStore.authorizedUsers(otherUserId)).collectList().block().contains(userId);
+        try {
+            if (usersRepository.isAdministrator(userId) || isAuthorized) {

Review Comment:
   ```suggestion
               if (isAuthorized || usersRepository.isAdministrator(userId)) {
   ```
   When `isAuthorized` is true, Java will not need compute `usersRepository.isAdministrator(userId)`
   
   



-- 
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.

To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For additional commands, e-mail: notifications-help@james.apache.org