Skip to content

Commit

Permalink
feat: update dependencies for v2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
markusberg-sectra committed Feb 9, 2024
1 parent b9bf9fc commit 96fd6de
Show file tree
Hide file tree
Showing 11 changed files with 1,948 additions and 6,092 deletions.
1 change: 1 addition & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
npx lint-staged
1 change: 1 addition & 0 deletions .husky/pre-push
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
npm test
3 changes: 3 additions & 0 deletions .lintstagedrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"*": "prettier --write --check"
}
4 changes: 3 additions & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{
"trailingComma": "all",
"tabWidth": 2,
"semi": false,
"trailingComma": "all"
"singleQuote": true
}
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

All notable changes to this project will be documented in this file.

## [2.0.0] - 2024-02-09

- Drop support for Node.js versions below 18
- Ecmascript only
- Migrate to vitest and node:test for testing

## [1.0.0] - 2019-07-19

### Added
Expand Down
58 changes: 29 additions & 29 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createHash } from "crypto"
import { decode, encode } from "iconv-lite"
import { createHash } from 'node:crypto'
import iconv from 'iconv-lite'

export {
generate,
Expand Down Expand Up @@ -69,7 +69,7 @@ function setSecrets(secrets: Secrets) {
* @returns {Buffer} Username encoded in CP-850 and stuffed into a Buffer
*/
function generateUserNameBuf(userName: string): Buffer {
return encode(userName, "ibm850")
return iconv.encode(userName, 'ibm850')
}

/***
Expand All @@ -88,22 +88,22 @@ function generate(userNameBuf: Buffer, domain: string, timeStart?: number) {
const size = userNameBuf.length + 40
const ltpaToken = Buffer.alloc(size)

ltpaToken.write("00010203", 0, 4, "hex")
ltpaToken.write('00010203', 0, 4, 'hex')
ltpaToken.write(timeCreation, 4)
ltpaToken.write(timeExpiration, 12)
userNameBuf.copy(ltpaToken, 20)
const serverSecret = ltpaSecrets[domain]
ltpaToken.write(serverSecret, size - 20, 20, "base64")
ltpaToken.write(serverSecret, size - 20, 20, 'base64')

const hash = createHash("sha1")
const hash = createHash('sha1')
hash.update(ltpaToken)

// Paranoid overwrite of the server secret
ltpaToken.write("0123456789abcdefghij", size - 20, 20, "utf8")
ltpaToken.write('0123456789abcdefghij', size - 20, 20, 'utf8')

// Append the token hash
ltpaToken.write(hash.digest("hex"), size - 20, 20, "hex")
return ltpaToken.toString("base64")
ltpaToken.write(hash.digest('hex'), size - 20, 20, 'hex')
return ltpaToken.toString('base64')
}

/***
Expand All @@ -116,59 +116,59 @@ function validate(token: string, domain: string): void {
* Basic sanity checking of in-data
*/
if (!token || token.length === 0) {
throw new Error("No token provided")
throw new Error('No token provided')
}
if (!domain || domain.length === 0) {
throw new Error("No domain provided")
throw new Error('No domain provided')
}

const serverSecret = ltpaSecrets[domain]
if (!serverSecret) {
throw new Error("No such server secret exists")
throw new Error('No such server secret exists')
}

const tokenSize = Buffer.byteLength(token, "base64")
const ltpaToken = Buffer.alloc(tokenSize, token, "base64")
const tokenSize = Buffer.byteLength(token, 'base64')
const ltpaToken = Buffer.alloc(tokenSize, token, 'base64')
if (ltpaToken.length < 41) {
// userName must be at least one character long
throw new Error("Ltpa Token too short")
throw new Error('Ltpa Token too short')
}

/**
* Check time validity
*/
const timeCreation = parseInt(ltpaToken.toString("utf8", 4, 12), 16)
const timeCreation = parseInt(ltpaToken.toString('utf8', 4, 12), 16)
// we don't look at the expiration stored in the token, but calculate our own
const timeExpiration = parseInt(ltpaToken.toString("utf8", 12, 20), 16)
const timeExpiration = parseInt(ltpaToken.toString('utf8', 12, 20), 16)
const now = Math.floor(Date.now() / 1000)

if (timeCreation - gracePeriod > now) {
throw new Error("Ltpa Token not yet valid")
throw new Error('Ltpa Token not yet valid')
}

const exp = strictExpirationValidation
? timeExpiration
: timeCreation + validity + gracePeriod * 2
// need to check two gracePeriods into the future because we add one to the beginning
if (exp < now) {
throw new Error("Ltpa Token has expired")
throw new Error('Ltpa Token has expired')
}

/**
* Check version, and hash itself
*/
const version = ltpaToken.toString("hex", 0, 4)
if (version !== "00010203") {
throw new Error("Incorrect magic string")
const version = ltpaToken.toString('hex', 0, 4)
if (version !== '00010203') {
throw new Error('Incorrect magic string')
}

const signature = ltpaToken.toString("hex", ltpaToken.length - 20)
ltpaToken.write(serverSecret, ltpaToken.length - 20, 20, "base64")
const signature = ltpaToken.toString('hex', ltpaToken.length - 20)
ltpaToken.write(serverSecret, ltpaToken.length - 20, 20, 'base64')

const hash = createHash("sha1")
const hash = createHash('sha1')
hash.update(ltpaToken)

if (hash.digest("hex") !== signature) {
if (hash.digest('hex') !== signature) {
throw new Error("Ltpa Token signature doesn't validate")
}
}
Expand All @@ -179,8 +179,8 @@ function validate(token: string, domain: string): void {
* @returns {buffer} Buffer containing the encoded username
*/
function getUserNameBuf(token: string): Buffer {
const size = Buffer.byteLength(token, "base64")
const ltpaToken = Buffer.alloc(size, token, "base64")
const size = Buffer.byteLength(token, 'base64')
const ltpaToken = Buffer.alloc(size, token, 'base64')
return ltpaToken.slice(20, ltpaToken.length - 20)
}

Expand All @@ -190,7 +190,7 @@ function getUserNameBuf(token: string): Buffer {
* @returns {string} Username as a UTF-8 string
*/
function getUserName(token: string): string {
return decode(getUserNameBuf(token), "ibm850")
return iconv.decode(getUserNameBuf(token), 'ibm850')
}

/***
Expand Down
19 changes: 0 additions & 19 deletions jest.config.js

This file was deleted.

Loading

0 comments on commit 96fd6de

Please sign in to comment.