Skip to content

Commit

Permalink
fixed a few things in eslint config
Browse files Browse the repository at this point in the history
  • Loading branch information
pelle committed Feb 20, 2019
1 parent 8b711aa commit 95940a2
Show file tree
Hide file tree
Showing 11 changed files with 103 additions and 95 deletions.
8 changes: 6 additions & 2 deletions .eslintrc.js
@@ -1,3 +1,7 @@
module.exports = {
"extends": "standard"
};
"extends": "standard",
"plugins": ["jest"],
"env": {
"jest/globals": true
}
}
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Expand Up @@ -60,6 +60,7 @@
"buffer": "^5.1.0",
"did-resolver": "0.0.6",
"elliptic": "^6.4.0",
"eslint-plugin-jest": "^22.3.0",
"js-sha256": "^0.9.0",
"js-sha3": "^0.7.0",
"jsdoc-to-markdown": "^4.0.1",
Expand Down
2 changes: 1 addition & 1 deletion src/Digest.js
@@ -1,5 +1,5 @@
import { sha256 as sha256js } from 'js-sha256'
import { keccak_256 } from 'js-sha3'
import { keccak_256 } from 'js-sha3' // eslint-disable-line
import { Buffer } from 'buffer'

export function sha256 (payload) {
Expand Down
20 changes: 10 additions & 10 deletions src/JWT.js
Expand Up @@ -9,7 +9,7 @@ const SUPPORTED_PUBLIC_KEY_TYPES = {
'Ed25519': ['ED25519SignatureVerification']
}

const JOSE_HEADER = {typ: 'JWT'}
const JOSE_HEADER = { typ: 'JWT' }
const defaultAlg = 'ES256K'

function encodeSection (data) {
Expand Down Expand Up @@ -74,10 +74,10 @@ export function decodeJWT (jwt) {
* @param {SimpleSigner} options.signer a signer, reference our SimpleSigner.js
* @return {Promise<Object, Error>} a promise which resolves with a signed JSON Web Token or rejects with an error
*/
export async function createJWT (payload, {issuer, signer, alg, expiresIn}) {
export async function createJWT (payload, { issuer, signer, alg, expiresIn }) {
if (!signer) throw new Error('No Signer functionality has been configured')
if (!issuer) throw new Error('No issuing DID has been configured')
const header = {...JOSE_HEADER, alg: alg || defaultAlg}
const header = { ...JOSE_HEADER, alg: alg || defaultAlg }
const timestamps = { iat: Math.floor(Date.now() / 1000) }
if (expiresIn) {
if (typeof expiresIn === 'number') {
Expand All @@ -87,7 +87,7 @@ export async function createJWT (payload, {issuer, signer, alg, expiresIn}) {
}
}
const signingInput = [encodeSection(header),
encodeSection({...timestamps, ...payload, iss: issuer})
encodeSection({ ...timestamps, ...payload, iss: issuer })
].join('.')

const jwtSigner = SignerAlgorithm(header.alg)
Expand Down Expand Up @@ -118,8 +118,8 @@ export async function createJWT (payload, {issuer, signer, alg, expiresIn}) {
*/
export async function verifyJWT (jwt, options = {}) {
const aud = options.audience ? normalizeDID(options.audience) : undefined
const {payload, header, signature, data} = decodeJWT(jwt)
const {doc, authenticators, issuer} = await resolveAuthenticator(header.alg, payload.iss, options.auth)
const { payload, header, signature, data } = decodeJWT(jwt)
const { doc, authenticators, issuer } = await resolveAuthenticator(header.alg, payload.iss, options.auth)
const signer = VerifierAlgorithm(header.alg)(data, signature, authenticators)
const now = Math.floor(Date.now() / 1000)
if (signer) {
Expand Down Expand Up @@ -147,7 +147,7 @@ export async function verifyJWT (jwt, options = {}) {
}
}
}
return ({payload, doc, issuer, signer, jwt})
return ({ payload, doc, issuer, signer, jwt })
} else {

}
Expand Down Expand Up @@ -175,12 +175,12 @@ export async function resolveAuthenticator (alg, mnidOrDid, auth) {
const issuer = normalizeDID(mnidOrDid)
const doc = await resolve(issuer)
if (!doc) throw new Error(`Unable to resolve DID document for ${issuer}`)
const authenticationKeys = auth ? (doc.authentication || []).map(({publicKey}) => publicKey) : true
const authenticators = (doc.publicKey || []).filter(({type, id}) => types.find(supported => supported === type && (!auth || authenticationKeys.indexOf(id) >= 0)))
const authenticationKeys = auth ? (doc.authentication || []).map(({ publicKey }) => publicKey) : true
const authenticators = (doc.publicKey || []).filter(({ type, id }) => types.find(supported => supported === type && (!auth || authenticationKeys.indexOf(id) >= 0)))

if (auth && (!authenticators || authenticators.length === 0)) throw new Error(`DID document for ${issuer} does not have public keys suitable for authenticationg user`)
if (!authenticators || authenticators.length === 0) throw new Error(`DID document for ${issuer} does not have public keys for ${alg}`)
return {authenticators, issuer, doc}
return { authenticators, issuer, doc }
}

export default { decodeJWT, createJWT, verifyJWT, resolveAuthenticator }
2 changes: 1 addition & 1 deletion src/SignerAlgorithm.js
Expand Up @@ -2,7 +2,7 @@ import base64url from 'base64url'
import { Buffer } from 'buffer'

export function ES256KSigner (recoverable = false) {
function toJose ({r, s, recoveryParam}) {
function toJose ({ r, s, recoveryParam }) {
const jose = Buffer.alloc(recoverable ? 65 : 64)
Buffer.from(r, 'hex').copy(jose, 0)
Buffer.from(s, 'hex').copy(jose, 32)
Expand Down
2 changes: 1 addition & 1 deletion src/VerifierAlgorithm.js
Expand Up @@ -12,7 +12,7 @@ export function toSignatureObject (signature, recoverable = false) {
if (rawsig.length !== (recoverable ? 65 : 64)) throw new Error('wrong signature length')
const r = rawsig.slice(0, 32).toString('hex')
const s = rawsig.slice(32, 64).toString('hex')
const sigObj = {r, s}
const sigObj = { r, s }
if (recoverable) {
sigObj.recoveryParam = rawsig[64]
}
Expand Down

0 comments on commit 95940a2

Please sign in to comment.