package com.xfestudio.xfeservermanager.infra.security;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.nio.file.attribute.PosixFilePermission;
import java.nio.file.attribute.PosixFilePermissions;
import java.util.EnumSet;
/** Loads or creates a high-entropy bearer token in an owner-only file. */
public final class BearerTokenFile {
private BearerTokenFile() { }
public static synchronized String loadOrCreate(Path file) throws IOException {
Path absolute = file.toAbsolutePath().normalize();
Path parent = absolute.getParent();
if (parent != null) {
Files.createDirectories(parent);
}
if (Files.exists(absolute)) {
return read(absolute);
}
String token = SecureTokens.randomUrlToken(32);
byte[] encoded = (token + System.lineSeparator()).getBytes(StandardCharsets.UTF_8);
try {
createOwnerOnly(absolute, encoded);
return token;
} catch (FileAlreadyExistsException raced) {
return read(absolute);
} finally {
java.util.Arrays.fill(encoded, (byte) 0);
}
}
private static String read(Path file) throws IOException {
if (!Files.isRegularFile(file, java.nio.file.LinkOption.NOFOLLOW_LINKS)
|| Files.size(file) > 1_024) {
throw new IOException("bearer token must be a small regular file, not a symbolic link");
}
String token = Files.readString(file, StandardCharsets.UTF_8).strip();
if (token.length() < 32 || token.length() > 256
|| !token.matches("[A-Za-z0-9_-]+")) {
throw new IOException("invalid bearer token file");
}
return token;
}
private static void createOwnerOnly(Path file, byte[] encoded) throws IOException {
var permissions = PosixFilePermissions.asFileAttribute(
EnumSet.of(PosixFilePermission.OWNER_READ, PosixFilePermission.OWNER_WRITE));
FileChannel channel;
try {
channel = FileChannel.open(
file,
EnumSet.of(StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE),
permissions);
} catch (UnsupportedOperationException ignored) {
channel = FileChannel.open(file, StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE);
}
try (FileChannel lockedChannel = channel) {
ByteBuffer buffer = ByteBuffer.wrap(encoded);
while (buffer.hasRemaining()) {
lockedChannel.write(buffer);
}
lockedChannel.force(true);
}
}
}
package com.xfestudio.xfeservermanager.infra.security;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.nio.file.attribute.PosixFilePermission;
import java.nio.file.attribute.PosixFilePermissions;
import java.util.EnumSet;
/** Loads or creates a high-entropy bearer token in an owner-only file. */
public final class BearerTokenFile {
private BearerTokenFile() { }
public static synchronized String loadOrCreate(Path file) throws IOException {
Path absolute = file.toAbsolutePath().normalize();
Path parent = absolute.getParent();
if (parent != null) {
Files.createDirectories(parent);
}
if (Files.exists(absolute)) {
return read(absolute);
}
String token = SecureTokens.randomUrlToken(32);
byte[] encoded = (token + System.lineSeparator()).getBytes(StandardCharsets.UTF_8);
try {
createOwnerOnly(absolute, encoded);
return token;
} catch (FileAlreadyExistsException raced) {
return read(absolute);
} finally {
java.util.Arrays.fill(encoded, (byte) 0);
}
}
private static String read(Path file) throws IOException {
if (!Files.isRegularFile(file, java.nio.file.LinkOption.NOFOLLOW_LINKS)
|| Files.size(file) > 1_024) {
throw new IOException("bearer token must be a small regular file, not a symbolic link");
}
String token = Files.readString(file, StandardCharsets.UTF_8).strip();
if (token.length() < 32 || token.length() > 256
|| !token.matches("[A-Za-z0-9_-]+")) {
throw new IOException("invalid bearer token file");
}
return token;
}
private static void createOwnerOnly(Path file, byte[] encoded) throws IOException {
var permissions = PosixFilePermissions.asFileAttribute(
EnumSet.of(PosixFilePermission.OWNER_READ, PosixFilePermission.OWNER_WRITE));
FileChannel channel;
try {
channel = FileChannel.open(
file,
EnumSet.of(StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE),
permissions);
} catch (UnsupportedOperationException ignored) {
channel = FileChannel.open(file, StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE);
}
try (FileChannel lockedChannel = channel) {
ByteBuffer buffer = ByteBuffer.wrap(encoded);
while (buffer.hasRemaining()) {
lockedChannel.write(buffer);
}
lockedChannel.force(true);
}
}
}