Skip to content

Commit 527ac7e

Browse files
Change DemoAuthProvider fallback behavior to fit plugin manager changes (#239)
1. change DemoAuthProvider fallback behavior to fit new plugin manager changes. 2. make fallback plugin completely align with dev only auth plugin; 3. add a checkPermission to avoid deletion override missing. --------- Co-authored-by: Yonny(Yu) Hao <popduke@gmail.com>
1 parent 3ec11c5 commit 527ac7e

1 file changed

Lines changed: 41 additions & 9 deletions

File tree

build/build-plugin-demo/src/main/java/org/apache/bifromq/demo/plugin/DemoAuthProvider.java

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,12 @@
2323
import java.util.concurrent.CompletableFuture;
2424
import lombok.extern.slf4j.Slf4j;
2525
import org.apache.bifromq.plugin.authprovider.IAuthProvider;
26+
import org.apache.bifromq.plugin.authprovider.type.CheckResult;
27+
import org.apache.bifromq.plugin.authprovider.type.Granted;
2628
import org.apache.bifromq.plugin.authprovider.type.MQTT3AuthData;
2729
import org.apache.bifromq.plugin.authprovider.type.MQTT3AuthResult;
2830
import org.apache.bifromq.plugin.authprovider.type.MQTTAction;
29-
import org.apache.bifromq.plugin.authprovider.type.Reject;
31+
import org.apache.bifromq.plugin.authprovider.type.Ok;
3032
import org.apache.bifromq.type.ClientInfo;
3133
import org.pf4j.Extension;
3234

@@ -40,7 +42,7 @@ public DemoAuthProvider() {
4042
IAuthProvider delegate1;
4143
String webhookUrl = System.getProperty(PLUGIN_AUTHPROVIDER_URL);
4244
if (webhookUrl == null) {
43-
log.info("No webhook url specified, the fallback behavior will reject all auth/check requests.");
45+
log.info("No webhook url specified, the fallback behavior will grant all auth/check requests.");
4446
delegate1 = new FallbackAuthProvider();
4547
} else {
4648
try {
@@ -64,20 +66,50 @@ public CompletableFuture<Boolean> check(ClientInfo client, MQTTAction action) {
6466
return delegate.check(client, action);
6567
}
6668

69+
@Override
70+
public CompletableFuture<CheckResult> checkPermission(ClientInfo client, MQTTAction action) {
71+
return delegate.checkPermission(client, action);
72+
}
73+
6774
static class FallbackAuthProvider implements IAuthProvider {
75+
private static final CheckResult GRANTED = CheckResult.newBuilder().setGranted(
76+
Granted.getDefaultInstance()).build();
6877
@Override
6978
public CompletableFuture<MQTT3AuthResult> auth(MQTT3AuthData authData) {
70-
return CompletableFuture.completedFuture(
71-
MQTT3AuthResult.newBuilder().setReject(Reject.newBuilder()
72-
.setCode(Reject.Code.Error)
73-
.setReason("No webhook url for auth provider configured")
74-
.build())
75-
.build());
79+
if (!(authData.getUsername() == null || authData.getUsername().isEmpty())) {
80+
String[] username = authData.getUsername().split("/");
81+
if (username.length == 2) {
82+
return CompletableFuture.completedFuture(MQTT3AuthResult
83+
.newBuilder()
84+
.setOk(Ok.newBuilder()
85+
.setTenantId(username[0])
86+
.setUserId(username[1])
87+
.build())
88+
.build());
89+
} else {
90+
return CompletableFuture.completedFuture(MQTT3AuthResult.newBuilder()
91+
.setOk(Ok.newBuilder()
92+
.setTenantId("DevOnly")
93+
.setUserId(authData.getUsername()).build())
94+
.build());
95+
}
96+
} else {
97+
return CompletableFuture.completedFuture(MQTT3AuthResult.newBuilder()
98+
.setOk(Ok.newBuilder()
99+
.setTenantId("DevOnly")
100+
.setUserId("DevUser").build())
101+
.build());
102+
}
76103
}
77104

78105
@Override
79106
public CompletableFuture<Boolean> check(ClientInfo client, MQTTAction action) {
80-
return CompletableFuture.completedFuture(false);
107+
return CompletableFuture.completedFuture(true);
108+
}
109+
110+
@Override
111+
public CompletableFuture<CheckResult> checkPermission(ClientInfo client, MQTTAction action) {
112+
return CompletableFuture.completedFuture(GRANTED);
81113
}
82114
}
83115
}

0 commit comments

Comments
 (0)