Skip to content

Commit

Permalink
Более удобный парсинг post data
Browse files Browse the repository at this point in the history
  • Loading branch information
JoCat committed Jun 14, 2021
1 parent fec4ba0 commit 65dc26f
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 65 deletions.
15 changes: 15 additions & 0 deletions src/api/webserver/requests/AbstractRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,19 @@ export abstract class AbstractRequest {
protected isInvalidValue(param: any): boolean {
return typeof param !== "string" || param.trim().length === 0
}

protected getPostData(req: IncomingMessage): Promise<string> {
return new Promise((resolve, reject) => {
let data = ""
req.on("data", (chunk) => {
data += chunk
})
req.on("end", () => {
resolve(data)
})
req.on("error", (error) => {
reject(error)
})
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,35 +28,36 @@ export class ProfilesRequest extends AbstractRequest {
url = /^\/authlib\/profiles\/minecraft$/

async emit(req: IncomingMessage, res: ServerResponse): Promise<void> {
let data: any = ""
req.on("data", (chunk) => {
data += chunk
})
req.on("end", async () => {
data = JsonHelper.fromJSON(data)
res.statusCode = 400

if ("object" !== typeof data || !Array.isArray(data) || data.length === 0) {
return res.end()
}

if (data.length >= 10) {
res.write(
this.returnError("IllegalArgumentException", "Not more that 10 profile name per call is allowed.")
)
return res.end()
}

let users
try {
users = await App.AuthManager.getAuthProvider().profiles(data)
} catch (error) {
return res.end()
}

res.statusCode = 200
res.write(JsonHelper.toJSON(users))
res.end()
})
let data: any = await this.getPostData(req)
res.statusCode = 400

if (data.length === 0) {
res.write(this.returnError("Bad Request"))
return res.end()
}

data = JsonHelper.fromJSON(data)

if ("object" !== typeof data || !Array.isArray(data) || data.length === 0) {
return res.end()
}

if (data.length >= 10) {
res.write(
this.returnError("IllegalArgumentException", "Not more that 10 profile name per call is allowed.")
)
return res.end()
}

let users
try {
users = await App.AuthManager.getAuthProvider().profiles(data)
} catch (error) {
return res.end()
}

res.statusCode = 200
res.write(JsonHelper.toJSON(users))
res.end()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { AbstractRequest } from "../../AbstractRequest"

export class HasJoinedRequest extends AbstractRequest {
method = "GET"
url = /^\/authlib\/session\/minecraft\/hasJoined/
url = /^\/authlib\/session\/minecraft\/hasJoined$/

async emit(req: IncomingMessage, res: ServerResponse): Promise<void> {
res.statusCode = 400
Expand Down
66 changes: 32 additions & 34 deletions src/api/webserver/requests/authlib/sessionServer/JoinRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,42 +28,40 @@ export class JoinRequest extends AbstractRequest {
method = "POST"
url = /^\/authlib\/session\/minecraft\/join$/

emit(req: IncomingMessage, res: ServerResponse): void {
let data: any = ""
req.on("data", (chunk) => {
data += chunk
})
req.on("end", async () => {
data = JsonHelper.fromJSON(data)
res.statusCode = 400
async emit(req: IncomingMessage, res: ServerResponse): Promise<void> {
let data: any = await this.getPostData(req)
res.statusCode = 400

if (
this.isInvalidValue(data.accessToken) ||
this.isInvalidValue(data.selectedProfile) ||
this.isInvalidValue(data.serverId)
) {
res.write(this.returnError("Bad Request"))
return res.end()
}
if (data.length === 0) {
res.write(this.returnError("Bad Request"))
return res.end()
}

try {
await App.AuthManager.getAuthProvider().join(
data.accessToken,
UUIDHelper.getWithDashes(data.selectedProfile),
data.serverId
)
} catch (error) {
res.write(
this.returnError(
"ForbiddenOperationException",
"Invalid credentials. Invalid username or password."
)
)
return res.end()
}
data = JsonHelper.fromJSON(data)

res.statusCode = 204
res.end()
})
if (
this.isInvalidValue(data.accessToken) ||
this.isInvalidValue(data.selectedProfile) ||
this.isInvalidValue(data.serverId)
) {
res.write(this.returnError("Bad Request"))
return res.end()
}

try {
await App.AuthManager.getAuthProvider().join(
data.accessToken,
UUIDHelper.getWithDashes(data.selectedProfile),
data.serverId
)
} catch (error) {
res.write(
this.returnError("ForbiddenOperationException", "Invalid credentials. Invalid username or password.")
)
return res.end()
}

res.statusCode = 204
res.end()
}
}

0 comments on commit 65dc26f

Please sign in to comment.