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 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
|
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
package integration
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"os"
"testing"
"time"
"github.com/AzureAD/microsoft-authentication-library-for-go/apps/confidential"
"github.com/AzureAD/microsoft-authentication-library-for-go/apps/errors"
"github.com/AzureAD/microsoft-authentication-library-for-go/apps/public"
)
const (
msIDlabDefaultScope = "https://msidlab.com/.default"
graphDefaultScope = "https://graph.windows.net/.default"
)
const microsoftAuthorityHost = "https://login.microsoftonline.com/"
const (
organizationsAuthority = microsoftAuthorityHost + "organizations/"
microsoftAuthority = microsoftAuthorityHost + "microsoft.onmicrosoft.com"
//msIDlabTenantAuthority = microsoftAuthorityHost + "msidlab4.onmicrosoft.com" - Will be needed in the future
)
var httpClient = http.Client{}
func httpRequest(ctx context.Context, url string, query url.Values, accessToken string) ([]byte, error) {
if _, ok := ctx.Deadline(); !ok {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, 10*time.Second)
defer cancel()
}
req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
if err != nil {
return nil, fmt.Errorf("failed to build new http request: %w", err)
}
req.Header.Set("Authorization", "Bearer "+accessToken)
req.URL.RawQuery = query.Encode()
resp, err := httpClient.Do(req)
if err != nil {
return nil, fmt.Errorf("http.Get(%s) failed: %w", req.URL.String(), err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("http.Get(%s): could not read body: %w", req.URL.String(), err)
}
return body, nil
}
type labClient struct {
app confidential.Client
}
// TODO : Add app object
type user struct {
AppID string `json:"appId"`
ObjectID string `json:"objectId"`
UserType string `json:"userType"`
DisplayName string `json:"displayName"`
Licenses string `json:"licences"`
Upn string `json:"upn"`
Mfa string `json:"mfa"`
ProtectionPolicy string `json:"protectionPolicy"`
HomeDomain string `json:"homeDomain"`
HomeUPN string `json:"homeUPN"`
B2cProvider string `json:"b2cProvider"`
LabName string `json:"labName"`
LastUpdatedBy string `json:"lastUpdatedBy"`
LastUpdatedDate string `json:"lastUpdatedDate"`
Password string
}
type secret struct {
Value string `json:"value"`
}
func newLabClient() (*labClient, error) {
clientID := os.Getenv("clientId")
secret := os.Getenv("clientSecret")
cred, err := confidential.NewCredFromSecret(secret)
if err != nil {
return nil, fmt.Errorf("could not create a cred from a secret: %w", err)
}
app, err := confidential.New(microsoftAuthority, clientID, cred)
if err != nil {
return nil, err
}
return &labClient{app: app}, nil
}
func (l *labClient) labAccessToken() (string, error) {
scopes := []string{msIDlabDefaultScope}
result, err := l.app.AcquireTokenSilent(context.Background(), scopes)
if err != nil {
result, err = l.app.AcquireTokenByCredential(context.Background(), scopes)
if err != nil {
return "", fmt.Errorf("AcquireTokenByCredential() error: %w", err)
}
}
return result.AccessToken, nil
}
func (l *labClient) user(ctx context.Context, query url.Values) (user, error) {
accessToken, err := l.labAccessToken()
if err != nil {
return user{}, fmt.Errorf("problem getting lab access token: %w", err)
}
responseBody, err := httpRequest(ctx, "https://msidlab.com/api/user", query, accessToken)
if err != nil {
return user{}, err
}
var users []user
err = json.Unmarshal(responseBody, &users)
if err != nil {
return user{}, err
}
if len(users) == 0 {
return user{}, errors.New("No user found")
}
user := users[0]
user.Password, err = l.secret(ctx, url.Values{"Secret": []string{user.LabName}})
if err != nil {
return user, err
}
return user, nil
}
func (l *labClient) secret(ctx context.Context, query url.Values) (string, error) {
accessToken, err := l.labAccessToken()
if err != nil {
return "", err
}
responseBody, err := httpRequest(ctx, "https://msidlab.com/api/LabUserSecret", query, accessToken)
if err != nil {
return "", err
}
var secret secret
err = json.Unmarshal(responseBody, &secret)
if err != nil {
return "", err
}
return secret.Value, nil
}
// TODO: Add getApp() when needed
func testUser(ctx context.Context, desc string, lc *labClient, query url.Values) user {
testUser, err := lc.user(ctx, query)
if err != nil {
panic(fmt.Sprintf("TestUsernamePassword(%s) setup: testUser(): Failed to get input user: %s", desc, err))
}
return testUser
}
func TestUsernamePassword(t *testing.T) {
if testing.Short() {
t.Skip("skipping integration test")
}
labClientInstance, err := newLabClient()
if err != nil {
panic("failed to get a lab client: " + err.Error())
}
tests := []struct {
desc string
vals url.Values
}{
{"Managed", url.Values{"usertype": []string{"cloud"}}},
{"ADFSv2", url.Values{"usertype": []string{"federated"}, "federationProvider": []string{"ADFSv2"}}},
{"ADFSv3", url.Values{"usertype": []string{"federated"}, "federationProvider": []string{"ADFSv3"}}},
{"ADFSv4", url.Values{"usertype": []string{"federated"}, "federationProvider": []string{"ADFSv4"}}},
}
for _, test := range tests {
ctx := context.Background()
user := testUser(ctx, test.desc, labClientInstance, test.vals)
app, err := public.New(user.AppID, public.WithAuthority(organizationsAuthority))
if err != nil {
panic(errors.Verbose(err))
}
result, err := app.AcquireTokenByUsernamePassword(
context.Background(),
[]string{graphDefaultScope},
user.Upn,
user.Password,
)
if err != nil {
t.Fatalf("TestUsernamePassword(%s): on AcquireTokenByUsernamePassword(): got err == %s, want err == nil", test.desc, errors.Verbose(err))
}
if result.AccessToken == "" {
t.Fatalf("TestUsernamePassword(%s): got AccessToken == '', want AccessToken != ''", test.desc)
}
if result.IDToken.IsZero() {
t.Fatalf("TestUsernamePassword(%s): got IDToken == empty, want IDToken == non-empty struct", test.desc)
}
if result.Account.PreferredUsername != user.Upn {
t.Fatalf("TestUsernamePassword(%s): got Username == %s, want Username == %s", test.desc, result.Account.PreferredUsername, user.Upn)
}
}
}
func TestConfidentialClientwithSecret(t *testing.T) {
if testing.Short() {
t.Skip("skipping integration test")
}
clientID := os.Getenv("clientId")
secret := os.Getenv("clientSecret")
cred, err := confidential.NewCredFromSecret(secret)
if err != nil {
panic(errors.Verbose(err))
}
app, err := confidential.New(microsoftAuthority, clientID, cred)
if err != nil {
panic(errors.Verbose(err))
}
scopes := []string{msIDlabDefaultScope}
result, err := app.AcquireTokenByCredential(context.Background(), scopes)
if err != nil {
t.Fatalf("TestConfidentialClientwithSecret: on AcquireTokenByCredential(): got err == %s, want err == nil", errors.Verbose(err))
}
if result.AccessToken == "" {
t.Fatal("TestConfidentialClientwithSecret: on AcquireTokenByCredential(): got AccessToken == '', want AccessToken != ''")
}
silentResult, err := app.AcquireTokenSilent(context.Background(), scopes)
if err != nil {
t.Fatalf("TestConfidentialClientwithSecret: on AcquireTokenSilent(): got err == %s, want err == nil", errors.Verbose(err))
}
if silentResult.AccessToken == "" {
t.Fatal("TestConfidentialClientwithSecret: on AcquireTokenSilent(): got AccessToken == '', want AccessToken != ''")
}
}
func TestOnBehalfOf(t *testing.T) {
if testing.Short() {
t.Skip("skipping integration test")
}
labClientInstance, err := newLabClient()
if err != nil {
panic("failed to get a lab client: " + err.Error())
}
ctx := context.Background()
//Confidential Client Application Config
ccaClientID := os.Getenv("oboConfidentialClientId")
ccaClientSecret := os.Getenv("oboConfidentialClientSecret")
ccaScopes := []string{"https://graph.microsoft.com/user.read"}
// Public Client Application Confifg
pcaClientID := os.Getenv("oboPublicClientId")
user := testUser(ctx, "OnBehalfOf", labClientInstance, url.Values{"usertype": []string{"cloud"}})
pcaScopes := []string{fmt.Sprintf("api://%s/.default", ccaClientID)}
// 1. An app obtains a token representing a user, for our mid-tier service
pca, err := public.New(pcaClientID, public.WithAuthority(organizationsAuthority))
if err != nil {
panic(errors.Verbose(err))
}
result, err := pca.AcquireTokenByUsernamePassword(
ctx, pcaScopes, user.Upn, user.Password,
)
if err != nil {
t.Fatalf("TestOnBehalfOf: on AcquireTokenByUsernamePassword(): got err == %s, want err == nil", errors.Verbose(err))
}
if result.AccessToken == "" {
t.Fatal("TestOnBehalfOf: on AcquireTokenByUsernamePassword(): got AccessToken == '', want AccessToken != ''")
}
// 2. Our mid-tier service uses OBO to obtain a token for downstream service
cred, err := confidential.NewCredFromSecret(ccaClientSecret)
if err != nil {
panic(errors.Verbose(err))
}
cca, err := confidential.New("https://login.microsoftonline.com/common", ccaClientID, cred)
if err != nil {
panic(errors.Verbose(err))
}
result1, err := cca.AcquireTokenOnBehalfOf(ctx, result.AccessToken, ccaScopes)
if err != nil {
t.Fatalf("TestOnBehalfOf: on AcquireTokenOnBehalfOf(): got err == %s, want err == nil", errors.Verbose(err))
}
if result1.AccessToken == "" {
t.Fatal("TestOnBehalfOf: on AcquireTokenOnBehalfOf(): got AccessToken == '', want AccessToken != ''")
}
// 3. Same scope and assertion should return cached access token
result2, err := cca.AcquireTokenOnBehalfOf(ctx, result.AccessToken, ccaScopes)
if err != nil {
t.Fatalf("TestOnBehalfOf: on AcquireTokenOnBehalfOf() silent token retrieval: got err == %s, want err == nil", errors.Verbose(err))
}
if result1.AccessToken != result2.AccessToken {
t.Fatal("TestOnBehalfOf: on AcquireTokenOnBehalfOf(): Access Tokens don't match")
}
// 4. scope2 should return new token
scope2 := []string{"https://graph.windows.net/.default"}
result3, err := cca.AcquireTokenOnBehalfOf(ctx, result.AccessToken, scope2)
if err != nil {
t.Fatalf("TestOnBehalfOf: on AcquireTokenOnBehalfOf(): got err == %s, want err == nil", errors.Verbose(err))
}
if result3.AccessToken == "" {
t.Fatal("TestOnBehalfOf: on AcquireTokenOnBehalfOf(): got AccessToken == '', want AccessToken != ''")
}
if result3.AccessToken == result2.AccessToken {
t.Fatal("TestOnBehalfOf: on AcquireTokenOnBehalfOf(): Access Tokens match when they should not")
}
// 5. scope2 should return cached token
result4, err := cca.AcquireTokenOnBehalfOf(ctx, result.AccessToken, scope2)
if err != nil {
t.Fatalf("TestOnBehalfOf: on AcquireTokenOnBehalfOf(): got err == %s, want err == nil", errors.Verbose(err))
}
if result4.AccessToken == "" {
t.Fatal("TestOnBehalfOf: on AcquireTokenOnBehalfOf(): got AccessToken == '', want AccessToken != ''")
}
if result4.AccessToken != result3.AccessToken {
t.Fatal("TestOnBehalfOf: on AcquireTokenOnBehalfOf(): Access Tokens don't match")
}
// 6. New user assertion should return new token
pca1, err := public.New(pcaClientID, public.WithAuthority(organizationsAuthority))
if err != nil {
panic(errors.Verbose(err))
}
result5, err := pca1.AcquireTokenByUsernamePassword(
ctx, pcaScopes, user.Upn, user.Password,
)
if err != nil {
t.Fatalf("TestOnBehalfOf: on AcquireTokenByUsernamePassword(): got err == %s, want err == nil", errors.Verbose(err))
}
result6, err := cca.AcquireTokenOnBehalfOf(ctx, result5.AccessToken, scope2)
if err != nil {
t.Fatalf("TestOnBehalfOf: on AcquireTokenOnBehalfOf(): got err == %s, want err == nil", errors.Verbose(err))
}
if result6.AccessToken == "" {
t.Fatal("TestOnBehalfOf: on AcquireTokenOnBehalfOf(): got AccessToken == '', want AccessToken != ''")
}
if result6.AccessToken == result4.AccessToken {
t.Fatal("TestOnBehalfOf: on AcquireTokenOnBehalfOf(): Access Tokens match when they should not")
}
if result6.AccessToken == result3.AccessToken {
t.Fatal("TestOnBehalfOf: on AcquireTokenOnBehalfOf(): Access Tokens match when they should not")
}
if result6.AccessToken == result2.AccessToken {
t.Fatal("TestOnBehalfOf: on AcquireTokenOnBehalfOf(): Access Tokens match when they should not")
}
}
func TestRemoveAccount(t *testing.T) {
if testing.Short() {
t.Skip("skipping integration test")
}
labClientInstance, err := newLabClient()
if err != nil {
panic("failed to get a lab client: " + err.Error())
}
ctx := context.Background()
user := testUser(ctx, "TestRemoveAccount", labClientInstance, url.Values{"usertype": []string{"cloud"}})
app, err := public.New(user.AppID, public.WithAuthority(organizationsAuthority))
if err != nil {
panic(errors.Verbose(err))
}
// Populate the cache
_, err = app.AcquireTokenByUsernamePassword(
context.Background(),
[]string{graphDefaultScope},
user.Upn,
user.Password,
)
if err != nil {
t.Fatalf("TestRemoveAccount: on AcquireTokenByUsernamePassword(): got err == %s, want err == nil", errors.Verbose(err))
}
accounts, err := app.Accounts(ctx)
if err != nil {
t.Fatal(err)
}
if len(accounts) == 0 {
t.Fatal("TestRemoveAccount: No user accounts found in cache")
}
testAccount := accounts[0] // Only one account is populated and that is what we will remove.
err = app.RemoveAccount(ctx, testAccount)
if err != nil {
t.Fatalf("TestRemoveAccount: on RemoveAccount(): got err == %s, want err == nil", errors.Verbose(err))
}
// Remove Account will clear the cache fields associated with this account so acquire token silent should fail
_, err = app.AcquireTokenSilent(ctx, []string{graphDefaultScope}, public.WithSilentAccount(testAccount))
if err == nil {
t.Fatal("TestRemoveAccount: RemoveAccount() didn't clear the cache as expected")
}
}
|