Comparing version 3.1.1 to 3.1.2
@@ -54,4 +54,4 @@ /// <reference types="node" /> | ||
} | ||
export declare type FailedHintCode = "expiredTransaction" | "certificateErr" | "userCancel" | "cancelled" | "startFailed"; | ||
export declare type PendingHintCode = "outstandingTransaction" | "noClient" | "started" | "userSign"; | ||
export type FailedHintCode = "expiredTransaction" | "certificateErr" | "userCancel" | "cancelled" | "startFailed"; | ||
export type PendingHintCode = "outstandingTransaction" | "noClient" | "started" | "userSign"; | ||
export interface CancelRequest extends CollectRequest { | ||
@@ -83,4 +83,4 @@ } | ||
} | ||
export declare type BankIdRequest = AuthRequest | SignRequest | CollectRequest | CancelRequest; | ||
export declare type BankIdResponse = CancelResponse | AuthResponse | SignResponse | CollectResponse; | ||
export type BankIdRequest = AuthRequest | SignRequest | CollectRequest | CancelRequest; | ||
export type BankIdResponse = CancelResponse | AuthResponse | SignResponse | CollectResponse; | ||
interface BankIdClientSettings { | ||
@@ -103,2 +103,3 @@ production: boolean; | ||
export declare class BankIdClient { | ||
#private; | ||
readonly options: Required<BankIdClientSettings>; | ||
@@ -112,8 +113,7 @@ readonly axios: AxiosInstance; | ||
cancel(parameters: CollectRequest): Promise<CancelResponse>; | ||
_awaitPendingCollect(orderRef: string): Promise<CollectResponse>; | ||
authenticateAndCollect(parameters: AuthRequest): Promise<CollectResponse>; | ||
signAndCollect(parameters: SignRequest): Promise<CollectResponse>; | ||
_awaitPendingCollect(orderRef: string): Promise<CollectResponse>; | ||
_call<Req extends BankIdRequest, Res extends BankIdResponse>(method: BankIdMethod, payload: Req): Promise<Res>; | ||
_createAxiosInstance(): AxiosInstance; | ||
awaitPendingCollect(orderRef: string): Promise<CollectResponse>; | ||
} | ||
export {}; |
"use strict"; | ||
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) { | ||
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter"); | ||
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it"); | ||
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver); | ||
}; | ||
var _BankIdClient_instances, _BankIdClient_call, _BankIdClient_createAxiosInstance; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -19,3 +25,3 @@ exports.BankIdClient = exports.RequestError = exports.BankIdError = exports.BankIdMethod = exports.REQUEST_FAILED_ERROR = exports.BankIdErrorCode = void 0; | ||
BankIdErrorCode["MAINTENANCE"] = "maintenance"; | ||
})(BankIdErrorCode = exports.BankIdErrorCode || (exports.BankIdErrorCode = {})); | ||
})(BankIdErrorCode || (exports.BankIdErrorCode = BankIdErrorCode = {})); | ||
exports.REQUEST_FAILED_ERROR = "BANKID_NO_RESPONSE"; | ||
@@ -31,3 +37,3 @@ // | ||
BankIdMethod["cancel"] = "cancel"; | ||
})(BankIdMethod = exports.BankIdMethod || (exports.BankIdMethod = {})); | ||
})(BankIdMethod || (exports.BankIdMethod = BankIdMethod = {})); | ||
// | ||
@@ -60,2 +66,3 @@ // Error types | ||
constructor(options) { | ||
_BankIdClient_instances.add(this); | ||
this.options = { | ||
@@ -74,3 +81,3 @@ production: false, | ||
if (this.options.pfx === undefined) { | ||
this.options.pfx = path.resolve(__dirname, "../cert/", "FPTestcert4_20220818.p12"); | ||
this.options.pfx = path.resolve(__dirname, "../cert/", "FPTestcert4_20230629.p12"); | ||
} | ||
@@ -87,3 +94,3 @@ if (this.options.passphrase === undefined) { | ||
} | ||
this.axios = this._createAxiosInstance(); | ||
this.axios = __classPrivateFieldGet(this, _BankIdClient_instances, "m", _BankIdClient_createAxiosInstance).call(this); | ||
this.baseUrl = this.options.production | ||
@@ -97,3 +104,3 @@ ? "https://appapi2.bankid.com/rp/v5.1/" | ||
} | ||
return this._call(BankIdMethod.auth, parameters); | ||
return __classPrivateFieldGet(this, _BankIdClient_instances, "m", _BankIdClient_call).call(this, BankIdMethod.auth, parameters); | ||
} | ||
@@ -115,19 +122,23 @@ sign(parameters) { | ||
}; | ||
return this._call(BankIdMethod.sign, parameters); | ||
return __classPrivateFieldGet(this, _BankIdClient_instances, "m", _BankIdClient_call).call(this, BankIdMethod.sign, parameters); | ||
} | ||
collect(parameters) { | ||
return this._call(BankIdMethod.collect, parameters); | ||
return __classPrivateFieldGet(this, _BankIdClient_instances, "m", _BankIdClient_call).call(this, BankIdMethod.collect, parameters); | ||
} | ||
cancel(parameters) { | ||
return this._call(BankIdMethod.cancel, parameters); | ||
return __classPrivateFieldGet(this, _BankIdClient_instances, "m", _BankIdClient_call).call(this, BankIdMethod.cancel, parameters); | ||
} | ||
_awaitPendingCollect(orderRef) { | ||
console.warn("This method has been renamed to 'awaitPendingCollect"); | ||
return this.awaitPendingCollect(orderRef); | ||
} | ||
async authenticateAndCollect(parameters) { | ||
const authResponse = await this.authenticate(parameters); | ||
return this._awaitPendingCollect(authResponse.orderRef); | ||
return this.awaitPendingCollect(authResponse.orderRef); | ||
} | ||
async signAndCollect(parameters) { | ||
const signResponse = await this.sign(parameters); | ||
return this._awaitPendingCollect(signResponse.orderRef); | ||
return this.awaitPendingCollect(signResponse.orderRef); | ||
} | ||
_awaitPendingCollect(orderRef) { | ||
awaitPendingCollect(orderRef) { | ||
return new Promise((resolve, reject) => { | ||
@@ -153,40 +164,39 @@ const timer = setInterval(() => { | ||
} | ||
_call(method, payload) { | ||
return new Promise((resolve, reject) => { | ||
this.axios | ||
.post(this.baseUrl + method, payload) | ||
.then(response => { | ||
resolve(response.data); | ||
}) | ||
.catch((error) => { | ||
let thrownError = error; | ||
if (axios_1.default.isAxiosError(error)) { | ||
if (error.response) { | ||
thrownError = new BankIdError(error.response.data.errorCode, error.response.data.details); | ||
} | ||
else if (error.request) { | ||
thrownError = new RequestError(error.request); | ||
} | ||
} | ||
exports.BankIdClient = BankIdClient; | ||
_BankIdClient_instances = new WeakSet(), _BankIdClient_call = function _BankIdClient_call(method, payload) { | ||
return new Promise((resolve, reject) => { | ||
this.axios | ||
.post(this.baseUrl + method, payload) | ||
.then(response => { | ||
resolve(response.data); | ||
}) | ||
.catch((error) => { | ||
let thrownError = error; | ||
if (axios_1.default.isAxiosError(error)) { | ||
if (error.response) { | ||
thrownError = new BankIdError(error.response.data.errorCode, error.response.data.details); | ||
} | ||
reject(thrownError); | ||
}); | ||
else if (error.request) { | ||
thrownError = new RequestError(error.request); | ||
} | ||
} | ||
reject(thrownError); | ||
}); | ||
} | ||
_createAxiosInstance() { | ||
const ca = Buffer.isBuffer(this.options.ca) | ||
? this.options.ca | ||
: fs.readFileSync(this.options.ca, "utf-8"); | ||
const pfx = Buffer.isBuffer(this.options.pfx) | ||
? this.options.pfx | ||
: fs.readFileSync(this.options.pfx); | ||
const passphrase = this.options.passphrase; | ||
return axios_1.default.create({ | ||
httpsAgent: new https.Agent({ pfx, passphrase, ca }), | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
}); | ||
} | ||
} | ||
exports.BankIdClient = BankIdClient; | ||
}); | ||
}, _BankIdClient_createAxiosInstance = function _BankIdClient_createAxiosInstance() { | ||
const ca = Buffer.isBuffer(this.options.ca) | ||
? this.options.ca | ||
: fs.readFileSync(this.options.ca, "utf-8"); | ||
const pfx = Buffer.isBuffer(this.options.pfx) | ||
? this.options.pfx | ||
: fs.readFileSync(this.options.pfx); | ||
const passphrase = this.options.passphrase; | ||
return axios_1.default.create({ | ||
httpsAgent: new https.Agent({ pfx, passphrase, ca }), | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
}); | ||
}; | ||
//# sourceMappingURL=bankid.js.map |
@@ -9,3 +9,3 @@ { | ||
], | ||
"version": "3.1.1", | ||
"version": "3.1.2", | ||
"main": "lib/bankid.js", | ||
@@ -32,4 +32,4 @@ "repository": { | ||
"@types/node": "^14.18.12", | ||
"typescript": "4.6.3", | ||
"prettier": "^2.2.1" | ||
"prettier": "^2.2.1", | ||
"typescript": "5.x" | ||
}, | ||
@@ -36,0 +36,0 @@ "peerDependencies": { |
@@ -17,5 +17,5 @@ # bankid | ||
```javascript | ||
const BankId = require("bankid"); | ||
import { BankIdClient } from "bankid"; | ||
const client = new BankId.BankIdClient(); | ||
const client = new BankIdClient(); | ||
const pno = "YYYYMMDDXXXX"; | ||
@@ -48,5 +48,5 @@ | ||
```javascript | ||
const BankId = require("bankid"); | ||
import { BankIdClient } from "bankid"; | ||
const client = new BankId.BankIdClient(); | ||
const client = new BankIdClient(); | ||
const pno = "YYYYMMDDXXXX"; | ||
@@ -65,3 +65,3 @@ const message = "some message displayed to the user to sign"; | ||
client | ||
.collect(res.orderRef) | ||
.collect({ orderRef: res.orderRef }) | ||
.then(res => { | ||
@@ -101,5 +101,5 @@ if (res.status === "complete") { | ||
```javascript | ||
const BankId = require("bankid"); | ||
import { BankIdClient } from "bankid"; | ||
const client = new BankId.BankIdClient({ | ||
const client = new BankIdClient({ | ||
production: true, | ||
@@ -126,5 +126,5 @@ pfx: "PATH_TO_YOUR_PFX", // alternatively also accepts buffer | ||
```javascript | ||
const BankId = require("bankid"); | ||
import { BankIdClient } from "bankid"; | ||
const client = new BankId.BankIdClient({ | ||
const client = new BankIdClient({ | ||
pfx: "certs/bankid.pfx", | ||
@@ -134,2 +134,32 @@ }); | ||
### Compatibility | ||
In Node.js v17+, OpenSSL is upgraded from v1.1.1 to v3, introducing subtle breaking changes for this library that yield this error: | ||
``` | ||
Error: unsupported | ||
at configSecureContext (node:internal/tls/secure-context:278:15) | ||
``` | ||
This is due to the legacy algorithms used to generate BankID certificates - and to handle this (until BankID updates their default certificate formats) there are two solutions. | ||
#### Manual certificate modernization (suggested) | ||
First, ensure `OpenSSL` v3.x needs to be installed on your machine. | ||
Then, you can run the following commands to get an updated certificate (`new.pfx`): | ||
```sh | ||
openssl pkcs12 -in old.pfx -nodes -legacy -out combined.pem | ||
openssl pkcs12 -in combined.pem -export -out new.pfx | ||
``` | ||
#### Enable legacy OpenSSL support | ||
If for any reason you do not want to modify the certificates, you can also enable the legacy OpenSSL provider when running Node.js: | ||
```sh | ||
node --openssl-legacy-provider ... | ||
``` | ||
## Deploy/Publish | ||
@@ -140,1 +170,13 @@ | ||
GitHub Actions should automagically release it to npm. ✨ | ||
## Ownership | ||
Repo ownership: [Jeff Trinidad - @jefftrinidad29](https://github.com/jefftrinidad29) \ | ||
Last audit: 2023-04-27 by [@jefftrinidad29](https://github.com/jefftrinidad29) | ||
# Audit Notes | ||
> 27th April 2023 by @jefftrinidad29 | ||
- Upgraded all non-critical dependencies | ||
- yarn audit fix |
@@ -14,4 +14,4 @@ { | ||
"@types/node": "^14.14.22", | ||
"typescript": "^4.1.3" | ||
"typescript": "5.x" | ||
} | ||
} |
import { BankIdClient } from "bankid"; | ||
const DELAY_BETWEEN_REQUETS = 10000; // milliseconds | ||
const DELAY_BETWEEN_REQUETS = 5000; // milliseconds | ||
@@ -46,3 +46,3 @@ async function main() { | ||
console.log(signRequest); | ||
await client._awaitPendingCollect(signRequest.orderRef); | ||
await client.awaitPendingCollect(signRequest.orderRef); | ||
console.log("sign request successful"); | ||
@@ -57,3 +57,3 @@ } catch (e) { | ||
client | ||
await client | ||
.authenticateAndCollect({ | ||
@@ -70,3 +70,3 @@ endUserIp: ip, | ||
client | ||
await client | ||
.signAndCollect({ | ||
@@ -101,3 +101,3 @@ endUserIp: ip, | ||
main() | ||
.then(() => console.log("test completed successfully")) | ||
.then(() => console.log("✅ All tests completed successfully")) | ||
.catch(err => console.log(err)); | ||
@@ -104,0 +104,0 @@ |
@@ -9,2 +9,3 @@ { | ||
"strict": true, | ||
"skipLibCheck": true, | ||
"target": "ES2018" | ||
@@ -11,0 +12,0 @@ }, |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
37533
469
176