/** * Internal method to check the security context * @param {SecurityContext} securityContext - The user's security context * @throws {SecurityException} if the user context is invalid */ static securityCheck(securityContext) { if (Util.isNull(securityContext)) { throw new SecurityException(Globalize.formatMessage('util-securitycheck-novalidcontext')); } else if (!(securityContext instanceof HFCSecurityContext)) { throw new SecurityException(Globalize.formatMessage('util-securitycheck-novalidcontext')); } }
/** * Login as a participant on the business network. * @param {string} enrollmentID The enrollment ID of the participant. * @param {string} enrollmentSecret The enrollment secret of the participant. * @return {Promise} A promise that is resolved with a {@link SecurityContext} * object representing the logged in participant, or rejected with a login error. */ login(enrollmentID, enrollmentSecret) { const self = this; LOG.info('login', 'Login attempt', self.businessNetworkIdentifier ); if (!enrollmentID) { throw new Error(Globalize.formatMessage('concerto-login-noenrollmentid')); } else if (!enrollmentSecret) { throw new Error(Globalize.formatMessage('concerto-login-noenrollmentsecret')); } return new Promise((resolve, reject) => { self.chain.enroll(enrollmentID, enrollmentSecret, (error, enrolledMember) => { if (error) { LOG.warn('login', 'Failed login', self.getIdentifier()); return reject(error); } self.chain.setRegistrar(enrolledMember); let result = new HFCSecurityContext(this); result.setUser(enrollmentID); result.setEnrolledMember(enrolledMember); result.setEventHub(self.chain.getEventHub()); LOG.info('login', 'Successful login', self.getIdentifier()); resolve(result); }); }) .then((securityContext) => { return self.getConnectionManager().getConnectionProfileManager().getConnectionProfileStore().load(self.connectionProfile) .then((profile) => { LOG.info('login', 'Loaded profile', JSON.stringify(profile) ); // This will not be set for admin connections. if (self.businessNetworkIdentifier) { let chaincodeID = null; if (profile.networks) { chaincodeID = profile.networks[self.businessNetworkIdentifier]; LOG.info('login', 'Loaded chaincode id for network ' + self.businessNetworkIdentifier, chaincodeID ); } if (chaincodeID) { securityContext.setChaincodeID(chaincodeID); } else { const msg = 'Failed to set chaincode id on security context. Check that the connection profile ' + self.connectionProfile + ' defines the network ' + self.businessNetworkIdentifier; LOG.error('login', msg, self.getIdentifier()); throw new Error(msg); } } }) .then(() => { this.subscribeToEvents(securityContext.getChaincodeID()); return securityContext; }); }); }