Skip to content

Commit

Permalink
Merge pull request #125 from utilitywarehouse/token-renew
Browse files Browse the repository at this point in the history
Force fetching a new oauth token when using Renew button
  • Loading branch information
ffilippopoulos authored Sep 16, 2021
2 parents 7ce0b81 + 14a1e60 commit 4429cb9
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 20 deletions.
24 changes: 6 additions & 18 deletions agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,27 +114,15 @@ func (a *Agent) callbackHandler(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, rootURL, 302)
}

// renewHandler will initiate the auth challenge. Leases renewal will be handled
// in the callback handler.
func (a *Agent) renewHandler(w http.ResponseWriter, r *http.Request) {
token, err := a.oa.getTokenFromFile()
if err != nil || token.AccessToken == "" || token.Expiry.Before(time.Now()) {
logger.Errorf("cannot get a valid cached token, need a new one")
// Get a url for the token challenge and redirect there
url, err := a.oa.prepareTokenWebChalenge(w)
if err != nil {
fmt.Fprintf(
w,
"error creating web url to get token: %v",
err,
)
return
}
http.Redirect(w, r, url, 302)
url, err := a.oa.prepareTokenWebChalenge(w)
if err != nil {
fmt.Fprintf(w, "error creating web url to get token: %v", err)
return
}
a.renewAllLeases(token.AccessToken)
// Redirect to / after renewing leases
rootURL := fmt.Sprintf("http://%s/", r.Host)
http.Redirect(w, r, rootURL, 302)
http.Redirect(w, r, url, 302)
}

func (a *Agent) mainHandler(w http.ResponseWriter, r *http.Request) {
Expand Down
5 changes: 3 additions & 2 deletions agent_http.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ var htmlStatusTemplate = `
<div>
<h3 class="text-left">Token Info</h2>
{{ if .TokenMissing }}
<p>No token found, please hit Renew Leases button bellow.</p>
<p>No token found, please hit Renew button below.</p>
{{else}}
{{ if .TokenActive }}
<p><b style="color:green;">Active</b><b> until: {{.TokenExpiry}}</b></p>
Expand All @@ -63,8 +63,9 @@ var htmlStatusTemplate = `
<p>Please use renew button bellow.</p>
{{end}}
{{end}}
<i>Fetches a new token before renewing leases.</i>
<form action="/renew" method="get">
<button type="submit">Renew Leases</button>
<button type="submit">Renew</button>
</form>
</div>
<div>
Expand Down

0 comments on commit 4429cb9

Please sign in to comment.