def _order_node_iyo_see(app_user, node_order_id, pdf_url): iyo_username = get_iyo_username(app_user) organization_id = get_iyo_organization_id() iyo_see_doc = IYOSeeDocumentView(username=iyo_username, globalid=organization_id, uniqueid=u'Zero-Node order %s' % NodeOrder.create_human_readable_id(node_order_id), version=1, category=u'Terms and conditions', link=pdf_url, content_type=u'application/pdf', markdown_short_description=u'Terms and conditions for ordering a Zero-Node', markdown_full_description=u'Terms and conditions for ordering a Zero-Node') logging.debug('Creating IYO SEE document: %s', iyo_see_doc) iyo_see_doc = create_see_document(iyo_username, iyo_see_doc) attachment_name = u' - '.join([iyo_see_doc.uniqueid, iyo_see_doc.category]) def trans(): order = get_node_order(node_order_id) order.tos_iyo_see_id = iyo_see_doc.uniqueid order.put() deferred.defer(_create_quotation, app_user, node_order_id, pdf_url, attachment_name, _transactional=True) ndb.transaction(trans)
def update_grant(username, oldgrant, newgrant): client = get_itsyouonline_client() data = UpdateGrantBody(username=convert_to_str(username), oldgrant=oldgrant, newgrant=newgrant) return client.organizations.UpdateUserGrant(data, get_iyo_organization_id())
def sign_see_document(username, iyo_see_id, sign_result, user_detail): # type: (unicode, unicode, SignWidgetResultTO, UserDetailsTO) -> None public_key = sign_result.public_key.public_key iyo_organization_id = get_iyo_organization_id() logging.debug('Getting IYO SEE document %s', iyo_see_id) doc = get_see_document(iyo_organization_id, username, iyo_see_id) doc_view = IYOSeeDocumentView(username=doc.username, globalid=doc.globalid, uniqueid=doc.uniqueid, **serialize_complex_value( doc.versions[-1], IYOSeeDocumenVersion, False)) doc_view.signature = sign_result.payload_signature keystore_label = get_publickey_label(public_key, user_detail) if not keystore_label: raise Exception( 'Could not find keystore label for user %s and public key %s' % (user_detail, public_key)) doc_view.keystore_label = keystore_label logging.debug('Signing IYO SEE document') try: _sign_see_document(iyo_organization_id, username, doc_view) except HTTPError as e: # Already signed, ignore if e.response.status_code != httplib.CONFLICT: raise e
def store_public_key(user_detail): # type: (UserDetailsTO) -> None logging.info('Storing %s key in IYO for user %s:%s', KEY_NAME, user_detail.email, user_detail.app_id) for rt_key in user_detail.public_keys: if rt_key.name == KEY_NAME and rt_key.algorithm == KEY_ALGORITHM: break else: raise PermanentTaskFailure('No key with name "%s" and algorithm "%s" in %s' % (KEY_NAME, KEY_ALGORITHM, repr(user_detail))) username = get_iyo_username(user_detail) keys = get_keystore(username) if any(True for iyo_key in keys if iyo_key.key == rt_key.public_key): logging.info('No new key to store starting with name "%s" and algorithm "%s" in %s', KEY_NAME, KEY_ALGORITHM, repr(user_detail)) return used_labels = [key.label for key in keys] label = KEY_NAME suffix = 2 while label in used_labels: label = u'%s %d' % (KEY_NAME, suffix) suffix += 1 organization_id = get_iyo_organization_id() key = IYOKeyStoreKey(key=rt_key.public_key, username=username, globalid=organization_id, label=label) key.keydata = IYOKeyStoreKeyData(comment=u'ThreeFold app', algorithm=rt_key.algorithm) key.keydata.timestamp = MISSING # Must be missing, else we get bad request since it can't be null result = create_keystore_key(username, key) # We cache the public key - label mapping here so we don't have to go to itsyou.online every time mapping_key = PublicKeyMapping.create_key(result.key, user_detail.email) mapping = PublicKeyMapping(key=mapping_key) mapping.label = result.label mapping.put()
def api_iyo_see_detail(params, user_detail): try: iyo_organization_id = get_iyo_organization_id() iyo_username = get_iyo_username(user_detail) return get_see_document(iyo_organization_id, iyo_username, params['uniqueid'], u'all') except: logging.error('iyo.see.detail exception occurred', exc_info=True) raise ApiCallException(u'Could not load ThreeFold document details. Please try again later.')
def get_investment_agreement_details(agreement_id): agreement = get_investment_agreement(agreement_id) if agreement.iyo_see_id: iyo_organization_id = get_iyo_organization_id() username = get_iyo_username(agreement.app_user) see_document = get_see_document(iyo_organization_id, username, agreement.iyo_see_id) else: see_document = None return InvestmentAgreementDetailsTO.from_model(agreement, see_document)
def get_node_order_details(order_id): # type: (long) -> NodeOrderDetailsTO node_order = get_node_order(order_id) if node_order.tos_iyo_see_id: iyo_organization_id = get_iyo_organization_id() username = get_iyo_username(node_order.app_user) see_document = get_see_document(iyo_organization_id, username, node_order.tos_iyo_see_id) else: see_document = None return NodeOrderDetailsTO.from_model(node_order, see_document)
def _create_investment_agreement_iyo_see_doc(agreement_key, app_user, pdf_url, content_type='application/pdf', send_sign_message=True): # type: (ndb.Key, users.User, unicode) -> None iyo_username = get_iyo_username(app_user) doc_id = u'Internal Token Offering %s' % agreement_key.id() doc_category = u'Purchase Agreement' iyo_see_doc = IYOSeeDocumentView( username=iyo_username, globalid=get_iyo_organization_id(), uniqueid=doc_id, version=1, category=doc_category, link=pdf_url, content_type=content_type, markdown_short_description= u'Internal Token Offering - Purchase Agreement', markdown_full_description= u'Internal Token Offering - Purchase Agreement') logging.debug('Creating IYO SEE document: %s', iyo_see_doc) try: create_see_document(iyo_username, iyo_see_doc) except HTTPError as e: if e.response.status_code != httplib.CONFLICT: raise e def trans(): agreement = agreement_key.get() agreement.iyo_see_id = doc_id agreement.put() if send_sign_message: attachment_name = u' - '.join([doc_id, doc_category]) deferred.defer(_send_ito_agreement_sign_message, agreement.key, app_user, pdf_url, attachment_name, _transactional=True) ndb.transaction(trans)
def create_see_document(document_id, category, description, username, pdf_url, content_type=u'application/pdf'): iyo_see_doc = IYOSeeDocumentView(username=username, globalid=get_iyo_organization_id(), uniqueid=document_id, version=1, category=category, link=pdf_url, content_type=content_type, markdown_short_description=description, markdown_full_description=description) logging.debug('Creating IYO SEE document: %s', iyo_see_doc) try: _create_see_document(username, iyo_see_doc) except HTTPError as e: if e.response.status_code != httplib.CONFLICT: raise e
def list_all_users_with_grant(grant): client = get_itsyouonline_client() return client.organizations.ListUsersWithGrant(grant, get_iyo_organization_id())
def investment_agreement_signed(status, form_result, answer_id, member, message_key, tag, received_timestamp, acked_timestamp, parent_message_key, result_key, service_identity, user_details): """ Args: status (int) form_result (FormResultTO) answer_id (unicode) member (unicode) message_key (unicode) tag (unicode) received_timestamp (int) acked_timestamp (int) parent_message_key (unicode) result_key (unicode) service_identity (unicode) user_details(list[UserDetailsTO]) Returns: FormAcknowledgedCallbackResultTO """ try: user_detail = user_details[0] tag_dict = json.loads(tag) agreement = InvestmentAgreement.create_key( tag_dict['agreement_id']).get() # type: InvestmentAgreement if answer_id != FormTO.POSITIVE: logging.info('Investment agreement was canceled') agreement.status = InvestmentAgreement.STATUS_CANCELED agreement.cancel_time = now() agreement.put() return None logging.info('Received signature for Investment Agreement') sign_result = form_result.result.get_value() assert isinstance(sign_result, SignWidgetResultTO) payload_signature = sign_result.payload_signature iyo_organization_id = get_iyo_organization_id() iyo_username = get_iyo_username(user_detail) logging.debug('Getting IYO SEE document %s', agreement.iyo_see_id) doc = get_see_document(iyo_organization_id, iyo_username, agreement.iyo_see_id) doc_view = IYOSeeDocumentView(username=doc.username, globalid=doc.globalid, uniqueid=doc.uniqueid, **serialize_complex_value( doc.versions[-1], IYOSeeDocumenVersion, False)) doc_view.signature = payload_signature keystore_label = get_publickey_label(sign_result.public_key.public_key, user_detail) if not keystore_label: return create_error_message(FormAcknowledgedCallbackResultTO()) doc_view.keystore_label = keystore_label logging.debug('Signing IYO SEE document') sign_see_document(iyo_organization_id, iyo_username, doc_view) logging.debug('Storing signature in DB') agreement.populate(status=InvestmentAgreement.STATUS_SIGNED, signature=payload_signature, sign_time=now()) agreement.put_async() deferred.defer(add_user_to_role, user_detail, RogerthatRoles.INVESTOR) intercom_tags = get_intercom_tags_for_investment(agreement) if intercom_tags: for i_tag in intercom_tags: deferred.defer(intercom_helpers.tag_intercom_users, i_tag, [iyo_username]) deferred.defer(update_investor_progress, user_detail.email, user_detail.app_id, INVESTMENT_TODO_MAPPING[agreement.status]) deferred.defer(_inform_support_of_new_investment, iyo_username, agreement.id, agreement.token_count_float) logging.debug('Sending confirmation message') prefix_message = u'Thank you. We successfully received your digital signature.' \ u' We have stored a copy of this agreement in your ThreeFold Documents.' \ u' We will contact you again when we have received your payment.' \ u' Thanks again for your purchase and your support of the ThreeFold Foundation!' \ u'\n\nWe would like to take this opportunity to remind you once again to keep a back-up of' \ u' your wallet in a safe place, by writing down the 29 words that can be used to restore it' \ u' to a different device.' \ u' As usual, if you have any questions, don\'t hesitate to contact us.\n\n' msg = u'%sReference: %s' % (prefix_message, agreement.reference) deferred.defer(send_payment_instructions, agreement.app_user, agreement.id, prefix_message) message = MessageCallbackResultTypeTO() message.alert_flags = Message.ALERT_FLAG_VIBRATE message.answers = [] message.branding = get_main_branding_hash() message.dismiss_button_ui_flags = 0 message.flags = Message.FLAG_ALLOW_DISMISS | Message.FLAG_AUTO_LOCK message.message = msg message.step_id = u'investment_agreement_accepted' message.tag = None result = FormAcknowledgedCallbackResultTO() result.type = TYPE_MESSAGE result.value = message return result except: logging.exception('An unexpected error occurred') return create_error_message(FormAcknowledgedCallbackResultTO())
def remove_all_grants(username): client = get_itsyouonline_client() return client.organizations.DeleteAllUserGrants(convert_to_str(username), get_iyo_organization_id())
def remove_grant(username, grant): client = get_itsyouonline_client() return client.organizations.DeleteUserGrant(grant, convert_to_str(username), get_iyo_organization_id())
def order_node_signed(status, form_result, answer_id, member, message_key, tag, received_timestamp, acked_timestamp, parent_message_key, result_key, service_identity, user_details): """ Args: status (int) form_result (FormResultTO) answer_id (unicode) member (unicode) message_key (unicode) tag (unicode) received_timestamp (int) acked_timestamp (int) parent_message_key (unicode) result_key (unicode) service_identity (unicode) user_details(list[UserDetailsTO]) Returns: FormAcknowledgedCallbackResultTO """ try: user_detail = user_details[0] tag_dict = json.loads(tag) order = get_node_order(tag_dict['order_id']) if answer_id != FormTO.POSITIVE: logging.info('Zero-Node order was canceled') deferred.defer(_cancel_quotation, order.id) return None logging.info('Received signature for Zero-Node order') sign_result = form_result.result.get_value() assert isinstance(sign_result, SignWidgetResultTO) payload_signature = sign_result.payload_signature iyo_organization_id = get_iyo_organization_id() iyo_username = get_iyo_username(user_detail) logging.debug('Getting IYO SEE document %s', order.tos_iyo_see_id) doc = get_see_document(iyo_organization_id, iyo_username, order.tos_iyo_see_id) doc_view = IYOSeeDocumentView(username=doc.username, globalid=doc.globalid, uniqueid=doc.uniqueid, **serialize_complex_value(doc.versions[-1], IYOSeeDocumenVersion, False)) doc_view.signature = payload_signature keystore_label = get_publickey_label(sign_result.public_key.public_key, user_detail) if not keystore_label: return create_error_message(FormAcknowledgedCallbackResultTO()) doc_view.keystore_label = keystore_label logging.debug('Signing IYO SEE document') sign_see_document(iyo_organization_id, iyo_username, doc_view) logging.debug('Storing signature in DB') order.populate(status=NodeOrderStatus.SIGNED, signature=payload_signature, sign_time=now()) order.put() # TODO: send mail to TF support deferred.defer(add_user_to_role, user_detail, RogerthatRoles.HOSTERS) deferred.defer(update_hoster_progress, user_detail.email, user_detail.app_id, HosterSteps.FLOW_SIGN) intercom_tags = get_intercom_tags_for_node_order(order) for intercom_tag in intercom_tags: deferred.defer(tag_intercom_users, intercom_tag, [iyo_username]) logging.debug('Sending confirmation message') message = MessageCallbackResultTypeTO() message.alert_flags = Message.ALERT_FLAG_VIBRATE message.answers = [] message.branding = get_main_branding_hash() message.dismiss_button_ui_flags = 0 message.flags = Message.FLAG_ALLOW_DISMISS | Message.FLAG_AUTO_LOCK message.message = u'Thank you. We successfully received your digital signature.' \ u' We have stored a copy of this agreement in your ThreeFold Documents.\n\n' \ u'Your order with ID "%s" has been placed successfully.\n' % order.human_readable_id message.step_id = u'order_completed' message.tag = None result = FormAcknowledgedCallbackResultTO() result.type = TYPE_MESSAGE result.value = message return result except: logging.exception('An unexpected error occurred') return create_error_message(FormAcknowledgedCallbackResultTO())
def add_grant(username, grant): client = get_itsyouonline_client() data = CreateGrantBody(username=convert_to_str(username), grant=grant) return client.organizations.CreateUserGrant(data, get_iyo_organization_id())
def list_grants(username): client = get_itsyouonline_client() return client.organizations.GetUserGrants(convert_to_str(username), get_iyo_organization_id())
def has_grant(client, username, grant): assert isinstance(client, itsyouonline.Client) grants = client.organizations.GetUserGrants( convert_to_str(username), get_iyo_organization_id()).json() return grants and grant in grants