Skip to content

Commit da8f340

Browse files
committed
Move MUAUser to MUARecord class
1 parent 2de3ecf commit da8f340

File tree

3 files changed

+35
-51
lines changed

3 files changed

+35
-51
lines changed

src/main/java/org/teacon/mua2fa/data/MUARecord.java

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import net.minecraft.util.ExtraCodecs;
1818

1919
import javax.annotation.ParametersAreNonnullByDefault;
20+
import java.security.interfaces.EdECPrivateKey;
2021
import java.security.interfaces.EdECPublicKey;
2122
import java.time.Instant;
2223
import java.time.temporal.ChronoUnit;
@@ -31,35 +32,35 @@
3132
public final class MUARecord {
3233
public static final Codec<MUARecord> CODEC;
3334
public static final StreamCodec<ByteBuf, MUARecord> STREAM_CODEC;
34-
public static final StreamCodec<ByteBuf, Pair<GameProfile, MUAUser>> STREAM_CODEC_PART;
35+
public static final StreamCodec<ByteBuf, Pair<GameProfile, User>> STREAM_CODEC_PART;
3536

3637
static {
3738
CODEC = RecordCodecBuilder.create(builder -> builder.group(
3839
RecordCodecBuilder.<GameProfile>mapCodec(b -> b.group(
3940
UUIDUtil.AUTHLIB_CODEC.fieldOf("id").forGetter(GameProfile::getId),
4041
ExtraCodecs.PLAYER_NAME.fieldOf("name").forGetter(GameProfile::getName))
4142
.apply(b, GameProfile::new)).forGetter(MUARecord::getProfile),
42-
MUAUser.CODEC.fieldOf("mua").forGetter(MUARecord::getUser),
43+
User.CODEC.fieldOf("mua").forGetter(MUARecord::getUser),
4344
Codec.list(SignEntry.CODEC).fieldOf("signatures").forGetter(MUARecord::getSignatures))
4445
.apply(builder, MUARecord::new));
4546
STREAM_CODEC = StreamCodec.composite(
46-
ByteBufCodecs.GAME_PROFILE, MUARecord::getProfile, MUAUser.STREAM_CODEC, MUARecord::getUser,
47+
ByteBufCodecs.GAME_PROFILE, MUARecord::getProfile, User.STREAM_CODEC, MUARecord::getUser,
4748
SignEntry.STREAM_CODEC.apply(ByteBufCodecs.list()), MUARecord::getSignatures, MUARecord::new);
4849
STREAM_CODEC_PART = StreamCodec.composite(
49-
ByteBufCodecs.GAME_PROFILE, Pair::getFirst, MUAUser.STREAM_CODEC, Pair::getSecond, Pair::of);
50+
ByteBufCodecs.GAME_PROFILE, Pair::getFirst, User.STREAM_CODEC, Pair::getSecond, Pair::of);
5051
}
5152

52-
private final MUAUser user;
53+
private final User user;
5354
private final GameProfile profile;
5455
private final List<SignEntry> signatures;
5556

56-
public MUARecord(GameProfile profile, MUAUser user, Collection<? extends SignEntry> signatures) {
57+
public MUARecord(GameProfile profile, User user, Collection<? extends SignEntry> signatures) {
5758
this.user = user;
5859
this.profile = profile;
5960
this.signatures = List.copyOf(signatures);
6061
}
6162

62-
public MUAUser getUser() {
63+
public User getUser() {
6364
return this.user;
6465
}
6566

@@ -163,4 +164,29 @@ public HashCode getSignature() {
163164
return this.signature;
164165
}
165166
}
167+
168+
@FieldsAreNonnullByDefault
169+
@MethodsReturnNonnullByDefault
170+
@ParametersAreNonnullByDefault
171+
public record User(String sub, String nickname, String email) {
172+
public static final Codec<User> CODEC;
173+
public static final StreamCodec<ByteBuf, User> STREAM_CODEC;
174+
175+
static {
176+
CODEC = RecordCodecBuilder.create(builder -> builder.group(
177+
Codec.STRING.fieldOf("sub").forGetter(User::sub),
178+
Codec.STRING.fieldOf("nickname").forGetter(User::nickname),
179+
Codec.STRING.fieldOf("email").forGetter(User::email)).apply(builder, User::new));
180+
STREAM_CODEC = StreamCodec.composite(
181+
ByteBufCodecs.STRING_UTF8, User::sub,
182+
ByteBufCodecs.STRING_UTF8, User::nickname,
183+
ByteBufCodecs.STRING_UTF8, User::email, User::new);
184+
}
185+
186+
public MUARecord sign(GameProfile profile, Instant expire, Pair<EdECPublicKey, EdECPrivateKey> keys) {
187+
var keyBytes = Ed25519.serialize(keys.getFirst());
188+
var signature = Ed25519.sign(keys.getSecond(), expire, Pair.of(profile, this), STREAM_CODEC_PART);
189+
return new MUARecord(profile, this, List.of(new SignEntry(keyBytes, expire, signature)));
190+
}
191+
}
166192
}

src/main/java/org/teacon/mua2fa/data/MUAUser.java

Lines changed: 0 additions & 42 deletions
This file was deleted.

src/main/java/org/teacon/mua2fa/data/OAuthHttp.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public final class OAuthHttp implements Closeable {
7070
private static final Scheduler IO_SCHEDULER = Schedulers.fromExecutor(Util.ioPool());
7171

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

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

0 commit comments

Comments
 (0)