Skip to content

Commit

Permalink
refactor: Update CRLDistributionPointsExtension constructor to accept…
Browse files Browse the repository at this point in the history
… URLs directly
  • Loading branch information
microshine committed Aug 8, 2024
1 parent a191443 commit bac1a01
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 21 deletions.
37 changes: 17 additions & 20 deletions src/extensions/crl_distribution_points.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,6 @@ import { GeneralName } from "../general_name";
export class CRLDistributionPointsExtension extends Extension {
public static override NAME = "CRL Distribution Points";

/**
* Creates a new CRLDistributionPointsExtension from an array of URLs.
* @param urls An array of URLs to be used as distribution points.
* @returns A new instance of CRLDistributionPointsExtension.
*/
public static create(urls: string[]): CRLDistributionPointsExtension {
const dps = urls.map(url => {
const dp = new asn1X509.DistributionPoint({
distributionPoint: new asn1X509.DistributionPointName({
fullName: [new asn1X509.GeneralName({ uniformResourceIdentifier: url })],
}),
});

return dp;
});

return new CRLDistributionPointsExtension(dps);
}

public distributionPoints: asn1X509.DistributionPoint[];

/**
Expand All @@ -43,9 +24,26 @@ export class CRLDistributionPointsExtension extends Extension {
* @param critical Indicates whether the extension is critical. Default is `false`
*/
public constructor(value: asn1X509.DistributionPoint[], critical?: boolean);
/**
* Creates a new instance from an array of URLs
* @param urls An array of URLs to be used as distribution points.
* @param critical Indicates whether the extension is critical. Default is `false`
*/
public constructor(urls: string[], critical?: boolean);
public constructor(...args: any[]) {
if (BufferSourceConverter.isBufferSource(args[0])) {
super(args[0]);
} else if (Array.isArray(args[0]) && typeof args[0][0] === "string") {
const urls = args[0] as string[];
const dps = urls.map(url => {
return new asn1X509.DistributionPoint({
distributionPoint: new asn1X509.DistributionPointName({
fullName: [new asn1X509.GeneralName({ uniformResourceIdentifier: url })],
}),
});
});
const value = new asn1X509.CRLDistributionPoints(dps);
super(asn1X509.id_ce_cRLDistributionPoints, args[1], AsnConvert.serialize(value));
} else {
const value = new asn1X509.CRLDistributionPoints(args[0]);
super(asn1X509.id_ce_cRLDistributionPoints, args[1], AsnConvert.serialize(value));
Expand All @@ -64,7 +62,6 @@ export class CRLDistributionPointsExtension extends Extension {
public override toTextObject(): TextObject {
const obj = this.toTextObjectWithoutValue();


obj["Distribution Point"] = this.distributionPoints.map(dp => {
const dpObj: any = {};
if (dp.distributionPoint) {
Expand Down
2 changes: 1 addition & 1 deletion test/extensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ describe("Extensions", () => {
context("create", () => {
it("should create an instance from an array of URLs", () => {
const urls = ["http://example.com"];
const ext = CRLDistributionPointsExtension.create(urls);
const ext = new CRLDistributionPointsExtension(urls);
assert.strictEqual(ext.toString("text"), [
"CRL Distribution Points:",
" Distribution Point:",
Expand Down

0 comments on commit bac1a01

Please sign in to comment.