This commit is contained in:
Douglas Barone 2023-10-25 11:31:39 -04:00
parent 7a892747d4
commit f2da32a597
9 changed files with 55 additions and 15 deletions

View File

@ -1,4 +1,5 @@
import { Auth, LoginResult } from '../schemas/Auth' import { Auth } from '../schemas/Auth'
import { LoginResult } from '../schemas/LoginResult'
export class FakeAuth implements Auth { export class FakeAuth implements Auth {
constructor() {} constructor() {}

View File

@ -1,5 +1,6 @@
import { Client } from 'ldapts' import { Client } from 'ldapts'
import { Auth, LoginResult } from '../schemas/Auth' import { Auth } from '../schemas/Auth'
import { LoginResult } from '../schemas/LoginResult'
export class LdapAuth implements Auth { export class LdapAuth implements Auth {
constructor( constructor(

View File

@ -1,6 +1,6 @@
import { Context } from '../trpc' import { Request } from 'express'
export function getIpFromContext({ req }: Context) { export function getIpFromRequest(req: Request) {
if (process.env.NODE_ENV === 'development') return '10.7.16.254' if (process.env.NODE_ENV === 'development') return '10.7.16.254'
if (req.headers['x-forwarded-for']) if (req.headers['x-forwarded-for'])

View File

@ -1,9 +1,4 @@
export type LoginResult = { import { LoginResult } from './LoginResult'
username: string
displayName: string
domain: string
jwt?: string
}
export interface Auth { export interface Auth {
login(username: string, password: string): Promise<LoginResult> | LoginResult login(username: string, password: string): Promise<LoginResult> | LoginResult

View File

@ -0,0 +1,7 @@
export type JwtPayload = {
username: string
displayName: string
domain: string
iat: number // issued at
exp: number // expires at
}

View File

@ -0,0 +1,6 @@
export type LoginResult = {
username: string
displayName: string
domain: string
jwt?: string
}

View File

@ -0,0 +1,6 @@
type NetworkInfo = {
ip: string
source: 'IP' | 'X-Forwarded-For'
status: 'Mapped' | 'Expired' | 'ERROR' | 'Prohibited' | 'Unknown'
// campus: 'PP' | 'DR' | 'RT' | 'JD'
}

View File

@ -113,4 +113,27 @@ export class PaFirewall {
} }
}) })
} }
async getIpStatus(ip: string) {
const params = new URLSearchParams({
key: this.key,
type: 'log',
'log-type': 'userid',
query: `( ip in '${ip}' )`,
nlogs: '1'
})
const url = `https://${this.ip}/api/?${params}`
const response = await fetch(url)
const data = await response.text()
const parsedData = xmlParser.parse(data)
if (parsedData.response.attr_status !== 'success') {
console.log(data)
throw new Error('Failed to get IP status')
}
}
} }

View File

@ -3,13 +3,14 @@ import * as trpcExpress from '@trpc/server/adapters/express'
import { z } from 'zod' import { z } from 'zod'
import { login } from './lib/login' import { login } from './lib/login'
import { getIpFromContext } from './lib/getIpFromContext' import { getIpFromRequest } from './lib/getIpFromContext'
import { logout } from './lib/logout' import { logout } from './lib/logout'
import { PaFirewall } from './services/PaFirewall' import { PaFirewall } from './services/PaFirewall'
// Created for each request // Created for each request
function createContext({ req, res }: trpcExpress.CreateExpressContextOptions) { function createContext({ req, res }: trpcExpress.CreateExpressContextOptions) {
return { req } const ip = getIpFromRequest(req)
return { ip }
} }
export type Context = inferAsyncReturnType<typeof createContext> export type Context = inferAsyncReturnType<typeof createContext>
@ -18,18 +19,18 @@ export const t = initTRPC.context<Context>().create()
export const appRouter = t.router({ export const appRouter = t.router({
myIp: t.procedure.query(({ ctx }) => { myIp: t.procedure.query(({ ctx }) => {
return getIpFromContext(ctx) return ctx.ip
}), }),
login: t.procedure login: t.procedure
.input(z.object({ username: z.string(), password: z.string() })) .input(z.object({ username: z.string(), password: z.string() }))
.mutation(async ({ input, ctx }) => { .mutation(async ({ input, ctx }) => {
return await login(input.username, input.password, getIpFromContext(ctx)) return await login(input.username, input.password, ctx.ip)
}), }),
logout: t.procedure logout: t.procedure
.input(z.object({ username: z.string(), domain: z.string() })) .input(z.object({ username: z.string(), domain: z.string() }))
.mutation(async ({ input, ctx }) => { .mutation(async ({ input, ctx }) => {
return await logout(input.username, input.domain, getIpFromContext(ctx)) return await logout(input.username, input.domain, ctx.ip)
}), }),
addFirewall: t.procedure addFirewall: t.procedure