-
Notifications
You must be signed in to change notification settings - Fork 134
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4289 from cloudfoundry/cf-push-token-expiry
CF Push: A better fix for the token expiry issue
- Loading branch information
Showing
5 changed files
with
77 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package cfapppush | ||
|
||
import ( | ||
"time" | ||
|
||
"code.cloudfoundry.org/cli/api/cloudcontroller" | ||
"code.cloudfoundry.org/cli/command" | ||
|
||
"github.com/cloudfoundry-incubator/stratos/src/jetstream/repository/interfaces" | ||
) | ||
|
||
// PushConnectionWrapper can wrap a given connection allowing the wrapper to modify | ||
// all requests going in and out of the given connection. | ||
type PushConnectionWrapper struct { | ||
inner cloudcontroller.Connection | ||
portalProxy interfaces.PortalProxy | ||
config *CFPushAppConfig | ||
cmdConfig command.Config | ||
} | ||
|
||
// Wrap an existing connection | ||
func (cw PushConnectionWrapper) Wrap(innerconnection cloudcontroller.Connection) cloudcontroller.Connection { | ||
cw.inner = innerconnection | ||
return cw | ||
} | ||
|
||
// Make makes an HTTP request | ||
func (cw PushConnectionWrapper) Make(request *cloudcontroller.Request, passedResponse *cloudcontroller.Response) error { | ||
// Check to see if the token is about to expire, if it is, refresh it first | ||
token, found := cw.portalProxy.GetCNSITokenRecord(cw.config.EndpointID, cw.config.UserID) | ||
if found { | ||
// Aways update the access token, in case someone else refreshed it | ||
cw.config.AuthToken = token.AuthToken | ||
|
||
// Check if this is about to expire in the next 30 seconds | ||
expiry := token.TokenExpiry - 30 | ||
expTime := time.Unix(expiry, 0) | ||
if expTime.Before(time.Now()) { | ||
cnsiRecord, err := cw.portalProxy.GetCNSIRecord(cw.config.EndpointID) | ||
if err == nil { | ||
// Refresh token first - makes sure it will be valid when we do the push | ||
refreshedTokenRec, err := cw.portalProxy.RefreshOAuthToken(cnsiRecord.SkipSSLValidation, cnsiRecord.GUID, cw.config.UserID, cnsiRecord.ClientId, cnsiRecord.ClientSecret, cnsiRecord.TokenEndpoint) | ||
if err == nil { | ||
cw.config.AuthToken = refreshedTokenRec.AuthToken | ||
} | ||
} | ||
} | ||
} | ||
|
||
cw.cmdConfig.SetAccessToken("bearer " + cw.config.AuthToken) | ||
|
||
return cw.inner.Make(request, passedResponse) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters