View Javadoc
1   package cn.home1.oss.environment.admin;
2   
3   import cn.home1.oss.lib.common.crypto.EncodeDecryptor;
4   
5   import lombok.Data;
6   import lombok.extern.slf4j.Slf4j;
7   
8   import java.io.Serializable;
9   import java.util.Map;
10  
11  /**
12   * Created by Yuliang Jin on 16/10/26.
13   */
14  @Data
15  @Slf4j
16  public class ClientKey implements Serializable {
17    private static final long serialVersionUID = 1L;
18  
19    private static final String CONSTANT_STRING_FOR_DISPLAY = "******";
20    private static final String CONSTANT_STRING_FOR_USERNAME= "username";
21    @SuppressWarnings("squid:S2068")
22    private static final String CONSTANT_STRING_FOR_PASSWORD= "password";
23    private static final String CONSTANT_STRING_FOR_MANAGEMENT_AUTHENTICATION= "managementAuthentication";
24  
25    private String serviceId;
26    private String userName;
27    private String password;
28  
29    public ClientKey(final String serviceId, final String userName, final String password) {
30      this.serviceId = serviceId;
31      this.userName = userName;
32      this.password = password;
33    }
34  
35    public static String serviceIdFrom(final String requestUri) {
36      log.debug("Request Uri is {}.", requestUri);
37      return requestUri.split("/")[3];
38    }
39  
40    /**
41     * To get the plain username and password; To modify the encrypted info to a constant String.
42     * @param infoObject
43     * @param decryptor
44     * @return
45     */
46    public static String[] usernamePasswordFromInfoObject( //
47        final Map<String, Object> infoObject, final EncodeDecryptor decryptor) {
48      final String[] result;
49      if (infoObject != null && infoObject.containsKey(CONSTANT_STRING_FOR_MANAGEMENT_AUTHENTICATION)) {
50        @SuppressWarnings("unchecked")
51        final Map<String, Object> managementAuth = (Map<String, Object>) infoObject.get(CONSTANT_STRING_FOR_MANAGEMENT_AUTHENTICATION);
52        final Boolean encrypted = (Boolean) managementAuth.get("encrypted");
53        final String username;
54        final String password;
55        if (encrypted) {
56          username = decryptor.decrypt((String) managementAuth.get(CONSTANT_STRING_FOR_USERNAME));
57          password = decryptor.decrypt((String) managementAuth.get(CONSTANT_STRING_FOR_PASSWORD));
58        } else {
59          username = (String) managementAuth.get(CONSTANT_STRING_FOR_USERNAME);
60          password = (String) managementAuth.get(CONSTANT_STRING_FOR_PASSWORD);
61        }
62  
63        managementAuth.put(CONSTANT_STRING_FOR_USERNAME, CONSTANT_STRING_FOR_DISPLAY);
64        managementAuth.put(CONSTANT_STRING_FOR_PASSWORD, CONSTANT_STRING_FOR_DISPLAY);
65  
66        infoObject.put(CONSTANT_STRING_FOR_MANAGEMENT_AUTHENTICATION, managementAuth);
67  
68        result = new String[]{username, password};
69      } else {
70        result = null;
71      }
72      return result;
73    }
74  }