Skip to content

Commit

Permalink
src: runtime declarations
Browse files Browse the repository at this point in the history
  • Loading branch information
panva committed Nov 15, 2020
1 parent 78d424c commit 8bc4c67
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 24 deletions.
8 changes: 5 additions & 3 deletions src/runtime/browser/digest.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import crypto, { ensureSecureContext } from './webcrypto.js'
import type { DigestFunction } from '../interfaces.d'

export default async (
digest: 'sha256' | 'sha384' | 'sha512',
const digest: DigestFunction = async (
algorithm: 'sha256' | 'sha384' | 'sha512',
data: Uint8Array,
): Promise<Uint8Array> => {
ensureSecureContext()
const subtleDigest = `SHA-${digest.substr(-3)}`
const subtleDigest = `SHA-${algorithm.substr(-3)}`
return new Uint8Array(await crypto.subtle.digest(subtleDigest, data))
}
export default digest
6 changes: 4 additions & 2 deletions src/runtime/browser/fetch.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import type { FetchFunction } from '../interfaces.d'
import { JOSEError } from '../../util/errors.js'

export default async (url: URL, timeout: number) => {
const fetch: FetchFunction = async (url: URL, timeout: number) => {
let controller!: AbortController
if (typeof AbortController === 'function') {
controller = new AbortController()
setTimeout(() => controller.abort(), timeout)
}

const response = await fetch(url.href, {
const response = await window.fetch(url.href, {
signal: controller ? controller.signal : undefined,
redirect: 'manual',
referrerPolicy: 'no-referrer',
Expand All @@ -26,3 +27,4 @@ export default async (url: URL, timeout: number) => {
throw new JOSEError('Failed to parse the JSON Web Key Set HTTP response as JSON')
}
}
export default fetch
8 changes: 5 additions & 3 deletions src/runtime/browser/jwk_to_key.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import crypto, { ensureSecureContext } from './webcrypto.js'
import type { JWKParseFunction } from '../interfaces.d'
import { JOSENotSupported } from '../../util/errors.js'
import type { JWK } from '../../types.d'
import { decode as base64url } from './base64url.js'

function parse(
function subtleMapping(
jwk: JWK,
): { algorithm: RsaHashedImportParams | EcKeyAlgorithm | Algorithm; keyUsages: KeyUsage[] } {
let algorithm: RsaHashedImportParams | EcKeyAlgorithm | Algorithm
Expand Down Expand Up @@ -101,8 +102,8 @@ function parse(
return { algorithm, keyUsages }
}

export default async (jwk: JWK): Promise<CryptoKey> => {
const { algorithm, keyUsages } = parse(jwk)
const parse: JWKParseFunction = async (jwk: JWK): Promise<CryptoKey> => {
const { algorithm, keyUsages } = subtleMapping(jwk)
let format = 'jwk'
let keyData: JWK | Uint8Array = { ...jwk }
delete keyData.alg
Expand All @@ -119,3 +120,4 @@ export default async (jwk: JWK): Promise<CryptoKey> => {
(jwk.key_ops as KeyUsage[]) ?? keyUsages,
)
}
export default parse
6 changes: 3 additions & 3 deletions src/runtime/digest.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import type { AsyncOrSync } from '../types.i.d'
declare const _default: (digest: string, data: Uint8Array) => AsyncOrSync<Uint8Array>
export default _default
import type { DigestFunction } from './interfaces.d'
declare const digest: DigestFunction
export default digest
5 changes: 3 additions & 2 deletions src/runtime/fetch.d.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
declare const _default: (url: URL, timeout: number) => Promise<any>
export default _default
import type { FetchFunction } from './interfaces.d'
declare const fetch: FetchFunction
export default fetch
9 changes: 5 additions & 4 deletions src/runtime/interfaces.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { KeyLike } from '../types.d'
import type { JWK, KeyLike } from '../types.d'
import type { EpkJwk, AsyncOrSync } from '../types.i.d'

/**
Expand Down Expand Up @@ -109,11 +109,12 @@ export interface DecryptFunction {
additionalData: Uint8Array,
): Promise<Uint8Array>
}
// TODO:
export interface FetchFunction {
(url: URL, timeout: number): Promise<any>
}
// TODO:
export interface DigestFunction {
(digest: string, data: Uint8Array): AsyncOrSync<Uint8Array>
(digest: 'sha256' | 'sha384' | 'sha512', data: Uint8Array): AsyncOrSync<Uint8Array>
}
export interface JWKParseFunction {
(jwk: JWK): AsyncOrSync<KeyLike>
}
6 changes: 3 additions & 3 deletions src/runtime/jwk_to_key.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import type { KeyLike, JWK } from '../types.d'
declare const _default: (jwk: JWK) => Promise<KeyLike>
export default _default
import type { JWKParseFunction } from './interfaces.d'
declare const parse: JWKParseFunction
export default parse
9 changes: 7 additions & 2 deletions src/runtime/node/digest.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { createHash } from 'crypto'
import type { DigestFunction } from '../interfaces.d'

export default (digest: 'sha256' | 'sha384' | 'sha512', data: Uint8Array): Uint8Array => {
return createHash(digest).update(data).digest()
const digest: DigestFunction = (
algorithm: 'sha256' | 'sha384' | 'sha512',
data: Uint8Array,
): Uint8Array => {
return createHash(algorithm).update(data).digest()
}
export default digest
5 changes: 4 additions & 1 deletion src/runtime/node/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { get as http } from 'http'
import type { ClientRequest } from 'http'
import { get as https, RequestOptions } from 'https'

import type { FetchFunction } from '../interfaces.d'
import { JOSEError } from '../../util/errors.js'
import { concat, decoder } from '../../lib/buffer_utils.js'

Expand All @@ -10,7 +11,7 @@ const protocols: { [protocol: string]: (...args: Parameters<typeof https>) => Cl
'http:': http,
}

export default async (url: URL, timeout: number, options?: RequestOptions) => {
const fetch: FetchFunction = async (url: URL, timeout: number, options?: RequestOptions) => {
if (!(url.protocol in protocols)) {
throw new TypeError('Unsupported URL protocol.')
}
Expand Down Expand Up @@ -41,3 +42,5 @@ export default async (url: URL, timeout: number, options?: RequestOptions) => {
).on('error', reject)
}) as Promise<any>
}

export default fetch
4 changes: 3 additions & 1 deletion src/runtime/node/jwk_to_key.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import type { KeyObject, PublicKeyInput, PrivateKeyInput } from 'crypto'
import { createPrivateKey, createPublicKey, createSecretKey } from 'crypto'

import type { JWKParseFunction } from '../interfaces.d'
import { decode as base64url } from './base64url.js'
import { JOSENotSupported } from '../../util/errors.js'
import { setCurve } from './get_named_curve.js'
import { setModulusLength } from './check_modulus_length.js'
import Asn1SequenceEncoder from './asn1_sequence_encoder.js'
import type { JWK } from '../../types.d'

export default async (jwk: JWK): Promise<KeyObject> => {
const parse: JWKParseFunction = (jwk: JWK): KeyObject => {
switch (jwk.kty) {
case 'oct': {
return createSecretKey(base64url(jwk.k!))
Expand Down Expand Up @@ -119,3 +120,4 @@ export default async (jwk: JWK): Promise<KeyObject> => {
throw new JOSENotSupported('unsupported or invalid JWK "kty" (Key Type) Parameter value')
}
}
export default parse

0 comments on commit 8bc4c67

Please sign in to comment.