Skip to content

Commit 0a294ad

Browse files
Merge pull request #11 from rodrigoborgesdeoliveira/refactor/then-catch
Replace then/catch with async/await
2 parents 4344906 + 11e39ab commit 0a294ad

File tree

2 files changed

+111
-104
lines changed

2 files changed

+111
-104
lines changed

index.ts

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -116,38 +116,44 @@ export class AppStoreServerAPIClient {
116116
stringBody = JSON.stringify(body)
117117
headers['Content-Type'] = 'application/json'
118118
}
119-
return fetch(this.urlBase + path + '?' + parsedQueryParameters, {
119+
120+
const response = await fetch(this.urlBase + path + '?' + parsedQueryParameters, {
120121
method: method,
121122
body: stringBody,
122123
headers: headers
123-
}).then(r => {
124-
if(r.status >= 200 && r.status < 300) {
125-
// Success
126-
if (validator == null) {
127-
return null
128-
}
129-
return r.json().then(responseBody => {
130-
if (!validator.validate(responseBody)) {
131-
throw new Error("Unexpected response body format")
132-
}
133-
return responseBody
134-
});
135-
} else {
136-
return r.json().then(responseBody => {
137-
const errorCode = responseBody['errorCode']
138-
if (Object.values(APIError).includes(errorCode)) {
139-
throw new APIException(r.status, errorCode as APIError)
140-
} else {
141-
throw new APIException(r.status)
142-
}
143-
}).catch(e => {
144-
if (e instanceof APIException) {
145-
throw e
146-
}
147-
throw new APIException(r.status)
148-
});
124+
})
125+
126+
if(response.ok) {
127+
// Success
128+
if (validator == null) {
129+
return null as T
130+
}
131+
132+
const responseBody = await response.json()
133+
134+
if (!validator.validate(responseBody)) {
135+
throw new Error("Unexpected response body format")
136+
}
137+
138+
return responseBody
139+
}
140+
141+
try {
142+
const responseBody = await response.json()
143+
const errorCode = responseBody['errorCode']
144+
145+
if (Object.values(APIError).includes(errorCode)) {
146+
throw new APIException(response.status, errorCode as APIError)
149147
}
150-
}) as T
148+
149+
throw new APIException(response.status)
150+
} catch (e) {
151+
if (e instanceof APIException) {
152+
throw e
153+
}
154+
155+
throw new APIException(response.status)
156+
}
151157
}
152158

153159
/**

jwt_verification.ts

Lines changed: 77 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -205,89 +205,90 @@ export class SignedDataVerifier {
205205
const request = new KJUR.asn1.ocsp.OCSPRequest({reqList: [{issuerCert: issuer.toString(), subjectCert: cert.toString() , alg: "sha256"}]})
206206
const headers = new Headers()
207207
headers.append('Content-Type', 'application/ocsp-request')
208-
return fetch(matchResult[1], {
208+
209+
const response = await fetch(matchResult[1], {
209210
headers: headers,
210211
method: 'POST',
211212
body: Buffer.from(request.getEncodedHex(), 'hex')
212-
}).then(r => r.buffer())
213-
.then(resp => {
214-
const parsedResponse = new (KJUR.asn1.ocsp as any).OCSPParser().getOCSPResponse(resp.toString('hex'))
215-
// The issuer could also be the signer
216-
const jsrassignX509Issuer = new X509()
217-
jsrassignX509Issuer.readCertHex(issuer.raw.toString('hex'))
218-
const allCerts: X509[] = [jsrassignX509Issuer]
219-
for (const certHex of parsedResponse.certs) {
220-
const cert = new X509()
221-
cert.readCertHex(certHex)
222-
allCerts.push(cert)
223-
}
224-
let signingCert: X509Certificate | null = null
225-
if (parsedResponse.respid.key) {
226-
for (const cert of allCerts) {
227-
const shasum = createHash('sha1')
228-
shasum.update(Buffer.from(cert.getSPKIValue(), 'hex'))
229-
const spkiHash = shasum.digest('hex')
230-
if (spkiHash === parsedResponse.respid.key) {
231-
signingCert = new X509Certificate(Buffer.from(cert.hex, 'hex'))
232-
}
233-
}
234-
} else if (parsedResponse.respid.name) {
235-
for (const cert of allCerts) {
236-
if (cert.getSubject().str === parsedResponse.respid.name.str) {
237-
signingCert = new X509Certificate(Buffer.from(cert.hex, 'hex'))
238-
}
239-
}
240-
}
241-
if (signingCert == null) {
242-
throw new VerificationException(VerificationStatus.FAILURE)
243-
}
244-
// Verify Signing Cert is issued by issuer
245-
if (signingCert.publicKey === issuer.publicKey && signingCert.subject === issuer.subject) {
246-
// This is directly signed by the issuer
247-
} else if (signingCert.verify(issuer.publicKey)) {
248-
// This is issued by the issuer, let's check the dates and purpose
249-
const signingCertAsign = new X509()
250-
signingCertAsign.readCertPEM(signingCert.toString())
251-
if (!signingCertAsign.getExtExtKeyUsage().array.includes("ocspSigning")) {
252-
throw new VerificationException(VerificationStatus.INVALID_CERTIFICATE)
253-
}
254-
this.checkDates(signingCert, new Date())
255-
} else {
256-
throw new VerificationException(VerificationStatus.INVALID_CERTIFICATE)
213+
})
214+
215+
const responseBuffer = await response.buffer()
216+
const parsedResponse = new (KJUR.asn1.ocsp as any).OCSPParser().getOCSPResponse(responseBuffer.toString('hex'))
217+
// The issuer could also be the signer
218+
const jsrassignX509Issuer = new X509()
219+
jsrassignX509Issuer.readCertHex(issuer.raw.toString('hex'))
220+
const allCerts: X509[] = [jsrassignX509Issuer]
221+
for (const certHex of parsedResponse.certs) {
222+
const cert = new X509()
223+
cert.readCertHex(certHex)
224+
allCerts.push(cert)
225+
}
226+
let signingCert: X509Certificate | null = null
227+
if (parsedResponse.respid.key) {
228+
for (const cert of allCerts) {
229+
const shasum = createHash('sha1')
230+
shasum.update(Buffer.from(cert.getSPKIValue(), 'hex'))
231+
const spkiHash = shasum.digest('hex')
232+
if (spkiHash === parsedResponse.respid.key) {
233+
signingCert = new X509Certificate(Buffer.from(cert.hex, 'hex'))
257234
}
258-
259-
// Extract raw responseData
260-
const responseData = ASN1HEX.getTLVbyList(resp.toString('hex'), 0, [1, 0, 1, 0, 0]) as string
261-
// Verify Payload signed by cert
262-
const shortAlg = parsedResponse.alg.substring(0, 6).toUpperCase()
263-
if (shortAlg !== "SHA256" && shortAlg !== "SHA384" && shortAlg !== "SHA512") {
264-
throw new VerificationException(VerificationStatus.FAILURE)
235+
}
236+
} else if (parsedResponse.respid.name) {
237+
for (const cert of allCerts) {
238+
if (cert.getSubject().str === parsedResponse.respid.name.str) {
239+
signingCert = new X509Certificate(Buffer.from(cert.hex, 'hex'))
265240
}
241+
}
242+
}
243+
if (signingCert == null) {
244+
throw new VerificationException(VerificationStatus.FAILURE)
245+
}
246+
// Verify Signing Cert is issued by issuer
247+
if (signingCert.publicKey === issuer.publicKey && signingCert.subject === issuer.subject) {
248+
// This is directly signed by the issuer
249+
} else if (signingCert.verify(issuer.publicKey)) {
250+
// This is issued by the issuer, let's check the dates and purpose
251+
const signingCertAsign = new X509()
252+
signingCertAsign.readCertPEM(signingCert.toString())
253+
if (!signingCertAsign.getExtExtKeyUsage().array.includes("ocspSigning")) {
254+
throw new VerificationException(VerificationStatus.INVALID_CERTIFICATE)
255+
}
256+
this.checkDates(signingCert, new Date())
257+
} else {
258+
throw new VerificationException(VerificationStatus.INVALID_CERTIFICATE)
259+
}
260+
261+
// Extract raw responseData
262+
const responseData = ASN1HEX.getTLVbyList(responseBuffer.toString('hex'), 0, [1, 0, 1, 0, 0]) as string
263+
// Verify Payload signed by cert
264+
const shortAlg = parsedResponse.alg.substring(0, 6).toUpperCase()
265+
if (shortAlg !== "SHA256" && shortAlg !== "SHA384" && shortAlg !== "SHA512") {
266+
throw new VerificationException(VerificationStatus.FAILURE)
267+
}
266268

267-
if (!verify(shortAlg, Buffer.from(responseData, 'hex'), signingCert.publicKey, Buffer.from(parsedResponse.sighex, 'hex'))) {
268-
throw new VerificationException(VerificationStatus.FAILURE)
269-
}
270-
271-
for (const singleResponse of parsedResponse.array) {
272-
// Confirm entry is for this cert
273-
const certIdBuilder = new KJUR.asn1.ocsp.CertID() as any
274-
const currentCertCertId = certIdBuilder.getParamByCerts(issuer.toString(), cert.toString(), 'sha256')
275-
if (!(currentCertCertId.alg === singleResponse.certid.alg && currentCertCertId.issname === singleResponse.certid.issname &&
276-
currentCertCertId.isskey === singleResponse.certid.isskey && currentCertCertId.sbjsn === singleResponse.certid.sbjsn)) {
277-
continue
278-
}
279-
// Validate contents
280-
const issueDate = this.parseX509Date(singleResponse.thisupdate)
281-
const nextDate = this.parseX509Date(singleResponse.nextupdate)
282-
283-
if (singleResponse.status.status !== 'good' || new Date().getTime() - MAX_SKEW < issueDate.getTime() || nextDate.getTime() < new Date().getTime() + MAX_SKEW) {
284-
throw new VerificationException(VerificationStatus.FAILURE)
285-
}
286-
// Success
287-
return
288-
}
269+
if (!verify(shortAlg, Buffer.from(responseData, 'hex'), signingCert.publicKey, Buffer.from(parsedResponse.sighex, 'hex'))) {
270+
throw new VerificationException(VerificationStatus.FAILURE)
271+
}
272+
273+
for (const singleResponse of parsedResponse.array) {
274+
// Confirm entry is for this cert
275+
const certIdBuilder = new KJUR.asn1.ocsp.CertID() as any
276+
const currentCertCertId = certIdBuilder.getParamByCerts(issuer.toString(), cert.toString(), 'sha256')
277+
if (!(currentCertCertId.alg === singleResponse.certid.alg && currentCertCertId.issname === singleResponse.certid.issname &&
278+
currentCertCertId.isskey === singleResponse.certid.isskey && currentCertCertId.sbjsn === singleResponse.certid.sbjsn)) {
279+
continue
280+
}
281+
// Validate contents
282+
const issueDate = this.parseX509Date(singleResponse.thisupdate)
283+
const nextDate = this.parseX509Date(singleResponse.nextupdate)
284+
285+
if (singleResponse.status.status !== 'good' || new Date().getTime() - MAX_SKEW < issueDate.getTime() || nextDate.getTime() < new Date().getTime() + MAX_SKEW) {
289286
throw new VerificationException(VerificationStatus.FAILURE)
290-
});
287+
}
288+
// Success
289+
return
290+
}
291+
throw new VerificationException(VerificationStatus.FAILURE)
291292
}
292293

293294
private checkDates(cert: X509Certificate, effectiveDate: Date) {

0 commit comments

Comments
 (0)