-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
bazke
committed
Aug 31, 2024
1 parent
72d6ce5
commit b12b9ba
Showing
10 changed files
with
237 additions
and
16 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,3 +7,4 @@ ltlib_version=[1.3,1.4) | |
|
||
org.gradle.jvmargs=-Xmx3G | ||
org.gradle.daemon=false | ||
org.jline.terminal.dumb=false |
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
87 changes: 87 additions & 0 deletions
87
src/main/java/com/lovetropics/perms/keycloak/KeycloakService.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,87 @@ | ||
package com.lovetropics.perms.keycloak; | ||
|
||
import com.mojang.authlib.GameProfile; | ||
import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder; | ||
import org.keycloak.OAuth2Constants; | ||
import org.keycloak.admin.client.Keycloak; | ||
import org.keycloak.admin.client.KeycloakBuilder; | ||
import org.keycloak.representations.idm.GroupRepresentation; | ||
import org.keycloak.representations.idm.UserRepresentation; | ||
|
||
import java.util.List; | ||
import java.util.stream.Stream; | ||
|
||
public class KeycloakService { | ||
|
||
private static Keycloak INSTANCE; | ||
final static String serverUrl = "https://identity.lovetropics.org"; | ||
final static String realm = "LoveTropics"; | ||
final static String clientId = "lt-minecraft-query"; | ||
final static String clientSecret = "secret"; | ||
|
||
public KeycloakService() { | ||
|
||
} | ||
|
||
public void getRolesForPlayer(final GameProfile gameProfile) { | ||
getInstance().realm(realm) | ||
.users() | ||
.list() | ||
.forEach(userRepresentation -> { | ||
System.out.println(userRepresentation.getUsername()); | ||
}); | ||
} | ||
|
||
public static boolean hasAccessToLt24(final GameProfile gameProfile) { | ||
|
||
final List<UserRepresentation> search = getInstance().realm(realm) | ||
.users().search(".tommi."); | ||
|
||
//final List<String> stringStream = getInstance().realm(realm).users().search(".tommi.").get(0).getGroups(); | ||
|
||
getInstance().realm(realm).users().get("10d21d80-08d1-4857-aa9b-5bdbc9bfdb1e").groups().stream() | ||
.map(GroupRepresentation::getName) | ||
.forEach(s -> System.out.println(s)); | ||
|
||
//.map(GroupRepresentation::getName).findFirst().map(Object::toString).orElse("NO_GROUP") | ||
|
||
getInstance().realm(realm) | ||
.users() | ||
.list() | ||
.forEach(userRepresentation -> { | ||
final String username = userRepresentation.getUsername(); | ||
final List<String> realmRoles = userRepresentation.getRealmRoles(); | ||
//join realmRoles | ||
|
||
final String roles = realmRoles == null ? "" : String.join(", ", realmRoles); | ||
System.out.println(username + " has roles: " + roles); | ||
|
||
}); | ||
|
||
|
||
return true; | ||
} | ||
|
||
public static Keycloak getInstance() { | ||
if (INSTANCE == null) { | ||
INSTANCE = createInstance(); | ||
} | ||
return INSTANCE; | ||
} | ||
|
||
private static Keycloak createInstance() { | ||
if (INSTANCE == null) { | ||
INSTANCE = KeycloakBuilder.builder() | ||
.serverUrl(serverUrl) | ||
.grantType(OAuth2Constants.PASSWORD) | ||
.realm("master") | ||
.clientId("admin-cli") | ||
// .clientSecret(clientSecret) | ||
.resteasyClient(ResteasyClientBuilder.newClient()) | ||
.username(clientId) | ||
.password(clientSecret) | ||
.build(); | ||
} | ||
return INSTANCE; | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/main/java/com/lovetropics/perms/mixin/rule/UserWhiteListMixin.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,33 @@ | ||
package com.lovetropics.perms.mixin.rule; | ||
|
||
import com.lovetropics.perms.keycloak.KeycloakService; | ||
import com.mojang.authlib.GameProfile; | ||
import net.minecraft.server.players.UserWhiteList; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
import org.spongepowered.asm.mixin.injection.Inject; | ||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; | ||
|
||
import java.net.URI; | ||
import java.net.URISyntaxException; | ||
import java.net.http.HttpRequest; | ||
|
||
@Mixin(UserWhiteList.class) | ||
public class UserWhiteListMixin { | ||
|
||
@Inject(method = "isWhiteListed", at = @At("HEAD"), cancellable = true) | ||
public void isWhiteListed(final GameProfile gameProfile, final CallbackInfoReturnable<Boolean> cir) throws URISyntaxException { | ||
// cir.setReturnValue(false); | ||
System.out.println("chekin yo whitelist he he he he"); | ||
|
||
final boolean hasAccess = KeycloakService.hasAccessToLt24(gameProfile); | ||
|
||
if (!hasAccess) { | ||
System.out.println("User " + gameProfile.getName() + " does not have access to the server"); | ||
} | ||
|
||
|
||
cir.setReturnValue(hasAccess); | ||
|
||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"required": true, | ||
"package": "com.lovetropics.perms.mixin", | ||
"compatibilityLevel": "JAVA_17", | ||
"refmap": "ltpermissions.refmap.json", | ||
"mixins": [ | ||
"rule.LecternMenuMixin", | ||
"rule.SignBlockMixin", | ||
"rule.UserWhiteListMixin" | ||
], | ||
"injectors": { | ||
"defaultRequire": 1 | ||
}, | ||
"minVersion": "0.8" | ||
} |
Oops, something went wrong.