/
AuthorizationContextVersioningDirective.java
executable file
·90 lines (76 loc) · 4.06 KB
/
AuthorizationContextVersioningDirective.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
/*
* Copyright (c) 2017-2018 Bosch Software Innovations GmbH.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/org/documents/epl-2.0/index.php
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.services.gateway.endpoints.directives.auth;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
import org.eclipse.ditto.model.base.auth.AuthorizationContext;
import org.eclipse.ditto.model.base.auth.AuthorizationModelFactory;
import org.eclipse.ditto.model.base.auth.AuthorizationSubject;
import org.eclipse.ditto.model.policies.SubjectId;
import org.eclipse.ditto.services.gateway.endpoints.utils.DirectivesLoggingUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import akka.http.javadsl.server.Route;
/**
* Custom Akka HTTP Directive which adjusts a given {@link AuthorizationContext} with prefixed subjects for the
* requested API version.
*/
public final class AuthorizationContextVersioningDirective {
private static final Logger LOGGER = LoggerFactory.getLogger(AuthorizationContextVersioningDirective.class);
private AuthorizationContextVersioningDirective() {
// no op
}
/**
* Adapts an {@link AuthorizationContext} with prefixed subjects for use in the requested API version.
*
* @param correlationId the correlationId (used for logging)
* @param apiVersion the API version from the URI (e.g. from "/api/1" it would be "1")
* @param authContextWithPrefixedSubjects an {@link AuthorizationContext} with prefixed subjects
* @param inner the inner Route to wrap with the mapped {@link AuthorizationContext}
* @return the new Route the mapped authentication {@link AuthorizationContext}
*/
public static Route mapAuthorizationContext(final String correlationId, final int apiVersion,
final AuthorizationContext authContextWithPrefixedSubjects,
final Function<AuthorizationContext, Route> inner) {
return DirectivesLoggingUtils.enhanceLogWithCorrelationId(correlationId, () -> {
LOGGER.debug("Original authorization context: {}", authContextWithPrefixedSubjects);
final AuthorizationContext mappedAuthorizationContext =
mapAuthorizationContext(authContextWithPrefixedSubjects, apiVersion);
LOGGER.debug("Mapped authorization context: {}", mappedAuthorizationContext);
return inner.apply(mappedAuthorizationContext);
});
}
private static AuthorizationContext mapAuthorizationContext(
final AuthorizationContext authenticationContextWithPrefixedSubjects, final int version) {
final List<AuthorizationSubject> subjectsWithPrefix = authenticationContextWithPrefixedSubjects
.getAuthorizationSubjects();
final List<AuthorizationSubject> subjectsWithoutPrefix =
subjectsWithPrefix.stream().map(AuthorizationSubject::getId)
.map(SubjectId::newInstance)
.map(SubjectId::getSubject)
.map(AuthorizationModelFactory::newAuthSubject)
.collect(Collectors.toList());
if (version == 1) {
/* while we still support authorization subjects in "old" V1, we must enhance the List of
AuthorizationSubjects by PREPENDING AuthorizationSubjects without prefix - because the first
AuthorizationSubject is used to create a default ACL
*/
return authenticationContextWithPrefixedSubjects.addHead(subjectsWithoutPrefix);
} else {
/* for V2 and above, we must enhance the List of
AuthorizationSubjects by APPENDING AuthorizationSubjects without prefix - because the first
AuthorizationSubject is used to create a default ACL
*/
return authenticationContextWithPrefixedSubjects.addTail(subjectsWithoutPrefix);
}
}
}