Skip to content

Commit

Permalink
feat: replaced depedency md5.js with ts-md5
Browse files Browse the repository at this point in the history
  • Loading branch information
reijosirila authored and steabert committed Apr 11, 2021
1 parent 7574edf commit db23b1d
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 12 deletions.
Binary file not shown.
28 changes: 17 additions & 11 deletions lib/components/auth/digest.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// https://tools.ietf.org/html/rfc2617#section-3.2.1

import MD5 from 'md5.js'
import { Md5 as MD5 } from 'ts-md5'
import { ChallengeParams } from './www-authenticate'

export class DigestAuth {
Expand All @@ -21,8 +20,9 @@ export class DigestAuth {
}
this.realm = realm
this.ha1Base = new MD5()
.update(`${username}:${realm}:${password}`)
.digest('hex')
.appendStr(`${username}:${realm}:${password}`)
.end()
.toString()

const nonce = params.get('nonce')
if (nonce === undefined) {
Expand Down Expand Up @@ -73,16 +73,19 @@ export class DigestAuth {
ha1 = (cnonce: string): string => {
let ha1 = this.ha1Base
if (this.algorithm === 'md5-sess') {
ha1 = new MD5().update(`${ha1}:${this.nonce}:${cnonce}`).digest('hex')
ha1 = new MD5()
.appendStr(`${ha1}:${this.nonce}:${cnonce}`)
.end()
.toString()
}
return ha1
}

ha2 = (method: string, uri: string, body = ''): string => {
let ha2 = new MD5().update(`${method}:${uri}`).digest('hex')
let ha2 = new MD5().appendStr(`${method}:${uri}`).end().toString()
if (this.algorithm === 'md5-sess') {
const hbody = new MD5().update(body).digest('hex')
ha2 = new MD5().update(`${method}:${uri}:${hbody}`).digest('hex')
const hbody = new MD5().appendStr(body).end().toString()
ha2 = new MD5().appendStr(`${method}:${uri}:${hbody}`).end().toString()
}
return ha2
}
Expand All @@ -97,10 +100,13 @@ export class DigestAuth {

const response =
this.qop === undefined
? new MD5().update(`${ha1}:${this.nonce}:${ha2}`).digest('hex')
? new MD5().appendStr(`${ha1}:${this.nonce}:${ha2}`).end().toString()
: new MD5()
.update(`${ha1}:${this.nonce}:${nc}:${cnonce}:${this.qop}:${ha2}`)
.digest('hex')
.appendStr(
`${ha1}:${this.nonce}:${nc}:${cnonce}:${this.qop}:${ha2}`,
)
.end()
.toString()

const authorizationParams: string[] = []
authorizationParams.push(`username="${this.username}"`)
Expand Down
1 change: 0 additions & 1 deletion lib/components/auth/md5.d.ts

This file was deleted.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"md5.js": "1.3.5",
"process": "0.11.10",
"stream-browserify": "3.0.0",
"ts-md5": "1.2.7",
"ws": "7.4.4"
},
"devDependencies": {
Expand Down

0 comments on commit db23b1d

Please sign in to comment.