Fixed: Registration & My Account, Generate new keys with a valid RSA format - http...
[unfold.git] / portal / registrationview.py
index 59f0802..1e0c4f9 100644 (file)
@@ -7,7 +7,7 @@ from django.template.loader     import render_to_string
 from django.shortcuts           import render
 
 from unfold.page                import Page
-from myslice.viewutils          import topmenu_items
+from ui.topmenu                 import topmenu_items
 
 from manifold.manifoldapi       import execute_query
 from manifold.core.query        import Query
@@ -61,16 +61,24 @@ class RegistrationView (View):
             # XXX validate authority hrn !!
             if PendingUser.objects.filter(email__iexact=reg_email):
                 errors.append('Email already registered.Please provide a new email address.')
+
+# XXX TODO: Factorize with portal/accountview.py
             if 'generate' in request.POST['question']:
-                # Generate public and private keys using SFA Library
-                from sfa.trust.certificate  import Keypair
-                k = Keypair(create=True)
-                public_key = k.get_pubkey_string()
-                private_key = k.as_pem()
-                private_key = ''.join(private_key.split())
-                public_key = "ssh-rsa " + public_key
+                from Crypto.PublicKey import RSA
+                private = RSA.generate(1024)
+                private_key = json.dumps(private.exportKey())
+                public  = private.publickey()
+                public_key = json.dumps(public.exportKey(format='OpenSSH'))
+
+#                # Generate public and private keys using SFA Library
+#                from sfa.trust.certificate  import Keypair
+#                k = Keypair(create=True)
+#                public_key = k.get_pubkey_string()
+#                private_key = k.as_pem()
+#                private_key = ''.join(private_key.split())
+#                public_key = "ssh-rsa " + public_key
                 # Saving to DB
-                keypair = '{"user_public_key":"'+ public_key + '", "user_private_key":"'+ private_key + '"}'
+                keypair = '{"user_public_key":'+ public_key + ', "user_private_key":'+ private_key + '}'
                 #keypair = re.sub("\r", "", keypair)
                 #keypair = re.sub("\n", "\\n", keypair)
                 #keypair = keypair.rstrip('\r\n')
@@ -86,6 +94,8 @@ class RegistrationView (View):
                     keypair = re.sub("\r", "", keypair)
                     keypair = re.sub("\n", "\\n",keypair)
                     keypair = ''.join(keypair.split())
+                    # for sending email
+                    public_key = file_content
                 else:
                     errors.append('Please upload a valid RSA public key [.txt or .pub].')
 
@@ -106,19 +116,22 @@ class RegistrationView (View):
 
                 # Send email
                 ctx = {
-                    'first_name'   : reg_fname, 
-                    'last_name'    : reg_lname, 
-                    'authority_hrn': reg_auth,
-                    'email'        : reg_email, 
-                    'keypair'      : keypair,
-                    'cc_myself'    : True # form.cleaned_data['cc_myself']
+                    'first_name'    : reg_fname, 
+                    'last_name'     : reg_lname, 
+                    'authority_hrn' : reg_auth,
+                    'email'         : reg_email, 
+                    'keypair'       : 'Public Key :' + public_key,
+                    'cc_myself'     : True # form.cleaned_data['cc_myself']
                     }
                 #not working
                 #recipients = authority_get_pi_emails(request,reg_auth)
-                recipients = 'devel@myslice.info'
-                recipients.append(ctx['email'])
+                recipients = ['devel@myslice.info']
+                if ctx['cc_myself']:
+                    recipients.append(ctx['email'])
 
                 msg = render_to_string('user_request_email.txt', ctx)
+                print "tesing msg"
+                print msg
                 send_mail("Onelab New User request for %s submitted"%reg_email, msg, reg_email, recipients)
 
                 return render(request, 'user_register_complete.html')