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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
|
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
package fake
import (
"context"
"errors"
"fmt"
"github.com/AzureAD/microsoft-authentication-library-for-go/apps/internal/oauth/ops/accesstokens"
"github.com/AzureAD/microsoft-authentication-library-for-go/apps/internal/oauth/ops/authority"
"github.com/AzureAD/microsoft-authentication-library-for-go/apps/internal/oauth/ops/wstrust"
"github.com/AzureAD/microsoft-authentication-library-for-go/apps/internal/oauth/ops/wstrust/defs"
)
// ResolveEndpoints is a fake implementation of the oauth.resolveEndpointer interface.
type ResolveEndpoints struct {
// Set this to true to have all APIs return an error.
Err bool
// fake result to return
Endpoints authority.Endpoints
}
func (f ResolveEndpoints) ResolveEndpoints(ctx context.Context, authorityInfo authority.Info, userPrincipalName string) (authority.Endpoints, error) {
if f.Err {
return authority.Endpoints{}, errors.New("error")
}
return f.Endpoints, nil
}
// AccessTokens is a fake implementation of the oauth.accessTokens interface.
type AccessTokens struct {
// Set this to true to have all APIs return an error.
Err bool
// Result is for use with FromDeviceCodeResult. On each call it returns
// the next item in this slice. They must be either an error or nil.
Result []error
Next int
// fake result to return
AccessToken accesstokens.TokenResponse
// fake result to return
DeviceCode accesstokens.DeviceCodeResult
// FromRefreshTokenCallback is an optional callback invoked by FromRefreshToken
FromRefreshTokenCallback func(appType accesstokens.AppType, authParams authority.AuthParams, cc *accesstokens.Credential, refreshToken string)
// ValidateAssertion is an optional callback for validating an assertion generated by confidential.Client
ValidateAssertion func(string)
}
func (f *AccessTokens) FromUsernamePassword(ctx context.Context, authParameters authority.AuthParams) (accesstokens.TokenResponse, error) {
if f.Err {
return accesstokens.TokenResponse{}, fmt.Errorf("error")
}
return f.AccessToken, nil
}
func (f *AccessTokens) FromAuthCode(ctx context.Context, req accesstokens.AuthCodeRequest) (accesstokens.TokenResponse, error) {
if f.Err {
return accesstokens.TokenResponse{}, fmt.Errorf("error")
}
return f.AccessToken, nil
}
func (f *AccessTokens) FromRefreshToken(ctx context.Context, appType accesstokens.AppType, authParams authority.AuthParams, cc *accesstokens.Credential, refreshToken string) (accesstokens.TokenResponse, error) {
if f.FromRefreshTokenCallback != nil {
f.FromRefreshTokenCallback(appType, authParams, cc, refreshToken)
}
if f.Err {
return accesstokens.TokenResponse{}, fmt.Errorf("error")
}
return f.AccessToken, nil
}
func (f *AccessTokens) FromClientSecret(ctx context.Context, authParameters authority.AuthParams, clientSecret string) (accesstokens.TokenResponse, error) {
if f.Err {
return accesstokens.TokenResponse{}, fmt.Errorf("error")
}
return f.AccessToken, nil
}
func (f *AccessTokens) FromAssertion(ctx context.Context, authParameters authority.AuthParams, assertion string) (accesstokens.TokenResponse, error) {
if f.Err {
return accesstokens.TokenResponse{}, fmt.Errorf("error")
}
if f.ValidateAssertion != nil {
f.ValidateAssertion(assertion)
}
return f.AccessToken, nil
}
func (f *AccessTokens) FromUserAssertionClientSecret(ctx context.Context, authParameters authority.AuthParams, userAssertion, clientSecret string) (accesstokens.TokenResponse, error) {
if f.Err {
return accesstokens.TokenResponse{}, fmt.Errorf("error")
}
return f.AccessToken, nil
}
func (f *AccessTokens) FromUserAssertionClientCertificate(ctx context.Context, authParameters authority.AuthParams, userAssertion, assertion string) (accesstokens.TokenResponse, error) {
if f.Err {
return accesstokens.TokenResponse{}, fmt.Errorf("error")
}
return f.AccessToken, nil
}
func (f *AccessTokens) DeviceCodeResult(ctx context.Context, authParameters authority.AuthParams) (accesstokens.DeviceCodeResult, error) {
if f.Err {
return accesstokens.DeviceCodeResult{}, fmt.Errorf("error")
}
return f.DeviceCode, nil
}
func (f *AccessTokens) FromDeviceCodeResult(ctx context.Context, authParameters authority.AuthParams, deviceCodeResult accesstokens.DeviceCodeResult) (accesstokens.TokenResponse, error) {
if f.Next < len(f.Result) {
defer func() { f.Next++ }()
v := f.Result[f.Next]
if v == nil {
return f.AccessToken, nil
}
return accesstokens.TokenResponse{}, v
}
panic("AccessTokens.FromDeviceCodeResult() asked for more return values than provided")
}
func (f *AccessTokens) FromSamlGrant(ctx context.Context, authParameters authority.AuthParams, samlGrant wstrust.SamlTokenInfo) (accesstokens.TokenResponse, error) {
if f.Err {
return accesstokens.TokenResponse{}, fmt.Errorf("error")
}
return f.AccessToken, nil
}
// Authority is a fake implementation of the oauth.fetchAuthority interface.
type Authority struct {
// Set this to true to have all APIs return an error.
Err bool
// The fake UserRealm to return from the UserRealm() API.
Realm authority.UserRealm
// fake result to return
InstanceResp authority.InstanceDiscoveryResponse
}
func (f Authority) UserRealm(ctx context.Context, params authority.AuthParams) (authority.UserRealm, error) {
if f.Err {
return authority.UserRealm{}, errors.New("error")
}
return f.Realm, nil
}
func (f Authority) AADInstanceDiscovery(ctx context.Context, info authority.Info) (authority.InstanceDiscoveryResponse, error) {
if f.Err {
return authority.InstanceDiscoveryResponse{}, errors.New("error")
}
return f.InstanceResp, nil
}
// WSTrust is a fake implementation of the oauth.fetchWSTrust interface.
type WSTrust struct {
// Set these to true to have their respective APIs return an error.
GetMexErr, GetSAMLTokenInfoErr bool
// fake result to return
MexDocument defs.MexDocument
// fake result to return
SamlTokenInfo wstrust.SamlTokenInfo
}
func (f WSTrust) Mex(ctx context.Context, federationMetadataURL string) (defs.MexDocument, error) {
if f.GetMexErr {
return defs.MexDocument{}, errors.New("error")
}
return f.MexDocument, nil
}
func (f WSTrust) SAMLTokenInfo(ctx context.Context, authParameters authority.AuthParams, cloudAudienceURN string, endpoint defs.Endpoint) (wstrust.SamlTokenInfo, error) {
if f.GetSAMLTokenInfoErr {
return wstrust.SamlTokenInfo{}, errors.New("error")
}
return f.SamlTokenInfo, nil
}
|