View Javadoc
1   /*
2    * This file is part of Waarp Project (named also Waarp or GG).
3    *
4    *  Copyright (c) 2019, Waarp SAS, and individual contributors by the @author
5    *  tags. See the COPYRIGHT.txt in the distribution for a full listing of
6    * individual contributors.
7    *
8    *  All Waarp Project is free software: you can redistribute it and/or
9    * modify it under the terms of the GNU General Public License as published by
10   * the Free Software Foundation, either version 3 of the License, or (at your
11   * option) any later version.
12   *
13   * Waarp is distributed in the hope that it will be useful, but WITHOUT ANY
14   * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15   * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16   *
17   *  You should have received a copy of the GNU General Public License along with
18   * Waarp . If not, see <http://www.gnu.org/licenses/>.
19   */
20  
21  package org.waarp.http.protocol.servlet;
22  
23  import org.waarp.common.command.exception.Reply421Exception;
24  import org.waarp.common.command.exception.Reply530Exception;
25  import org.waarp.http.protocol.HttpSession;
26  import org.waarp.openr66.context.R66Session;
27  
28  import java.util.Map;
29  
30  /**
31   * Default implementation of HttpAuthent based on standard Waarp R66
32   * authentication
33   */
34  public class HttpAuthentDefault implements HttpAuthent {
35  
36    /**
37     * Package protected for the tests
38     */
39    protected String user;
40    protected byte[] key;
41  
42    /**
43     * Empty for instantiation from class name
44     */
45    public HttpAuthentDefault() {
46      // Empty
47    }
48  
49    @Override
50    public final void initializeAuthent(final Map<String, String> arguments) {
51      user = arguments.get(FIELD_USER);
52      final String skey = arguments.get(FIELD_KEY);
53      key = org.waarp.common.digest.FilesystemBasedDigest.getFromHex(skey);
54    }
55  
56    @Override
57    public final void checkAuthent(final HttpSession httpSession,
58                                   final R66Session session)
59        throws IllegalArgumentException {
60      try {
61        session.getAuth().connection(user, key, false);
62      } catch (final Reply530Exception e) {
63        httpSession.error(e, session.getBusinessObject());
64      } catch (final Reply421Exception e) {
65        httpSession.error(e, session.getBusinessObject());
66      }
67    }
68  
69    @Override
70    public final String getUserId() {
71      return user;
72    }
73  
74    @Override
75    public final void finalizeTransfer(final HttpSession httpSession,
76                                       final R66Session session)
77        throws IllegalArgumentException {
78      // Do nothing
79    }
80  }