1 package com.atlassian.sal.trustedapps;
2
3 import com.atlassian.plugin.StateAware;
4 import com.atlassian.sal.core.trusted.CertificateFactory;
5 import com.atlassian.security.auth.trustedapps.EncryptedCertificate;
6 import com.atlassian.security.auth.trustedapps.TrustedApplicationsManager;
7 import org.osgi.framework.BundleContext;
8 import org.osgi.util.tracker.ServiceTracker;
9 import org.slf4j.Logger;
10 import org.slf4j.LoggerFactory;
11
12
13
14
15
16
17 public class TrustedAppsPluginCertificateFactory implements CertificateFactory, StateAware {
18 private static final Logger log = LoggerFactory.getLogger(TrustedAppsPluginCertificateFactory.class);
19 private static final String TRUSTED_APPLICATIONS_MANAGER = "com.atlassian.security.auth.trustedapps.TrustedApplicationsManager";
20 private ServiceTracker serviceTracker;
21 private final BundleContext bundleContext;
22
23 public TrustedAppsPluginCertificateFactory(BundleContext bundleContext) {
24 this.bundleContext = bundleContext;
25 serviceTracker = new ServiceTracker(bundleContext, TRUSTED_APPLICATIONS_MANAGER, null);
26 serviceTracker.open();
27 }
28
29 public EncryptedCertificate createCertificate(String username) {
30 throw new UnsupportedOperationException("Not implemented. Since v2.10.9 Trusted Apps requires a url for its signature");
31 }
32
33 public EncryptedCertificate createCertificate(String username, String url) {
34 if (serviceTracker != null) {
35 try {
36 TrustedApplicationsManager trustedApplicationsManager = (TrustedApplicationsManager) serviceTracker.getService();
37 if (trustedApplicationsManager != null) {
38 return trustedApplicationsManager.getCurrentApplication().encode(username, url);
39 }
40 } catch (NoClassDefFoundError ncdfe) {
41
42
43
44
45
46
47 } catch (ClassCastException cce) {
48 log.warn(
49 "A TrustedApplicationsManager was found, but a ClassCastException was thrown when attempting to cast it. This is possible if TrustedApplicationsManager has been uninstalled and installed",
50 cce);
51 }
52 }
53 throw new UnsupportedOperationException("Trusted apps support is not installed.");
54 }
55
56 public void enabled() {
57 if (serviceTracker == null) {
58 serviceTracker = new ServiceTracker(bundleContext, TRUSTED_APPLICATIONS_MANAGER, null);
59 serviceTracker.open();
60 }
61 }
62
63 public void disabled() {
64 serviceTracker.close();
65 serviceTracker = null;
66 }
67 }