View Javadoc

1   package com.atlassian.asap.core.client;
2   
3   import com.atlassian.asap.api.client.http.AuthorizationHeaderGenerator;
4   import com.atlassian.asap.core.client.http.AuthorizationHeaderGeneratorImpl;
5   import com.atlassian.asap.core.keys.DataUriKeyReader;
6   import com.atlassian.asap.core.keys.KeyProvider;
7   import com.atlassian.asap.core.keys.privatekey.DataUriKeyProvider;
8   import com.atlassian.asap.core.serializer.JwtSerializer;
9   import com.atlassian.asap.nimbus.serializer.NimbusJwtSerializer;
10  import org.springframework.beans.factory.annotation.Autowired;
11  import org.springframework.beans.factory.annotation.Value;
12  import org.springframework.context.annotation.Bean;
13  import org.springframework.context.annotation.Configuration;
14  
15  import java.net.URI;
16  import java.net.URISyntaxException;
17  import java.security.PrivateKey;
18  
19  /**
20   * Client-side ASAP configuration.
21   */
22  @Configuration
23  public class AsapClientConfiguration {
24      private final String issuer;
25  
26      private final String keyId;
27  
28      @Autowired
29      AsapClientConfiguration(@Value("${asap.issuer}") String issuer,
30                              @Value("${asap.key_id}") String keyId) {
31          this.issuer = issuer;
32          this.keyId = keyId;
33      }
34  
35      /**
36       * Definition of the provider of private keys.
37       *
38       * @param privateKeyDataUri a private key, in the data URI format
39       * @return a provider of private keys that provides the key passed as argument
40       */
41      @Bean
42      public KeyProvider<PrivateKey> privateKeyProvider(@Value("${asap.private_key}") String privateKeyDataUri) {
43          final URI parsedPrivateKeyDataUri;
44          try {
45              parsedPrivateKeyDataUri = new URI(privateKeyDataUri);
46          } catch (URISyntaxException e) {
47              throw new IllegalArgumentException("Cannot parse private data URI argument as a URI");
48          }
49          return new DataUriKeyProvider(parsedPrivateKeyDataUri, new DataUriKeyReader());
50      }
51  
52      /**
53       * Definition of the {@link JwtSerializer} bean.
54       *
55       * @return an instance of {@link JwtSerializer}
56       */
57      @Bean
58      public JwtSerializer jwtSerializer() {
59          return new NimbusJwtSerializer();
60      }
61  
62      /**
63       * Definition of the {@link AuthorizationHeaderGenerator} bean.
64       *
65       * @param jwtSerializer      the token serializer
66       * @param privateKeyProvider the provider of the private key used to sign the token
67       * @return an instance of {@link AuthorizationHeaderGenerator}
68       */
69      @Bean
70      public AuthorizationHeaderGenerator authorizationHeaderGenerator(JwtSerializer jwtSerializer,
71                                                                       KeyProvider<PrivateKey> privateKeyProvider) {
72          return new AuthorizationHeaderGeneratorImpl(jwtSerializer, privateKeyProvider);
73      }
74  
75      /**
76       * @return the issuer
77       */
78      public String getIssuer() {
79          return issuer;
80      }
81  
82      /**
83       * @return the key identifier
84       */
85      public String getKeyId() {
86          return keyId;
87      }
88  }