-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into feature/uc-attachments
- Loading branch information
Showing
25 changed files
with
455 additions
and
88 deletions.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
api/src/main/java/run/halo/app/event/user/UserConnectionDisconnectedEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package run.halo.app.event.user; | ||
|
||
import lombok.Getter; | ||
import org.springframework.context.ApplicationEvent; | ||
import run.halo.app.core.extension.UserConnection; | ||
import run.halo.app.plugin.SharedEvent; | ||
|
||
/** | ||
* An event that will be triggered after a user connection is disconnected. | ||
* | ||
* @author johnniang | ||
* @since 2.20.0 | ||
*/ | ||
@SharedEvent | ||
public class UserConnectionDisconnectedEvent extends ApplicationEvent { | ||
|
||
@Getter | ||
private final UserConnection userConnection; | ||
|
||
public UserConnectionDisconnectedEvent(Object source, UserConnection userConnection) { | ||
super(source); | ||
this.userConnection = userConnection; | ||
} | ||
|
||
} |
74 changes: 74 additions & 0 deletions
74
application/src/main/java/run/halo/app/core/endpoint/uc/UserConnectionEndpoint.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package run.halo.app.core.endpoint.uc; | ||
|
||
import static org.springdoc.core.fn.builders.apiresponse.Builder.responseBuilder; | ||
|
||
import io.swagger.v3.oas.annotations.enums.ParameterIn; | ||
import org.springdoc.core.fn.builders.parameter.Builder; | ||
import org.springdoc.webflux.core.fn.SpringdocRouteBuilder; | ||
import org.springframework.security.authentication.AuthenticationTrustResolver; | ||
import org.springframework.security.authentication.AuthenticationTrustResolverImpl; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.core.context.ReactiveSecurityContextHolder; | ||
import org.springframework.security.core.context.SecurityContext; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.reactive.function.server.RouterFunction; | ||
import org.springframework.web.reactive.function.server.ServerResponse; | ||
import run.halo.app.core.extension.UserConnection; | ||
import run.halo.app.core.extension.endpoint.CustomEndpoint; | ||
import run.halo.app.core.user.service.UserConnectionService; | ||
import run.halo.app.extension.GroupVersion; | ||
|
||
/** | ||
* User connection endpoint. | ||
* | ||
* @author johnniang | ||
* @since 2.20.0 | ||
*/ | ||
@Component | ||
public class UserConnectionEndpoint implements CustomEndpoint { | ||
|
||
private final UserConnectionService connectionService; | ||
|
||
private final AuthenticationTrustResolver authenticationTrustResolver = | ||
new AuthenticationTrustResolverImpl(); | ||
|
||
public UserConnectionEndpoint(UserConnectionService connectionService) { | ||
this.connectionService = connectionService; | ||
} | ||
|
||
@Override | ||
public RouterFunction<ServerResponse> endpoint() { | ||
var tag = "UserConnectionV1alpha1Uc"; | ||
return SpringdocRouteBuilder.route() | ||
.PUT( | ||
"/user-connections/{registerId}/disconnect", | ||
request -> { | ||
var removedUserConnections = ReactiveSecurityContextHolder.getContext() | ||
.map(SecurityContext::getAuthentication) | ||
.filter(authenticationTrustResolver::isAuthenticated) | ||
.map(Authentication::getName) | ||
.flatMapMany(username -> connectionService.removeUserConnection( | ||
request.pathVariable("registerId"), username) | ||
); | ||
return ServerResponse.ok().body(removedUserConnections, UserConnection.class); | ||
}, | ||
builder -> builder.operationId("DisconnectMyConnection") | ||
.description("Disconnect my connection from a third-party platform.") | ||
.tag(tag) | ||
.parameter(Builder.parameterBuilder() | ||
.in(ParameterIn.PATH) | ||
.name("registerId") | ||
.description("The registration ID of the third-party platform.") | ||
.required(true) | ||
.implementation(String.class) | ||
) | ||
.response(responseBuilder().implementationArray(UserConnection.class)) | ||
) | ||
.build(); | ||
} | ||
|
||
@Override | ||
public GroupVersion groupVersion() { | ||
return GroupVersion.parseAPIVersion("uc.api.auth.halo.run/v1alpha1"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
application/src/main/java/run/halo/app/security/HaloServerRequestCache.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package run.halo.app.security; | ||
|
||
import static org.springframework.security.web.server.util.matcher.ServerWebExchangeMatchers.pathMatchers; | ||
|
||
import java.net.URI; | ||
import java.util.Collections; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.springframework.http.HttpMethod; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.http.server.RequestPath; | ||
import org.springframework.http.server.reactive.ServerHttpRequest; | ||
import org.springframework.security.web.server.savedrequest.WebSessionServerRequestCache; | ||
import org.springframework.security.web.server.util.matcher.AndServerWebExchangeMatcher; | ||
import org.springframework.security.web.server.util.matcher.MediaTypeServerWebExchangeMatcher; | ||
import org.springframework.security.web.server.util.matcher.NegatedServerWebExchangeMatcher; | ||
import org.springframework.security.web.server.util.matcher.ServerWebExchangeMatcher; | ||
import org.springframework.web.server.ServerWebExchange; | ||
import org.springframework.web.server.WebSession; | ||
import reactor.core.publisher.Mono; | ||
|
||
/** | ||
* Halo server request cache implementation for saving redirect URI from query. | ||
* | ||
* @author johnniang | ||
*/ | ||
public class HaloServerRequestCache extends WebSessionServerRequestCache { | ||
|
||
/** | ||
* Currently, we have no idea to customize the sessionAttributeName in | ||
* WebSessionServerRequestCache, so we have to copy the attr into here. | ||
*/ | ||
private static final String DEFAULT_SAVED_REQUEST_ATTR = "SPRING_SECURITY_SAVED_REQUEST"; | ||
|
||
private static final String REDIRECT_URI_QUERY = "redirect_uri"; | ||
|
||
private final String sessionAttrName = DEFAULT_SAVED_REQUEST_ATTR; | ||
|
||
public HaloServerRequestCache() { | ||
super(); | ||
setSaveRequestMatcher(createDefaultRequestMatcher()); | ||
} | ||
|
||
@Override | ||
public Mono<Void> saveRequest(ServerWebExchange exchange) { | ||
var redirectUriQuery = exchange.getRequest().getQueryParams().getFirst(REDIRECT_URI_QUERY); | ||
if (StringUtils.isNotBlank(redirectUriQuery)) { | ||
var redirectUri = URI.create(redirectUriQuery); | ||
return saveRedirectUri(exchange, redirectUri); | ||
} | ||
return super.saveRequest(exchange); | ||
} | ||
|
||
@Override | ||
public Mono<URI> getRedirectUri(ServerWebExchange exchange) { | ||
return super.getRedirectUri(exchange); | ||
} | ||
|
||
@Override | ||
public Mono<ServerHttpRequest> removeMatchingRequest(ServerWebExchange exchange) { | ||
return super.removeMatchingRequest(exchange); | ||
} | ||
|
||
private Mono<Void> saveRedirectUri(ServerWebExchange exchange, URI redirectUri) { | ||
var requestPath = exchange.getRequest().getPath(); | ||
var redirectPath = RequestPath.parse(redirectUri, requestPath.contextPath().value()); | ||
var query = redirectUri.getRawQuery(); | ||
var finalRedirect = | ||
redirectPath.pathWithinApplication() + (query == null ? "" : "?" + query); | ||
return exchange.getSession() | ||
.map(WebSession::getAttributes) | ||
.doOnNext(attributes -> attributes.put(this.sessionAttrName, finalRedirect)) | ||
.then(); | ||
} | ||
|
||
private static ServerWebExchangeMatcher createDefaultRequestMatcher() { | ||
var get = pathMatchers(HttpMethod.GET, "/**"); | ||
var notFavicon = new NegatedServerWebExchangeMatcher( | ||
pathMatchers( | ||
"/favicon.*", "/login/**", "/signup/**", "/password-reset/**", "/challenges/**" | ||
)); | ||
var html = new MediaTypeServerWebExchangeMatcher(MediaType.TEXT_HTML); | ||
html.setIgnoredMediaTypes(Collections.singleton(MediaType.ALL)); | ||
return new AndServerWebExchangeMatcher(get, notFavicon, html); | ||
} | ||
|
||
} |
Oops, something went wrong.