View Javadoc

1   package com.atlassian.security.auth.trustedapps;
2   
3   import com.atlassian.security.auth.trustedapps.Transcoder.Base64Transcoder;
4   
5   import java.security.NoSuchAlgorithmException;
6   import java.security.NoSuchProviderException;
7   import java.security.PublicKey;
8   import java.security.spec.InvalidKeySpecException;
9   import java.util.ArrayList;
10  import java.util.Iterator;
11  import java.util.List;
12  
13  public class ListApplicationRetriever implements ApplicationRetriever
14  {
15      private final List /* <String> */values;
16      private final EncryptionProvider encryptionProvider;
17      private final Transcoder transcoder = new Base64Transcoder();
18  
19      ListApplicationRetriever(EncryptionProvider encryptionProvider, List /* <String> */values)
20      {
21          Null.not("encryptionProvider", encryptionProvider);
22          Null.not("values", values);
23          int i = 0;
24          for (Iterator it = values.iterator(); it.hasNext();)
25          {
26              Null.not("value: " + i++, it.next());
27          }
28  
29          this.encryptionProvider = encryptionProvider;
30          this.values = new ArrayList /* <String> */(values);
31      }
32  
33      public Application getApplication() throws RetrievalException
34      {
35          if (values.size() < 2)
36          {
37              throw new ApplicationNotFoundException("Application Certificate too small");
38          }
39          if (values.size() == 2)
40          {
41              return getApplicationProtocolV0();
42          }
43          return getApplicationProtocolV1();
44      }
45  
46      private Application getApplicationProtocolV1() throws RetrievalException
47      {
48          // decorate the protocol zero version
49          Application result = getApplicationProtocolV0();
50          // with some validation of the certificate
51          String protocol = (String) values.get(2);
52          String magic = (String) values.get(3);
53          try
54          {
55              final Integer protocolVersion = isBlank(protocol) ? null : Integer.valueOf(protocol);
56              try
57              {
58                  TrustedApplicationUtils.validateMagicNumber("application details", result.getID(), protocolVersion, magic);
59              }
60              catch (InvalidCertificateException e)
61              {
62                  throw new InvalidApplicationDetailsException(e);
63              }
64          }
65          catch (NumberFormatException e)
66          {
67              throw new InvalidApplicationDetailsException(e);
68          }
69          return result;
70      }
71  
72      private Application getApplicationProtocolV0() throws RetrievalException
73      {
74          try
75          {
76              String id = (String) values.get(0);
77              String keyStr = (String) values.get(1);
78  
79              if (keyStr == null)
80              {
81                  throw new ApplicationNotFoundException("Public Key not found");
82              }
83  
84              final byte[] data = transcoder.decode(keyStr);
85              final PublicKey key = encryptionProvider.toPublicKey(data);
86              return new SimpleApplication(id, key);
87          }
88          catch (InvalidKeySpecException e)
89          {
90              throw new RuntimeException(e);
91          }
92          catch (NoSuchAlgorithmException e)
93          {
94              throw new RuntimeException(e);
95          }
96          catch (NoSuchProviderException e)
97          {
98              throw new RuntimeException(e);
99          }
100     }
101 
102     private static boolean isBlank(String input)
103     {
104         return (input == null) || input.trim().length() == 0;
105     }
106 }