From 0df0beea96a326041103ac9d7ea3534441438361 Mon Sep 17 00:00:00 2001 From: Douglas Barone Date: Thu, 26 Oct 2023 08:35:53 -0400 Subject: [PATCH] Convert parameters to destructured object --- src/server/auth/FakeAuth.ts | 8 +++++- src/server/auth/LdapAuth.ts | 8 +++++- src/server/lib/autoLogin.ts | 12 +++++++-- src/server/lib/login.ts | 4 +-- src/server/schemas/Auth.ts | 8 +++++- src/server/services/PaFirewallService.ts | 34 +++++++++++++++++++++--- src/server/trpc.ts | 4 +-- 7 files changed, 65 insertions(+), 13 deletions(-) diff --git a/src/server/auth/FakeAuth.ts b/src/server/auth/FakeAuth.ts index 4a76453..fe7f3f3 100644 --- a/src/server/auth/FakeAuth.ts +++ b/src/server/auth/FakeAuth.ts @@ -4,7 +4,13 @@ import { AuthResult } from '../schemas/AuthResult' export class FakeAuth implements Auth { constructor() {} - async login(username: string, password: string): Promise { + async login({ + username, + password + }: { + username: string + password: string + }): Promise { if (username == 'test' && password == 'test') return { displayName: 'Test User', diff --git a/src/server/auth/LdapAuth.ts b/src/server/auth/LdapAuth.ts index 7700736..1931653 100644 --- a/src/server/auth/LdapAuth.ts +++ b/src/server/auth/LdapAuth.ts @@ -9,7 +9,13 @@ export class LdapAuth implements Auth { private searchDN: string ) {} - async login(username: string, password: string): Promise { + async login({ + username, + password + }: { + username: string + password: string + }): Promise { try { await this.client.bind(`${this.domain}\\${username}`, password) diff --git a/src/server/lib/autoLogin.ts b/src/server/lib/autoLogin.ts index b95148f..35c5c9f 100644 --- a/src/server/lib/autoLogin.ts +++ b/src/server/lib/autoLogin.ts @@ -1,12 +1,20 @@ import { PaFirewallService } from '../services/PaFirewallService' import { db } from '../prisma' -export async function autoLogin(username: string, domain: string, ip: string) { +export async function autoLogin({ + username, + domain, + ip +}: { + username: string + domain: string + ip: string +}) { console.log('AutoLogin?', username, domain, ip) const paHosts = await db.paHost.findMany() const pa = new PaFirewallService(paHosts[0].ip, paHosts[0].key) - return await pa.login(username, ip, domain) + return await pa.login({ username, ip, domain }) } diff --git a/src/server/lib/login.ts b/src/server/lib/login.ts index 2572b3d..be47ea3 100644 --- a/src/server/lib/login.ts +++ b/src/server/lib/login.ts @@ -18,13 +18,13 @@ export async function login( const ldapAuth = new LdapAuth(ldapClient, 'ifms', 'DC=ifms,DC=edu,DC=br') try { - const user = await ldapAuth.login(username, password) + const user = await ldapAuth.login({ username, password }) const paHosts = await db.paHost.findMany() const pa = new PaFirewallService(paHosts[0].ip, paHosts[0].key) - await pa.login(username, ip, user.domain) + await pa.login({ username, ip, domain: user.domain }) const jwt = await jwtService.generateToken({ displayName: user.displayName, diff --git a/src/server/schemas/Auth.ts b/src/server/schemas/Auth.ts index a4fb096..8c2d250 100644 --- a/src/server/schemas/Auth.ts +++ b/src/server/schemas/Auth.ts @@ -1,5 +1,11 @@ import { LoginResult } from './LoginResult' export interface Auth { - login(username: string, password: string): Promise | LoginResult + login({ + username, + password + }: { + username: string + password: string + }): Promise | LoginResult } diff --git a/src/server/services/PaFirewallService.ts b/src/server/services/PaFirewallService.ts index 1d21f2e..d164c77 100644 --- a/src/server/services/PaFirewallService.ts +++ b/src/server/services/PaFirewallService.ts @@ -11,8 +11,16 @@ const xmlParser = new XMLParser({ export class PaFirewallService { constructor(private ip: string, private key: string) {} - async login(username: string, ip: string, domain: string) { - const command = this.createLoginCommand(username, ip, domain) + async login({ + username, + ip, + domain + }: { + username: string + ip: string + domain: string + }) { + const command = this.createLoginCommand({ username, ip, domain }) const url = `https://${this.ip}/api/?type=user-id&key=${this.key}` const formData = encodeURIComponent(command) @@ -59,7 +67,15 @@ export class PaFirewallService { return true } - private createLoginCommand(username: string, ip: string, domain: string) { + private createLoginCommand({ + username, + ip, + domain + }: { + username: string + ip: string + domain: string + }) { return ` 1.0 @@ -85,7 +101,15 @@ export class PaFirewallService { ` } - static async addFirewall(ip: string, username: string, password: string) { + static async addFirewall({ + ip, + username, + password + }: { + ip: string + username: string + password: string + }) { const response = await fetch( `https://${ip}/api/?type=keygen&user=${username}&password=${password}` ) @@ -135,5 +159,7 @@ export class PaFirewallService { console.log(data) throw new Error('Failed to get IP status') } + + console.log(parsedData) } } diff --git a/src/server/trpc.ts b/src/server/trpc.ts index 417f5e1..c61e2bd 100644 --- a/src/server/trpc.ts +++ b/src/server/trpc.ts @@ -49,7 +49,7 @@ export const appRouter = t.router({ const { username, domain } = ctx.user - return await autoLogin(username, ctx.ip, domain) + return await autoLogin({ username, domain: ctx.ip, ip: domain }) }), addFirewall: t.procedure @@ -63,7 +63,7 @@ export const appRouter = t.router({ .mutation(async ({ input }) => { const { ip, username, password } = input - await PaFirewallService.addFirewall(ip, username, password) + await PaFirewallService.addFirewall({ ip, username, password }) return true })