Refactor
This commit is contained in:
parent
7a892747d4
commit
f2da32a597
|
@ -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() {}
|
||||||
|
|
|
@ -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(
|
||||||
|
|
|
@ -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'])
|
||||||
|
|
|
@ -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
|
||||||
|
|
7
src/server/schemas/JwtPayload.ts
Normal file
7
src/server/schemas/JwtPayload.ts
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
export type JwtPayload = {
|
||||||
|
username: string
|
||||||
|
displayName: string
|
||||||
|
domain: string
|
||||||
|
iat: number // issued at
|
||||||
|
exp: number // expires at
|
||||||
|
}
|
6
src/server/schemas/LoginResult.ts
Normal file
6
src/server/schemas/LoginResult.ts
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
export type LoginResult = {
|
||||||
|
username: string
|
||||||
|
displayName: string
|
||||||
|
domain: string
|
||||||
|
jwt?: string
|
||||||
|
}
|
6
src/server/schemas/Network.ts
Normal file
6
src/server/schemas/Network.ts
Normal 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'
|
||||||
|
}
|
|
@ -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')
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
|
||||||
|
|
Loading…
Reference in New Issue
Block a user