1   /*
2    * Copyright 2023 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  
22  import java.util.regex.Pattern;
23  
24  import javax.annotation.Nullable;
25  
26  import org.slf4j.Logger;
27  import org.slf4j.LoggerFactory;
28  
29  import com.fasterxml.jackson.annotation.JsonCreator;
30  import com.fasterxml.jackson.annotation.JsonProperty;
31  import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
32  import com.google.common.base.MoreObjects.ToStringHelper;
33  
34  public final class AccessTokenMirrorCredential extends AbstractMirrorCredential {
35  
36      private static final Logger logger = LoggerFactory.getLogger(AccessTokenMirrorCredential.class);
37  
38      private final String accessToken;
39  
40      @JsonCreator
41      public AccessTokenMirrorCredential(@JsonProperty("id") @Nullable String id,
42                                         @JsonProperty("hostnamePatterns") @Nullable
43                                         @JsonDeserialize(contentAs = Pattern.class)
44                                         Iterable<Pattern> hostnamePatterns,
45                                         @JsonProperty("accessToken") String accessToken) {
46          super(id, hostnamePatterns);
47          this.accessToken = requireNonEmpty(accessToken, "accessToken");
48      }
49  
50      public String accessToken() {
51          try {
52              return convertValue(accessToken, "credentials.accessToken");
53          } catch (Throwable t) {
54              // The accessToken probably has `:` without prefix. Just return it as is for backward compatibility.
55              logger.debug("Failed to convert the access token of the credential: {}", id(), t);
56              return accessToken;
57          }
58      }
59  
60      @Override
61      public int hashCode() {
62          int result = super.hashCode();
63          result = 31 * result + accessToken.hashCode();
64          return result;
65      }
66  
67      @Override
68      public boolean equals(Object o) {
69          if (this == o) {
70              return true;
71          }
72  
73          if (!(o instanceof AccessTokenMirrorCredential)) {
74              return false;
75          }
76  
77          if (!super.equals(o)) {
78              return false;
79          }
80  
81          final AccessTokenMirrorCredential that = (AccessTokenMirrorCredential) o;
82          return accessToken.equals(that.accessToken);
83      }
84  
85      @Override
86      void addProperties(ToStringHelper helper) {
87          // Access token must be kept secret.
88      }
89  }