aboutsummaryrefslogtreecommitdiff
path: root/www/provision.js
blob: 5441bcedebf8104b82f6beeb8a3911fc5864860d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
var sign;

jQuery(function($) {
    // NB.  By browsing secondary server source code, I have
    // discovered that 'user is not authenticated as target user' is
    // the only acceptable error message here.
    //
    // See: static/shared/user.js, primaryUserAuthenticationInfo
    var ifLoggedIn = function(email, thunk) {
        $.ajax({
            type: 'POST',
            crossDomain: true,
            url: '/browserid/logged_in_p.pl',
            dataType: 'json',
            data: { email: email },
            success: function(data, status, xhr) {
                console.log('Logged in: ' + data.logged_in_p);
                if (data.logged_in_p === 0) {
                    console.log('* Not logged in.');
                    return navigator.id.raiseProvisioningFailure('user is not authenticated as target user');
                } else {
                    thunk();
                }
            },
            error: function(data, status, xhr) {
                console.log('Logged in: no.');
                return navigator.id.raiseProvisioningFailure('user is not authenticated as target user');
            }
        });
    };

    sign = function(email, pubkey, cert_duration) {
        console.log('email: '    + email);
        console.log('pubkey: '); console.log(pubkey);
        console.log('duration: ' + cert_duration);
        $.ajax({
            type: 'POST',
            crossDomain: true,
            url: '/browserid/sign.pl',
            dataType: 'json',
            data: { email: email, pubkey: JSON.stringify(pubkey), duration: cert_duration },
            success: function(data, status, xhr) {
		console.log('Success!');
                console.log(data);
                navigator.id.registerCertificate(data.signature);
            },
            error: function(data, status, xhr) {
                return navigator.id.raiseProvisioningFailure(data.responseText);
            }
        });
    };

    navigator.id.beginProvisioning(function(email, cert_duration) {
        console.log('email: ' + email);
        console.log('cert_duration: ' + cert_duration);
        ifLoggedIn(email, function() {
            navigator.id.genKeyPair(function(pubkey) {
                if (typeof(pubkey) === 'string') {
                    pubkey = JSON.parse(pubkey);
                }
                sign(email, pubkey, cert_duration);
            });
        });
    });
});