Verified Commit 4ba2f9b6 authored by Maxime FRIESS's avatar Maxime FRIESS 💙
Browse files

[GW] Added auth via cookies

parent c8c376aa
......@@ -72,7 +72,7 @@ class Client {
}
sendEvent(name: string, type: string, data: any) {
if (!this.intents.includes(name))
if (!this.intents.includes(name) && !this.intents.includes('*'))
return;
const d: any = {p: "event", d: {resource: name, action: type, data: data}};
......
......@@ -76,27 +76,34 @@ class _Gateway {
}
private async authenticate(req: IncomingMessage): Promise<Client> {
if (req.headers.authorization && req.headers.authorization.split(' ')[0] === 'Bearer') {
const url: URL = new URL(req.url ?? "", `http://${req.headers.host}`);
const intents_str = url.searchParams.get('intents');
if (intents_str === null) {
throw new Error();
}
const url: URL = new URL(req.url ?? "", `http://${req.headers.host}`);
const intents_str = url.searchParams.get('intents');
if (intents_str === null) {
throw new Error();
}
const token = req.headers.authorization.split(' ')[1];
const profile: any = await SebAPI.profile(token);
let profile: any;
if (profile.good) {
let intents: string[] = this.getEffectiveIntents(intents_str.split(",") ?? [], profile?.data?.data?.permissions ?? []);
return new Client(intents);
} else {
throw new Error();
}
if (req.headers.authorization && req.headers.authorization.split(' ')[0] === 'Bearer') {
const token = req.headers.authorization.split(' ')[1];
profile = await SebAPI.profile({
mode: 'bearer',
token: token
});
} else {
// TODO: Check for session cookies
let cookies = req.headers.cookie;
profile = await SebAPI.profile({
mode: 'cookie',
cookies: cookies
});
}
throw new Error();
if (profile.good) {
let intents: string[] = this.getEffectiveIntents(intents_str.split(",") ?? [], profile?.data?.data?.permissions ?? []);
return new Client(intents);
} else {
throw new Error();
}
}
private async upgrade(req: IncomingMessage, socket: Socket, head: Buffer): Promise<void> {
......
import axios, { Method } from "axios";
import axios, { AxiosRequestHeaders, Method } from "axios";
import HardConfig from "./config/HardConfig";
import Loggers, { Logger } from "./utils/Logger";
interface Authorization {
mode: 'cookie' | 'bearer';
cookies?: string;
token?: string;
}
class _SebAPI {
private logger: Logger;
......@@ -10,10 +16,11 @@ class _SebAPI {
this.logger = Loggers.getLogger("SebAPI");
}
private async request(method: Method, path: string, token: string): Promise<any> {
private async request(method: Method, path: string, auth: Authorization): Promise<any> {
this.logger.info(method + " " + path);
try {
let res = await axios.request({ method: method, headers: { 'Authorization': 'Bearer ' + token }, url: HardConfig.getSebURL().replace(/\/$/, '') + path });
const headers: AxiosRequestHeaders = auth.mode === 'bearer' ? { 'Authorization': 'Bearer ' + auth.token ?? "" } : { 'Cookie': auth.cookies ?? "" };
let res = await axios.request({ method: method, headers: headers, url: HardConfig.getSebURL().replace(/\/$/, '') + path });
return {
good: true,
......@@ -52,8 +59,8 @@ class _SebAPI {
}
}
async profile(token: string): Promise<any> {
return await this.request('GET', '/api/profile/me', token);
async profile(auth: Authorization): Promise<any> {
return await this.request('GET', '/api/profile/me', auth);
}
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment