Add Discord OAuth2 account linking and login support
Introduces Discord OAuth2 integration for account association and login, including new routes for linking, unlinking, and authenticating via Discord. Adds supporting services, repositories, and schema validation for the OAuth2 flow. Refactors database schema and queries for consistency, and updates dependencies to include required OAuth2 libraries.
This commit is contained in:
+69
-2
@@ -61,6 +61,7 @@ async function authenticate({ identifier, password, clientToken, requireUser })
|
||||
uuid: userResult.user.uuid,
|
||||
username: userResult.user.username,
|
||||
clientToken: $clientToken,
|
||||
type: "Minecraft"
|
||||
}, keys.authenticationKeys.private, {
|
||||
subject: userResult.user.uuid,
|
||||
issuer: "LentiaYggdrasil",
|
||||
@@ -105,6 +106,70 @@ async function authenticate({ identifier, password, clientToken, requireUser })
|
||||
}
|
||||
}
|
||||
|
||||
async function authenticateWithoutPassword({ identifier, requireUser }) {
|
||||
let userResult
|
||||
try {
|
||||
userResult = await authRepository.getUser(identifier, true)
|
||||
} catch (error) {
|
||||
if (error.code === 404) {
|
||||
throw new DefaultError(403, "Invalid credentials. Invalid username or password.", "ForbiddenOperationException")
|
||||
}
|
||||
throw error
|
||||
}
|
||||
|
||||
delete userResult.user.password
|
||||
|
||||
const $clientToken = crypto.randomUUID()
|
||||
const accessToken = jwt.sign({
|
||||
uuid: userResult.user.uuid,
|
||||
username: userResult.user.username,
|
||||
clientToken: $clientToken,
|
||||
type: "Minecraft_OAuth2"
|
||||
}, keys.authenticationKeys.private, {
|
||||
subject: userResult.user.uuid,
|
||||
issuer: "LentiaYggdrasil",
|
||||
expiresIn: "1d",
|
||||
algorithm: "RS256"
|
||||
})
|
||||
|
||||
const clientSessionProcess = await authRepository.insertClientSession(accessToken, $clientToken, userResult.user.uuid)
|
||||
|
||||
const userObject = {
|
||||
clientToken: clientSessionProcess.clientToken,
|
||||
accessToken: clientSessionProcess.accessToken,
|
||||
availableProfiles: [{
|
||||
name: userResult.user.username,
|
||||
id: userResult.user.uuid,
|
||||
}],
|
||||
selectedProfile: {
|
||||
name: userResult.user.username,
|
||||
id: userResult.user.uuid,
|
||||
}
|
||||
}
|
||||
|
||||
if (requireUser) {
|
||||
try {
|
||||
const propertiesRequest = await authRepository.getPlayerProperties(userResult.user.uuid)
|
||||
userObject.user = {
|
||||
username: userResult.user.username,
|
||||
properties: propertiesRequest.properties
|
||||
}
|
||||
} catch (error) {
|
||||
if (error.code !== 404) throw error
|
||||
userObject.user = {
|
||||
username: userResult.user.username,
|
||||
properties: []
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
code: 200,
|
||||
response: userObject
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
async function refreshToken({ previousAccessToken, clientToken, requireUser }) {
|
||||
let sessionCheck
|
||||
try {
|
||||
@@ -125,6 +190,7 @@ async function refreshToken({ previousAccessToken, clientToken, requireUser }) {
|
||||
uuid: userResult.user.uuid,
|
||||
username: userResult.user.username,
|
||||
clientToken: $clientToken,
|
||||
type: "Minecraft"
|
||||
}, keys.authenticationKeys.private, {
|
||||
subject: userResult.user.uuid,
|
||||
issuer: "LentiaYggdrasil",
|
||||
@@ -281,9 +347,10 @@ module.exports = {
|
||||
signout,
|
||||
validate,
|
||||
invalidate,
|
||||
registerUser,
|
||||
authenticate,
|
||||
refreshToken,
|
||||
registerUser,
|
||||
verifyAccessToken,
|
||||
checkUsernameAvailability
|
||||
checkUsernameAvailability,
|
||||
authenticateWithoutPassword,
|
||||
}
|
||||
Reference in New Issue
Block a user