/
_ReactorUaaClient.java
140 lines (119 loc) · 4.44 KB
/
_ReactorUaaClient.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
/*
* Copyright 2013-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.cloudfoundry.reactor.uaa;
import org.cloudfoundry.Nullable;
import org.cloudfoundry.reactor.ConnectionContext;
import org.cloudfoundry.reactor.TokenProvider;
import org.cloudfoundry.reactor.uaa.authorizations.ReactorAuthorizations;
import org.cloudfoundry.reactor.uaa.clients.ReactorClients;
import org.cloudfoundry.reactor.uaa.groups.ReactorGroups;
import org.cloudfoundry.reactor.uaa.identityproviders.ReactorIdentityProviders;
import org.cloudfoundry.reactor.uaa.identityzones.ReactorIdentityZones;
import org.cloudfoundry.reactor.uaa.tokens.ReactorTokens;
import org.cloudfoundry.reactor.uaa.users.ReactorUsers;
import org.cloudfoundry.uaa.UaaClient;
import org.cloudfoundry.uaa.authorizations.Authorizations;
import org.cloudfoundry.uaa.clients.Clients;
import org.cloudfoundry.uaa.groups.Groups;
import org.cloudfoundry.uaa.identityproviders.IdentityProviders;
import org.cloudfoundry.uaa.identityzones.IdentityZones;
import org.cloudfoundry.uaa.tokens.Tokens;
import org.cloudfoundry.uaa.users.Users;
import org.immutables.value.Value;
import org.springframework.web.util.UriComponentsBuilder;
import reactor.core.publisher.Mono;
import java.util.function.Function;
/**
* The Reactor-based implementation of {@link UaaClient}
*/
@Value.Immutable
abstract class _ReactorUaaClient implements UaaClient {
@Override
@Value.Derived
public Authorizations authorizations() {
return new ReactorAuthorizations(getConnectionContext(), getRoot(), getTokenProvider());
}
@Override
@Value.Derived
public Clients clients() {
return new ReactorClients(getConnectionContext(), getRoot(), getTokenProvider());
}
@Override
@Value.Derived
public Mono<String> getUsername() {
return getUsernameProvider().get();
}
@Override
@Value.Derived
public Groups groups() {
return new ReactorGroups(getConnectionContext(), getRoot(), getTokenProvider());
}
@Override
@Value.Derived
public IdentityProviders identityProviders() {
return new ReactorIdentityProviders(getConnectionContext(), getRoot(), getTokenProvider());
}
@Override
@Value.Derived
public IdentityZones identityZones() {
return new ReactorIdentityZones(getConnectionContext(), getRoot(), getTokenProvider());
}
@Override
@Value.Derived
public Tokens tokens() {
return new ReactorTokens(getConnectionContext(), getRoot(), getTokenProvider());
}
@Override
@Value.Derived
public Users users() {
return new ReactorUsers(getConnectionContext(), getRoot(), getTokenProvider());
}
/**
* The connection context
*/
abstract ConnectionContext getConnectionContext();
/**
* The identity zone subdomain
*/
@Nullable
abstract String getIdentityZoneSubdomain();
@Value.Default
Mono<String> getRoot() {
Mono<String> cached = getConnectionContext().getRootProvider().getRoot("uaa", getConnectionContext())
.map(getIdentityZoneEndpoint(getIdentityZoneSubdomain()));
return getConnectionContext().getCacheDuration()
.map(cached::cache)
.orElseGet(cached::cache);
}
/**
* The token provider
*/
abstract TokenProvider getTokenProvider();
@Value.Default
UsernameProvider getUsernameProvider() {
return new UsernameProvider(getConnectionContext(), getTokenProvider(), tokens());
}
private static Function<String, String> getIdentityZoneEndpoint(String identityZoneId) {
return raw -> {
if (identityZoneId == null) {
return raw;
}
UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(raw);
builder.host(String.format("%s.%s", identityZoneId, builder.build().getHost()));
return builder.build().encode().toUriString();
};
}
}