| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
package adal |
| 19 |
|
| 20 |
import ( |
| 21 |
"context" |
| 22 |
"fmt" |
| 23 |
"net/http" |
| 24 |
"time" |
| 25 |
) |
| 26 |
|
| 27 |
func getMSIEndpoint(ctx context.Context, sender Sender) (*http.Response, error) { |
| 28 |
tempCtx, cancel := context.WithTimeout(ctx, 2*time.Second) |
| 29 |
defer cancel() |
| 30 |
|
| 31 |
req, _ := http.NewRequestWithContext(tempCtx, http.MethodGet, msiEndpoint, nil) |
| 32 |
q := req.URL.Query() |
| 33 |
q.Add("api-version", msiAPIVersion) |
| 34 |
req.URL.RawQuery = q.Encode() |
| 35 |
return sender.Do(req) |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
func (mt *MultiTenantServicePrincipalToken) EnsureFreshWithContext(ctx context.Context) error { |
| 41 |
if err := mt.PrimaryToken.EnsureFreshWithContext(ctx); err != nil { |
| 42 |
return fmt.Errorf("failed to refresh primary token: %w", err) |
| 43 |
} |
| 44 |
for _, aux := range mt.AuxiliaryTokens { |
| 45 |
if err := aux.EnsureFreshWithContext(ctx); err != nil { |
| 46 |
return fmt.Errorf("failed to refresh auxiliary token: %w", err) |
| 47 |
} |
| 48 |
} |
| 49 |
return nil |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
func (mt *MultiTenantServicePrincipalToken) RefreshWithContext(ctx context.Context) error { |
| 54 |
if err := mt.PrimaryToken.RefreshWithContext(ctx); err != nil { |
| 55 |
return fmt.Errorf("failed to refresh primary token: %w", err) |
| 56 |
} |
| 57 |
for _, aux := range mt.AuxiliaryTokens { |
| 58 |
if err := aux.RefreshWithContext(ctx); err != nil { |
| 59 |
return fmt.Errorf("failed to refresh auxiliary token: %w", err) |
| 60 |
} |
| 61 |
} |
| 62 |
return nil |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
func (mt *MultiTenantServicePrincipalToken) RefreshExchangeWithContext(ctx context.Context, resource string) error { |
| 67 |
if err := mt.PrimaryToken.RefreshExchangeWithContext(ctx, resource); err != nil { |
| 68 |
return fmt.Errorf("failed to refresh primary token: %w", err) |
| 69 |
} |
| 70 |
for _, aux := range mt.AuxiliaryTokens { |
| 71 |
if err := aux.RefreshExchangeWithContext(ctx, resource); err != nil { |
| 72 |
return fmt.Errorf("failed to refresh auxiliary token: %w", err) |
| 73 |
} |
| 74 |
} |
| 75 |
return nil |
| 76 |
} |