AzureAD/microsoft-authentication-library-for-go
GitHub: AzureAD/microsoft-authentication-library-for-go
微软官方的 Go 身份认证库,基于 OAuth2 和 OpenID Connect 协议帮助应用获取安全令牌以调用受保护的 Microsoft API。
Stars: 284 | Forks: 106
# 面向 Go 的 Microsoft Authentication Library (MSAL)
Microsoft Authentication Library (MSAL) for Go 是[面向开发者的 Microsoft 身份平台](https://aka.ms/aaddevv2)(前身为 Azure AD)v2.0 的一部分。它允许你使用 Microsoft 身份([Azure AD](https://azure.microsoft.com/services/active-directory/) 和 [Microsoft 账户](https://account.microsoft.com))让用户或应用登录,并获取 token 以调用 Microsoft API(例如 [Microsoft Graph](https://graph.microsoft.io/)),或调用注册到 Microsoft 身份平台的你自己的 API。它是使用行业标准 OAuth2 和 OpenID Connect 协议构建的。
最新的代码位于 `dev` 分支中。
快速链接:
## 构建状态

## 安装说明
### 设置 Go
要安装 Go,请访问[此链接](https://golang.org/dl/)。
### 安装 MSAL Go
`go get -u github.com/AzureAD/microsoft-authentication-library-for-go/`
## 用法
在使用 MSAL Go 之前,你需要[在 Microsoft 身份平台中注册你的应用程序](https://docs.microsoft.com/azure/active-directory/develop/quickstart-v2-register-an-app)。
### 获取 Token
使用 MSAL Go 获取 token 遵循以下通用模式。其他 token 获取流程可能会存在一些细微的差异。这是一个基本示例:
1. 创建一个客户端。MSAL 区分了[公共客户端和机密客户端应用程序](https://tools.ietf.org/html/rfc6749#section-2.1),因此请调用 `public.New()` 或 `confidential.New()` 来为你的应用程序创建合适的客户端。
* 初始化公共客户端:
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)
* 为 SystemAssigned 初始化 Managed Identity 客户端:
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
}
* 为 UserAssigned 初始化 Managed Identity 客户端:
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
}
2. 调用 `AcquireTokenSilent()` 来查找缓存的 token。如果 `AcquireTokenSilent()` 返回错误,请调用另一个 `AcquireToken...` 方法进行身份验证。
* 公共客户端应该指定一个用户账户(如果有的话):
// 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
* ManagedIdentity 客户端可以直接调用 `AcquireToken()`:
resource := ""
result, err := miSystemAssigned.AcquireToken(context.TODO(), resource)
if err != nil {
// TODO: handle error
}
accessToken := result.AccessToken
## 提交反馈
我们希望听取您对本库的想法。请完成[这份简短的调查。](https://forms.office.com/r/s4waBAytFJ)
## 安全库
该库控制用户登录和访问服务的方式。我们建议您尽可能在应用中始终使用我们最新版本的库。我们采用[语义化版本控制](http://semver.org),以便您可以控制更新应用所带来的风险。例如,始终下载最新的次要版本号(例如 x.*y*.x)可确保您获得最新的安全和功能增强,同时我们的 API 接口保持不变。您随时可以在 GitHub 的 Releases 选项卡下查看最新版本和发布说明。
## 安全报告
如果您发现我们的库或服务存在安全问题,请将其报告至 [secure@microsoft.com](mailto:secure@microsoft.com),并尽可能提供详细信息。您的提交可能符合条件,通过 [Microsoft Bounty](http://aka.ms/bugbounty) 计划获得赏金。请勿将安全问题发布到 GitHub Issues 或任何其他公共网站。收到信息后,我们将尽快与您联系。我们鼓励您访问[此页面](https://technet.microsoft.com/en-us/security/dd252948)并订阅安全公告警报,以便在发生安全事件时收到通知。
版权所有 (c) Microsoft Corporation。保留所有权利。在 MIT 许可证(即“许可证”)下授权。
标签:EVTX分析, 日志审计