-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser.js
179 lines (156 loc) · 6.53 KB
/
user.js
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
'use strict';
const helper = require('./helper');
/**
* Register a new user into organization's CA
* @param userName new username to create. Can't be reserved word like admin
* @param secret a valid secret to use
* @param organization new user's organization. Must exists.
*/
async function registerUser (userName, secret, organization, affiliation, fullname, email) {
// parameter validation
if (!userName || !secret || !organization || !affiliation || !fullname || !email) {
throw new Error ('Can\'t register user. UserName, secret, organization, affiliation, fullName and email are required parameters');
}
// we get the clients
var fabricClient = await helper.getFabricClient(organization);
const caClient = fabricClient.getCertificateAuthority();
// we register organization's admin
var admin = await registerAdmin(organization);
// affiliation name is MSPId.Affiliation
const affiliationName = fabricClient.getMspid() + "." + affiliation;
try {
// we will create the affiliation
await createAffiliation(caClient, affiliationName, admin);
} catch (e) {
// do nothing, affiliation probably already created
}
// lets register the new user
try {
await caClient.register({
enrollmentID: userName,
enrollmentSecret: secret,
role: 'client',
affiliation: affiliationName,
maxEnrollments: 0,
attr_reqs: [
{name: "hf.Registrar.Roles"},
{name: "hf.Registrar.Attributes"},
{name: "hf.AffiliationMgr"}
],
attrs: [
{name: "email",
value: email,
ecert: true},
{name: "fullName",
value: fullname,
ecert: true}
]
},
admin);
let response = `User ${userName} registered successfully.`;
return response;
} catch (e) {
throw new Error ('There was an error registering user ' + userName + '.' + e);
}
}
/**
* Enrolls the provided user in organization's CA.
* @param userName existing valid user
* @param secret created during registration
* @param organization that the user belongs to
* @returns {Promise<User>} the user context if enrolled successfully.
*/
async function enrollUser (userName, secret, organization) {
// parameter validation
if (!userName || !organization) {
throw new Error ('Can\'t register user. UserName and Organization are required parameters');
}
// we get the clients
var fabricClient = await helper.getFabricClient(organization);
const caClient = fabricClient.getCertificateAuthority();
try {
let user;
if (secret) {
// first enrollment requires password.
// lets enroll admin using registrar credentials
var userEnrollment = await caClient.enroll({
enrollmentID: userName,
enrollmentSecret: secret
})
// we create the user from ca credentials
var caUser = await fabricClient.createUser({
username: userName,
mspid: fabricClient.getMspid(),
cryptoContent: {
privateKeyPEM: userEnrollment.key.toBytes(),
signedCertPEM: userEnrollment.certificate
},
skipPersistence: false
})
const orgUnits = caUser.getIdentity().getOrganizationUnits();
const affiliation = caUser.getAffiliation();
const identity = caUser.getIdentity().serialize().toString('utf8');
user = await fabricClient.setUserContext(caUser, false);
} else {
// for second enrollments we don't use password.
user = await fabricClient.setUserContext({username: userName});
}
// to retrieve affiliation and attributes, we need to get them using an Identity Service with admin
const identityService = caClient.newIdentityService();
const admin = await registerAdmin(organization);
const result = await identityService.getOne(userName, admin);
user = result.result;
return user;
} catch (e) {
throw new Error ('There was an error enrolling user ' + userName + '.' + e);
}
}
/**
* enrolls the admin into the Organization's CA and sets the user context.
* @param organization a string representing the organization to load the connection profile from
* @returns {Promise<Client.User>} the admin user context
*/
async function registerAdmin(organization) {
// we get the client, ca and his registrar data
var fabricClient = await helper.getFabricClient(organization);
let caClient = fabricClient.getCertificateAuthority();
var registrar = caClient.getRegistrar();
// we make sure we have all data
if (!registrar.enrollId || !registrar.secret) {
throw new Error ('Registrar information is not complete. We need both id and secret for admin identity.');
}
try {
// lets enroll admin using registrar credentials
var adminEnrollment = await caClient.enroll({
enrollmentID: registrar.enrollId,
enrollmentSecret: registrar.secret,
attr_reqs: [
{name: "hf.Registrar.Roles"},
{name: "hf.Registrar.Attributes"},
{name: "hf.AffiliationMgr"}
]
})
// we create the user from ca credentials
var adminUser = await fabricClient.createUser({
username: registrar.enrollId,
mspid: fabricClient.getMspid(),
cryptoContent: {
privateKeyPEM: adminEnrollment.key.toBytes(),
signedCertPEM: adminEnrollment.certificate
},
skipPersistence: false
})
adminUser.setAffiliation(organization.toLowerCase());
// we set the admin contect and return it.
return await fabricClient.setUserContext(adminUser);
} catch (e) {
throw new Error ('There was an error registering admin user for organization ' + organization);
}
}
async function createAffiliation(fabricCA, name, admin) {
const affiliationService = fabricCA.newAffiliationService();
const affiliationRequest = {name: name, caname: fabricCA.getName(), force: true};
await affiliationService.create(affiliationRequest,admin)
}
exports.registerUser = registerUser;
exports.enrollUser = enrollUser;