What news from AWS re:Invent last week will have the most impact on you?
Amazon Q, an AI chatbot for explaining how AWS works.
Super-fast S3 Express storage.
New Graviton 4 processor instances.
Emily Freeman leaving AWS.
I don't use AWS, so none of this will affect me.
API Management / Security

Secure Go APIs with Decentralized Identity Tokens, Part 1

Securing your Go APIs with decentralized identity tokens is a good practice to enhance the security and trustworthiness of your application. In the first of a three-part series, get an overview of the advantages these tokens have when used with Go APIs.
Sep 27th, 2023 3:00am by
Featued image for: Secure Go APIs with Decentralized Identity Tokens, Part 1
This article is the first of a three-part series on the use of decentralized identity tokens to secure Go APIs. You can read part two here and part three here.

APIs enable the exchange of data and functionality between different software applications, making them a crucial component of modern software systems. However, as we rely on APIs more and more, ensuring their security becomes essential to protect sensitive data, maintain user privacy, and prevent unauthorized access or misuse of resources.

The rise of decentralized identity tokens adds a new dimension to API security. Traditionally, API authentication and authorization relied heavily on centralized identity providers, such as username/password combinations or access tokens issued by third-party services like OAuth. While these approaches have been widely used and effective, they introduce a level of dependency on centralized authorities and increase the risk of data breaches and single points of failure.

Decentralized identity tokens, on the other hand, leverage decentralized identity frameworks and technologies like blockchain or distributed ledgers to provide a more secure and privacy-enhancing alternative. They enable individuals to have greater control over their identities and authenticate themselves without relying on a central authority.

By using decentralized identity tokens, APIs get increased protection against identity theft and impersonation attacks.

Securing your Go APIs with decentralized identity tokens is a good practice to enhance the security and trustworthiness of your application. Here’s an overview of how you can secure your Go APIs with decentralized identity tokens

Choose a decentralized identity framework. There are several frameworks available, such as Ethereum-based solutions like uPort, Sovrin or Hyperledger Indy. Select a framework that aligns with your requirements and integrates well with Go.

Generate and issue tokens. Once you have chosen a framework, you need to generate and issue identity tokens to your users. Typically, this involves a registration process, where users prove their identity and receive a unique token.

Validate tokens in Your API. In your Go APIs, you need to implement token validation logic. This usually involves verifying the signature of the token using a public key associated with the decentralized identity framework. You can find libraries or packages that simplify this process, such as for Go.

Extract claims and authenticate users. After validating the token, you can extract the claims embedded within it. Claims typically include information about the user, such as their identity, roles or permissions. You can then use these claims to authenticate the user and authorize their access to specific API resources.

Implement authorization checks. Once you have authenticated the user, you can enforce authorization checks based on the user’s claims. For example, you might allow or deny access to certain API endpoints or data based on the user’s role or permissions. Implement appropriate authorization logic in your API handlers or middleware.

Handle token expiry and revocation. Decentralized identity tokens often have an expiration time; It’s crucial to prompt users to reauthenticate when their token expires. Additionally, you should consider implementing token revocation mechanisms in case a user’s token needs to be invalidated before it expires naturally.

Keep security best practices in mind. Best practices include protecting the private keys associated with token validation and securely transmitting tokens over HTTPS. Also consider additional security measures like rate limiting and request throttling.

The Advantages of Decentralized ID Tokens for Go APIs

In addition to the benefits already mentioned — enhanced security, greater privacy and control for users, reduced dependency on third-party or centralized authorities — decentralized identity tokens bring the following benefits when used in Go APIs: decentralized identity tokens bring:

Interoperability. Decentralized identity frameworks aim to establish interoperable standards, enabling seamless integration across different platforms, services, and organizations. This interoperability simplifies the implementation and adoption of decentralized identity tokens in Go APIs, promoting compatibility and consistency in identity-related interactions.

Future-proofing. The rise of decentralized identity represents an evolving landscape in digital identity management. By incorporating decentralized identity tokens in Go APIs, developers can future-proof their applications and be prepared for the increasing adoption of decentralized identity frameworks and technologies.

Developer flexibility. Decentralized identity tokens provide developers with flexibility in choosing and integrating with different frameworks that align with their requirements. This flexibility allows developers to leverage the benefits of decentralized identity while tailoring the implementation to their specific use cases and preferences.

Introduction to JWT and Its Key Components

Decentralized identity token standards, such as JSON Web Tokens (JWT), provide a structured format for representing and exchanging identity information in a secure and verifiable manner. JWT is a widely adopted standard for creating self-contained tokens that can be used to assert claims about the identity and access rights of a user.

JWT  is an open standard (RFC 7519) that defines a compact and self-contained way to transmit information between parties as a JSON object. It consists of three parts: header, payload and signature.

The header of a JWT contains metadata about the token, such as the token type and the cryptographic algorithm used to sign the token. It is encoded in Base64Url format and is part of the token itself.

The payload contains the claims or statements about the identity of the user and additional data. Claims can include information like the user’s identity, roles, permissions, expiration time or any custom data required for authentication and authorization. The payload is also Base64Url encoded.

The signature is created by combining the encoded header, payload, and a secret or private key known only to the issuer. It ensures the integrity of the token and verifies that it hasn’t been tampered with. Verifying the signature with the corresponding public key allows the recipient to validate the authenticity of the token.

JWT-based tokens have gained popularity due to their simplicity, flexibility and compatibility across different platforms and programming languages. They can be used in various use cases, such as single sign-on, secure API authentication, and authorization in distributed systems.

Note that while JWT is a widely used token format, it is just one example of a decentralized identity token standard. Other frameworks might have their own token formats and standards, depending on the specific technology and ecosystem being used.

Other Popular Decentralized Identity Frameworks


uPort is a decentralized identity platform built on the Ethereum blockchain. It allows individuals to create and manage their identities, control their data and interact securely with decentralized applications. uPort provides a JavaScript library that can be used in conjunction with Go APIs to integrate decentralized identity features. Since it was introduced in 2015, uPort has since evolved into two separate projects: Veramo and Serto.


Sovrin is a decentralized identity network that leverages blockchain technology. It aims to provide self-sovereign identity capabilities with privacy and security. Sovrin uses a combination of distributed ledger technology and cryptographic techniques. Integration with Go APIs can be achieved through the Sovrin client libraries and SDKs.

Hyperledger Indy

Hyperledger Indy is an open source project under The Linux Foundation that focuses on decentralized identity and verifiable claims. It provides a framework for building self-sovereign identity systems and allows individuals to manage their identities and control the release of their personal information. Indy SDK offers Go language bindings that enable integration with Go APIs.


SelfKey is a decentralized identity and digital asset management platform. It allows users to control their identity attributes and manage their digital identity securely. SelfKey provides a range of developer tools, including APIs and SDKs, to integrate with Go APIs and implement decentralized identity functionality.

Integrating these frameworks with Go APIs typically involves using the provided libraries, SDKs or client APIs. These tools often offer methods for token generation, validation and access to decentralized identity features. Developers can leverage these resources to implement authentication and authorization mechanisms using tokens within their Go applications.

When integrating decentralized identity frameworks with Go, refer to the respective documentation and resources provided by the framework of choice. These resources often include code examples, tutorials and reference documentation that guide developers through the integration process and help them make the most of the decentralized identity capabilities within their Go APIs.

How to Generate and Issue Decentralized Identity Tokens

1. Choose a Decentralized Identity Framework.

Select a framework that aligns with your project’s requirements and integrates well with your technology stack. Examples include the aforementioned uPort, Sovrin, Hyperledger Indy or SelfKey. Refer to the documentation and resources provided by the chosen framework to understand their token issuance process.

2. Set up the Required Infrastructure.

Set up the necessary infrastructure to support token generation and issuance. This typically includes deploying or connecting to the decentralized identity framework’s network or blockchain. Follow the documentation provided by the framework for detailed instructions on infrastructure setup.

3. Implement a User Registration and Identity Verification Process.

This step ensures that the identities associated with the decentralized identity tokens are valid and trustworthy. Verification methods can include email verification, government-issued ID verification, or other mechanisms based on the requirements of your application.

4. Generate a Decentralized Identity Token.

Once the user’s identity is verified, generate a decentralized identity token for the user. The specific steps for generating a token will depend on the chosen decentralized identity framework. Typically, you’ll need to call the framework’s API or use their provided libraries to create a token with the required claims and data.

5. Embed User Claims and Information.

In the generated decentralized identity token, include the relevant claims and information about the user. Claims can include the user’s identity, roles, permissions or any additional data required for authentication and authorization within your application. Ensure that the token payload accurately represents the user’s verified identity and associated attributes.

6. Sign the Token.

Sign the token with a private key or secret known only to the issuer. This step ensures the integrity of the token and allows recipients to verify its authenticity. Follow the framework’s documentation for guidance on signing the token using the appropriate cryptographic algorithms and keys.

7. Deliver the Token to the User.

Provide the generated decentralized identity token to the user. The delivery method can vary based on your application’s architecture and requirements. You may send the token as a response after successful registration, store it securely on the user’s device, or utilize a token delivery mechanism provided by the chosen decentralized identity framework.

8. Valid the Token and Its Usage.

In your Go APIs, implement token validation logic to ensure that only valid and authentic decentralized identity tokens are accepted. Use the appropriate libraries or packages for JWT validation in Go, such as Validate the token’s signature, expiration and other relevant claims to authenticate and authorize the user’s access to API resources.

Registering and Verifying User Identities

The registration process and verification of user identities help ensure that only legitimate users receive the tokens. Here’s how a typical registration and identity verification process should work:

User registration. Users typically provide their basic information in order to register, such as name, email address and username, through a registration form or user interface.

Identity information collection. During registration, you may collect additional identity information depending on the requirements of your application. This can include personal details, contact information, and any other data necessary to establish the user’s identity. You should clearly communicate the purpose and use of this information to the user.

Verification Methods

To validate the user’s identity, you’ll need to implement one or more verification methods. Common methods include:

  • Email verification. Send a verification email to the user’s provided email address with a unique verification link. When the user clicks the link, it confirms that the email address is valid and accessible.
  • Document verification. Request users to provide government-issued identification documents, such as a passport or driver’s license, for verification. This process may involve manual or automated checks to ensure the authenticity of the documents.
  • Two-factor authentication (2FA). Implement a second layer of authentication, such as SMS verification or app-based authentication, to confirm the user’s identity.
  • Social media verification. Integrate with social media platforms to validate the user’s identity by verifying their accounts on platforms like Facebook, LinkedIn or Twitter.

Choose the verification methods that align with your application’s requirements, level of assurance needed and the sensitivity of the user’s data.

Verification process. Once the user provides the necessary information and selects the desired verification method(s), initiate the verification process. This involves validating the information provided and conducting the necessary checks based on the chosen verification method.

Identity confirmation. Upon successful verification, notify the user that their identity has been confirmed. Provide clear instructions on the next steps, including how to access the decentralized identity token associated with their verified identity.

Token generation and delivery. After confirming the user’s identity, generate the decentralized identity token with the relevant user claims and information. Follow the token generation steps specific to your chosen decentralized identity framework, as outlined in the earlier steps. Deliver the token securely to the user, whether it’s through an API response, email, or any other secure mechanism.

The registration process and identity verification should be designed to strike a balance between security, user experience and privacy considerations. Ensure that you handle user data responsibly, follow data protection regulations, and provide transparent communication regarding the storage and usage of user information.

To implement token validation logic in Go using the library, you can follow these steps:

Install the Library.

Use the following command to install the jwt-go library:

Import the Required Packages.

Import the necessary packages in your Go code:

Define a Validation Function.

Create a function that takes the token string as input and returns an error if the token is invalid:

Implement Token Validation.

In your API handler or middleware, call the validateToken function with the token string to validate the token:

In the validateToken function, you can customize the key retrieval method based on your token signing approach. For example, if you are using a symmetric signing method, you can return the secret key directly.

If you are using an asymmetric signing method, you may need to retrieve the public key based on the token’s signing key ID (kid) or use a key retrieval service.

Make sure to handle errors appropriately and define the logic for handling different validation scenarios based on your application’s requirements.

The code provided offers a basic structure for token validation using the jwt-go library. You may need to adapt it to your specific use case, including handling additional token claims, expiration checks and custom validation logic.

Remember to refer to the jwt-go library documentation for detailed information on using the library and exploring its advanced features and capabilities.

Verifying a Token’s Digital Signature

The process of verifying the token’s signature using public keys ensures that the token has been signed by the appropriate private key and has not been modified since its creation. Depending on your requirements, you may need to perform additional verification steps beyond the ones described here, such as checking the token’s expiration time, issuer (iss), audience (aud), or other custom claims.

Also, the exact implementation of the signature verification process may vary depending on the chosen library or cryptographic algorithms used.

The process described here provides a general understanding of the token signature verification concept using public keys. Always refer to the documentation and guidelines provided by the specific library or framework you are using for accurate implementation details.

The process of verifying a token’s signature typically involves the following steps:

Understand the Token Structure.

A token consists of three parts: the header, payload, and signature. The header contains metadata about the token, the payload contains the claims, and the signature ensures the integrity of the token.

Obtain the Public Key.

Retrieve the corresponding public key associated with the token’s signing algorithm. The public key can be obtained from a trusted source, such as a key management system, a certificate authority or a key distribution mechanism.

Verify the Signature.

Use the obtained public key to verify the token’s signature. The process involves the following steps

  • Extract the algorithm and signing key identifier (kid) from the token’s header.
  • Retrieve the public key corresponding to the signing key identifier (kid).
  • Verify the signature using the public key and the algorithm specified in the header.
  • If the signature verification is successful, it indicates that the token has not been tampered with and is authentic.

Code Example for Token Validation

Best Practices for Securely Validating ID Tokens in Go

  • Always use a secure key storage mechanism. Store and manage the private and public keys securely. Avoid hardcoding or exposing the keys in the codebase. Use secure key management systems or encryption mechanisms to protect the keys.
  • Validate the token signature algorithm. Verify that the token’s signature algorithm matches the expected algorithm. Only accept tokens signed with trusted algorithms, such as RSA or ECDSA.
  • Verify the token’s expiration time. Check the token’s expiration time (exp) to ensure it has not expired. Reject tokens that have exceeded their expiration time. You can use the time.Now() function to compare the token’s expiration time with the current time.
  • Validate the token issuer and audience. Check the token’s issuer (iss) and audience (aud) claims, if applicable, to ensure they match the expected values. Reject tokens that are not issued by a trusted issuer or intended for the expected audience.
  • Implement additional custom claim checks. If your tokens contain custom claims, implement additional checks to validate those claims according to your application’s requirements. For example, you can check the user’s role, permissions or any other specific claims relevant to your authorization logic.
  • Handle token revocation. Consider implementing a token revocation mechanism if the framework or system you’re using supports it. This allows you to invalidate tokens in case of compromise, logout, or other scenarios where token revocation is necessary.
  • Apply rate limiting and throttling. Implement rate limiting and request throttling mechanisms to prevent brute-force attacks or excessive token validation requests. This helps protect your token validation endpoint from abuse.
  • Implement logging and monitoring. Log token validation activities and errors for auditing purposes. Monitor token validation performance and errors to identify any potential issues or anomalies.
  • Keep libraries and dependencies up-to-date. Regularly update the jwt-go library and other dependencies to ensure you have the latest security patches and bug fixes.

These code examples and best practices provide a starting point for securely validating decentralized identity tokens in Go. Customize them based on your specific requirements and security considerations, and refer to the jwt-go library documentation for further details and advanced usage.

Group Created with Sketch.
THE NEW STACK UPDATE A newsletter digest of the week’s most important stories & analyses.