Add support for Persona Identity Provider
[cascardo/ipsilon.git] / templates / persona / provisioning.html
1 {% extends "master.html" %}
2 {% block main %}
3 <div class="col-sm-12">
4   <div id="welcome">
5       <p>This page is used internally</p>
6   </div>
7 </div>
8
9 <script type="text/javascript" src="https://login.persona.org/provisioning_api.js"></script>
10 <script type="text/javascript">
11     var xmlhttp = new XMLHttpRequest()
12
13     var loggedin = {{ loggedin|lower }};
14
15     xmlhttp.onreadystatechange = function()
16     {
17         if(xmlhttp.readyState == 4)
18         {
19             if(xmlhttp.status == 200)
20             {
21                 navigator.id.registerCertificate(xmlhttp.responseText);
22             }
23             else if((xmlhttp.status == 401) || (xmlhttp.status == 403))
24             {
25                 navigator.id.raiseProvisioningFailure('Error in provisioning!');
26             }
27             else
28             {
29                 alert("Response code: " + xmlhttp.status);
30                 alert("Response text: " + xmlhttp.responseText);
31             }
32         }
33     }
34
35     function generateServerSide(email, publicKey, certDuration, callback)
36     {
37         xmlhttp.open("POST", "Sign/", true);
38         xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
39         xmlhttp.send("email=" + encodeURIComponent(email)
40                      + "&publicKey=" + encodeURIComponent(publicKey)
41                      + "&certDuration=" + encodeURIComponent(certDuration));
42     }
43
44     function startProvisioning()
45     {
46         navigator.id.beginProvisioning(function(email, certDuration)
47         {
48             if(loggedin)
49             {
50                 navigator.id.genKeyPair(function(publicKey)
51                 {
52                     generateServerSide(email, publicKey, certDuration);
53                 });
54             } else {
55                 navigator.id.raiseProvisioningFailure('user is not authenticated');
56             }
57         });
58     }
59
60     startProvisioning();
61 </script>
62 {% endblock %}