1 //go:build !go1.13
2 // +build !go1.13
3 4 // Copyright 2017 Microsoft Corporation
5 //
6 // Licensed under the Apache License, Version 2.0 (the "License");
7 // you may not use this file except in compliance with the License.
8 // You may obtain a copy of the License at
9 //
10 // http://www.apache.org/licenses/LICENSE-2.0
11 //
12 // Unless required by applicable law or agreed to in writing, software
13 // distributed under the License is distributed on an "AS IS" BASIS,
14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 // See the License for the specific language governing permissions and
16 // limitations under the License.
17 18 package adal
19 20 import (
21 "context"
22 "net/http"
23 "time"
24 )
25 26 func getMSIEndpoint(ctx context.Context, sender Sender) (*http.Response, error) {
27 tempCtx, cancel := context.WithTimeout(ctx, 2*time.Second)
28 defer cancel()
29 req, _ := http.NewRequest(http.MethodGet, msiEndpoint, nil)
30 req = req.WithContext(tempCtx)
31 q := req.URL.Query()
32 q.Add("api-version", msiAPIVersion)
33 req.URL.RawQuery = q.Encode()
34 return sender.Do(req)
35 }
36 37 // EnsureFreshWithContext will refresh the token if it will expire within the refresh window (as set by
38 // RefreshWithin) and autoRefresh flag is on. This method is safe for concurrent use.
39 func (mt *MultiTenantServicePrincipalToken) EnsureFreshWithContext(ctx context.Context) error {
40 if err := mt.PrimaryToken.EnsureFreshWithContext(ctx); err != nil {
41 return err
42 }
43 for _, aux := range mt.AuxiliaryTokens {
44 if err := aux.EnsureFreshWithContext(ctx); err != nil {
45 return err
46 }
47 }
48 return nil
49 }
50 51 // RefreshWithContext obtains a fresh token for the Service Principal.
52 func (mt *MultiTenantServicePrincipalToken) RefreshWithContext(ctx context.Context) error {
53 if err := mt.PrimaryToken.RefreshWithContext(ctx); err != nil {
54 return err
55 }
56 for _, aux := range mt.AuxiliaryTokens {
57 if err := aux.RefreshWithContext(ctx); err != nil {
58 return err
59 }
60 }
61 return nil
62 }
63 64 // RefreshExchangeWithContext refreshes the token, but for a different resource.
65 func (mt *MultiTenantServicePrincipalToken) RefreshExchangeWithContext(ctx context.Context, resource string) error {
66 if err := mt.PrimaryToken.RefreshExchangeWithContext(ctx, resource); err != nil {
67 return err
68 }
69 for _, aux := range mt.AuxiliaryTokens {
70 if err := aux.RefreshExchangeWithContext(ctx, resource); err != nil {
71 return err
72 }
73 }
74 return nil
75 }
76