Convert parameters to destructured object

This commit is contained in:
Douglas Barone 2023-10-26 08:35:53 -04:00
parent 27345c8840
commit 0df0beea96
7 changed files with 65 additions and 13 deletions

View File

@ -4,7 +4,13 @@ import { AuthResult } from '../schemas/AuthResult'
export class FakeAuth implements Auth { export class FakeAuth implements Auth {
constructor() {} constructor() {}
async login(username: string, password: string): Promise<AuthResult> { async login({
username,
password
}: {
username: string
password: string
}): Promise<AuthResult> {
if (username == 'test' && password == 'test') if (username == 'test' && password == 'test')
return { return {
displayName: 'Test User', displayName: 'Test User',

View File

@ -9,7 +9,13 @@ export class LdapAuth implements Auth {
private searchDN: string private searchDN: string
) {} ) {}
async login(username: string, password: string): Promise<AuthResult> { async login({
username,
password
}: {
username: string
password: string
}): Promise<AuthResult> {
try { try {
await this.client.bind(`${this.domain}\\${username}`, password) await this.client.bind(`${this.domain}\\${username}`, password)

View File

@ -1,12 +1,20 @@
import { PaFirewallService } from '../services/PaFirewallService' import { PaFirewallService } from '../services/PaFirewallService'
import { db } from '../prisma' 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) console.log('AutoLogin?', username, domain, ip)
const paHosts = await db.paHost.findMany() const paHosts = await db.paHost.findMany()
const pa = new PaFirewallService(paHosts[0].ip, paHosts[0].key) const pa = new PaFirewallService(paHosts[0].ip, paHosts[0].key)
return await pa.login(username, ip, domain) return await pa.login({ username, ip, domain })
} }

View File

@ -18,13 +18,13 @@ export async function login(
const ldapAuth = new LdapAuth(ldapClient, 'ifms', 'DC=ifms,DC=edu,DC=br') const ldapAuth = new LdapAuth(ldapClient, 'ifms', 'DC=ifms,DC=edu,DC=br')
try { try {
const user = await ldapAuth.login(username, password) const user = await ldapAuth.login({ username, password })
const paHosts = await db.paHost.findMany() const paHosts = await db.paHost.findMany()
const pa = new PaFirewallService(paHosts[0].ip, paHosts[0].key) 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({ const jwt = await jwtService.generateToken({
displayName: user.displayName, displayName: user.displayName,

View File

@ -1,5 +1,11 @@
import { LoginResult } from './LoginResult' import { LoginResult } from './LoginResult'
export interface Auth { export interface Auth {
login(username: string, password: string): Promise<LoginResult> | LoginResult login({
username,
password
}: {
username: string
password: string
}): Promise<LoginResult> | LoginResult
} }

View File

@ -11,8 +11,16 @@ const xmlParser = new XMLParser({
export class PaFirewallService { export class PaFirewallService {
constructor(private ip: string, private key: string) {} constructor(private ip: string, private key: string) {}
async login(username: string, ip: string, domain: string) { async login({
const command = this.createLoginCommand(username, ip, domain) 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 url = `https://${this.ip}/api/?type=user-id&key=${this.key}`
const formData = encodeURIComponent(command) const formData = encodeURIComponent(command)
@ -59,7 +67,15 @@ export class PaFirewallService {
return true return true
} }
private createLoginCommand(username: string, ip: string, domain: string) { private createLoginCommand({
username,
ip,
domain
}: {
username: string
ip: string
domain: string
}) {
return ` return `
<uid-message> <uid-message>
<version>1.0</version> <version>1.0</version>
@ -85,7 +101,15 @@ export class PaFirewallService {
</uid-message>` </uid-message>`
} }
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( const response = await fetch(
`https://${ip}/api/?type=keygen&user=${username}&password=${password}` `https://${ip}/api/?type=keygen&user=${username}&password=${password}`
) )
@ -135,5 +159,7 @@ export class PaFirewallService {
console.log(data) console.log(data)
throw new Error('Failed to get IP status') throw new Error('Failed to get IP status')
} }
console.log(parsedData)
} }
} }

View File

@ -49,7 +49,7 @@ export const appRouter = t.router({
const { username, domain } = ctx.user const { username, domain } = ctx.user
return await autoLogin(username, ctx.ip, domain) return await autoLogin({ username, domain: ctx.ip, ip: domain })
}), }),
addFirewall: t.procedure addFirewall: t.procedure
@ -63,7 +63,7 @@ export const appRouter = t.router({
.mutation(async ({ input }) => { .mutation(async ({ input }) => {
const { ip, username, password } = input const { ip, username, password } = input
await PaFirewallService.addFirewall(ip, username, password) await PaFirewallService.addFirewall({ ip, username, password })
return true return true
}) })