In production environments, your API may have to access a message broker that's protected by some auth mechanisms. Examples of these are:
- User & password
- API keys
- OAuth 2
If you're using AsyncAPI to define an API that connects to a message broker, you'll most probably make use of user/password or certificates. Traditionally, message brokers are infrastructure pieces that serve an internal purpose and they're not exposed to the public. That's why their security mechanisms are also simpler than what we're used to with REST APIs. However, AsyncAPI also helps you define your HTTP streaming APIs and therefore it supports more sophisticated mechanisms like OAuth2 or OpenID.
Continuing with the
hello world application example, let's learn how to define a simple security scheme (mechanism) for it.
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
asyncapi: '2.0.0' info: title: Hello world application version: '0.1.0' servers: production: url: broker.mycompany.com protocol: amqp description: This is "My Company" broker. security: - user-password:  channels: hello: publish: message: $ref: '#/components/messages/hello-msg' goodbye: publish: message: $ref: '#/components/messages/goodbye-msg' components: messages: hello-msg: payload: type: object properties: name: type: string sentAt: $ref: '#/components/schemas/sent-at' goodbye-msg: payload: type: object properties: sentAt: $ref: '#/components/schemas/sent-at' schemas: sent-at: type: string description: The date and time a message was sent. format: datetime securitySchemes: user-password: type: userPassword
The example above shows how to specify that your server (the Kafka broker) requires a user and a password to establish a connection. Let's break this down:
- There's a new property in the server object called
security. It's an array and can contain multiple security mechanisms. You chose to add one called "user-password". This is simply a memorable name that you give to this
securityscheme but, whatever name you choose, it must be defined in the
components/securitySchemessection. You might have also noticed its value is an empty array. It's because some security schemes allow for extra configuration but, since this is not the case in this example, leave the array empty.
- We've added a new section called
components. Inside it, you can find the definition of your
user-passwordmechanism. This section makes it clear that you're speaking about a
user/passwordmechanism is the
type: userPasswordin line 44.
There are many more security schemes. Learn more about them here.
You're now able to define what security mechanisms your application needs to connect to the server. You've seen how to define the requirement of a user and a password, which is the most common use case.
At this point, you know AsyncAPI well enough to create a simple
Hello world application. However, real use cases are more complicated than that. Next tutorials can teach you how to create real-world use cases, from zero to production.