Skip to content

Commit

Permalink
Move MUAUser to MUARecord class
Browse files Browse the repository at this point in the history
  • Loading branch information
ustc-zzzz committed Oct 7, 2024
1 parent 2de3ecf commit da8f340
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 51 deletions.
40 changes: 33 additions & 7 deletions src/main/java/org/teacon/mua2fa/data/MUARecord.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import net.minecraft.util.ExtraCodecs;

import javax.annotation.ParametersAreNonnullByDefault;
import java.security.interfaces.EdECPrivateKey;
import java.security.interfaces.EdECPublicKey;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
Expand All @@ -31,35 +32,35 @@
public final class MUARecord {
public static final Codec<MUARecord> CODEC;
public static final StreamCodec<ByteBuf, MUARecord> STREAM_CODEC;
public static final StreamCodec<ByteBuf, Pair<GameProfile, MUAUser>> STREAM_CODEC_PART;
public static final StreamCodec<ByteBuf, Pair<GameProfile, User>> STREAM_CODEC_PART;

static {
CODEC = RecordCodecBuilder.create(builder -> builder.group(
RecordCodecBuilder.<GameProfile>mapCodec(b -> b.group(
UUIDUtil.AUTHLIB_CODEC.fieldOf("id").forGetter(GameProfile::getId),
ExtraCodecs.PLAYER_NAME.fieldOf("name").forGetter(GameProfile::getName))
.apply(b, GameProfile::new)).forGetter(MUARecord::getProfile),
MUAUser.CODEC.fieldOf("mua").forGetter(MUARecord::getUser),
User.CODEC.fieldOf("mua").forGetter(MUARecord::getUser),
Codec.list(SignEntry.CODEC).fieldOf("signatures").forGetter(MUARecord::getSignatures))
.apply(builder, MUARecord::new));
STREAM_CODEC = StreamCodec.composite(
ByteBufCodecs.GAME_PROFILE, MUARecord::getProfile, MUAUser.STREAM_CODEC, MUARecord::getUser,
ByteBufCodecs.GAME_PROFILE, MUARecord::getProfile, User.STREAM_CODEC, MUARecord::getUser,
SignEntry.STREAM_CODEC.apply(ByteBufCodecs.list()), MUARecord::getSignatures, MUARecord::new);
STREAM_CODEC_PART = StreamCodec.composite(
ByteBufCodecs.GAME_PROFILE, Pair::getFirst, MUAUser.STREAM_CODEC, Pair::getSecond, Pair::of);
ByteBufCodecs.GAME_PROFILE, Pair::getFirst, User.STREAM_CODEC, Pair::getSecond, Pair::of);
}

private final MUAUser user;
private final User user;
private final GameProfile profile;
private final List<SignEntry> signatures;

public MUARecord(GameProfile profile, MUAUser user, Collection<? extends SignEntry> signatures) {
public MUARecord(GameProfile profile, User user, Collection<? extends SignEntry> signatures) {
this.user = user;
this.profile = profile;
this.signatures = List.copyOf(signatures);
}

public MUAUser getUser() {
public User getUser() {
return this.user;
}

Expand Down Expand Up @@ -163,4 +164,29 @@ public HashCode getSignature() {
return this.signature;
}
}

@FieldsAreNonnullByDefault
@MethodsReturnNonnullByDefault
@ParametersAreNonnullByDefault
public record User(String sub, String nickname, String email) {
public static final Codec<User> CODEC;
public static final StreamCodec<ByteBuf, User> STREAM_CODEC;

static {
CODEC = RecordCodecBuilder.create(builder -> builder.group(
Codec.STRING.fieldOf("sub").forGetter(User::sub),
Codec.STRING.fieldOf("nickname").forGetter(User::nickname),
Codec.STRING.fieldOf("email").forGetter(User::email)).apply(builder, User::new));
STREAM_CODEC = StreamCodec.composite(
ByteBufCodecs.STRING_UTF8, User::sub,
ByteBufCodecs.STRING_UTF8, User::nickname,
ByteBufCodecs.STRING_UTF8, User::email, User::new);
}

public MUARecord sign(GameProfile profile, Instant expire, Pair<EdECPublicKey, EdECPrivateKey> keys) {
var keyBytes = Ed25519.serialize(keys.getFirst());
var signature = Ed25519.sign(keys.getSecond(), expire, Pair.of(profile, this), STREAM_CODEC_PART);
return new MUARecord(profile, this, List.of(new SignEntry(keyBytes, expire, signature)));
}
}
}
42 changes: 0 additions & 42 deletions src/main/java/org/teacon/mua2fa/data/MUAUser.java

This file was deleted.

4 changes: 2 additions & 2 deletions src/main/java/org/teacon/mua2fa/data/OAuthHttp.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public final class OAuthHttp implements Closeable {
private static final Scheduler IO_SCHEDULER = Schedulers.fromExecutor(Util.ioPool());

private final AtomicReference<DisposableServer> server = new AtomicReference<>();
private final Sinks.Many<MUAUser> records = Sinks.many().replay().limit(NETWORK_TOLERANCE, IO_SCHEDULER);
private final Sinks.Many<MUARecord.User> records = Sinks.many().replay().limit(NETWORK_TOLERANCE, IO_SCHEDULER);

private Mono<JsonObject> json(HttpClientResponse res, ByteBufMono body) {
return body.asString().flatMap(content -> Mono.fromCallable(() -> {
Expand Down Expand Up @@ -181,7 +181,7 @@ var record = user.sign(profile, expire.toInstant(), key);
});
return userRes.flatMap(json -> {
var header = res.header(HttpHeaderNames.CONTENT_TYPE, "text/html;charset=utf-8");
var user = MUAUser.CODEC.decode(JsonOps.INSTANCE, json).getOrThrow().getFirst();
var user = MUARecord.User.CODEC.decode(JsonOps.INSTANCE, json).getOrThrow().getFirst();
this.records.emitNext(user, Sinks.EmitFailureHandler.FAIL_FAST);
MUA2FA.LOGGER.info(MARKER, "Finished the oauth process of player {}, replying ...", state.name());
return header.sendString(Mono.just(String.format(HTML, "#066805", state.completeHint()))).then();
Expand Down

0 comments on commit da8f340

Please sign in to comment.