Skip to content

Commit

Permalink
NEW: group tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
orsondmc committed Oct 24, 2021
1 parent ca5575e commit 0c283fb
Show file tree
Hide file tree
Showing 21 changed files with 315 additions and 205 deletions.
1 change: 0 additions & 1 deletion server/src/main/java/com/collarmc/server/CollarServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import com.collarmc.security.messages.CipherException;
import com.collarmc.security.mojang.MinecraftPlayer;
import com.collarmc.security.mojang.Mojang;
import com.collarmc.server.http.ApiToken;
import com.collarmc.server.protocol.*;
import io.github.bucket4j.Bandwidth;
import io.github.bucket4j.Bucket;
Expand Down
4 changes: 2 additions & 2 deletions server/src/main/java/com/collarmc/server/Services.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import com.collarmc.server.security.hashing.PasswordHashing;
import com.collarmc.server.security.mojang.MinecraftSessionVerifier;
import com.collarmc.server.services.authentication.ServerAuthenticationService;
import com.collarmc.server.services.authentication.TokenCrypter;
import com.collarmc.security.TokenCrypter;
import com.collarmc.server.services.friends.FriendsService;
import com.collarmc.server.services.groups.GroupService;
import com.collarmc.server.services.groups.GroupStore;
Expand Down Expand Up @@ -61,7 +61,7 @@ public Services(Configuration configuration) throws Exception {
this.tokenCrypter = configuration.tokenCrypter;
this.auth = new ServerAuthenticationService(profiles, passwordHashing, tokenCrypter, configuration.email, urlProvider);
this.minecraftSessionVerifier = configuration.minecraftSessionVerifier;
this.groupStore = new GroupStore(profileCache, sessions, configuration.database);
this.groupStore = new GroupStore(profileCache, sessions, tokenCrypter, configuration.database);
this.groups = new GroupService(groupStore, profileCache, sessions, identityStore.cipher());
this.playerLocations = new PlayerLocationService(this);
this.textures = new TextureService(configuration.database);
Expand Down
13 changes: 10 additions & 3 deletions server/src/main/java/com/collarmc/server/WebServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.collarmc.api.groups.GroupType;
import com.collarmc.api.groups.MembershipRole;
import com.collarmc.api.groups.http.CreateGroupTokenRequest;
import com.collarmc.api.groups.http.UpdateGroupMembershipRequest;
import com.collarmc.api.http.*;
import com.collarmc.api.http.HttpException.BadRequestException;
import com.collarmc.api.http.HttpException.NotFoundException;
Expand All @@ -18,9 +19,9 @@
import com.collarmc.server.common.ServerStatus;
import com.collarmc.server.common.ServerVersion;
import com.collarmc.server.configuration.Configuration;
import com.collarmc.server.http.ApiToken;
import com.collarmc.security.ApiToken;
import com.collarmc.server.http.HandlebarsTemplateEngine;
import com.collarmc.server.services.authentication.TokenCrypter;
import com.collarmc.security.TokenCrypter;
import com.collarmc.api.groups.http.ValidateGroupTokenRequest;
import com.collarmc.server.services.textures.TextureService;
import com.collarmc.server.session.ClientRegistrationService;
Expand Down Expand Up @@ -210,7 +211,7 @@ public void start(Consumer<Services> callback) throws Exception {
});

path("/groups", () -> {
get("/groups", (request, response) -> {
get("/", (request, response) -> {
RequestContext context = from(request);
context.assertNotAnonymous();
return services.groupStore.findGroupsContaining(context.owner).collect(Collectors.toList());
Expand All @@ -228,6 +229,12 @@ public void start(Consumer<Services> callback) throws Exception {
CreateGroupTokenRequest req = services.jsonMapper.readValue(request.bodyAsBytes(), CreateGroupTokenRequest.class);
return services.groups.createGroupToken(context, req);
}, services.jsonMapper::writeValueAsString);
post("/members/add", (request, response) -> {
RequestContext context = from(request);
context.assertNotAnonymous();
UpdateGroupMembershipRequest req = services.jsonMapper.readValue(request.bodyAsBytes(), UpdateGroupMembershipRequest.class);
return services.groups.updateMembers(context, req);
}, services.jsonMapper::writeValueAsString);
});

path("/auth", () -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@
import com.collarmc.server.mail.LocalEmail;
import com.collarmc.server.mail.MailGunEmail;
import com.collarmc.server.mongo.Mongo;
import com.collarmc.server.security.TokenCrypterImpl;
import com.collarmc.server.security.hashing.PasswordHashing;
import com.collarmc.server.security.mojang.MinecraftSessionVerifier;
import com.collarmc.server.security.mojang.MojangMinecraftSessionVerifier;
import com.collarmc.server.security.mojang.NojangMinecraftSessionVerifier;
import com.collarmc.server.services.authentication.TokenCrypter;
import com.collarmc.security.TokenCrypter;
import com.mongodb.client.MongoDatabase;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand Down Expand Up @@ -87,7 +88,7 @@ public static Configuration fromEnvironment() {
return new Configuration(
Mongo.database(),
appUrlProvider,
new TokenCrypter(crypterPassword),
new TokenCrypterImpl(crypterPassword),
new PasswordHashing(passwordSalt),
useMojang ? new MojangMinecraftSessionVerifier(http) : new NojangMinecraftSessionVerifier(),
appUrlProvider.homeUrl(),
Expand All @@ -104,7 +105,7 @@ public static Configuration defaultConfiguration() {
return new Configuration(
Mongo.database("mongodb://localhost/collar-dev"),
appUrlProvider,
new TokenCrypter("insecureTokenCrypterPassword"),
new TokenCrypterImpl("insecureTokenCrypterPassword"),
new PasswordHashing("VSZL*bR8-=r]r5P_"),
new NojangMinecraftSessionVerifier(),
"*",
Expand All @@ -120,7 +121,7 @@ public static Configuration testConfiguration(MongoDatabase db, MinecraftSession
return new Configuration(
db,
appUrlProvider,
new TokenCrypter("insecureTokenCrypterPassword"),
new TokenCrypterImpl("insecureTokenCrypterPassword"),
new PasswordHashing("VSZL*bR8-=r]r5P_"),
sessionVerifier,
"*",
Expand Down
91 changes: 0 additions & 91 deletions server/src/main/java/com/collarmc/server/http/ApiToken.java

This file was deleted.

72 changes: 0 additions & 72 deletions server/src/main/java/com/collarmc/server/http/Cookie.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
package com.collarmc.server.services.authentication;
package com.collarmc.server.security;


import com.collarmc.security.TokenCrypter;
import org.jasypt.util.binary.AES256BinaryEncryptor;

public class TokenCrypter {
public class TokenCrypterImpl implements TokenCrypter {

private final AES256BinaryEncryptor encryptor;

public TokenCrypter(String password) {
public TokenCrypterImpl(String password) {
encryptor = new AES256BinaryEncryptor();
encryptor.setPassword(password);
}

@Override
public byte[] decrypt(byte[] bytes) {
return encryptor.decrypt(bytes);
}

@Override
public byte[] crypt(byte[] bytes) {
return encryptor.encrypt(bytes);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
import com.collarmc.api.profiles.ProfileService.CreateProfileRequest;
import com.collarmc.api.profiles.ProfileService.GetProfileRequest;
import com.collarmc.api.profiles.ProfileService.UpdateProfileRequest;
import com.collarmc.server.http.ApiToken;
import com.collarmc.security.ApiToken;
import com.collarmc.server.http.AppUrlProvider;
import com.collarmc.server.mail.Email;
import com.collarmc.security.TokenCrypter;
import com.collarmc.server.security.hashing.PasswordHashing;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand All @@ -20,6 +21,7 @@
import java.util.Date;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.TimeUnit;

Expand Down Expand Up @@ -161,7 +163,7 @@ public ResetPasswordResponse resetPassword(RequestContext context, ResetPassword
* @return token
*/
public String createToken(Profile profile) {
ApiToken apiToken = new ApiToken(profile.id, profile.roles);
ApiToken apiToken = new ApiToken(profile.id, profile.roles, Set.of());
String token;
try {
token = apiToken.serialize(tokenCrypter);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.collarmc.server.services.authentication;

import com.collarmc.security.TokenCrypter;
import com.google.common.io.BaseEncoding;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand Down
Loading

0 comments on commit 0c283fb

Please sign in to comment.