diff --git a/lib/oauthutil/oauthutil.go b/lib/oauthutil/oauthutil.go index 9ac665f5e..0f146a477 100644 --- a/lib/oauthutil/oauthutil.go +++ b/lib/oauthutil/oauthutil.go @@ -5,6 +5,7 @@ import ( "crypto/rand" "encoding/json" "fmt" + "html/template" "log" "net" "net/http" @@ -40,6 +41,35 @@ const ( // RedirectLocalhostURL is redirect to local webserver when active with localhost RedirectLocalhostURL = "http://localhost:" + bindPort + "/" + + // AuthResponse is a template to handle the redirect URL for oauth requests + AuthResponse = ` + +
+ ++{{ if eq .OK false }} +Error: {{ .AuthError.Name }}+ + +` ) // oldToken contains an end-user's tokens. @@ -429,6 +459,13 @@ type AuthError struct { HelpURL string } +// AuthResponseData can fill the AuthResponse template +type AuthResponseData struct { + OK bool // Failure or Success? + Code string // code to paste into rclone config + AuthError +} + // startWebServer runs an internal web server to receive config details func (s *authServer) Start() { fs.Debugf(nil, "Starting auth server on %s", s.bindAddress) @@ -451,31 +488,42 @@ func (s *authServer) Start() { fs.Debugf(nil, "Received request on auth server") code := req.FormValue("code") var err error + var t = template.Must(template.New("authResponse").Parse(AuthResponse)) + resp := AuthResponseData{AuthError: AuthError{}} if code != "" { state := req.FormValue("state") if state != s.state { fs.Debugf(nil, "State did not match: want %q got %q", s.state, state) - _, _ = fmt.Fprintf(w, "
+{{ if .AuthError.Description }}Description: {{ .AuthError.Description }}
{{ end }} +{{ if .AuthError.Code }}Code: {{ .AuthError.Code }}
{{ end }} +{{ if .AuthError.HelpURL }}Look here for help: {{ .AuthError.HelpURL }}
{{ end }} +{{ else }} + {{ if .Code }} +Please copy this code into rclone: +{{ .Code }} + {{ else }} +All done. Please go back to rclone. + {{ end }} +{{ end }} +
Auth state doesn't match
") + resp.OK = false + resp.AuthError = AuthError{ + Name: "Auth State doesn't match", + } } else { fs.Debugf(nil, "Successfully got code") - if s.code != nil { - _, _ = fmt.Fprintf(w, "Go back to rclone to continue
") - } else { - _, _ = fmt.Fprintf(w, "Cut and paste this code into rclone: %s
" + strings.Replace(errorDesc, "\n", "
", -1) + "