Skip to content

Commit

Permalink
chore: remove unnecessary code
Browse files Browse the repository at this point in the history
  • Loading branch information
LwveMike committed Apr 16, 2024
1 parent 97e4f0f commit c2ea866
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 54 deletions.
21 changes: 19 additions & 2 deletions packages/playground/server/tac_plus.conf
Original file line number Diff line number Diff line change
@@ -1,17 +1,29 @@
# key = "tac_test"
key = "tac_test"

# Set where to send accounting records
default authentication = file /etc/passwd
accounting syslog;
accounting file = /var/log/tac_plus/tac_plus.acct

group = management {
default service = permit

service = management {
priv-lvl=15
role="admin"
}
}

user = test_chap {
chap = cleartext test_chap_password

service = management {
priv-lvl=15
role="admin"
}

service = idk {
role="idk"
}
}

user = test_login {
Expand All @@ -25,4 +37,9 @@ user = test_login {

user = test_pap {
pap = cleartext test_pap_password

service = idk {
priv-lvl=10
role="user"
}
}
13 changes: 3 additions & 10 deletions packages/playground/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
/* eslint-disable no-console */
import type { AuthenType } from '@noction/tacacs-plus'
import { AUTHEN_TYPES, Client, PRIVILEGE_LEVELS } from '@noction/tacacs-plus'

const client = new Client({
host: '127.0.0.1',
port: 49,
// secret: 'tac_test',
secret: 'tac_test',
logger: {
// TODO(lwvemike): remove before release
/* eslint-disable-next-line no-console */
log: console.log,
// TODO(lwvemike): remove before release
/* eslint-disable-next-line no-console */
debug: console.debug,
error: console.error,
warn: console.warn,
},
})

const FUNCTION = 'authentication' as 'authentication' | 'authorization'
const SERVICES = ['management', 'idk']
const SERVICES = ['management']
const CREDENTIALS = {
username: 'test_pap',
password: 'test_pap_password',
Expand All @@ -40,11 +37,9 @@ async function authorization() {
services: SERVICES,
})

// eslint-disable-next-line no-console
console.log(res)
}
catch (err) {
// eslint-disable-next-line no-console
console.log(err)
}
}
Expand All @@ -58,11 +53,9 @@ async function authenticatinon() {
authenType: SELECTED_AUTHEN_TYPE,
})

// eslint-disable-next-line no-console
console.log(res)
}
catch (err) {
// eslint-disable-next-line no-console
console.log(err)
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
import type { Buffer } from 'node:buffer'
import type { Header } from '../../header'
import { getNameFromCollectionValue } from '../../utils'
import type { ToHumanReadable } from '../../types'
import type { AuthenticationReplyRecord } from './types'
import type { AuthenticationReplyFlag, AuthenticationReplyStatus } from './types'
import { validateAuthenticationReply } from './validate'

/**
* @throws Error
*/
export class AuthenticationReply implements ToHumanReadable {
// TODO(lwvemike): remove authenticationReplyRecord
readonly #status: AuthenticationReplyRecord['status']
readonly #serverMsg: AuthenticationReplyRecord['serverMsg']
readonly #flags: AuthenticationReplyRecord['flags']
readonly #serverMsgLen: AuthenticationReplyRecord['serverMsgLen']
readonly #dataLen: AuthenticationReplyRecord['dataLen']
readonly #data: AuthenticationReplyRecord['data']
export class AuthenticationReply {
readonly #status: AuthenticationReplyStatus
readonly #serverMsg: string | null
readonly #flags: AuthenticationReplyFlag
readonly #serverMsgLen: number
readonly #dataLen: number
readonly #data: string | null

constructor(body: Buffer, length: Header['length']) {
if (body.length < AuthenticationReply.MIN_LENGTH) {
Expand Down Expand Up @@ -60,17 +57,6 @@ export class AuthenticationReply implements ToHumanReadable {
return this.#status
}

toHumanReadable() {
return (
`status: ${this.#status} | ${getNameFromCollectionValue(this.#status, AuthenticationReply.STATUSES)}
flags: ${this.#flags} | ${getNameFromCollectionValue(this.#flags, AuthenticationReply.FLAGS)}
serverMsgLen: ${this.#serverMsgLen}
dataLen: ${this.#dataLen}
serverMsg: ${this.#serverMsg}
data: ${this.#data}`
)
}

static readonly MIN_LENGTH = 6

static STATUSES = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
import type { AuthenticationReply } from './index'

export interface AuthenticationReplyRecord {
status: AuthenticationReplyStatus
flags: AuthenticationReplyFlag
serverMsgLen: number
dataLen: number
data: string | null
serverMsg: string | null
}

export type UnknownAuthenticationReply = Record<'status' | 'flags', number>

export type AuthenticationReplyFlag = typeof AuthenticationReply.FLAGS[keyof typeof AuthenticationReply.FLAGS]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { Buffer } from 'node:buffer'
import type { ToHumanReadable } from '../../types'
import type { Packet } from '../../packet'
import type { Arg, AuthorizationReplyStatus, UnknownAuthorizationReply } from './types'
import { isArg, validateAuthorizationReply } from './validate'

export class AuthorizationReply implements ToHumanReadable {
export class AuthorizationReply {
readonly #status: AuthorizationReplyStatus
readonly #argCount: number
readonly #serverMsgLen: number
Expand Down Expand Up @@ -85,7 +84,7 @@ export class AuthorizationReply implements ToHumanReadable {
serverMsgLen,
dataLen,
args,
serverMsg: serverMsg.toString('utf8'),
serverMsg: serverMsg.toString('utf-8'),
data,
})
}
Expand All @@ -102,10 +101,6 @@ export class AuthorizationReply implements ToHumanReadable {
return this.#argCount
}

toHumanReadable() {
return `Authorization reply: ${this.#status} ${this.#argCount} ${this.#serverMsgLen} ${this.#dataLen} ${this.#args.join(' ')} ${this.#serverMsg} ${this.#data.toString('hex')}`
}

static STATUSES = {
TAC_PLUS_AUTHOR_STATUS_PASS_ADD: 0x01,
TAC_PLUS_AUTHOR_STATUS_PASS_REPL: 0x02,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ export class AuthorizationRequest implements ToBuffer {

constructor({
username,
args = [],
remAddr = TAC_PLUS_VIRTUAL_REM_ADDR,
port = TAC_PLUS_VIRTUAL_PORT,
...rest
args = [],
remAddr = TAC_PLUS_VIRTUAL_REM_ADDR,
port = TAC_PLUS_VIRTUAL_PORT,
...rest
}: CreateAuthorizationRequestArgs) {
const {
authenMethod,
Expand Down
2 changes: 2 additions & 0 deletions packages/tacacs-plus/src/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ export class Client {
socket.on('data', (data: Buffer) => {
const decodedPacket = Packet.decodePacket(data, this.#secret)

this.#logger.debug(`Received:\n${decodedPacket.toHumanReadable()}\n`)

const authorizationReply = AuthorizationReply.decode(decodedPacket)

switch (authorizationReply.status) {
Expand Down

0 comments on commit c2ea866

Please sign in to comment.