diff --git a/cmd/ausoceantv/main.go b/cmd/ausoceantv/main.go index 3fb1713a..166203e4 100644 --- a/cmd/ausoceantv/main.go +++ b/cmd/ausoceantv/main.go @@ -1,6 +1,8 @@ /* AUTHORS Alan Noble + David Sutton + Trek Hopton LICENSE Copyright (C) 2024 the Australian Ocean Lab (AusOcean) @@ -55,7 +57,7 @@ const ( projectID = "ausoceantv" oauthClientID = "1005382600755-7st09cc91eqcqveviinitqo091dtcmf0.apps.googleusercontent.com" oauthMaxAge = 60 * 60 * 24 * 7 // 7 days. - version = "v0.5.7" + version = "v0.5.8" ) // service defines the properties of our web service. diff --git a/gauth/userauth.go b/gauth/userauth.go index 7ca69f4d..a22d5d79 100644 --- a/gauth/userauth.go +++ b/gauth/userauth.go @@ -219,8 +219,24 @@ func (ua *UserAuth) LoginHandler(h backend.Handler) error { return fmt.Errorf("could not save session %s: %w", sessID, err) } + // Check for refresh token in the user's main session. + hasRefreshToken := false + mainSession, err := h.LoadSession(ua.SessionID) + if err == nil { + tok := &oauth2.Token{} + if err := mainSession.Get(oauthTokenSessionKey, &tok); err == nil && tok != nil && tok.RefreshToken != "" { + hasRefreshToken = true + } + } + + // Build auth URL. + opts := []oauth2.AuthCodeOption{oauth2.AccessTypeOffline} + if !hasRefreshToken { + opts = append(opts, oauth2.SetAuthURLParam("prompt", "consent")) + } + // NB: Offline access is required to obtain a refresh token. - url := ua.cfg.AuthCodeURL(sessID, oauth2.AccessTypeOffline) + url := ua.cfg.AuthCodeURL(sessID, opts...) return h.Redirect(url, http.StatusFound) }