package public
import "github.com/AzureAD/microsoft-authentication-library-for-go/apps/public"
Package public provides a client for authentication of "public" applications. A "public"
application is defined as an app that runs on client devices (android, ios, windows, linux, ...).
These devices are "untrusted" and access resources via web APIs that must authenticate.
This example demonstrates the general pattern for authenticating with MSAL Go:
Code:play
Example¶
package main
import (
"context"
"github.com/AzureAD/microsoft-authentication-library-for-go/apps/public"
)
func main() {
client, err := public.New("client_id", public.WithAuthority("https://login.microsoftonline.com/your_tenant"))
if err != nil {
// TODO: handle error
}
var result public.AuthResult
scopes := []string{"scope"}
// 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
_ = result.Account
_ = result.AccessToken
}
Index ¶
- Constants
- func WithAuthenticationScheme(authnScheme AuthenticationScheme) interface { AcquireSilentOption AcquireInteractiveOption AcquireByUsernamePasswordOption options.CallOption }
- func WithChallenge(challenge string) interface { AcquireByAuthCodeOption options.CallOption }
- func WithClaims(claims string) interface { AcquireByAuthCodeOption AcquireByDeviceCodeOption AcquireByUsernamePasswordOption AcquireInteractiveOption AcquireSilentOption AuthCodeURLOption options.CallOption }
- func WithDomainHint(domain string) interface { AcquireInteractiveOption AuthCodeURLOption options.CallOption }
- func WithLoginHint(username string) interface { AcquireInteractiveOption AuthCodeURLOption options.CallOption }
- func WithOpenURL(openURL func(url string) error) interface { AcquireInteractiveOption options.CallOption }
- func WithRedirectURI(redirectURI string) interface { AcquireInteractiveOption options.CallOption }
- func WithSilentAccount(account Account) interface { AcquireSilentOption options.CallOption }
- func WithTenantID(tenantID string) interface { AcquireByAuthCodeOption AcquireByDeviceCodeOption AcquireByUsernamePasswordOption AcquireInteractiveOption AcquireSilentOption AuthCodeURLOption options.CallOption }
- type Account
- type AcquireByAuthCodeOption
- type AcquireByDeviceCodeOption
- type AcquireByUsernamePasswordOption
- type AcquireInteractiveOption
- type AcquireSilentOption
- type AuthCodeURLOption
- type AuthResult
- type AuthenticationScheme
- type Client
- func New(clientID string, options ...Option) (Client, error)
- func (pca Client) Accounts(ctx context.Context) ([]Account, error)
- func (pca Client) AcquireTokenByAuthCode(ctx context.Context, code string, redirectURI string, scopes []string, opts ...AcquireByAuthCodeOption) (AuthResult, error)
- func (pca Client) AcquireTokenByDeviceCode(ctx context.Context, scopes []string, opts ...AcquireByDeviceCodeOption) (DeviceCode, error)
- func (pca Client) AcquireTokenByUsernamePassword(ctx context.Context, scopes []string, username, password string, opts ...AcquireByUsernamePasswordOption) (AuthResult, error)
- func (pca Client) AcquireTokenInteractive(ctx context.Context, scopes []string, opts ...AcquireInteractiveOption) (AuthResult, error)
- func (pca Client) AcquireTokenSilent(ctx context.Context, scopes []string, opts ...AcquireSilentOption) (AuthResult, error)
- func (pca Client) AuthCodeURL(ctx context.Context, clientID, redirectURI string, scopes []string, opts ...AuthCodeURLOption) (string, error)
- func (pca Client) RemoveAccount(ctx context.Context, account Account) error
- type DeviceCode
- type DeviceCodeResult
- type Option
- func WithAuthority(authority string) Option
- func WithCache(accessor cache.ExportReplace) Option
- func WithClientCapabilities(capabilities []string) Option
- func WithHTTPClient(httpClient ops.HTTPClient) Option
- func WithInstanceDiscovery(enabled bool) Option
- type TokenSource
Examples ¶
Constants ¶
const ( TokenSourceIdentityProvider = base.TokenSourceIdentityProvider TokenSourceCache = base.TokenSourceCache )
Functions ¶
func WithAuthenticationScheme ¶
func WithAuthenticationScheme(authnScheme AuthenticationScheme) interface { AcquireSilentOption AcquireInteractiveOption AcquireByUsernamePasswordOption options.CallOption }
WithAuthenticationScheme is an extensibility mechanism designed to be used only by Azure Arc for proof of possession access tokens.
func WithChallenge ¶
func WithChallenge(challenge string) interface { AcquireByAuthCodeOption options.CallOption }
WithChallenge allows you to provide a code for the .AcquireTokenByAuthCode() call.
func WithClaims ¶
func WithClaims(claims string) interface { AcquireByAuthCodeOption AcquireByDeviceCodeOption AcquireByUsernamePasswordOption AcquireInteractiveOption AcquireSilentOption AuthCodeURLOption options.CallOption }
WithClaims sets additional claims to request for the token, such as those required by conditional access policies. Use this option when Azure AD returned a claims challenge for a prior request. The argument must be decoded. This option is valid for any token acquisition method.
func WithDomainHint ¶
func WithDomainHint(domain string) interface { AcquireInteractiveOption AuthCodeURLOption options.CallOption }
WithDomainHint adds the IdP domain as domain_hint query parameter in the auth url.
func WithLoginHint ¶
func WithLoginHint(username string) interface { AcquireInteractiveOption AuthCodeURLOption options.CallOption }
WithLoginHint pre-populates the login prompt with a username.
func WithOpenURL ¶
func WithOpenURL(openURL func(url string) error) interface { AcquireInteractiveOption options.CallOption }
WithOpenURL allows you to provide a function to open the browser to complete the interactive login, instead of launching the system default browser.
func WithRedirectURI ¶
func WithRedirectURI(redirectURI string) interface { AcquireInteractiveOption options.CallOption }
WithRedirectURI sets a port for the local server used in interactive authentication, for example http://localhost:port. All URI components other than the port are ignored.
func WithSilentAccount ¶
func WithSilentAccount(account Account) interface { AcquireSilentOption options.CallOption }
WithSilentAccount uses the passed account during an AcquireTokenSilent() call.
func WithTenantID ¶
func WithTenantID(tenantID string) interface { AcquireByAuthCodeOption AcquireByDeviceCodeOption AcquireByUsernamePasswordOption AcquireInteractiveOption AcquireSilentOption AuthCodeURLOption options.CallOption }
WithTenantID specifies a tenant for a single authentication. It may be different than the tenant set in New by WithAuthority. This option is valid for any token acquisition method.
Types ¶
type Account ¶
type AcquireByAuthCodeOption ¶
type AcquireByAuthCodeOption interface {
// contains filtered or unexported methods
}
AcquireByAuthCodeOption is implemented by options for AcquireTokenByAuthCode
type AcquireByDeviceCodeOption ¶
type AcquireByDeviceCodeOption interface {
// contains filtered or unexported methods
}
AcquireByDeviceCodeOption is implemented by options for AcquireTokenByDeviceCode
type AcquireByUsernamePasswordOption ¶
type AcquireByUsernamePasswordOption interface {
// contains filtered or unexported methods
}
AcquireByUsernamePasswordOption is implemented by options for AcquireTokenByUsernamePassword
type AcquireInteractiveOption ¶
type AcquireInteractiveOption interface {
// contains filtered or unexported methods
}
AcquireInteractiveOption is implemented by options for AcquireTokenInteractive
type AcquireSilentOption ¶
type AcquireSilentOption interface {
// contains filtered or unexported methods
}
AcquireSilentOption is implemented by options for AcquireTokenSilent
type AuthCodeURLOption ¶
type AuthCodeURLOption interface {
// contains filtered or unexported methods
}
AuthCodeURLOption is implemented by options for AuthCodeURL
type AuthResult ¶
type AuthResult = base.AuthResult
AuthResult contains the results of one token acquisition operation. For details see https://aka.ms/msal-net-authenticationresult
type AuthenticationScheme ¶
type AuthenticationScheme = authority.AuthenticationScheme
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client is a representation of authentication client for public applications as defined in the package doc. For more information, visit https://docs.microsoft.com/azure/active-directory/develop/msal-client-applications.
func New ¶
New is the constructor for Client.
func (Client) Accounts ¶
Accounts gets all the accounts in the token cache. If there are no accounts in the cache the returned slice is empty.
func (Client) AcquireTokenByAuthCode ¶
func (pca Client) AcquireTokenByAuthCode(ctx context.Context, code string, redirectURI string, scopes []string, opts ...AcquireByAuthCodeOption) (AuthResult, error)
AcquireTokenByAuthCode is a request to acquire a security token from the authority, using an authorization code. The specified redirect URI must be the same URI that was used when the authorization code was requested.
Options: WithChallenge, WithClaims, WithTenantID
func (Client) AcquireTokenByDeviceCode ¶
func (pca Client) AcquireTokenByDeviceCode(ctx context.Context, scopes []string, opts ...AcquireByDeviceCodeOption) (DeviceCode, error)
AcquireTokenByDeviceCode acquires a security token from the authority, by acquiring a device code and using that to acquire the token. Users need to create an AcquireTokenDeviceCodeParameters instance and pass it in.
Options: WithClaims, WithTenantID
func (Client) AcquireTokenByUsernamePassword ¶
func (pca Client) AcquireTokenByUsernamePassword(ctx context.Context, scopes []string, username, password string, opts ...AcquireByUsernamePasswordOption) (AuthResult, error)
AcquireTokenByUsernamePassword acquires a security token from the authority, via Username/Password Authentication. NOTE: this flow is NOT recommended.
Options: WithClaims, WithTenantID
func (Client) AcquireTokenInteractive ¶
func (pca Client) AcquireTokenInteractive(ctx context.Context, scopes []string, opts ...AcquireInteractiveOption) (AuthResult, error)
AcquireTokenInteractive acquires a security token from the authority using the default web browser to select the account. https://docs.microsoft.com/en-us/azure/active-directory/develop/msal-authentication-flows#interactive-and-non-interactive-authentication
Options: WithDomainHint, WithLoginHint, WithOpenURL, WithRedirectURI, WithTenantID
func (Client) AcquireTokenSilent ¶
func (pca Client) AcquireTokenSilent(ctx context.Context, scopes []string, opts ...AcquireSilentOption) (AuthResult, error)
AcquireTokenSilent acquires a token from either the cache or using a refresh token.
Options: WithClaims, WithSilentAccount, WithTenantID
func (Client) AuthCodeURL ¶
func (pca Client) AuthCodeURL(ctx context.Context, clientID, redirectURI string, scopes []string, opts ...AuthCodeURLOption) (string, error)
AuthCodeURL creates a URL used to acquire an authorization code.
Options: WithClaims, WithDomainHint, WithLoginHint, WithTenantID
func (Client) RemoveAccount ¶
RemoveAccount signs the account out and forgets account from token cache.
type DeviceCode ¶
type DeviceCode struct { // Result holds the information about the device code (such as the code). Result DeviceCodeResult // contains filtered or unexported fields }
DeviceCode provides the results of the device code flows first stage (containing the code) that must be entered on the second device and provides a method to retrieve the AuthenticationResult once that code has been entered and verified.
func (DeviceCode) AuthenticationResult ¶
func (d DeviceCode) AuthenticationResult(ctx context.Context) (AuthResult, error)
AuthenticationResult retreives the AuthenticationResult once the user enters the code on the second device. Until then it blocks until the .AcquireTokenByDeviceCode() context is cancelled or the token expires.
type DeviceCodeResult ¶
type DeviceCodeResult = accesstokens.DeviceCodeResult
type Option ¶
type Option func(o *clientOptions)
Option is an optional argument to the New constructor.
func WithAuthority ¶
WithAuthority allows for a custom authority to be set. This must be a valid https url.
func WithCache ¶
func WithCache(accessor cache.ExportReplace) Option
WithCache provides an accessor that will read and write authentication data to an externally managed cache.
func WithClientCapabilities ¶
WithClientCapabilities allows configuring one or more client capabilities such as "CP1"
func WithHTTPClient ¶
func WithHTTPClient(httpClient ops.HTTPClient) Option
WithHTTPClient allows for a custom HTTP client to be set.
func WithInstanceDiscovery ¶
WithInstanceDiscovery set to false to disable authority validation (to support private cloud scenarios)
type TokenSource ¶
type TokenSource = base.TokenSource
Source Files ¶
- Version
- v1.4.2 (latest)
- Published
- Mar 26, 2025
- Platform
- linux/amd64
- Imports
- 20 packages
- Last checked
- 2 days ago –
Tools for package owners.