mirror of
https://github.com/SimplexDevelopment/FreedomNetworkSuite.git
synced 2025-07-01 13:16:42 +00:00
Tyr Backbone Creation
# Changes: ## Patchwork - Renamed FreedomExecutor to ExecutorProvider and moved the class to the provider package. - Created an SQL Registry to prevent dependencies on Datura for SQL data. SQL is returned through an Optional, in the event that there is no SQL service registered. - Created SQLResult, a generic ORM for ResultSets to avoid working directly with SQL data. ## Tyr - Created Identity, which houses a username and related secret key. - Created SQLEntry which stores the information from the Identity class into an SQL table called sessionData. - Created TOTP, a simple static class that allows easy access to TimeBasedOneTimePasswordUtils class. - Created OAuth2 which houses identities and performs the appropriate credential validations (incomplete)
This commit is contained in:
@ -16,11 +16,10 @@ bukkit {
|
||||
}
|
||||
|
||||
dependencies {
|
||||
compileOnly project(":Patchwork")
|
||||
compileOnly project(":Datura")
|
||||
compileOnly project(path: ":Patchwork")
|
||||
compileOnly project(path: ":Datura")
|
||||
|
||||
library 'com.hierynomus:sshj:0.28.0'
|
||||
library 'org.bouncycastle:bcprov-jdk18on:1.76'
|
||||
library 'com.j256.two-factor-auth:two-factor-auth:1.3'
|
||||
|
||||
testImplementation platform('org.junit:junit-bom:5.9.1')
|
||||
testImplementation 'org.junit.jupiter:junit-jupiter'
|
||||
|
48
Tyr/src/main/java/fns/tyr/Tyr.java
Normal file
48
Tyr/src/main/java/fns/tyr/Tyr.java
Normal file
@ -0,0 +1,48 @@
|
||||
/*
|
||||
* This file is part of FreedomNetworkSuite - https://github.com/SimplexDevelopment/FreedomNetworkSuite
|
||||
* Copyright (C) 2023 Simplex Development and contributors
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
package fns.tyr;
|
||||
|
||||
import fns.datura.Datura;
|
||||
import fns.patchwork.base.Shortcuts;
|
||||
import fns.patchwork.sql.SQL;
|
||||
import fns.patchwork.utils.logging.FNS4J;
|
||||
|
||||
public class Tyr
|
||||
{
|
||||
public void onEnable()
|
||||
{
|
||||
final SQL sql = Shortcuts.provideModule(Datura.class).getSQL();
|
||||
sql.createTable("sessionData",
|
||||
"user VARCHAR(16) NOT NULL PRIMARY KEY, secretKey VARCHAR(64) NOT NULL;")
|
||||
.whenCompleteAsync((result, throwable) ->
|
||||
{
|
||||
if (throwable != null)
|
||||
FNS4J.getLogger("Tyr")
|
||||
.error(throwable.getMessage());
|
||||
}, Shortcuts.getExecutors()
|
||||
.getAsync());
|
||||
|
||||
|
||||
}
|
||||
}
|
93
Tyr/src/main/java/fns/tyr/data/SQLEntry.java
Normal file
93
Tyr/src/main/java/fns/tyr/data/SQLEntry.java
Normal file
@ -0,0 +1,93 @@
|
||||
/*
|
||||
* This file is part of FreedomNetworkSuite - https://github.com/SimplexDevelopment/FreedomNetworkSuite
|
||||
* Copyright (C) 2023 Simplex Development and contributors
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
package fns.tyr.data;
|
||||
|
||||
import fns.patchwork.base.Shortcuts;
|
||||
import fns.patchwork.utils.logging.FNS4J;
|
||||
import fns.tyr.oauth.Identity;
|
||||
import java.sql.SQLException;
|
||||
|
||||
public class SQLEntry
|
||||
{
|
||||
private final Identity identity;
|
||||
|
||||
public SQLEntry(final Identity identity)
|
||||
{
|
||||
this.identity = identity;
|
||||
}
|
||||
|
||||
public static SQLEntry load(final String username)
|
||||
{
|
||||
return Shortcuts.getSQL()
|
||||
.map(c ->
|
||||
c.executeQuery("SELECT * FROM sessionData WHERE user = ?;", username)
|
||||
.thenApplyAsync(result ->
|
||||
{
|
||||
SQLEntry entry = null;
|
||||
try
|
||||
{
|
||||
if (result.next())
|
||||
{
|
||||
final String user = result.getString("user");
|
||||
final String secretKey = result.getString("secretKey");
|
||||
|
||||
final Identity i = new Identity(user, secretKey);
|
||||
|
||||
entry = new SQLEntry(i);
|
||||
FNS4J.getLogger("Tyr")
|
||||
.info("Loaded entry for " + username);
|
||||
}
|
||||
else
|
||||
{
|
||||
entry = new SQLEntry(Identity.of(username));
|
||||
FNS4J.getLogger("Tyr")
|
||||
.info("Created a new entry for " + username);
|
||||
}
|
||||
}
|
||||
catch (SQLException ex)
|
||||
{
|
||||
FNS4J.getLogger("Tyr").error(ex.getMessage());
|
||||
}
|
||||
return entry;
|
||||
}, Shortcuts.getExecutors()
|
||||
.getAsync())
|
||||
.join())
|
||||
.orElseThrow(() -> new IllegalStateException("SQL is not initialized!"));
|
||||
}
|
||||
|
||||
public void save()
|
||||
{
|
||||
Shortcuts.getSQL()
|
||||
.orElseThrow(() -> new IllegalStateException("SQL is not available!"))
|
||||
.executeUpdate("INSERT INTO sessionData (user, secretKey) VALUES (?, ?);",
|
||||
this.identity.username(),
|
||||
this.identity.secretKey())
|
||||
.whenCompleteAsync((result, throwable) ->
|
||||
{
|
||||
if (throwable != null)
|
||||
FNS4J.getLogger("Tyr").error(throwable.getMessage());
|
||||
}, Shortcuts.getExecutors()
|
||||
.getAsync());
|
||||
}
|
||||
}
|
31
Tyr/src/main/java/fns/tyr/oauth/Identity.java
Normal file
31
Tyr/src/main/java/fns/tyr/oauth/Identity.java
Normal file
@ -0,0 +1,31 @@
|
||||
/*
|
||||
* This file is part of FreedomNetworkSuite - https://github.com/SimplexDevelopment/FreedomNetworkSuite
|
||||
* Copyright (C) 2023 Simplex Development and contributors
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
package fns.tyr.oauth;
|
||||
|
||||
public record Identity(String username, String secretKey)
|
||||
{
|
||||
public static Identity of(final String username) {
|
||||
return new Identity(username, TOTP.createSecretKey());
|
||||
}
|
||||
}
|
74
Tyr/src/main/java/fns/tyr/oauth/OAuth2.java
Normal file
74
Tyr/src/main/java/fns/tyr/oauth/OAuth2.java
Normal file
@ -0,0 +1,74 @@
|
||||
/*
|
||||
* This file is part of FreedomNetworkSuite - https://github.com/SimplexDevelopment/FreedomNetworkSuite
|
||||
* Copyright (C) 2023 Simplex Development and contributors
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
package fns.tyr.oauth;
|
||||
|
||||
import fns.patchwork.base.Shortcuts;
|
||||
import java.util.HashSet;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
|
||||
public class OAuth2
|
||||
{
|
||||
private final Set<Identity> identitySet;
|
||||
|
||||
public OAuth2()
|
||||
{
|
||||
this.identitySet = new HashSet<>();
|
||||
}
|
||||
|
||||
public void addIdentity(Identity identity)
|
||||
{
|
||||
this.identitySet.add(identity);
|
||||
}
|
||||
|
||||
public void removeIdentity(Identity identity)
|
||||
{
|
||||
this.identitySet.remove(identity);
|
||||
}
|
||||
|
||||
public Optional<Identity> getIdentity(final String username)
|
||||
{
|
||||
return this.identitySet.stream()
|
||||
.filter(identity -> identity.username().equals(username))
|
||||
.findFirst();
|
||||
}
|
||||
|
||||
public void loadAll()
|
||||
{
|
||||
Shortcuts.getSQL()
|
||||
.ifPresent(sql -> sql.executeQuery("SELECT * FROM sessionData;")
|
||||
.thenAcceptAsync(result ->
|
||||
{
|
||||
for (int i = 1; i < result.rowCount(); i++)
|
||||
{
|
||||
final String username = result.getString(i,
|
||||
"user");
|
||||
final String secretKey = result.getString(i,
|
||||
"secretKey");
|
||||
this.addIdentity(
|
||||
new Identity(username, secretKey));
|
||||
}
|
||||
}));
|
||||
}
|
||||
}
|
63
Tyr/src/main/java/fns/tyr/oauth/TOTP.java
Normal file
63
Tyr/src/main/java/fns/tyr/oauth/TOTP.java
Normal file
@ -0,0 +1,63 @@
|
||||
/*
|
||||
* This file is part of FreedomNetworkSuite - https://github.com/SimplexDevelopment/FreedomNetworkSuite
|
||||
* Copyright (C) 2023 Simplex Development and contributors
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
package fns.tyr.oauth;
|
||||
|
||||
import com.j256.twofactorauth.TimeBasedOneTimePasswordUtil;
|
||||
import fns.patchwork.utils.logging.FNS4J;
|
||||
import java.security.GeneralSecurityException;
|
||||
|
||||
/**
|
||||
* User-friendly version of TimeBasedOneTimePasswordUtil.
|
||||
*/
|
||||
public final class TOTP
|
||||
{
|
||||
private TOTP()
|
||||
{
|
||||
throw new AssertionError("This class cannot be instantiated.");
|
||||
}
|
||||
|
||||
public static String createSecretKey()
|
||||
{
|
||||
return TimeBasedOneTimePasswordUtil.generateBase32Secret(32);
|
||||
}
|
||||
|
||||
public static String createQRCode(final String username, final String secretKey)
|
||||
{
|
||||
return TimeBasedOneTimePasswordUtil.qrImageUrl(username, secretKey);
|
||||
}
|
||||
|
||||
public static boolean verify(final String secretKey, final int userCode)
|
||||
{
|
||||
try
|
||||
{
|
||||
int vCode = TimeBasedOneTimePasswordUtil.generateCurrentNumber(secretKey);
|
||||
return vCode == userCode;
|
||||
}
|
||||
catch (GeneralSecurityException ex)
|
||||
{
|
||||
FNS4J.getLogger("Tyr").error("Failed to verify TOTP code: " + ex.getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user