View Javadoc

1   package com.atlassian.asap.core.server;
2   
3   import com.atlassian.asap.core.keys.KeyProvider;
4   import com.atlassian.asap.core.keys.publickey.PublicKeyProviderFactory;
5   import com.google.common.collect.ImmutableSet;
6   import com.google.common.collect.Iterables;
7   
8   import javax.annotation.Nonnull;
9   import java.security.PublicKey;
10  import java.util.Set;
11  
12  import static com.google.common.base.Preconditions.checkState;
13  import static com.google.common.collect.ImmutableSet.copyOf;
14  import static java.util.Objects.requireNonNull;
15  
16  /**
17   * Provides the contextual information needed to authenticate a JWT.
18   */
19  public class AuthenticationContext {
20      private final Set<String> resourceServerAudiences;
21      private final KeyProvider<PublicKey> publicKeyProvider;
22  
23      /**
24       * Create a new {@link AuthenticationContext} encapsulating the required information to authenticate a JWT.
25       *
26       * @param resourceServerAudience the audience value of the resource server
27       * @param publicKeyServerBaseUrl the base url of the public key server
28       */
29      public AuthenticationContext(@Nonnull String resourceServerAudience,
30                                   @Nonnull String publicKeyServerBaseUrl) {
31          this(ImmutableSet.of(requireNonNull(resourceServerAudience)), publicKeyServerBaseUrl);
32      }
33  
34      /**
35       * Create a new {@link AuthenticationContext} encapsulating the required information to authenticate a JWT.
36       *
37       * @param resourceServerAudiences the audience values of the resource server
38       * @param publicKeyServerBaseUrl  the base url of the public key server
39       */
40      public AuthenticationContext(@Nonnull Set<String> resourceServerAudiences,
41                                   @Nonnull String publicKeyServerBaseUrl) {
42          this(resourceServerAudiences,
43                  PublicKeyProviderFactory.createDefault().createPublicKeyProvider(publicKeyServerBaseUrl));
44      }
45  
46      /**
47       * Create a new {@link AuthenticationContext} encapsulating the required information to authenticate a JWT.
48       *
49       * @param resourceServerAudience the audience value of the resource server
50       * @param publicKeyProvider      the public key provider
51       */
52      public AuthenticationContext(@Nonnull String resourceServerAudience,
53                                   @Nonnull KeyProvider<PublicKey> publicKeyProvider) {
54          this(ImmutableSet.of(requireNonNull(resourceServerAudience)), publicKeyProvider);
55      }
56  
57      /**
58       * Create a new {@link AuthenticationContext} encapsulating the required information to authenticate a JWT.
59       *
60       * @param resourceServerAudiences the audience value of the resource server
61       * @param publicKeyProvider       the public key provider
62       */
63      public AuthenticationContext(@Nonnull Set<String> resourceServerAudiences,
64                                   @Nonnull KeyProvider<PublicKey> publicKeyProvider) {
65          this.resourceServerAudiences = copyOf(requireNonNull(resourceServerAudiences));
66          this.publicKeyProvider = requireNonNull(publicKeyProvider);
67      }
68  
69      /**
70       * @return the audience value of the resource server
71       * @deprecated since 2.12, use {@link #getResourceServerAudiences()} instead
72       */
73      @Deprecated
74      public String getResourceServerAudience() {
75          checkState(resourceServerAudiences.size() == 1,
76                  "Legacy getResourceServerAudience can only be called if a single audience value has been set.");
77          return Iterables.getFirst(resourceServerAudiences, null);
78      }
79  
80      /**
81       * @return the audience values of the resource server
82       */
83      public Set<String> getResourceServerAudiences() {
84          return resourceServerAudiences;
85      }
86  
87      /**
88       * @return the public key provider
89       */
90      public KeyProvider<PublicKey> getPublicKeyProvider() {
91          return publicKeyProvider;
92      }
93  }