1   /*
2    * Copyright 2018 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  package com.linecorp.centraldogma.server.auth;
17  
18  import static java.util.Objects.requireNonNull;
19  
20  import java.io.Serializable;
21  import java.time.Duration;
22  import java.time.Instant;
23  
24  import javax.annotation.Nullable;
25  
26  import com.fasterxml.jackson.annotation.JsonCreator;
27  import com.fasterxml.jackson.annotation.JsonProperty;
28  import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
29  import com.fasterxml.jackson.databind.annotation.JsonSerialize;
30  import com.google.common.base.MoreObjects;
31  
32  import com.linecorp.centraldogma.internal.Util;
33  
34  /**
35   * An authenticated session which can be replicated to the other Central Dogma replicas as a serialized form.
36   */
37  public final class Session {
38  
39      private static final long serialVersionUID = 4253152956820423809L;
40  
41      private final String id;
42      private final String username;
43      private final Instant creationTime;
44      private final Instant expirationTime;
45      @Nullable
46      private final Serializable rawSession;
47  
48      /**
49       * Creates a new {@link Session} instance.
50       *
51       * @param id the session ID
52       * @param username the name of the user which belongs to this session
53       * @param sessionValidDuration the {@link Duration} that this session is valid
54       */
55      public Session(String id, String username, Duration sessionValidDuration) {
56          this.id = requireNonNull(id, "id");
57          this.username = requireNonNull(username, "username");
58          creationTime = Instant.now();
59          expirationTime = creationTime.plus(requireNonNull(sessionValidDuration, "sessionValidDuration"));
60          rawSession = null;
61      }
62  
63      /**
64       * Creates a new {@link Session} instance.
65       *
66       * @param id the session ID
67       * @param username the name of the user which belongs to this session
68       * @param creationTime the created time {@link Instant}
69       * @param expirationTime the time {@link Instant} that this session is to be expired at
70       * @param rawSession the serializable session object which is specific to authentication provider
71       */
72      @JsonCreator
73      public Session(@JsonProperty("id") String id,
74                     @JsonProperty("username") String username,
75                     @JsonProperty("creationTime") Instant creationTime,
76                     @JsonProperty("expirationTime") Instant expirationTime,
77                     @JsonProperty("rawSession")
78                     @JsonDeserialize(using = RawSessionJsonDeserializer.class)
79                     @Nullable Serializable rawSession) {
80          this.id = requireNonNull(id, "id");
81          this.username = requireNonNull(username, "username");
82          this.creationTime = requireNonNull(creationTime, "creationTime");
83          this.expirationTime = requireNonNull(expirationTime, "expirationTime");
84          this.rawSession = rawSession;
85      }
86  
87      /**
88       * Returns the session ID.
89       */
90      @JsonProperty
91      public String id() {
92          return id;
93      }
94  
95      /**
96       * Returns the name of the user which belongs to this session.
97       */
98      @JsonProperty
99      public String username() {
100         return username;
101     }
102 
103     /**
104      * Returns the created time {@link Instant}.
105      */
106     @JsonProperty
107     public Instant creationTime() {
108         return creationTime;
109     }
110 
111     /**
112      * Returns the time {@link Instant} that this session is to be expired at.
113      */
114     @JsonProperty
115     public Instant expirationTime() {
116         return expirationTime;
117     }
118 
119     /**
120      * Returns a raw session instance.
121      */
122     @Nullable
123     @JsonProperty
124     @JsonSerialize(using = RawSessionJsonSerializer.class)
125     public Serializable rawSession() {
126         return rawSession;
127     }
128 
129     /**
130      * Returns a raw session instance which is casted to {@code T} type.
131      *
132      * @throws NullPointerException if the {@code rawSession} is {@code null}
133      * @throws ClassCastException if the {@code rawSession} cannot be casted to {@code T}
134      */
135     <T> T castRawSession() {
136         return Util.unsafeCast(requireNonNull(rawSession, "rawSession"));
137     }
138 
139     @Override
140     public String toString() {
141         return MoreObjects.toStringHelper(this)
142                           .add("id", id)
143                           .add("username", username)
144                           .add("creationTime", creationTime)
145                           .add("expirationTime", expirationTime)
146                           .add("rawSession",
147                                rawSession != null ? rawSession.getClass().getSimpleName() : null)
148                           .toString();
149     }
150 }