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.admin.auth;
18  
19  import static java.util.Objects.requireNonNull;
20  
21  import java.text.ParseException;
22  import java.time.Instant;
23  import java.time.format.DateTimeFormatter;
24  
25  import javax.annotation.Nullable;
26  
27  import com.fasterxml.jackson.annotation.JsonCreator;
28  import com.fasterxml.jackson.annotation.JsonInclude;
29  import com.fasterxml.jackson.annotation.JsonInclude.Include;
30  import com.fasterxml.jackson.annotation.JsonProperty;
31  import com.google.common.base.MoreObjects;
32  
33  import com.linecorp.centraldogma.server.metadata.Token;
34  import com.linecorp.centraldogma.server.metadata.User;
35  
36  /**
37   * Specifies details of an application token.
38   *
39   * @deprecated Use {@link Token}.
40   */
41  @Deprecated
42  @JsonInclude(Include.NON_NULL)
43  public final class LegacyToken {
44  
45      public static final LegacyToken EMPTY_TOKEN =
46              new LegacyToken(null, null, null, null, null);
47  
48      @Nullable
49      private final String appId;
50      @Nullable
51      private final String secret;
52      @Nullable
53      private final User creator;
54      @Nullable
55      private final Instant creationTime;
56      @Nullable
57      private String creationTimeAsText;
58  
59      @JsonCreator
60      public LegacyToken(@JsonProperty("appId") String appId,
61                         @JsonProperty("secret") String secret,
62                         @JsonProperty("creator") User creator,
63                         @JsonProperty("creationTime") String creationTimeAsText) throws ParseException {
64          this(requireNonNull(appId, "appId"),
65               requireNonNull(secret, "secret"),
66               requireNonNull(creator, "creator"),
67               Instant.from(DateTimeFormatter.ISO_INSTANT.parse(
68                       requireNonNull(creationTimeAsText, "creationTimeAsText"))),
69               creationTimeAsText);
70      }
71  
72      public LegacyToken(String appId, String secret, User creator, Instant creationTime) {
73          this(requireNonNull(appId, "appId"),
74               requireNonNull(secret, "secret"),
75               requireNonNull(creator, "creator"),
76               requireNonNull(creationTime, "creationTime"),
77               null);
78      }
79  
80      private LegacyToken(@Nullable String appId, @Nullable String secret, @Nullable User creator,
81                          @Nullable Instant creationTime, @Nullable String creationTimeAsText) {
82          this.appId = appId;
83          this.secret = secret;
84          this.creator = creator;
85          this.creationTime = creationTime;
86          this.creationTimeAsText = creationTimeAsText;
87      }
88  
89      @Nullable
90      @JsonProperty
91      public String appId() {
92          return appId;
93      }
94  
95      @Nullable
96      @JsonProperty
97      public String secret() {
98          return secret;
99      }
100 
101     @Nullable
102     @JsonProperty
103     public User creator() {
104         return creator;
105     }
106 
107     @Nullable
108     @JsonProperty
109     public String creationTime() {
110         if (creationTimeAsText == null && creationTime != null) {
111             creationTimeAsText = DateTimeFormatter.ISO_INSTANT.format(creationTime);
112         }
113         return creationTimeAsText;
114     }
115 
116     @Override
117     public String toString() {
118         return MoreObjects.toStringHelper(this)
119                           .add("appId", appId())
120                           .add("creator", creator())
121                           .add("creationTime", creationTime())
122                           .toString();
123     }
124 
125     /**
126      * Returns a new {@link LegacyToken} instance without its secret.
127      */
128     public LegacyToken withoutSecret() {
129         return new LegacyToken(appId, null, creator, creationTime, creationTimeAsText);
130     }
131 }