Skip to content

Commit

Permalink
keep previous constructor with new structure
Browse files Browse the repository at this point in the history
  • Loading branch information
JhontSouth committed Dec 7, 2023
1 parent 9043afa commit 4927b70
Showing 1 changed file with 33 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,16 @@ export class CertificateServiceClientCredentialsFactory extends ServiceClientCre
* @param certificateThumbprint A hex encoded thumbprint of the certificate.
* @param certificatePrivateKey A PEM encoded certificate private key.
* @param tenantId Optional. The oauth token tenant.
* @param x5c Optional. Enables application developers to achieve easy certificates roll-over in Azure AD:
* set this parameter to send the public certificate (BEGIN CERTIFICATE) to Azure AD, so that Azure AD can use it to validate the subject name based on a trusted issuer policy.
*/
constructor(appId: string, certificateThumbprint: string, certificatePrivateKey: string, tenantId?: string);
constructor(
appId: string,
certificateThumbprint: string,
certificatePrivateKey: string,
tenantId?: string,
x5c?: string
);

/**
* Initializes a new instance of the CertificateServiceClientCredentialsFactory class.
Expand All @@ -48,24 +56,39 @@ export class CertificateServiceClientCredentialsFactory extends ServiceClientCre
/**
* @internal
*/
constructor(appId: string, certificateThumbprintOrx5c: string, certificatePrivateKey: string, tenantId?: string) {
constructor(
appId: string,
certificateThumbprintOrx5c: string,
certificatePrivateKey: string,
tenantId?: string,
x5c?: string
) {
super();

ok(appId?.trim(), 'CertificateServiceClientCredentialsFactory.constructor(): missing appId.');
ok(
certificateThumbprintOrx5c?.trim(),
'CertificateServiceClientCredentialsFactory.constructor(): missing certificateThumbprint or x5c value.'
);
ok(
certificatePrivateKey?.trim(),
'CertificateServiceClientCredentialsFactory.constructor(): missing certificatePrivateKey.'
);

if (certificateThumbprintOrx5c.includes('-----BEGIN CERTIFICATE-----')) {
this.x5c = certificateThumbprintOrx5c;
ok(
certificateThumbprintOrx5c.trim(),
'CertificateServiceClientCredentialsFactory.constructor(): missing x5c.'
);
} else {
ok(
certificateThumbprintOrx5c.trim(),
'CertificateServiceClientCredentialsFactory.constructor(): missing certificateThumbprint.'
);
this.certificateThumbprint = certificateThumbprintOrx5c;
this.x5c = x5c;
}

this.appId = appId;
this.certificateThumbprint = certificateThumbprintOrx5c?.length <= 40 ? certificateThumbprintOrx5c : undefined;
this.certificatePrivateKey = certificatePrivateKey;
this.tenantId = tenantId;
this.x5c = certificateThumbprintOrx5c?.length > 40 ? certificateThumbprintOrx5c : undefined;
}

/**
Expand All @@ -79,13 +102,8 @@ export class CertificateServiceClientCredentialsFactory extends ServiceClientCre
* @param cert Value with the certificate content.
* @returns The thumbprint value calculated from the cert content.
*/
async getThumbprint(cert) {
const certString = Buffer.from(cert).toString();
const begin = certString.lastIndexOf('-----BEGIN CERTIFICATE-----');
const end = certString.lastIndexOf('-----END CERTIFICATE-----') + '-----END CERTIFICATE-----'.length;
const certificate = certString.slice(begin, end);

const fingerprintResponse = await openssl('x509', Buffer.from(certificate), { fingerprint: true, noout: true });
private async getThumbprint(cert) {
const fingerprintResponse = await openssl('x509', Buffer.from(cert), { fingerprint: true, noout: true });
return Buffer.from(fingerprintResponse)
.toString()
.replace(/^.*Fingerprint=/, '')
Expand Down

0 comments on commit 4927b70

Please sign in to comment.