The MSAL library for Go is part of the Microsoft identity platform for developers (formerly named Azure AD) v2.0. It enables you to acquire security tokens to call protected APIs. It uses industry standard OAuth2 and OpenID Connect.
288
stars
395
commits
Go
primary language
Sep 8, 2026
updated
The Microsoft Authentication Library (MSAL) for Go is part of the Microsoft identity platform for developers (formerly named Azure AD) v2.0. It allows you to sign in users or apps with Microsoft identities (Azure AD and Microsoft Accounts) and obtain tokens to call Microsoft APIs such as Microsoft Graph or your own APIs registered with the Microsoft identity platform. It is built using industry standard OAuth2 and OpenID Connect protocols.
The latest code resides in the dev branch.
Quick links:
To install Go, visit this link.
go get -u github.com/AzureAD/microsoft-authentication-library-for-go/
Before using MSAL Go, you will need to register your application with the Microsoft identity platform.
Acquiring tokens with MSAL Go follows this general pattern. There might be some slight differences for other token acquisition flows. Here is a basic example:
Create a client. MSAL separates public and confidential client applications, so call public.New() or confidential.New() to create the appropriate client for your application.
import "github.com/AzureAD/microsoft-authentication-library-for-go/apps/public"
publicClient, err := public.New("client_id", public.WithAuthority("https://login.microsoftonline.com/your_tenant"))
import "github.com/AzureAD/microsoft-authentication-library-for-go/apps/confidential"
// confidential clients have a credential, such as a secret or a certificate
cred, err := confidential.NewCredFromSecret("client_secret")
if err != nil {
// TODO: handle error
}
confidentialClient, err := confidential.New("https://login.microsoftonline.com/your_tenant", "client_id", cred)
import mi "github.com/AzureAD/microsoft-authentication-library-for-go/apps/managedidentity"
// Managed identity client have a type of ID required, SystemAssigned or UserAssigned
miSystemAssigned, err := mi.New(mi.SystemAssigned())
if err != nil {
// TODO: handle error
}
import mi "github.com/AzureAD/microsoft-authentication-library-for-go/apps/managedidentity"
// Managed identity client have a type of ID required, SystemAssigned or UserAssigned
miSystemAssigned, err := mi.New(mi.UserAssignedClientID("YOUR_CLIENT_ID"))
if err != nil {
// TODO: handle error
}
Call AcquireTokenSilent() to look for a cached token. If AcquireTokenSilent() returns an error, call another AcquireToken... method to authenticate.
// If your application previously authenticated a user, call AcquireTokenSilent with that user's account
// to use cached authentication data. This example shows choosing an account from the cache, however this
// isn't always necessary because the AuthResult returned by authentication methods includes user account
// information.
accounts, err := client.Accounts(context.TODO())
if err != nil {
// TODO: handle error
}
if len(accounts) > 0 {
// There may be more accounts; here we assume the first one is wanted.
// AcquireTokenSilent returns a non-nil error when it can't provide a token.
result, err = client.AcquireTokenSilent(context.TODO(), scopes, public.WithSilentAccount(accounts[0]))
}
if err != nil || len(accounts) == 0 {
// cache miss, authenticate a user with another AcquireToken* method
result, err = client.AcquireTokenInteractive(context.TODO(), scopes)
if err != nil {
// TODO: handle error
}
}
// TODO: save the authenticated user's account, use the access token
userAccount := result.Account
accessToken := result.AccessToken
AcquireTokenSilent():scopes := []string{"scope"}
result, err := confidentialClient.AcquireTokenSilent(context.TODO(), scopes)
if err != nil {
// cache miss, authenticate with another AcquireToken... method
result, err = confidentialClient.AcquireTokenByCredential(context.TODO(), scopes)
if err != nil {
// TODO: handle error
}
}
accessToken := result.AccessToken
AcquireToken():resource := "<Your resource>"
result, err := miSystemAssigned.AcquireToken(context.TODO(), resource)
if err != nil {
// TODO: handle error
}
accessToken := result.AccessToken
confidential.CertFromPEM loads a certificate and an unencrypted private key from PEM data for
use with NewCredFromCert.
Security note — encrypted PEM is not supported:
CertFromPEMrejects legacy RFC 1423 encrypted PEM blocks (those with aDEK-Infoheader, e.g.DEK-Info: DES-EDE3-CBC,...) with an error. That format relies on a weak key derivation function (a single MD5 iteration) and obsolete DES/3DES ciphers, which provide little protection against offline password-guessing attacks.Provide the private key unencrypted and protect it with filesystem permissions. You can strip legacy encryption with OpenSSL:
openssl pkcs8 -topk8 -nocrypt -in legacy.key -out key.pem
We use Stack Overflow to work with the community on supporting Azure Active Directory and its SDKs, including this one! We highly recommend you ask your questions on Stack Overflow (we're all on there!) Also browse existing issues to see if someone has had your question before. Please use the "msal" tag when asking your questions.
If you find and bug or have a feature request, please raise the issue on GitHub Issues.
We'd like your thoughts on this library. Please complete this short survey.
This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.opensource.microsoft.com.
When you submit a pull request, a CLA bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., status check, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.
This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.
This library controls how users sign-in and access services. We recommend you always take the latest version of our library in your app when possible. We use semantic versioning so you can control the risk associated with updating your app. As an example, always downloading the latest minor version number (e.g. x.y.x) ensures you get the latest security and feature enhancements but our API surface remains the same. You can always see the latest version and release notes under the Releases tab of GitHub.
If you find a security issue with our libraries or services please report it to secure@microsoft.com with as much detail as possible. Your submission may be eligible for a bounty through the Microsoft Bounty program. Please do not post security issues to GitHub Issues or any other public site. We will contact you shortly upon receiving the information. We encourage you to get notifications of when security incidents occur by visiting this page and subscribing to Security Advisory Alerts.
Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT License (the "License").
(top 30 of 47)
Go
100.0%
The MSAL library for Go is part of the Microsoft identity platform for developers (formerly named Azure AD) v2.0. It enables you to acquire security tokens to call protected APIs. It uses industry standard OAuth2 and OpenID Connect.
288
stars
395
commits
Go
primary language
Sep 8, 2026
updated
The Microsoft Authentication Library (MSAL) for Go is part of the Microsoft identity platform for developers (formerly named Azure AD) v2.0. It allows you to sign in users or apps with Microsoft identities (Azure AD and Microsoft Accounts) and obtain tokens to call Microsoft APIs such as Microsoft Graph or your own APIs registered with the Microsoft identity platform. It is built using industry standard OAuth2 and OpenID Connect protocols.
The latest code resides in the dev branch.
Quick links:
To install Go, visit this link.
go get -u github.com/AzureAD/microsoft-authentication-library-for-go/
Before using MSAL Go, you will need to register your application with the Microsoft identity platform.
Acquiring tokens with MSAL Go follows this general pattern. There might be some slight differences for other token acquisition flows. Here is a basic example:
Create a client. MSAL separates public and confidential client applications, so call public.New() or confidential.New() to create the appropriate client for your application.
import "github.com/AzureAD/microsoft-authentication-library-for-go/apps/public"
publicClient, err := public.New("client_id", public.WithAuthority("https://login.microsoftonline.com/your_tenant"))
import "github.com/AzureAD/microsoft-authentication-library-for-go/apps/confidential"
// confidential clients have a credential, such as a secret or a certificate
cred, err := confidential.NewCredFromSecret("client_secret")
if err != nil {
// TODO: handle error
}
confidentialClient, err := confidential.New("https://login.microsoftonline.com/your_tenant", "client_id", cred)
import mi "github.com/AzureAD/microsoft-authentication-library-for-go/apps/managedidentity"
// Managed identity client have a type of ID required, SystemAssigned or UserAssigned
miSystemAssigned, err := mi.New(mi.SystemAssigned())
if err != nil {
// TODO: handle error
}
import mi "github.com/AzureAD/microsoft-authentication-library-for-go/apps/managedidentity"
// Managed identity client have a type of ID required, SystemAssigned or UserAssigned
miSystemAssigned, err := mi.New(mi.UserAssignedClientID("YOUR_CLIENT_ID"))
if err != nil {
// TODO: handle error
}
Call AcquireTokenSilent() to look for a cached token. If AcquireTokenSilent() returns an error, call another AcquireToken... method to authenticate.
// If your application previously authenticated a user, call AcquireTokenSilent with that user's account
// to use cached authentication data. This example shows choosing an account from the cache, however this
// isn't always necessary because the AuthResult returned by authentication methods includes user account
// information.
accounts, err := client.Accounts(context.TODO())
if err != nil {
// TODO: handle error
}
if len(accounts) > 0 {
// There may be more accounts; here we assume the first one is wanted.
// AcquireTokenSilent returns a non-nil error when it can't provide a token.
result, err = client.AcquireTokenSilent(context.TODO(), scopes, public.WithSilentAccount(accounts[0]))
}
if err != nil || len(accounts) == 0 {
// cache miss, authenticate a user with another AcquireToken* method
result, err = client.AcquireTokenInteractive(context.TODO(), scopes)
if err != nil {
// TODO: handle error
}
}
// TODO: save the authenticated user's account, use the access token
userAccount := result.Account
accessToken := result.AccessToken
AcquireTokenSilent():scopes := []string{"scope"}
result, err := confidentialClient.AcquireTokenSilent(context.TODO(), scopes)
if err != nil {
// cache miss, authenticate with another AcquireToken... method
result, err = confidentialClient.AcquireTokenByCredential(context.TODO(), scopes)
if err != nil {
// TODO: handle error
}
}
accessToken := result.AccessToken
AcquireToken():resource := "<Your resource>"
result, err := miSystemAssigned.AcquireToken(context.TODO(), resource)
if err != nil {
// TODO: handle error
}
accessToken := result.AccessToken
confidential.CertFromPEM loads a certificate and an unencrypted private key from PEM data for
use with NewCredFromCert.
Security note — encrypted PEM is not supported:
CertFromPEMrejects legacy RFC 1423 encrypted PEM blocks (those with aDEK-Infoheader, e.g.DEK-Info: DES-EDE3-CBC,...) with an error. That format relies on a weak key derivation function (a single MD5 iteration) and obsolete DES/3DES ciphers, which provide little protection against offline password-guessing attacks.Provide the private key unencrypted and protect it with filesystem permissions. You can strip legacy encryption with OpenSSL:
openssl pkcs8 -topk8 -nocrypt -in legacy.key -out key.pem
We use Stack Overflow to work with the community on supporting Azure Active Directory and its SDKs, including this one! We highly recommend you ask your questions on Stack Overflow (we're all on there!) Also browse existing issues to see if someone has had your question before. Please use the "msal" tag when asking your questions.
If you find and bug or have a feature request, please raise the issue on GitHub Issues.
We'd like your thoughts on this library. Please complete this short survey.
This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.opensource.microsoft.com.
When you submit a pull request, a CLA bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., status check, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.
This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.
This library controls how users sign-in and access services. We recommend you always take the latest version of our library in your app when possible. We use semantic versioning so you can control the risk associated with updating your app. As an example, always downloading the latest minor version number (e.g. x.y.x) ensures you get the latest security and feature enhancements but our API surface remains the same. You can always see the latest version and release notes under the Releases tab of GitHub.
If you find a security issue with our libraries or services please report it to secure@microsoft.com with as much detail as possible. Your submission may be eligible for a bounty through the Microsoft Bounty program. Please do not post security issues to GitHub Issues or any other public site. We will contact you shortly upon receiving the information. We encourage you to get notifications of when security incidents occur by visiting this page and subscribing to Security Advisory Alerts.
Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT License (the "License").
(top 30 of 47)
Go
100.0%