Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ensure unique platform account binding per user #63

Merged
merged 1 commit into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ test {
}

halo {
version = '2.17'
version = '2.19'
debug = true
}
41 changes: 22 additions & 19 deletions src/main/java/run/halo/oauth/UserConnectionServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,24 +40,33 @@ public Mono<UserConnection> createConnection(String username,

UserConnection connection = convert(username, authentication);
String providerUserId = authentication.getPrincipal().getName();
return findByRegistrationId(connection.getSpec().getRegistrationId())
.hasElement()
.flatMap(exists -> {
if (exists) {
return checkExistingBinding(connection.getSpec().getRegistrationId(), username)
.flatMap(existing -> {
if (existing) {
return Mono.error(new ServerWebInputException(
"已经绑定过 " + connection.getSpec().getRegistrationId() + " 账号,请先解绑"));
"已经绑定过其他 " + connection.getSpec().getRegistrationId()
+ " 账号,请先解绑后重试"));
}
return fetchUserConnection(connection.getSpec().getRegistrationId(), providerUserId)
.flatMap(persisted -> {
connection.getMetadata().setName(persisted.getMetadata().getName());
connection.getMetadata()
.setVersion(persisted.getMetadata().getVersion());
return client.update(connection);
})
.switchIfEmpty(Mono.defer(() -> client.create(connection)));
return upsertUserConnection(connection, providerUserId);
});
}

private Mono<Boolean> checkExistingBinding(String registrationId, String username) {
return listByRegistrationIdAndUsername(registrationId, username)
.hasElements();
}

private Mono<UserConnection> upsertUserConnection(UserConnection connection,
String providerUserId) {
return fetchUserConnection(connection.getSpec().getRegistrationId(), providerUserId)
.flatMap(persisted -> {
connection.getMetadata().setName(persisted.getMetadata().getName());
connection.getMetadata().setVersion(persisted.getMetadata().getVersion());
return client.update(connection);
})
.switchIfEmpty(Mono.defer(() -> client.create(connection)));
}

@Override
public Flux<UserConnection> removeConnection(String registrationId) {
return ReactiveSecurityContextHolder.getContext()
Expand Down Expand Up @@ -90,12 +99,6 @@ Flux<UserConnection> listByRegistrationIdAndUsername(String registrationId, Stri
&& persisted.getSpec().getUsername().equals(username), null);
}

private Mono<UserConnection> findByRegistrationId(String registrationId) {
return client.list(UserConnection.class,
persisted -> persisted.getSpec().getRegistrationId().equals(registrationId), null)
.next();
}

private Mono<UserConnection> fetchUserConnection(String registrationId, String providerUserId) {
return client.list(UserConnection.class, persisted -> persisted.getSpec()
.getProviderUserId().equals(providerUserId)
Expand Down