小程序
传感搜
传感圈

A Deep Dive into Token-Based Authentication and OAuth 2.0 in MQTT

2023-07-28 21:48:11
关注

Illustration: © IoT For All

This article delves into additional authentication methods. Specifically, we will explore token-based authentication and OAuth 2.0, explaining their concepts and demonstrating their implementation in MQTT.

Token-Based Authentication

Let’s first look at token-based authentication and see some of the benefits of username and password authentication.

As the name implies, token-based authentication uses tokens to authenticate a client instead of its credentials such as username and password. This is similar to an electronic key to a hotel room. You show your ID to the receptionist, and they give you an electronic key that allows you access to your room. This electronic key performs the function of a token for the duration of your stay. You don’t need to keep identifying yourself to the receptionist every time you want to enter the room, you just use your key.

An important feature of tokens is that they can have an expiration that limits how long they are valid. For instance, your hotel key would no longer be valid after your stay is over. But you may check into a new hotel and get a different token for a room in the new hotel.

Thus, tokens are much more flexible and easier to manage than usernames and passwords. The electronic key reader on the hotel room door does not have to keep track of valid usernames and passwords, it just needs to verify that the room number and expiration date on the electronic key are valid.

Token-Based Authentication Method for MQTT

In MQTT, we usually use JWT to implement Token authentication. JWT (JSON Web Token) is a compact way of authenticating clients in MQTT brokers. The client sends a signed JWT token to the broker, and the broker uses the token to authenticate the client. The broker does not need to maintain a list of client usernames and passwords.

The JWT token consists of the following parts:

  • Header: Base64 encoded – Identifies which algorithm is used to generate the signature.
  • Payload: Base64 encoded – This contains the claims that can be used to authenticate the client.
  • Signature: Base64 encoding of the concatenation of the header and payload, all signed with a secret.

The following diagram shows the JWT structure:

EMQ Technologies Inc.

Note that the header and payload are not encrypted, they are just encoded using the base64 binary-to-text encoding function. It is not a one-way function, so the contents can be read easily by using a base64 decoding function. So, make sure that the header and payload sections do not contain sensitive information. It is also a good idea to use TLS to encrypt client connections. The JWT is signed using a secret.

The broker needs to verify that the JWT is valid. The broker either needs to know the secret, thus having a shared secret between the client and the broker, or the broker can use a JWKS (JSON Web Key Set). A JWKS is a set of public keys that are used to verify the secret key is valid. The broker can reference a JWKS endpoint rather than holding the keys itself.

When a JWT token is issued, it cannot be revoked until it expires. So, it is important to keep it stored in a safe location. If it is stolen, the attacker could use it to gain access to the broker.

An authentication server can be used to get the JWT token. In this case, the client connects to the authentication server, which verifies its identity and issues a JWT token to the client. The client uses this token to connect to the broker.

The following diagram shows this process:

EMQ Technologies Inc.

The following shows an example JWT payload.

{
  "clientid": "client1",
  "username": "user1",
  "iat": 1516239022,
  "nbf": 1678114325,
  "exp": 1709649185
}

Besides the clientid and username fields, the JWT token can contain some time fields that indicate when the token is valid. The times shown are all in Unix time, which is the number of seconds since 1970-Jan-01.

  • “iat”: Issued at – The date and time the token was issued. Expressed in Unix time.
  • “nbf”: Not before – The date and time the token becomes valid. Expressed in Unix time.
  • “exp”: Expired – The date and time the token expires. Expressed in Unix time.

Note that by using the nbf field, you can issue a JWT that will not be valid until a future date.

OAuth 2.0

In the previous section, we discussed JWT which describes the format of the tokens; however, it does not dictate how the tokens are obtained. Next, let’s look at how OAuth 2.0 and JWT can be used together to allow client access to the broker.

OAuth 2.0 is a framework that allows users to access resources using their credentials from a separate authentication and authorization server, such as Google, Facebook, GitHub, and many others. This can be used as a way of having an SSO (Single Sign On) mechanism because the user doesn’t have to remember multiple passwords. They can use the same Google credentials for different applications.

Originally OAuth 2.0 was designed to be an authorization framework to grant third-party applications a particular scope of access to recourses. A common example is read access to Gmail contacts. We allow the application to read our contacts, but we don’t want it to be able to delete them. One problem that OAuth 2.0 solves is that we can give the third-party application access to our contacts without having to give our Gmail password to the application which of course is not very secure.

Because it was also convenient to use this protocol for authentication, an extension to OAuth 2.0 called OpenID Connect was created. This created a standard way to use OAuth 2.0 for authentication. Since this article is about authentication, we are referring to OAuth 2.0 together with OpenID Connect as the mechanism to grant MQTT clients access to the broker.

How Does OAuth 2.0 Work With MQTT?

OAuth 2.0 and OpenID Connect can be used as a mechanism for the clients to retrieve the appropriate JWT, which can then be sent to the broker. Referring back to the above image, the first step is that the MQTT client requests the JWT token from the authentication server. We are now assuming that the authentication server supports OAuth 2.0 with the OpenID Connect extension.

The OpenID Connect specifies that the token returned by the authentication server will be in the JWT format. Once the client receives the JWT, it can be sent to the broker. Usually, the JWT is sent to the broker in the password field of the CONNECT packet.

Authentication Approaches

By adopting these additional authentication approaches, you can strengthen your overall system’s defenses against unauthorized access and potential security breaches. As technology continues to evolve, it becomes increasingly vital to stay up to date with the latest authentication techniques.

Tweet

Share

Share

Email

  • IT and Security
  • MQTT
  • Security

  • IT and Security
  • MQTT
  • Security

参考译文
深入解析MQTT中的基于令牌的认证与OAuth 2.0# 示例输入和输出 **输入** 人工智能(AI)是计算机科学的一个分支,旨在开发表现出人类智能的软件或机器。这包括从经验中学习、理解自然语言、解决问题以及识别模式。 **输出** 人工智能(AI)是计算机科学的一个分支,旨在开发表现出人类智能的软件或机器。这包括从经验中学习、理解自然语言、解决问题以及识别模式。
插图:© IoT For All --> 本文深入探讨了额外的认证方法。具体来说,我们将探索基于令牌的认证和 OAuth 2.0,解释它们的概念,并演示它们在 MQTT 中的实现方式。基于令牌的认证 首先,我们来看看基于令牌的认证,并了解用户名和密码认证的一些优势。顾名思义,基于令牌的认证使用令牌来认证客户端,而不是使用其凭证,比如用户名和密码。这类似于酒店的电子钥匙。你向前台出示身份证件,他们会给你一把电子钥匙,允许你进入房间。在你入住期间,这把电子钥匙就起到令牌的作用。你不需要每次进入房间时都向前台重新确认身份,只需要使用钥匙即可。令牌的一个重要特性是它们可以设置过期时间,以限制其有效时长。例如,你入住结束后,酒店钥匙将不再有效。但是,你可以入住新的酒店,并获得用于新房间的另一个令牌。因此,与用户名和密码相比,令牌更加灵活且更易于管理。酒店房间门口的电子钥匙读取器不需要记录有效的用户名和密码,它只需要验证电子钥匙上的房间号和过期日期是否有效。MQTT 中的基于令牌认证方法 在 MQTT 中,我们通常使用 JWT 来实现令牌认证。JWT(JSON Web Token)是一种紧凑的方式,用于在 MQTT 代理中认证客户端。客户端将签名的 JWT 令牌发送给代理,代理使用该令牌认证客户端。代理不需要维护客户端用户名和密码的列表。JWT 令牌由以下部分组成:- **头部(Header)**:Base64 编码,标识用于生成签名的算法。- **载荷(Payload)**:Base64 编码,包含可用于认证客户端的声明。- **签名(Signature)**:将头部和载荷拼接后,使用密钥签名并进行 Base64 编码。下图展示了 JWT 的结构:EMQ Technologies Inc.请注意,头部和载荷并未加密,只是使用了 Base64 编码,这是二进制到文本的编码方式,而非单向函数。因此,通过 Base64 解码函数可以轻松读取内容。因此,请确保头部和载荷部分不包含敏感信息。另外,使用 TLS 加密客户端连接是个好主意。JWT 使用一个**密钥**进行签名。代理需要验证 JWT 是否有效。为此,代理要么需要知道该密钥(即客户端和代理之间共享密钥),要么可以使用 JWKS(JSON Web Key Set)。JWKS 是一组用于验证密钥是否有效的公钥。代理可以引用一个 JWKS 终端,而无需自行存储密钥。一旦 JWT 令牌被签发,在其过期之前无法被撤销。因此,非常重要的一点是将令牌安全地保存在合适的位置。如果令牌被窃取,攻击者可以使用它来访问代理。可以使用认证服务器来获取 JWT 令牌。在这种情况下,客户端连接到认证服务器,服务器验证其身份并向客户端发放 JWT 令牌。客户端使用该令牌连接到代理。下图展示了该过程:EMQ Technologies Inc.下面是一个 JWT 载荷的示例:{ "clientid": "client1", "username": "user1", "iat": 1516239022, "nbf": 1678114325, "exp": 1709649185}除了 `clientid` 和 `username` 字段外,JWT 令牌还可以包含一些时间字段,这些字段表示令牌的有效时间。这些时间均以 Unix 时间表示,即自 1970 年 1 月 1 日以来的秒数。- **"iat"**:签发时间(Issued at)—— 令牌的签发日期和时间,以 Unix 时间表示。- **"nbf"**:开始生效时间(Not before)—— 令牌开始生效的日期和时间,以 Unix 时间表示。- **"exp"**:过期时间(Expired)—— 令牌的过期日期和时间,以 Unix 时间表示。请注意,通过使用 `nbf` 字段,你可以签发一个在未来某个日期才生效的 JWT 令牌。OAuth 2.0 在上一节中,我们讨论了 JWT,它描述了令牌的格式,但并未规定如何获取令牌。接下来,我们来看看 OAuth 2.0 和 JWT 如何协同工作,以允许客户端访问代理。OAuth 2.0 是一个框架,允许用户通过来自其他认证和授权服务器(如 Google、Facebook、GitHub 等)的凭证来访问资源。这可以作为单点登录(SSO, Single Sign On)机制,因为用户不需要记住多个密码。他们可以使用相同的 Google 凭证登录不同的应用程序。最初,OAuth 2.0 被设计为一种授权框架,用于向第三方应用授予特定范围的资源访问权限。一个常见的例子是读取 Gmail 的联系人。我们可以允许某个应用读取我们的联系人,但不希望它能删除联系人。OAuth 2.0 所解决的一个问题是:我们可以授权第三方应用访问联系人,而无需将 Gmail 密码交给该应用,这显然更安全。由于使用该协议进行认证也很方便,因此 OAuth 2.0 被扩展为一个名为 OpenID Connect 的协议。这个扩展创建了一种使用 OAuth 2.0 进行认证的标准方式。由于本文讨论的是认证,我们将 OAuth 2.0 与 OpenID Connect 一起作为授权 MQTT 客户端访问代理的机制。OAuth 2.0 如何与 MQTT 配合工作?OAuth 2.0 与 OpenID Connect 可以作为客户端获取相应 JWT 的机制,然后将该 JWT 发送给代理。参考上面的图示,第一步是 MQTT 客户端向认证服务器请求 JWT 令牌。我们假设该认证服务器支持 OpenID Connect 扩展的 OAuth 2.0 协议。OpenID Connect 规定,认证服务器返回的令牌必须为 JWT 格式。一旦客户端收到 JWT,就可以将其发送给代理。通常,JWT 会通过 CONNECT 数据包的密码字段发送给代理。认证方法 通过采用这些额外的认证方法,您可以加强整个系统对未经授权访问和潜在安全漏洞的防护。随着技术的不断发展,保持对最新认证技术的了解变得越来越重要。TweetShareShareEmail IT 和安全MQTT安全 --> IT 和安全MQTT安全
您觉得本篇内容如何
评分

评论

您需要登录才可以回复|注册

提交评论

广告

iotforall

这家伙很懒,什么描述也没留下

关注

点击进入下一篇

利用增强型身份验证提升MQTT安全性

提取码
复制提取码
点击跳转至百度网盘