1. 程式人生 > >Configuring Cognito User Pools to Communicate with AWS IoT Core

Configuring Cognito User Pools to Communicate with AWS IoT Core

AWS IoT Core supports certificate-based mutual authentication, custom authorizers, and Amazon Cognito Identity as way to authenticate requests to the AWS IoT device gateway. Amazon Cognito User Pools was made generally available last year. It allows customers to easily add user sign up and sign in to mobile and web apps. You can use Cognito User Pools identities to talk to AWS IoT Core by linking them to Cognito Federated Identities.

In this blog post, I will walk you through how to set up Amazon Cognito User Pools with Amazon Cognito Federated Identities to talk to AWS IoT Core using an authenticated Amazon Cognito identity over WebSocket. I assume you are familiar with the basic components and functionality of AWS IoT and Cognito.

To create a user pool

Sign in to the AWS Management Console and navigate to Amazon Cognito.
In the Amazon Cognito console, choose Manage User Pool, and then choose Create a user pool.
For Pool name, enter demo-pool.
Make a note of the user pool ID. You’ll need it later.

To create an app client

From the navigation pane, choose App clients, and then choose Add an app client.
For App client name, enter demo-app-client.
Make a note of the app client ID. You’ll need it later.

Clear the Generate client secret check box.
Review the demo pool settings, and then choose Create app client.

To create an identity pool

Next, create a federated identity pool using Amazon Cognito User Pools as the identity provider. Use the user pool ID and app client ID created in the previous steps.

In the Amazon Cognito console, choose Federated Identities.
Create an identity pool and name it demo identity pool.
For authentication provider, choose Cognito.
Enter the user pool ID. (You can find the ID under User Pools → demo-pool → General Settings → Pool ID)

Enter the app client ID. (You can find it under User Pools → demo-pool → App Integration → App client settings → demo-app-client→ ID)

Finally, create an identity pool that looks like the following:

Choose Create Pool.

Note: Two IAM roles will be created for you: one for an authenticated pool and another for an unauthenticated pool. In this post, we will use the authenticated pool only.

To grant AWS IoT permission to the Amazon Cognito identity pool

Each unique user who signs in receives a unique identity provided by the Cognito Identity authenticated pool. The authenticated pool policy applies to that identity, so make sure you add AWS IoT-specific permission to the IAM role policy for the authenticated pool.
In the Action for the allowed statement, add “iot:Connect”, “iot:Publish”, “iot:Subscribe”, “iot:Receive”, “iot:GetThingShadow”
and “iot:AttachPrincipalPolicy.” I’m adding the “iot:AttachPrincipalPolicy” for demonstration purposes only. You should not use it in production.

To create an AWS IoT policy for Amazon Cognito identities

An Amazon Cognito authenticated user needs two policies to access AWS IoT. The first policy is attached to the role of the authenticated pool to authenticate and authorize the Cognito user to communicate with AWS IoT. The second policy is attached to the authenticated Cognito user ID principal for fine-grained permissions.

Sign in to AWS IoT console.
From the navigation pane, choose Secure , and then choose Policies.
On the Create a policy page, for Name, enter demo-policy.
In Add statements, for Action, enter iot:Connect. For Resource ARN, enter *.
Add another statement. For Action, enter iot:Publish. For Resource ARN, enter the topic name (demoTopic) at the end of resource ARN.

Choose Create.

To register the user and get the Cognito Identity ID

In this blog post, I am using a JavaScript example from the amazon cognito auth JS GitHub repository.

To complete the app client setup for the demo, go back to the App client settings page in the Amazon Cognito console and grant permission to request code and choose allowed scopes. To set up redirect URLs, we’ll use localhost.

For Callback URL(s) and Sign out URL(s), enter http://localhost:8000.
Under Allowed OAuth Flows, select Authorization code grant and Implicit grant.
Under Allowed OAuth Scopes, select email and openid.


Choose Save changes.

Get the domain name for your app client.
In the Amazon Cognito console, under App Integration, choose Domain name.
Enter a domain name for your app (for example, iot–).
Choose Check availability.


Choose Save changes.

Sample Code to Get Cognito Identity Credentials

Because the AWS IoT JavaScript SDK is in Node.js, it won’t run directly in the browser. For this demo, I use Browserify to convert the SDK into browser-supported JavaScript.

Install the AWS IoT JavaScript SDK.
Create a file named AWSIotDeviceSdk.js with the following contents:

var AwsIot = require('aws-iot-device-sdk');
window.AwsIot = AwsIot; // make it global

Install Browserify to make the demo to work with a browser.
Run the following command in a terminal to browserify AWSIotDeviceSdk.js to bundle.js.

terminal> browserify path/to/AWSIotDeviceSdk.js -o bundle.js

Download the latest version of the AWS JavaScript SDK.
Download this sample from amazon-cognito-auth-js.
In the sample’s index.html, replace the sample code’s initCognitoSDK JavaScript function as shown. Use your values to request an identity. Make sure you import the aws sdk js and bundle.js as shown in the code.

    <script src="/path/to/aws-sdk.js"/>
    <script src="/path/to/bundle.js"/>
    <script type="text/javascript">
      function initCognitoSDK() {
        AWS.config.region = '<REGION>'; // like: us-east-1
        var authData = {
            ClientId: '<YOUR_CLIENT_ID>', // Your client id here
            AppWebDomain: '<YOUR APP WEB DOMAIN>',
            TokenScopesArray: ['email', 'openid'], // e.g.['phone', 'email', 'profile','openid', 'aws.cognito.signin.user.admin'],
            RedirectUriSignIn: '<REDIRECT_SIGN_IN_URI>',
            RedirectUriSignOut: '<REDIRECT_SIGN_OUT_URI>',
            UserPoolId: '<USER_POOL_ID>', // Your user pool id here
        };
        var login = {};
        var auth = new AmazonCognitoIdentity.CognitoAuth(authData);
        auth.userhandler = {
            onSuccess: function (result) {
                //alert("Sign in success");
                showSignedIn(result);
                var loginKey = 'cognito-idp.' + AWS.config.region + '.amazonaws.com/' + authData['UserPoolId'];
                login[loginKey] = result.getIdToken().getJwtToken();
                AWS.config.credentials = new AWS.CognitoIdentityCredentials({
                    IdentityPoolId: '<COGNITO_IDENTITY_POOL_ID>',
                    Logins: login
                });
                AWS.config.credentials.refresh((error) => {
                    if (error) {
                        console.error(error);
                    } else {
                        var principal = AWS.config.credentials.identityId;
                        console.log("IdentityId: " + principal);

                        //Now we have cognito identity and credentials to make AWS IoT calls.
                        //Attach pre-created IoT policy to this principal. 
                        //IMPORTANT: Make sure you have granted AttachPrincipalPolicy API permission in IAM to Cognito Identity Pool's Role.
                        //It is done here for the demo purpose only while cognito user should NOT be allowed to call AttachPrincipalPolicy in production, this step must be done by Administrator only
                        attachPrincipalPolicy("demo-policy", principal);

                        //Now we can use this principal for IoT actions
                        //We'll need aws-iot-device-sdk-js for mqtt over websocket calls using these cognito credentials.
                        connect();
                    }
                });
            },
            onFailure: function (err) {
                alert("Error!");
            }
        };
        return auth;
      }

      //Need aws-sdk.js to work
      function attachPrincipalPolicy(policyName, principal) {
          new AWS.Iot().attachPrincipalPolicy({ policyName: policyName, principal: principal }, function (err, data) {
            if (err) {
                    console.error(err); // an error occurred
                }
          });
       }
       
       //Need bundle.js to work
       function connect() {
            var clientID = 'webapp:' + new Date().getTime(); //needs to be unique
            device = AwsIot.device({
                clientId: clientID,
                host: '<YOUR_AWS_IOT_ENDPOINT>', //can be queried using 'aws iot describe-endpoint'
                protocol: 'wss',
                accessKeyId: AWS.config.credentials.accessKeyId,
                secretKey: AWS.config.credentials.secretAccessKey,
                sessionToken: AWS.config.credentials.sessionToken
            });
            device.on('connect', function () {
                console.log("connected")
                publishMessage(device, 'demoTopic', 'demo success!')
            });
            function publishMessage(device, topic, msg) {
                device.publish(topic, msg, { qos: 1 }, function (err) {
                    if (err) {
                        alert("failed to publish iot message!");
                        console.error(err);
                    } else {
                        console.log("published to demoTopic");
                        showMessage("Message Published", "Topic: " + topic, "Message: " + msg);
                    }
                });
            }
       }
    </script>

Now we are ready to send MQTT messages over WebSocket to AWS IoT using Amazon Cognito Identity credentials.

Run the local server to receive credentials back to the localhost callback URL, as defined in the app client settings.

terminal> cd path/to/cognito/sample
terminal> python3 -m http.server

You should get output like ‘Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) …’

Type http://localhost:8000/ in your browser to load our modified index.html from sample.

Choose Sign In to create a user or sign in with an existing one.

After you are signed in, you should see a message published to the AWS IoT demoTopic notification, as shown here.

We can also subscribe to the demoTopic from the AWS IoT console to see if this worked.
In the navigation pane, choose Test.
Under Subscribe to a topic, enter demoTopic, and then choose Subscribe to topic.

Try refreshing the index.html page to publish the message again using the authenticated Cognito credentials. You should receive a message on the AWS IoT console.

Conclusion

In this blog post, I have shown you how to configure Amazon Cognito User Pools with Amazon Cognito Federated Identities and use authenticated Cognito Identity credentials to publish MQTT message over WebSocket to the AWS IoT Core message broker.

Further Reading

相關推薦

Configuring Cognito User Pools to Communicate with AWS IoT Core

AWS IoT Core supports certificate-based mutual authentication, custom authorizers, and Amazon Cognito Identity as way to authenticate requests to

Amazon Cognito User Pools supports federation with SAML.

Last year, we launched SAML federation support for Amazon Cognito Identity. This feature enables you to get temporary scoped AWS credentials in ex

Setting Up Just-in-Time Provisioning with AWS IoT Core

In an earlier blog post about just-in-time registration of device certificates, we discussed how just-in-time registration (JITR) can be used to a

Ask HN: Which can be the best way to communicate with the user?

I work with a cms and the eternal argument with the clients is how to interact with the users of the website. WhatsApp business or chatbot?

Migrating Users to Amazon Cognito User Pools

Many customers ask about the best way to migrate their existing users in to Amazon Cognito User Pools. In this blog post, we describe the options

Sign Up and Confirm With Amazon Cognito User Pools Using C#

This post was authored by Tom Moore & Mike Morain, AWS Solutions Architects. With Amazon Cognito, you can add user sign-up and sign-in

Learning to Communicate with Deep Multi-Agent Reinforcement Learning

2017Nips的文章,看了一篇18的一篇相關方向的,但是沒太明白,第一次看communicate的文章(multi-agent RL with communication),理解的也不太透徹。 大概簡要介紹一下: 在MA的環境中,agent需要相互合作去完成任務,這個時

Use Cognito User Pool to Run Lambda Execution Role

import boto3 client = boto3.client('sts') def lambda_handler(event, context): role=event['requestContext']['authorizer']['claims']['cognito:

Using Continuous Jobs with AWS IoT Device Management

In an earlier Using Over-the-Air Updates with AWS IoT Device Management blog post, we showed you how to create a simple AWS IoT snapshot job and t

Containerize your IOT application with AWS IOT Analytics

Overview In an earlier blog post about IoT Analytics, we discussed how AWS IoT Analytics enables you to collect, visualize, process, query

Collect, Structure, and Search Industrial IoT data with AWS IoT SiteWise

AWS IoT SiteWise is a managed service that makes it easy to collect and organize data from industrial equipment at scale. You can easily monitor

Questions fréquentes (FAQ) sur AWS IoT Core

Q : Qu'est-ce que la passerelle pour les appareils ? La passerelle pour les appareils représente la pierre angulaire de la communica

AWS IoT Core Overview

AWS IoT Core is a managed cloud service that lets connected devices easily and securely interact with cloud applications and other devices. AWS Io

AWS IoT Core Resources

Amazon Web Services is Hiring. Amazon Web Services (AWS) is a dynamic, growing business unit within Amazon.com. We are currently hiring So

Présentations de AWS IoT Core

AWS IoT Core est un service basé sur le cloud géré qui permet aux appareils connectés d'interagir de manière simple et sécurisée avec d'autres app

AWS IoT Core Features

The Rules Engine makes it possible to build IoT applications that gather, process, analyze and act on data generated by connected devices at gl

第七篇:AWS IoT Core服務成本優化

AWS IoT 物聯網系列部落格 當前物聯網環境中,裝置型別多種多樣,連線方式不一而足。為了幫助讀者更好的理解並運用 AWS IoT 相關服務,我們提供了一個完整的 IoT 起步指南,包含裝置的註冊及上線、裝置管理、使用者身份及許可權管理以及成本控制,通過這一系列的起步指南,

Tarification AWS IoT Core

2 250 000 minutes de connexion 500 000 messages 225 000 opérations exécutées sur le registre ou les shadows d'appareil

How AWS IoT Core is Helping Customers Navigate the Upcoming Distrust of Symantec Certificate Authorities

NOTE: This blog post describes important public key infrastructure (PKI) issues related to browser and mobile application connectivity to AWS IoT

AWS IoT Core Pricing

2,250,000 minutes of connection 500,000 messages 225,000 Registry or Device Shadow operations 250,000 rules t