Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 | 1x 1x 1x 1x 1x 6997x | import {Identity, Wallet} from 'fabric-network';
import {LogChainConfigs} from '../../../configs';
import {AnyObject} from '../../../types';
import {AppUtil} from '../app-util';
const jsrsasign = require('jsrsasign');
const {X509, zulutodate} = jsrsasign;
/**
* Fabric CA Service
*/
export class CAService {
// default caller is org1 identity.
public caller: AnyObject = {
identifier: process.env.HLF_ORG1_ADMIN_IDENTITY,
company_id: LogChainConfigs.LogChainCompanyId,
};
constructor() {}
/**
* Registers and enroll user
* @param org
* @param identifier
* @param affiliation
*/
async registerAndEnrollUser(
identifier: string,
affiliation?: string,
caller?: AnyObject,
) {
if (!caller) {
caller = this.caller;
}
const configs = AppUtil.buildOrgConfigsBasedOnCompany(caller.company_id);
console.log(
`\n--> Fabric client user & Gateway init: Using ${configs.org} identity`,
);
const cpp = await AppUtil.buildCCPOrg(
configs.org,
configs.connectionConfigFile,
);
affiliation = configs.org.split('.').shift() ?? 'org1';
const { caClient, adminUser } = await AppUtil.buildCAClient(cpp, configs.caDomainOrg);
const wallet = await AppUtil.buildWallet();
// Check to see if we've already enrolled the user
const user_identity = await wallet.get(identifier);
if (user_identity) {
throw new Error(
`An identity for the user ${identifier} already exists in the wallet`,
);
}
// Must use an admin to register a new user
// const admin_identity = await wallet.get(configs.caAdminIdentity);
// if (!admin_identity) {
// throw new Error(
// 'An identity for the admin user does not exist in the wallet',
// );
// }
// build a user object for authenticating with the CA
// const provider = wallet
// .getProviderRegistry()
// .getProvider(admin_identity.type);
// const adminUser = await provider.getUserContext(
// admin_identity,
// configs.caAdminIdentity,
// );
// Register the user, enroll the user, and import the new identity into the wallet.
// if affiliation is specified by client, the affiliation value must be configured in CA
const secret = await caClient.register(
{
affiliation: affiliation,
enrollmentID: identifier,
role: 'client',
},
adminUser,
);
const enrollment = await caClient.enroll({
enrollmentID: identifier,
enrollmentSecret: secret,
});
const x509Identity = {
credentials: {
certificate: enrollment.certificate,
privateKey: enrollment.key.toBytes(),
},
mspId: configs.mspOrg,
type: 'X.509',
};
await wallet.put(identifier, x509Identity);
console.log(
`Successfully registered and enrolled user ${identifier} and imported it into the wallet`,
);
}
/**
* Get CA Identity by identifier of the user.
* @returns Promise<Identity>
*/
async getIdentityOfCaller(
identifier: string,
): Promise<{wallet: Wallet; identity: Identity}> {
const wallet = await AppUtil.buildWallet();
// get user identity
const identity = await wallet.get(identifier);
if (!identity) {
throw new Error(
`An identity for the user ${identifier} not exists in the wallet.`,
);
}
return {wallet, identity};
}
/**
* Get information from identity.
* @param identifier The identifier of the user.
*/
async getInfoFromIdentity(identifier: string) {
const wallet = await AppUtil.buildWallet();
// get user identity
const identity = await wallet.get(identifier);
if (!identity) {
throw new Error(
`An identity for the user ${identifier} not exists in the wallet.`,
);
}
const provider = wallet.getProviderRegistry().getProvider(identity.type);
const user = await provider.getUserContext(identity, identifier);
try {
const new_x509 = new X509(
JSON.parse(user.toString()).enrollment.identity.certificate,
);
const not_before = new_x509.getNotBefore();
const not_after = new_x509.getNotAfter();
const issuer = new_x509.getIssuer();
const subject = new_x509.getSubject();
const m = issuer.str.match(/CN=.+[^/]/);
const n = subject.str.match(/CN=.+[^/]/);
return {
valid_from: zulutodate(not_before),
valid_to: zulutodate(not_after),
issuer_cn: m[0].substring(3),
subject_cn: n[0].substring(3),
};
} catch (err) {
console.log(err);
}
}
/**
* Revoke an identity from the wallet.
* @param identifier The user id.
* @param reason The reason when revoke an identity
*/
async revokeUser(identifier: string, reason?: string, caller?: AnyObject) {
if (!caller) {
caller = this.caller;
}
const {
caDomainOrg,
// caAdminIdentity,
org,
connectionConfigFile,
} = AppUtil.buildOrgConfigsBasedOnCompany(caller.company_id);
const cpp = await AppUtil.buildCCPOrg(org, connectionConfigFile);
const { caClient, adminUser } = await AppUtil.buildCAClient(cpp, caDomainOrg);
const wallet = await AppUtil.buildWallet();
// Must use an admin to revoke an existing identity
// const adminIdentity = await wallet.get(caAdminIdentity);
// if (!adminIdentity) {
// throw new Error(
// 'An identity for the admin user does not exist in the wallet',
// );
// }
// build a user object for authenticating with the CA
// const provider = wallet
// .getProviderRegistry()
// .getProvider(adminIdentity.type);
// const adminUser = await provider.getUserContext(
// adminIdentity,
// caAdminIdentity,
// );
await caClient.revoke(
{
enrollmentID: identifier,
reason: reason,
},
adminUser,
);
await wallet.remove(identifier);
}
}
|