Skip to content

Commit

Permalink
Merge 6467e4b into 796fbb2
Browse files Browse the repository at this point in the history
  • Loading branch information
bucko13 committed Aug 2, 2022
2 parents 796fbb2 + 6467e4b commit 9aa6351
Show file tree
Hide file tree
Showing 8 changed files with 625 additions and 535 deletions.
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -21,6 +21,7 @@
"postversion": "git push && git push --tags"
},
"dependencies": {
"@stablelib/base64": "^1.0.1",
"@types/node": "^13.1.0",
"@types/sjcl": "^1.0.29",
"@types/uuid": "^3.4.6",
Expand Down
4 changes: 2 additions & 2 deletions src/lsat.ts
Expand Up @@ -4,7 +4,7 @@ const bufio = require('bufio')
import crypto from 'crypto'
import * as Macaroon from 'macaroon'

import { Caveat, decodeIdentifierFromMacaroon, Identifier } from '.'
import { Caveat, decodeIdentifierFromMacaroon, Identifier, getRawMacaroon } from '.'
import { LsatOptions } from './types'
import { isHex, getIdFromRequest, decode } from './helpers'

Expand Down Expand Up @@ -212,7 +212,7 @@ export class Lsat extends bufio.Struct {

const mac = Macaroon.importMacaroon(this.baseMacaroon)
mac.addFirstPartyCaveat(caveat.encode())
this.baseMacaroon = Macaroon.bytesToBase64(mac._exportBinaryV2())
this.baseMacaroon = getRawMacaroon(mac)
}

/**
Expand Down
16 changes: 14 additions & 2 deletions src/macaroon.ts
Expand Up @@ -2,6 +2,8 @@ import { Caveat, verifyCaveats } from "./caveat";
import { stringToBytes } from './helpers'
import * as Macaroon from 'macaroon'
import { MacaroonClass, Satisfier } from "./types";
import { bytesToBase64 } from "macaroon/src/macaroon";
import { encode, encodeURLSafe } from "@stablelib/base64";

/**
* @description utility function to get an array of caveat instances from
Expand Down Expand Up @@ -65,6 +67,16 @@ export function verifyMacaroonCaveats(
}
}

export function getRawMacaroon(mac: MacaroonClass): string {
return Macaroon.bytesToBase64(mac._exportBinaryV2())
/**
* A convenience wrapper for getting a base64 encoded string.
* We unfortunately can't use the built in tool `Macaroon#bytesToBase64`
* because it only supports url safe base64 encoding which isn't compatible with
* aperture
* @param mac MacaroonClass - a macaroon to convert to raw base64
* @returns base64 string
*/
export function getRawMacaroon(mac: MacaroonClass, urlSafe=false): string {
const bytes = mac._exportBinaryV2()
if (urlSafe) return encodeURLSafe(bytes)
return encode(bytes)
}
21 changes: 4 additions & 17 deletions tests/caveat.spec.ts
@@ -1,6 +1,6 @@
import { expect } from 'chai'
import * as Macaroon from 'macaroon'
import { Caveat, ErrInvalidCaveat, hasCaveat, verifyCaveats } from '../src'
import { Caveat, ErrInvalidCaveat, getRawMacaroon, hasCaveat, verifyCaveats } from '../src'

import { Satisfier } from '../src/types'

Expand Down Expand Up @@ -60,11 +60,7 @@ describe('Caveats', () => {
})
macaroon.addFirstPartyCaveat(caveat.encode())

const macBin = macaroon._exportBinaryV2()
if (macBin == null) {
return
}
const macb64 = Macaroon.bytesToBase64(macBin)
const macb64 = getRawMacaroon(macaroon)
// check that it returns the value for the caveat we're checking for
expect(hasCaveat(macb64, caveat)).to.equal(
caveat.value && caveat.value.toString()
Expand All @@ -77,12 +73,7 @@ describe('Caveats', () => {
// check that it will return the value of a newer caveat with the same condition
const newerCaveat = new Caveat({ condition, value: value - 1 })
macaroon.addFirstPartyCaveat(newerCaveat.encode())
const macBin2 = macaroon._exportBinaryV2()
if (macBin2 == null) {
return
}
const macb642 = Macaroon.bytesToBase64(macBin2)

const macb642 = getRawMacaroon(macaroon)
expect(hasCaveat(macb642, newerCaveat)).to.equal(
newerCaveat.value && newerCaveat.value.toString()
)
Expand All @@ -96,11 +87,7 @@ describe('Caveats', () => {
location: 'location',
})

const macBin3 = macaroon._exportBinaryV2()
if (macBin3 == null) {
return
}
const macb643 = Macaroon.bytesToBase64(macBin3)
const macb643 = getRawMacaroon(macaroon)

const test = (): boolean | ErrInvalidCaveat | string =>
hasCaveat(macb643, 'condition:fail')
Expand Down
9 changes: 3 additions & 6 deletions tests/lsat.spec.ts
Expand Up @@ -3,7 +3,7 @@ import { expect } from 'chai'
import * as Macaroon from 'macaroon'
import {
Caveat,
decodeIdentifierFromMacaroon,
getRawMacaroon,
Lsat,
parseChallengePart,
} from '../src'
Expand Down Expand Up @@ -41,11 +41,8 @@ describe('LSAT Token', () => {

const builder = getTestBuilder('secret')
builder.addFirstPartyCaveat(caveat.encode())
const builderBin = builder._exportBinaryV2()
if (builderBin == null) {
return
}
const macb64 = Macaroon.bytesToBase64(builderBin)

const macb64 = getRawMacaroon(builder)
macaroon = macb64
challenge = `macaroon="${macb64}", invoice="${invoice.payreq}"`
})
Expand Down
12 changes: 12 additions & 0 deletions tests/macaroon.spec.ts
Expand Up @@ -92,4 +92,16 @@ describe('macaroon', () => {
expect(isValid).to.be.false
})
})

describe('getRawMacaroon', () => {
it('should convert a macaroon to base64', () => {
// built-in Macaroon function doesn't handle slashes properly
// so want to test with one that has them
const original =
'AgEEbHNhdAJCAACpIed4t8z8YTUNlAsUMqi1cYNA0kTYT6ajy0FpYySS/c/Lpm7rxB1Qyskte0aSEf3Ze2buI3yl2wmZtVgMZjzVAAIWc2VydmljZXM9c3BoaW54X21lbWU6MAACJXNwaGlueF9tZW1lX2NhcGFiaWxpdGllcz1sYXJnZV91cGxvYWQAAhZsYXJnZV91cGxvYWRfbWF4X21iPTMyAAAGIEFPRpVD8ryeKlJsfMvtufUogBiUwvz/h9KP/FC6gHg8'

const mac = Macaroon.importMacaroon(original)
expect(getRawMacaroon(mac)).to.equal(original)
})
})
})
7 changes: 3 additions & 4 deletions tests/satisfiers.spec.ts
Expand Up @@ -82,7 +82,7 @@ describe('satisfiers', () => {
it('should fail to create satisfier on invalid target service', () => {
const invalidTargetServices = [12, { foo: 'bar' }, ['a', 'b', 'c']]
for (const target of invalidTargetServices) {
// @ts-expect-error
// @ts-expect-error this is a test that we expect to throw
expect(() => createServicesSatisfier(target)).to.throw(
InvalidServicesError
)
Expand Down Expand Up @@ -147,19 +147,18 @@ describe('satisfiers', () => {
it('should fail to create satisfier on invalid inputs', () => {
const invalidInputs = [12, { foo: 'bar' }, ['a', 'b', 'c']]
for (const target of invalidInputs) {
// @ts-expect-error
// @ts-expect-error test that expects to throw
expect(() => createCapabilitiesSatisfier(target, 'test')).to.throw(
InvalidCapabilitiesError
)
// @ts-expect-error
// @ts-expect-error test that expects to throw
expect(() => createCapabilitiesSatisfier('test', target)).to.throw(
InvalidCapabilitiesError
)
}
})

it('should not allow any capabilities that were not previously allowed', () => {
// const invalidCaveat = Caveat.decode(`${SERVICES_CAVEAT_CONDITION}=baz:0`)
const caveats = [secondCaveat, firstCaveat]
expect(runTest(caveats, 'foo')).to.be.false
})
Expand Down

0 comments on commit 9aa6351

Please sign in to comment.