Account: PKCS12 generated in the user account page
authorLoic Baron <loic.baron@lip6.fr>
Wed, 18 Jun 2014 13:43:48 +0000 (08:43 -0500)
committerLoic Baron <loic.baron@lip6.fr>
Wed, 18 Jun 2014 13:43:48 +0000 (08:43 -0500)
portal/accountview.py
portal/templates/account-view.html
portal/templates/fed4fire/fed4fire_account-view.html

index 0f14da7..4f4ecbf 100644 (file)
@@ -1,6 +1,7 @@
 from unfold.loginrequired               import LoginRequiredAutoLogoutView
 #
 from sfa.trust.credential               import Credential
+from sfa.trust.certificate              import Keypair
 #
 from manifold.core.query                import Query
 from manifoldapi.manifoldapi            import execute_query
@@ -17,6 +18,8 @@ from myslice.theme import ThemeView
 
 #
 import json, os, re, itertools
+from OpenSSL import crypto
+from Crypto.PublicKey import RSA
 
 # requires login
 class AccountView(LoginRequiredAutoLogoutView, ThemeView):
@@ -328,7 +331,6 @@ def account_process(request):
             for platform_detail in platform_details:
                 if platform_detail['platform_id'] == account_detail['platform_id']:
                     if 'myslice' in platform_detail['platform']:
-                        from Crypto.PublicKey import RSA
                         private = RSA.generate(1024)
                         private_key = json.dumps(private.exportKey())
                         public  = private.publickey()
@@ -513,7 +515,7 @@ def account_process(request):
             response['Content-Disposition'] = 'attachment; filename="user_cred.txt"'
             return response
         else:
-            messages.error(request, 'Download error: User credential  is not stored in the server')
+            messages.error(request, 'Download error: User credential is not stored in the server')
             return HttpResponseRedirect("/portal/account/")
 
     # Download user_cert
@@ -536,9 +538,54 @@ def account_process(request):
             response['Content-Disposition'] = 'attachment; filename="user_certificate.pem"'
             return response
         else:
-            messages.error(request, 'Download error: User credential  is not stored in the server')
+            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/")
+
+
+
     else:
         messages.info(request, 'Under Construction. Please try again later!')
         return HttpResponseRedirect("/portal/account/")
index 6877a4a..2b8a9ca 100644 (file)
                                                                <span class="glyphicon glyphicon-download"></span> Credential
                                                        </button>
                                                        <button class="btn btn-default btn-xs" name= "dl_user_cert" type="submit" title="Download User Certificate">
-                                                               <span class="glyphicon glyphicon-download"></span> Crertificate
+                                                               <span class="glyphicon glyphicon-download"></span> Certificate
                                                        </button>
-
+                             <button class="btn btn-default btn-xs" name= "dl_user_p12" type="submit" title="Download User PKCS12">
+                                 <span class="glyphicon glyphicon-download"></span> PKCS p12
+                             </button>
                                                </td>
                                        </tr>
                                        {%endfor%}
index bf01002..ac85870 100644 (file)
                                                                <span class="glyphicon glyphicon-download"></span> Credential
                                                        </button>
                                                        <button class="btn btn-default btn-xs" name= "dl_user_cert" type="submit" title="Download User Certificate">
-                                                               <span class="glyphicon glyphicon-download"></span> Crertificate
+                                                               <span class="glyphicon glyphicon-download"></span> Certificate
+                                                       </button>
+                                                       <button class="btn btn-default btn-xs" name= "dl_user_p12" type="submit" title="Download User PKCS12">
+                                                               <span class="glyphicon glyphicon-download"></span> PKCS p12
                                                        </button>
                                                </td>
                                        </tr>