+ messages.error(request, 'Account error: You need an account in myslice platform to perform this action')
+ return HttpResponseRedirect("/portal/account/")
+
+ elif 'dl_pubkey' in request.POST:
+ for account_detail in account_details:
+ for platform_detail in platform_details:
+ if platform_detail['platform_id'] == account_detail['platform_id']:
+ if 'myslice' in platform_detail['platform']:
+ account_config = json.loads(account_detail['config'])
+ public_key = account_config['user_public_key']
+ response = HttpResponse(public_key, content_type='text/plain')
+ response['Content-Disposition'] = 'attachment; filename="pubkey.txt"'
+ return response
+ break
+ else:
+ messages.error(request, 'Account error: You need an account in myslice platform to perform this action')
+ return HttpResponseRedirect("/portal/account/")
+
+ elif 'dl_pkey' in request.POST:
+ for account_detail in account_details:
+ for platform_detail in platform_details:
+ if platform_detail['platform_id'] == account_detail['platform_id']:
+ if 'myslice' in platform_detail['platform']:
+ account_config = json.loads(account_detail['config'])
+ if 'user_private_key' in account_config:
+ private_key = account_config['user_private_key']
+ response = HttpResponse(private_key, content_type='text/plain')
+ response['Content-Disposition'] = 'attachment; filename="privkey.txt"'
+ return response
+ else:
+ messages.error(request, 'Download error: Private key is not stored in the server')
+ return HttpResponseRedirect("/portal/account/")
+
+ else:
+ messages.error(request, 'Account error: You need an account in myslice platform to perform this action')
+ return HttpResponseRedirect("/portal/account/")
+
+ elif 'delete' in request.POST:
+ for account_detail in account_details:
+ for platform_detail in platform_details:
+ if platform_detail['platform_id'] == account_detail['platform_id']:
+ if 'myslice' in platform_detail['platform']:
+ account_config = json.loads(account_detail['config'])
+ if 'user_private_key' in account_config:
+ for key in account_config.keys():
+ if key == 'user_private_key':
+ del account_config[key]
+
+ updated_config = json.dumps(account_config)
+ user_params = { 'config': updated_config, 'auth_type':'user'}
+ manifold_update_account(request, user_id, user_params)
+ messages.success(request, 'Private Key deleted. You need to delegate credentials manually once it expires.')
+ messages.success(request, 'Once your credentials expire, Please delegate manually using SFA: http://trac.myslice.info/wiki/DelegatingCredentials')
+ return HttpResponseRedirect("/portal/account/")
+ else:
+ messages.error(request, 'Delete error: Private key is not stored in the server')
+ return HttpResponseRedirect("/portal/account/")
+
+ else:
+ messages.error(request, 'Account error: You need an account in myslice platform to perform this action')
+ return HttpResponseRedirect("/portal/account/")
+
+ # download identity for jfed
+ elif 'dl_identity' in request.POST:
+ for account_detail in account_details:
+ for platform_detail in platform_details:
+ if platform_detail['platform_id'] == account_detail['platform_id']:
+ if 'myslice' in platform_detail['platform']:
+ account_config = json.loads(account_detail['config'])
+ if 'user_private_key' in account_config:
+ private_key = account_config['user_private_key']
+ user_hrn = account_config.get('user_hrn','N/A')
+ registry = 'http://sfa-fed4fire.pl.sophia.inria.fr:12345/'
+ jfed_identity = user_hrn + '\n' + registry + '\n' + private_key
+ response = HttpResponse(jfed_identity, content_type='text/plain')
+ response['Content-Disposition'] = 'attachment; filename="jfed_identity.txt"'
+ return response
+ else:
+ messages.error(request, 'Download error: Private key is not stored in the server')
+ return HttpResponseRedirect("/portal/account/")
+
+ else:
+ messages.error(request, 'Account error: You need an account in myslice platform to perform this action')
+ return HttpResponseRedirect("/portal/account/")
+
+ # Download sfi_config
+ elif 'dl_sfi_config' in request.POST:
+ platform_detail = get_myslice_platform(request)
+ platform_config = json.loads(platform_detail['config'])
+ account_detail = get_myslice_account(request)
+ account_config = json.loads(account_detail['config'])
+
+ user_hrn = account_config.get('user_hrn','N/A')
+ t_user_hrn = user_hrn.split('.')
+ authority_hrn = t_user_hrn[0] + '.' + t_user_hrn[1]
+ import socket
+ hostname = socket.gethostbyaddr(socket.gethostname())[0]
+ registry = platform_config.get('registry','N/A')
+ admin_user = platform_config.get('user','N/A')
+ if 'localhost' in registry:
+ port = registry.split(':')[-1:][0]
+ registry = "http://" + hostname +':'+ port
+ manifold_host = ConfigEngine().manifold_url()
+ if 'localhost' in manifold_host:
+ manifold_host = manifold_host.replace('localhost',hostname)
+ sfi_config = '[sfi]\n'
+ sfi_config += 'auth = '+ authority_hrn +'\n'
+ sfi_config += 'user = '+ user_hrn +'\n'
+ sfi_config += 'registry = '+ registry +'\n'
+ sfi_config += 'sm = http://sfa3.planet-lab.eu:12346/\n\n'
+ sfi_config += '[myslice]\n'
+ sfi_config += 'backend = '+ manifold_host +'\n'
+ sfi_config += 'delegate = '+ admin_user +'\n'
+ sfi_config += 'platform = myslice\n'
+ sfi_config += 'username = '+ user_email +'\n'
+ response = HttpResponse(sfi_config, content_type='text/plain')
+ response['Content-Disposition'] = 'attachment; filename="sfi_config"'
+ return response
+
+ #clear all creds
+ elif 'clear_cred' in request.POST:
+ try:
+ result = clear_user_creds(request, user_email)
+ if result is not None:
+ messages.success(request, 'All Credentials cleared')
+ else:
+ messages.error(request, 'Delete error: Credentials are not stored in the server')
+ except Exception,e:
+ print "Exception in accountview.py in clear_user_creds %s" % e
+ messages.error(request, 'Account error: You need an account in myslice platform to perform this action')
+ return HttpResponseRedirect("/portal/account/")
+
+ # Download delegated_user_cred
+ elif 'dl_user_cred' in request.POST:
+ if 'delegated_user_credential' in account_config:
+ user_cred = account_config['delegated_user_credential']
+ response = HttpResponse(user_cred, content_type='text/plain')
+ response['Content-Disposition'] = 'attachment; filename="user_cred.txt"'
+ return response
+ else:
+ messages.error(request, 'Download error: User credential is not stored in the server')
+ return HttpResponseRedirect("/portal/account/")
+
+ # Download user_cert
+ elif 'dl_user_cert' in request.POST:
+ if 'user_credential' in account_config:
+ user_cred = account_config['user_credential']
+ obj_cred = Credential(string=user_cred)
+ obj_gid = obj_cred.get_gid_object()
+ str_cert = obj_gid.save_to_string()
+ response = HttpResponse(str_cert, content_type='text/plain')
+ response['Content-Disposition'] = 'attachment; filename="user_certificate.pem"'
+ return response
+
+ elif 'delegated_user_credential' in account_config:
+ user_cred = account_config['delegated_user_credential']
+ obj_cred = Credential(string=user_cred)
+ obj_gid = obj_cred.get_gid_object()
+ str_cert = obj_gid.save_to_string()
+ response = HttpResponse(str_cert, content_type='text/plain')
+ response['Content-Disposition'] = 'attachment; filename="user_certificate.pem"'
+ return response
+ else:
+ messages.error(request, 'Download error: User credential is not stored in the server')
+ return HttpResponseRedirect("/portal/account/")
+
+ # Download user p12 = private_key + Certificate
+ elif 'dl_user_p12' in request.POST:
+ if 'user_credential' in account_config and 'user_private_key' in account_config:
+ user_cred = account_config['user_credential']
+ obj_cred = Credential(string=user_cred)
+ obj_gid = obj_cred.get_gid_object()
+ str_cert = obj_gid.save_to_string()
+ cert = crypto.load_certificate(crypto.FILETYPE_PEM, str_cert)
+
+ user_private_key = account_config['user_private_key'].encode('ascii')
+ pkey = crypto.load_privatekey(crypto.FILETYPE_PEM, user_private_key)
+
+ p12 = crypto.PKCS12()
+ p12.set_privatekey(pkey)
+ p12.set_certificate(cert)
+ pkcs12 = p12.export()
+
+ response = HttpResponse(pkcs12, content_type='text/plain')
+ response['Content-Disposition'] = 'attachment; filename="user_pkcs.p12"'
+ return response
+
+ elif 'delegated_user_credential' in account_config and 'user_private_key' in account_config:
+ user_cred = account_config['delegated_user_credential']
+ obj_cred = Credential(string=user_cred)
+ obj_gid = obj_cred.get_gid_object()
+ str_cert = obj_gid.save_to_string()
+ cert = crypto.load_certificate(crypto.FILETYPE_PEM, str_cert)
+
+ user_private_key = account_config['user_private_key'].encode('ascii')
+ pkey = crypto.load_privatekey(crypto.FILETYPE_PEM, user_private_key)
+
+ p12 = crypto.PKCS12()
+ p12.set_privatekey(pkey)
+ p12.set_certificate(cert)
+ pkcs12 = p12.export()
+
+ response = HttpResponse(pkcs12, content_type='text/plain')
+ response['Content-Disposition'] = 'attachment; filename="user_pkcs.p12"'
+ return response
+ else:
+ messages.error(request, 'Download error: User private key or credential is not stored in the server')
+ return HttpResponseRedirect("/portal/account/")
+
+
+