2323import java .util .concurrent .CompletableFuture ;
2424import lombok .extern .slf4j .Slf4j ;
2525import org .apache .bifromq .plugin .authprovider .IAuthProvider ;
26+ import org .apache .bifromq .plugin .authprovider .type .CheckResult ;
27+ import org .apache .bifromq .plugin .authprovider .type .Granted ;
2628import org .apache .bifromq .plugin .authprovider .type .MQTT3AuthData ;
2729import org .apache .bifromq .plugin .authprovider .type .MQTT3AuthResult ;
2830import 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 ;
3032import org .apache .bifromq .type .ClientInfo ;
3133import 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