1   /*
2    * Copyright 2019 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.metadata;
18  
19  import static java.util.Objects.requireNonNull;
20  
21  import javax.annotation.Nullable;
22  
23  import com.fasterxml.jackson.annotation.JsonCreator;
24  import com.fasterxml.jackson.annotation.JsonIgnore;
25  import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
26  import com.fasterxml.jackson.annotation.JsonInclude;
27  import com.fasterxml.jackson.annotation.JsonInclude.Include;
28  import com.fasterxml.jackson.annotation.JsonProperty;
29  import com.google.common.base.MoreObjects;
30  
31  import com.linecorp.centraldogma.internal.Util;
32  
33  /**
34   * Specifies details of an application token.
35   */
36  @JsonIgnoreProperties(ignoreUnknown = true)
37  @JsonInclude(Include.NON_NULL)
38  public final class Token implements Identifiable {
39  
40      /**
41       * An application identifier.
42       */
43      private final String appId;
44  
45      /**
46       * A secret which is used to access an HTTP API.
47       */
48      @Nullable
49      private final String secret;
50  
51      /**
52       * Specifies whether this token is for administrators.
53       */
54      private final boolean isAdmin;
55  
56      /**
57       * Specifies when this token is created by whom.
58       */
59      private final UserAndTimestamp creation;
60  
61      /**
62       * Specifies when this repository is removed by whom.
63       */
64      @Nullable
65      private final UserAndTimestamp deactivation;
66  
67      @Nullable
68      private final UserAndTimestamp deletion;
69  
70      Token(String appId, String secret, boolean isAdmin, UserAndTimestamp creation) {
71          this(appId, secret, isAdmin, creation, null, null);
72      }
73  
74      /**
75       * Creates a new instance.
76       */
77      @JsonCreator
78      public Token(@JsonProperty("appId") String appId,
79                   @JsonProperty("secret") String secret,
80                   @JsonProperty("admin") boolean isAdmin,
81                   @JsonProperty("creation") UserAndTimestamp creation,
82                   @JsonProperty("deactivation") @Nullable UserAndTimestamp deactivation,
83                   @JsonProperty("deletion") @Nullable UserAndTimestamp deletion) {
84          this.appId = Util.validateFileName(appId, "appId");
85          this.secret = Util.validateFileName(secret, "secret");
86          this.isAdmin = isAdmin;
87          this.creation = requireNonNull(creation, "creation");
88          this.deactivation = deactivation;
89          this.deletion = deletion;
90      }
91  
92      private Token(String appId, boolean isAdmin, UserAndTimestamp creation,
93                    @Nullable UserAndTimestamp deactivation, @Nullable UserAndTimestamp deletion) {
94          this.appId = Util.validateFileName(appId, "appId");
95          this.isAdmin = isAdmin;
96          this.creation = requireNonNull(creation, "creation");
97          this.deactivation = deactivation;
98          this.deletion = deletion;
99          secret = null;
100     }
101 
102     @Override
103     public String id() {
104         return appId;
105     }
106 
107     /**
108      * Returns the ID of the application.
109      */
110     @JsonProperty
111     public String appId() {
112         return appId;
113     }
114 
115     /**
116      * Returns the secret.
117      */
118     @Nullable
119     @JsonProperty
120     public String secret() {
121         return secret;
122     }
123 
124     /**
125      * Returns whether this token has administrative privileges.
126      */
127     @JsonProperty
128     public boolean isAdmin() {
129         return isAdmin;
130     }
131 
132     /**
133      * Returns who created this token when.
134      */
135     @JsonProperty
136     public UserAndTimestamp creation() {
137         return creation;
138     }
139 
140     /**
141      * Returns who deactivated this token when.
142      */
143     @Nullable
144     @JsonProperty
145     public UserAndTimestamp deactivation() {
146         return deactivation;
147     }
148 
149     /**
150      * Returns who deleted this token when.
151      */
152     @Nullable
153     @JsonProperty
154     public UserAndTimestamp deletion() {
155         return deletion;
156     }
157 
158     /**
159      * Returns whether this token is activated.
160      */
161     @JsonIgnore
162     public boolean isActive() {
163         return deactivation == null && deletion == null;
164     }
165 
166     /**
167      * Returns whether this token is deleted.
168      */
169     @JsonIgnore
170     public boolean isDeleted() {
171         return deletion != null;
172     }
173 
174     @Override
175     public String toString() {
176         // Do not add "secret" to prevent it from logging.
177         return MoreObjects.toStringHelper(this).omitNullValues()
178                           .add("appId", appId())
179                           .add("isAdmin", isAdmin())
180                           .add("creation", creation())
181                           .add("deactivation", deactivation())
182                           .add("deletion", deletion())
183                           .toString();
184     }
185 
186     /**
187      * Returns a new {@link Token} instance without its secret.
188      */
189     public Token withoutSecret() {
190         return new Token(appId(), isAdmin(), creation(), deactivation(), deletion());
191     }
192 }