jj

Html code posted by jj
created at 31 Aug 23:38

Edit | Back
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<html>
<head>
    <script src="https://sdk.amazonaws.com/js/aws-sdk-2.103.0.min.js"></script>
    <title>Test</title>
    <head>
        <body onload="run()">
            <h2>AWS/Salesforce federation | Login avvenuto con successo</h2>
            <p>Di seguito l'elenco di tutti i bucket S3 presenti nel tuo account:</p>
            <p>
                <span id="message"></span>
                <ul id="buckets">
                </ul>
            </p>
            <script>
                function run() {
                    var url = window.location.href;

                    // recupera id_token dalla query string
                    var match = url.match('id_token=([^&]*)');
                    var id_token = "";

                    if (match) {
                        id_token = match[1];
                        console.log(id_token);
                    } else {
                        console.error("Impossibile recuperare il token");
                    }

                    // prepara i parametri di connessione
                    AWS.config.region = "eu-west-1"
                    const cognitoParams = {
                        AccountId: "XXX",
                        IdentityPoolId: "eu-west-1:YYY",
                        Logins: {
                            "login.salesforce.com": id_token
                        }
                    }

                    const identity = new AWS.CognitoIdentity()

                    // chiamata a getId
                    identity.getId(cognitoParams, function (err, identityData) {
                        if (err) {
                            printMessage(err);
                            return;
                        }

                        const identityParams = {
                            IdentityId: identityData.IdentityId,
                            Logins: cognitoParams.Logins
                        }

                        // chiamata getCredentialsForIdentity
                        identity.getCredentialsForIdentity(identityParams, function (err, data) {
                            if (err) {
                                printMessage(err);
                            } else {
                                // credenziali temporanee
                                var c = {
                                    region: 'eu-west-1',
                                    accessKeyId: data.Credentials.AccessKeyId,
                                    secretAccessKey: data.Credentials.SecretKey,
                                    sessionToken: data.Credentials.SessionToken
                                };

                                // connessione a S3 sfruttando tale credenziali
                                var s3 = new AWS.S3(c);
                                s3.listBuckets((response, data) => {
                                    data.Buckets.forEach(function (value) { appendMessage(value.Name) })
                                });
                            }
                        });
                    });

                    function printMessage(messageString) {
                        document.getElementById("message").innerHTML = messageString;
                    }

                    function appendMessage(messageString) {
                        var node = document.createElement("li");
                        var textnode = document.createTextNode(messageString);
                        node.appendChild(textnode);
                        document.getElementById("buckets").appendChild(node);
                    }
                }
            </script>
        </body>
</html>
3.64 KB in 11 ms with coderay