Skip to content

Commit

Permalink
reasons to disallow
Browse files Browse the repository at this point in the history
  • Loading branch information
ukstv committed Jul 3, 2024
1 parent 9a0ea86 commit 561a583
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions src/auth/auth.middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ CAR_FACTORY.codecs.add(DAG_JOSE)

const VERIFIER = new DID({ resolver: KeyDIDResolver.getResolver() })

enum DISALLOW_REASON {
LAMBDA_INVALID_DIGEST = 'lambda-invalid-digest',
DID_ALLOWLIST_NO_HEADER = 'did-allowlist-no-header',
DID_ALLOWLIST_NO_DID = 'did-allowlist-no-did',
DID_ALLOWLIST_NO_FIELDS = 'did-allowlist-no-fields',
DID_ALLOWLIST_REJECTED = 'did-allowlist-rejected',
DID_ALLOWLIST_INVALID_DIGEST = 'did-allowlist-invalid-digest',
}

export function parseAllowedDIDs(dids: string | undefined): Set<string> {
if (dids) {
const parts = dids.split(',')
Expand Down Expand Up @@ -55,7 +64,7 @@ export function auth(opts: AuthOpts): Handler {
return next()
} else {
logger?.verbose(`Disallowed: Auth lambda: Invalid digest`)
return disallow(res)
return disallow(res, DISALLOW_REASON.LAMBDA_INVALID_DIGEST)
}
}

Expand All @@ -66,23 +75,23 @@ export function auth(opts: AuthOpts): Handler {
const jws = bearerTokenMatch?.[1]
if (!jws) {
logger?.verbose(`Disallowed: No authorization header`)
return disallow(res)
return disallow(res, DISALLOW_REASON.DID_ALLOWLIST_NO_HEADER)
}
const verifyJWSResult = await VERIFIER.verifyJWS(jws)
const did = verifyJWSResult.didResolutionResult.didDocument?.id
if (!did) {
logger?.verbose(`Disallowed: No DID`)
return disallow(res)
return disallow(res, DISALLOW_REASON.DID_ALLOWLIST_NO_DID)
}
const nonce = verifyJWSResult.payload?.['nonce']
const digest = verifyJWSResult.payload?.['digest']
if (!nonce || !digest) {
logger?.verbose(`Disallowed: No nonce or No digest`)
return disallow(res)
return disallow(res, DISALLOW_REASON.DID_ALLOWLIST_NO_FIELDS)
}
if (!isAllowedDID(did, opts)) {
logger?.verbose(`Disallowed: ${did}`)
return disallow(res)
return disallow(res, DISALLOW_REASON.DID_ALLOWLIST_REJECTED)
}

const body = req.body
Expand All @@ -91,16 +100,16 @@ export function auth(opts: AuthOpts): Handler {
const isCorrectDigest = digestCalculated == digest
if (!isCorrectDigest) {
logger?.verbose(`Disallowed: Incorrect digest for DID ${did}`)
return disallow(res)
return disallow(res, DISALLOW_REASON.DID_ALLOWLIST_INVALID_DIGEST)
}
ServiceMetrics.count(METRIC_NAMES.AUTH_ALLOWED, 1, { did: did })
}
return next()
}
}

function disallow(res: Response): Response {
ServiceMetrics.count(METRIC_NAMES.AUTH_DISALLOWED, 1)
function disallow(res: Response, reason: DISALLOW_REASON): Response {
ServiceMetrics.count(METRIC_NAMES.AUTH_DISALLOWED, 1, { reason: reason })
return res.status(403).json({ error: 'Unauthorized' })
}

Expand Down

0 comments on commit 561a583

Please sign in to comment.