-
Notifications
You must be signed in to change notification settings - Fork 824
/
IdentityZoneHolder.java
161 lines (130 loc) · 5.45 KB
/
IdentityZoneHolder.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*******************************************************************************
* Cloud Foundry
* Copyright (c) [2009-2016] Pivotal Software, Inc. All Rights Reserved.
*
* This product is licensed to you under the Apache License, Version 2.0 (the "License").
* You may not use this product except in compliance with the License.
*
* This product includes a number of subcomponents with
* separate copyright notices and license terms. Your use of these
* subcomponents is subject to the terms and conditions of the
* subcomponent's license, as noted in the LICENSE file.
*******************************************************************************/
package org.cloudfoundry.identity.uaa.zone;
import org.cloudfoundry.identity.uaa.provider.saml.SamlKeyManagerFactory;
import org.springframework.security.saml.key.KeyManager;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import static java.util.Optional.ofNullable;
public class IdentityZoneHolder {
private static IdentityZoneProvisioning provisioning;
public static void setProvisioning(IdentityZoneProvisioning provisioning) {
IdentityZoneHolder.provisioning = provisioning;
}
private static final ThreadLocal<IdentityZoneWithKeyManager> THREADLOCAL = new InheritableThreadLocal<IdentityZoneWithKeyManager>() {
@Override
protected IdentityZoneWithKeyManager initialValue() {
if (provisioning == null) {
return new IdentityZoneWithKeyManager(IdentityZone.getUaa(), null);
}
IdentityZone zone = getUaaZone();
return new IdentityZoneWithKeyManager(zone, null);
}
};
public static IdentityZone get() {
return THREADLOCAL.get().getZone();
}
public static KeyManager getSamlSPKeyManager() {
IdentityZoneWithKeyManager withKeyManager = THREADLOCAL.get();
if (withKeyManager.getManager() == null) {
KeyManager keyManager = SamlKeyManagerFactory.getKeyManager(withKeyManager.getZone().getConfig().getSamlConfig());
if (keyManager == null) {
keyManager = SamlKeyManagerFactory.getKeyManager(getUaaZone().getConfig().getSamlConfig());
}
withKeyManager.setManager(keyManager);
}
return withKeyManager.getManager();
}
public static IdentityZone getUaaZone() {
if (provisioning == null) {
return IdentityZone.getUaa();
}
return provisioning.retrieve(IdentityZone.getUaa().getId());
}
public static void set(IdentityZone zone) {
THREADLOCAL.set(new IdentityZoneWithKeyManager(zone, null));
}
public static void clear() {
THREADLOCAL.remove();
}
public static boolean isUaa() {
return THREADLOCAL.get().getZone().getId().equals(IdentityZone.getUaa().getId());
}
public static class Initializer {
public Initializer(IdentityZoneProvisioning provisioning) {
IdentityZoneHolder.setProvisioning(provisioning);
}
public void reset() {
IdentityZoneHolder.setProvisioning(null);
}
}
public static class IdentityZoneWithKeyManager {
private IdentityZone zone;
private KeyManager manager;
IdentityZoneWithKeyManager(IdentityZone zone, KeyManager manager) {
this.zone = zone;
this.manager = manager;
}
public IdentityZone getZone() {
return zone;
}
public KeyManager getManager() {
return manager;
}
public void setManager(KeyManager manager) {
this.manager = manager;
}
}
private static class MergedZoneBrandingInformation implements BrandingInformationSource {
@Override
public BrandingInformation.Banner getBanner() {
return resolve(BrandingInformationSource::getBanner);
}
@Override
public String getCompanyName() {
return resolve(BrandingInformationSource::getCompanyName);
}
@Override
public String getProductLogo() {
return tryGet(get(), BrandingInformationSource::getProductLogo).orElse(null);
}
@Override
public String getSquareLogo() {
return resolve(BrandingInformationSource::getSquareLogo);
}
@Override
public String getFooterLegalText() {
return resolve(BrandingInformationSource::getFooterLegalText);
}
@Override
public Map<String, String> getFooterLinks() {
return resolve(BrandingInformationSource::getFooterLinks);
}
private static <T> T resolve(Function<BrandingInformationSource, T> brandingProperty) {
return
tryGet(get(), brandingProperty)
.orElse(tryGet(getUaaZone(), brandingProperty)
.orElse(null));
}
private static <T> Optional<T> tryGet(IdentityZone zone, Function<BrandingInformationSource, T> brandingProperty) {
return ofNullable(zone.getConfig())
.flatMap(c -> ofNullable(c.getBranding()))
.flatMap(b -> ofNullable(brandingProperty.apply(b)));
}
}
private static final BrandingInformationSource brandingResolver = new MergedZoneBrandingInformation();
public static BrandingInformationSource resolveBranding() {
return brandingResolver;
}
}