Skip to content

Commit 57b19dd

Browse files
committed
Add mojang-net-auth
1 parent e5bed7e commit 57b19dd

7 files changed

Lines changed: 155 additions & 0 deletions

File tree

scripts/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,5 @@
1515
### [CPS Counter](./cps_counter.js)
1616

1717
### [Timers Module](./timers)
18+
19+
### [mojang-net-auth](./mojang-net-auth/)
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { http as Http, HttpHeader, HttpRequest, HttpRequestMethod, HttpResponse, HttpClient as httpClient } from "mojang-net";
2+
3+
class authoration {
4+
static authorized: boolean = false;
5+
static url: string = "http://localhost:14589";
6+
static readonly PERMISSION_DENIED = "Permission Denied: This request is not authorized.";
7+
};
8+
9+
export class HttpClient extends httpClient {
10+
/**
11+
* @remarks
12+
* Performs a simple HTTP get request.
13+
* @param uri
14+
* URL to make an HTTP Request to.
15+
* @returns
16+
* An awaitable promise that contains the HTTP response.
17+
*/
18+
async get(uri: string): Promise<HttpResponse> {
19+
if (authoration.authorized !== true) throw Error(authoration.PERMISSION_DENIED);
20+
return await Http.get(uri);
21+
};
22+
/**
23+
* @remarks
24+
* Performs an HTTP request.
25+
* @param config
26+
* Contains an HTTP Request object with configuration data on
27+
* the HTTP request.
28+
* @returns
29+
* An awaitable promise that contains the HTTP response.
30+
*/
31+
async request(config: HttpRequest): Promise<HttpResponse> {
32+
if (authoration.authorized !== true) throw Error(authoration.PERMISSION_DENIED);
33+
return await Http.request(config);
34+
};
35+
constructor() {
36+
super()
37+
};
38+
};
39+
40+
export async function auth (token: string): Promise<boolean> {
41+
const localAuthRequest = new HttpRequest(authoration.url);
42+
localAuthRequest.setMethod(HttpRequestMethod.POST);
43+
localAuthRequest.setHeaders([new HttpHeader("token", token)]);
44+
45+
const response = await Http.request(localAuthRequest);
46+
47+
if (response.status === 200) {
48+
authoration.authorized = true;
49+
return true;
50+
}
51+
else {
52+
authoration.authorized = false;
53+
return false;
54+
};
55+
};
56+
57+
export const http = new HttpClient();

scripts/mojang-net-auth/node.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import * as http from "http";
2+
import { tokens } from "./tokens.json";
3+
4+
const port: number = 14589;
5+
const host: string = "localhost";
6+
7+
function listener (request: http.IncomingMessage, response: http.ServerResponse): void {
8+
const requestToken = request.headers?.token;
9+
10+
if (typeof requestToken !== "string") {
11+
respond_deny(response);
12+
} else if (tokens.includes(requestToken)) {
13+
console.log("request status", 200);
14+
response.writeHead(200, { "Content-Type": "text/plain" });
15+
response.end("post received");
16+
} else respond_deny(response);
17+
};
18+
19+
function respond_deny (response: http.ServerResponse): void {
20+
console.log("request status", 403);
21+
response.writeHead(403, { "Content-Type": "text/plain" });
22+
response.end("invalid token");
23+
}
24+
25+
const server: http.Server = http.createServer(listener);
26+
27+
server.listen(port, host);
28+
console.log(`Connecting @mojang-net/gametest to host [${host}] on port [${port}].`);
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "mojang-net-auth",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "node.js",
6+
"scripts": {
7+
"build": "npm run build_node && npm run build_gametest",
8+
"build_node": "tsc node.ts",
9+
"build_gametest": "tsc gametest.ts --module es2020"
10+
},
11+
"keywords": [],
12+
"author": "",
13+
"license": "ISC"
14+
}

scripts/mojang-net-auth/readme.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# mojang-net-auth
2+
3+
Package to make `mojang-net` HttpClient class requires authentication in order to use the module, specifically `HttpClient` class.
4+
5+
## Build
6+
On the current directory run the following command:
7+
8+
```npm run build```
9+
10+
## Usage
11+
- `node.ts`: Used by Node.js to accept or deny request made by GameTest. This file is not required if authentication takes place on the web.
12+
13+
The script uses `http://localhost/14589` (you can change the port) and fetch valid tokens from `tokens.json`.
14+
15+
- `gametest.ts`: Used by GameTest (in Bedrock Dedicated Server) to fetch authorization request to `node.ts` (`http://localhost/14589` by default).
16+
17+
```js
18+
import { http, auth } from "./mojang-net-auth/gametest.js";
19+
20+
auth("jayly"); // Authorization, must place above all http request for http to fetch request.
21+
22+
// Normal usage like what you do in "mojang-net"
23+
http.get("https://example.com").then(res => {
24+
console.log(res.body);
25+
});
26+
```
27+
28+
If request is not authorized, following error appears:
29+
```[Scripting][Error]-Permission Denied: This request is not authorized.```
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"tokens": [
3+
"jayly"
4+
]
5+
}

utilities/bp-template/package.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "bp-template",
3+
"version": "1.0.0",
4+
"description": "gametest-pack-generator",
5+
"main": "manifest.js",
6+
"scripts": {
7+
"test": "node manifest.js"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "git+https://github.com/JaylyDev/GametestDB.git"
12+
},
13+
"author": "JaylyMC",
14+
"license": "MIT",
15+
"type": "module",
16+
"bugs": {
17+
"url": "https://github.com/JaylyDev/GametestDB/issues"
18+
},
19+
"homepage": "https://github.com/JaylyDev/GametestDB#readme"
20+
}

0 commit comments

Comments
 (0)