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.util.concurrent.CompletableFuture;
21  import java.util.function.Function;
22  import java.util.function.Supplier;
23  
24  import com.linecorp.armeria.common.HttpRequest;
25  import com.linecorp.armeria.server.auth.Authorizer;
26  import com.linecorp.centraldogma.server.CentralDogmaConfig;
27  
28  /**
29   * Parameters which are used to create a new {@link AuthProvider} instance.
30   */
31  public final class AuthProviderParameters {
32  
33      private final Authorizer<HttpRequest> authorizer;
34      private final CentralDogmaConfig config;
35      private final AuthConfig authConfig;
36      private final Supplier<String> sessionIdGenerator;
37      private final Function<Session, CompletableFuture<Void>> loginSessionPropagator;
38      private final Function<String, CompletableFuture<Void>> logoutSessionPropagator;
39  
40      /**
41       * Creates a new instance.
42       *
43       * @param authorizer the {@link Authorizer} which is used to authenticate a session token
44       * @param config the configuration for the Central Dogma server
45       * @param sessionIdGenerator the session ID generator which must be used when generating a new session ID
46       * @param loginSessionPropagator the function which propagates the {@link Session}
47       *                               to the other replicas
48       * @param logoutSessionPropagator a function which propagates the logged out session ID to the other
49       *                                replicas
50       */
51      public AuthProviderParameters(
52              Authorizer<HttpRequest> authorizer,
53              CentralDogmaConfig config,
54              Supplier<String> sessionIdGenerator,
55              Function<Session, CompletableFuture<Void>> loginSessionPropagator,
56              Function<String, CompletableFuture<Void>> logoutSessionPropagator) {
57          this.authorizer = requireNonNull(authorizer, "authorizer");
58          this.config = requireNonNull(config, "config");
59          this.sessionIdGenerator = requireNonNull(sessionIdGenerator, "sessionIdGenerator");
60          this.loginSessionPropagator = requireNonNull(loginSessionPropagator, "loginSessionPropagator");
61          this.logoutSessionPropagator = requireNonNull(logoutSessionPropagator, "logoutSessionPropagator");
62          authConfig = requireNonNull(config.authConfig(), "authConfig");
63      }
64  
65      /**
66       * Returns an {@link Authorizer} which is used to authenticate a session token.
67       */
68      public Authorizer<HttpRequest> authorizer() {
69          return authorizer;
70      }
71  
72      /**
73       * Returns the configuration for the Central Dogma server.
74       */
75      public CentralDogmaConfig config() {
76          return config;
77      }
78  
79      /**
80       * Returns the authentication configuration.
81       */
82      public AuthConfig authConfig() {
83          return authConfig;
84      }
85  
86      /**
87       * Returns the session ID generator which must be used when generating a new session ID. The default
88       * session manager relies on the session ID format, so if the {@link AuthProvider} does not
89       * use this generator the session might be rejected from the session manager.
90       */
91      public Supplier<String> sessionIdGenerator() {
92          return sessionIdGenerator;
93      }
94  
95      /**
96       * Returns a function which propagates the {@link Session} to the other replicas.
97       * It would be invoked after a login process is successfully completed.
98       */
99      public Function<Session, CompletableFuture<Void>> loginSessionPropagator() {
100         return loginSessionPropagator;
101     }
102 
103     /**
104      * Returns a function which propagates the logged out session ID to the other replicas.
105      * It would be invoked after a logout process is successfully completed.
106      */
107     public Function<String, CompletableFuture<Void>> logoutSessionPropagator() {
108         return logoutSessionPropagator;
109     }
110 }