1   /*
2    * Copyright 2017 LINE Corporation
3    *
4    * LINE Corporation licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  
17  package com.linecorp.centraldogma.server.internal.mirror.credential;
18  
19  import static com.linecorp.centraldogma.server.CentralDogmaConfig.convertValue;
20  import static com.linecorp.centraldogma.server.internal.mirror.credential.MirrorCredentialUtil.requireNonEmpty;
21  import static java.util.Objects.requireNonNull;
22  
23  import java.util.regex.Pattern;
24  
25  import javax.annotation.Nullable;
26  
27  import org.slf4j.Logger;
28  import org.slf4j.LoggerFactory;
29  
30  import com.fasterxml.jackson.annotation.JsonCreator;
31  import com.fasterxml.jackson.annotation.JsonProperty;
32  import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
33  import com.google.common.base.MoreObjects.ToStringHelper;
34  
35  public final class PasswordMirrorCredential extends AbstractMirrorCredential {
36  
37      private static final Logger logger = LoggerFactory.getLogger(PasswordMirrorCredential.class);
38  
39      private final String username;
40      private final String password;
41  
42      @JsonCreator
43      public PasswordMirrorCredential(@JsonProperty("id") @Nullable String id,
44                                      @JsonProperty("hostnamePatterns") @Nullable
45                                      @JsonDeserialize(contentAs = Pattern.class)
46                                      Iterable<Pattern> hostnamePatterns,
47                                      @JsonProperty("username") String username,
48                                      @JsonProperty("password") String password) {
49          super(id, hostnamePatterns);
50  
51          this.username = requireNonEmpty(username, "username");
52          this.password = requireNonNull(password, "password");
53      }
54  
55      public String username() {
56          return username;
57      }
58  
59      public String password() {
60          try {
61              return convertValue(password, "credentials.password");
62          } catch (Throwable t) {
63              // The password probably has `:` without prefix. Just return it as is for backward compatibility.
64              logger.debug("Failed to convert the password of the credential. username: {}, id: {}",
65                           username, id(), t);
66              return password;
67          }
68      }
69  
70      @Override
71      public int hashCode() {
72          int result = super.hashCode();
73          result = 31 * result + username.hashCode();
74          result = 31 * result + password.hashCode();
75          return result;
76      }
77  
78      @Override
79      public boolean equals(Object o) {
80          if (this == o) {
81              return true;
82          }
83  
84          if (!(o instanceof PasswordMirrorCredential)) {
85              return false;
86          }
87  
88          if (!super.equals(o)) {
89              return false;
90          }
91  
92          final PasswordMirrorCredential that = (PasswordMirrorCredential) o;
93          return username.equals(that.username) && password.equals(that.password);
94      }
95  
96      @Override
97      void addProperties(ToStringHelper helper) {
98          helper.add("username", username);
99      }
100 }