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.mirror;
18  
19  import java.net.URI;
20  import java.util.Collections;
21  import java.util.Optional;
22  import java.util.Set;
23  import java.util.regex.Pattern;
24  
25  import com.fasterxml.jackson.annotation.JsonSubTypes;
26  import com.fasterxml.jackson.annotation.JsonSubTypes.Type;
27  import com.fasterxml.jackson.annotation.JsonTypeInfo;
28  
29  import com.linecorp.centraldogma.server.internal.mirror.credential.AccessTokenMirrorCredential;
30  import com.linecorp.centraldogma.server.internal.mirror.credential.NoneMirrorCredential;
31  import com.linecorp.centraldogma.server.internal.mirror.credential.PasswordMirrorCredential;
32  import com.linecorp.centraldogma.server.internal.mirror.credential.PublicKeyMirrorCredential;
33  
34  /**
35   * The authentication credentials which are required when accessing the Git repositories.
36   */
37  @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type")
38  @JsonSubTypes({
39          @Type(value = NoneMirrorCredential.class, name = "none"),
40          @Type(value = PasswordMirrorCredential.class, name = "password"),
41          @Type(value = PublicKeyMirrorCredential.class, name = "public_key"),
42          @Type(value = AccessTokenMirrorCredential.class, name = "access_token")
43  })
44  public interface MirrorCredential {
45  
46      MirrorCredential FALLBACK = new NoneMirrorCredential(null, Collections.singleton(Pattern.compile("^.*$")));
47  
48      /**
49       * Returns the ID of the credential.
50       */
51      Optional<String> id();
52  
53      /**
54       * Returns the {@link Pattern}s compiled from the regular expressions that match a host name.
55       */
56      Set<Pattern> hostnamePatterns();
57  
58      /**
59       * Returns {@code true} if the specified {@code uri} is matched by one of the host name patterns.
60       *
61       * @param uri a URI of a Git repository
62       */
63      boolean matches(URI uri);
64  }