@@ -45,41 +45,41 @@ type smSession struct {
4545}
4646
4747// todo: proxy support is not added for the self-managed flow. Add it when there is a requirement.
48- func getSMUserAuthCtx (pinnipedURL , uName , password string , config * proxy.TLSConfig ) (metadata map [string ]string , err error ) {
48+ func getSMUserAuthCtx (pinnipedURL , uName , password string , config * proxy.TLSConfig ) (metadata map [string ]string , expiry time. Time , err error ) {
4949 if pinnipedURL == "" || uName == "" || password == "" {
50- return nil , errors .New ("Invalid auth configuration for self_managed" )
50+ return nil , time. Time {}, errors .New ("Invalid auth configuration for self_managed" )
5151 }
5252
5353 tlsConfig , err := proxy .GetConnectorTLSConfig (config )
5454 if err != nil {
55- return nil , err
55+ return nil , time. Time {}, err
5656 }
5757
5858 session , err := initSession (pinnipedURL , uName , password , tlsConfig )
5959 if err != nil {
60- return nil , err
60+ return nil , time. Time {}, err
6161 }
6262
6363 expectedRedirectURL , err := url .Parse (session .sharedOauthConfig .RedirectURL )
6464 if err != nil {
65- return nil , errors .Wrapf (err , "failed to parse expected redirect URL %s" , session .sharedOauthConfig .RedirectURL )
65+ return nil , time. Time {}, errors .Wrapf (err , "failed to parse expected redirect URL %s" , session .sharedOauthConfig .RedirectURL )
6666 }
6767
6868 actualRedirectURL , err := session .initiateAuthorizeRequestUnamePwd ()
6969 if err != nil {
70- return nil , errors .Wrapf (err , "failed to initiate authorize request with issuer %s" , session .issuerURL )
70+ return nil , time. Time {}, errors .Wrapf (err , "failed to initiate authorize request with issuer %s" , session .issuerURL )
7171 }
7272
7373 // Check that the redirect was to the expected location.
7474 if actualRedirectURL .Scheme != expectedRedirectURL .Scheme ||
7575 actualRedirectURL .Host != expectedRedirectURL .Host || actualRedirectURL .Path != expectedRedirectURL .Path {
76- return nil , fmt .Errorf ("error getting authorization: redirected to the wrong location: %s" ,
76+ return nil , time. Time {}, fmt .Errorf ("error getting authorization: redirected to the wrong location: %s" ,
7777 actualRedirectURL .String ())
7878 }
7979
8080 // validate the state param to detect and prevent CSRF attacks.
8181 if err := session .stateVal .Validate (actualRedirectURL .Query ().Get ("state" )); err != nil {
82- return nil , errors .Wrap (err , "failed to validate state" )
82+ return nil , time. Time {}, errors .Wrap (err , "failed to validate state" )
8383 }
8484
8585 // Get the auth code or return the error from the server.
@@ -90,10 +90,10 @@ func getSMUserAuthCtx(pinnipedURL, uName, password string, config *proxy.TLSConf
9090
9191 optionalErrorDescription := actualRedirectURL .Query ().Get ("error_description" )
9292 if optionalErrorDescription == "" {
93- return nil , fmt .Errorf ("login failed with code %q" , requiredErrorCode )
93+ return nil , time. Time {}, fmt .Errorf ("login failed with code %q" , requiredErrorCode )
9494 }
9595
96- return nil , fmt .Errorf ("login failed with code %q: %s" , requiredErrorCode , optionalErrorDescription )
96+ return nil , time. Time {}, fmt .Errorf ("login failed with code %q: %s" , requiredErrorCode , optionalErrorDescription )
9797 }
9898
9999 customClient := & http.Client {
@@ -111,7 +111,7 @@ func getSMUserAuthCtx(pinnipedURL, uName, password string, config *proxy.TLSConf
111111
112112 token , err := session .sharedOauthConfig .Exchange (tokenCtx , authCode , session .pkceCodePair .Verifier ())
113113 if err != nil {
114- return nil , errors .Wrapf (err , "failed to exchange auth code for oauth tokens" )
114+ return nil , time. Time {}, errors .Wrapf (err , "failed to exchange auth code for oauth tokens" )
115115 }
116116
117117 extraFields := map [string ]interface {}{extraIDToken : token .Extra (extraIDToken ).(string )}
@@ -123,7 +123,7 @@ func getSMUserAuthCtx(pinnipedURL, uName, password string, config *proxy.TLSConf
123123
124124 token = token .WithExtra (extraFields )
125125
126- return getSMHeaders (token ), nil
126+ return getSMHeaders (token ), token . Expiry , nil
127127}
128128
129129// todo: if slowness is experienced, then we can avoid re-initialising same values again.
@@ -251,7 +251,15 @@ func (s *smSession) getAuthCodeURL() string {
251251}
252252
253253func refreshSMUserAuthCtx (config * TanzuContext ) {
254- md , _ := getSMUserAuthCtx (config .VMWCloudEndPoint , config .SMUsername , config .Token , config .TLSConfig )
254+ md , expiry , err := getSMUserAuthCtx (config .VMWCloudEndPoint , config .SMUsername , config .Token , config .TLSConfig )
255+ if err != nil {
256+ return
257+ }
258+
259+ if config .smTokenCache != nil {
260+ config .smTokenCache .Update (md , expiry )
261+ }
262+
255263 for key , value := range md {
256264 config .TMCConnection .Headers .Set (key , value )
257265 }
0 commit comments