Use consistent server User-Agent across application

pull/370/head
Matt Baer 4 years ago
parent dfa14c9c92
commit 95273697f4
  1. 4
      activitypub.go
  2. 10
      app.go
  3. 2
      oauth.go
  4. 4
      oauth_generic.go
  5. 4
      oauth_gitea.go
  6. 4
      oauth_gitlab.go
  7. 4
      oauth_slack.go
  8. 4
      oauth_writeas.go

@ -494,7 +494,7 @@ func makeActivityPost(hostName string, p *activitystreams.Person, url string, m
r, _ := http.NewRequest("POST", url, bytes.NewBuffer(b)) r, _ := http.NewRequest("POST", url, bytes.NewBuffer(b))
r.Header.Add("Content-Type", "application/activity+json") r.Header.Add("Content-Type", "application/activity+json")
r.Header.Set("User-Agent", "Go ("+serverSoftware+"/"+softwareVer+"; +"+hostName+")") r.Header.Set("User-Agent", ServerUserAgent(hostName))
h := sha256.New() h := sha256.New()
h.Write(b) h.Write(b)
r.Header.Add("Digest", "SHA-256="+base64.StdEncoding.EncodeToString(h.Sum(nil))) r.Header.Add("Digest", "SHA-256="+base64.StdEncoding.EncodeToString(h.Sum(nil)))
@ -544,7 +544,7 @@ func resolveIRI(hostName, url string) ([]byte, error) {
r, _ := http.NewRequest("GET", url, nil) r, _ := http.NewRequest("GET", url, nil)
r.Header.Add("Accept", "application/activity+json") r.Header.Add("Accept", "application/activity+json")
r.Header.Set("User-Agent", "Go ("+serverSoftware+"/"+softwareVer+"; +"+hostName+")") r.Header.Set("User-Agent", ServerUserAgent(hostName))
if debugging { if debugging {
dump, err := httputil.DumpRequestOut(r, true) dump, err := httputil.DumpRequestOut(r, true)

@ -903,3 +903,13 @@ func adminInitDatabase(app *App) error {
log.Info("Done.") log.Info("Done.")
return nil return nil
} }
// ServerUserAgent returns a User-Agent string to use in external requests. The
// hostName parameter may be left empty.
func ServerUserAgent(hostName string) string {
hostUAStr := ""
if hostName != "" {
hostUAStr = "; +" + hostName
}
return "Go (" + serverSoftware + "/" + softwareVer + hostUAStr + ")"
}

@ -408,7 +408,7 @@ func (r *callbackProxyClient) register(ctx context.Context, state string) error
if err != nil { if err != nil {
return err return err
} }
req.Header.Set("User-Agent", "writefreely") req.Header.Set("User-Agent", ServerUserAgent(""))
req.Header.Set("Accept", "application/json") req.Header.Set("Accept", "application/json")
req.Header.Set("Content-Type", "application/x-www-form-urlencoded") req.Header.Set("Content-Type", "application/x-www-form-urlencoded")

@ -62,7 +62,7 @@ func (c genericOauthClient) exchangeOauthCode(ctx context.Context, code string)
return nil, err return nil, err
} }
req.WithContext(ctx) req.WithContext(ctx)
req.Header.Set("User-Agent", "writefreely") req.Header.Set("User-Agent", ServerUserAgent(""))
req.Header.Set("Accept", "application/json") req.Header.Set("Accept", "application/json")
req.Header.Set("Content-Type", "application/x-www-form-urlencoded") req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.SetBasicAuth(c.ClientID, c.ClientSecret) req.SetBasicAuth(c.ClientID, c.ClientSecret)
@ -91,7 +91,7 @@ func (c genericOauthClient) inspectOauthAccessToken(ctx context.Context, accessT
return nil, err return nil, err
} }
req.WithContext(ctx) req.WithContext(ctx)
req.Header.Set("User-Agent", "writefreely") req.Header.Set("User-Agent", ServerUserAgent(""))
req.Header.Set("Accept", "application/json") req.Header.Set("Accept", "application/json")
req.Header.Set("Authorization", "Bearer "+accessToken) req.Header.Set("Authorization", "Bearer "+accessToken)

@ -62,7 +62,7 @@ func (c giteaOauthClient) exchangeOauthCode(ctx context.Context, code string) (*
return nil, err return nil, err
} }
req.WithContext(ctx) req.WithContext(ctx)
req.Header.Set("User-Agent", "writefreely") req.Header.Set("User-Agent", ServerUserAgent(""))
req.Header.Set("Accept", "application/json") req.Header.Set("Accept", "application/json")
req.Header.Set("Content-Type", "application/x-www-form-urlencoded") req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.SetBasicAuth(c.ClientID, c.ClientSecret) req.SetBasicAuth(c.ClientID, c.ClientSecret)
@ -91,7 +91,7 @@ func (c giteaOauthClient) inspectOauthAccessToken(ctx context.Context, accessTok
return nil, err return nil, err
} }
req.WithContext(ctx) req.WithContext(ctx)
req.Header.Set("User-Agent", "writefreely") req.Header.Set("User-Agent", ServerUserAgent(""))
req.Header.Set("Accept", "application/json") req.Header.Set("Accept", "application/json")
req.Header.Set("Authorization", "Bearer "+accessToken) req.Header.Set("Authorization", "Bearer "+accessToken)

@ -63,7 +63,7 @@ func (c gitlabOauthClient) exchangeOauthCode(ctx context.Context, code string) (
return nil, err return nil, err
} }
req.WithContext(ctx) req.WithContext(ctx)
req.Header.Set("User-Agent", "writefreely") req.Header.Set("User-Agent", ServerUserAgent(""))
req.Header.Set("Accept", "application/json") req.Header.Set("Accept", "application/json")
req.Header.Set("Content-Type", "application/x-www-form-urlencoded") req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.SetBasicAuth(c.ClientID, c.ClientSecret) req.SetBasicAuth(c.ClientID, c.ClientSecret)
@ -92,7 +92,7 @@ func (c gitlabOauthClient) inspectOauthAccessToken(ctx context.Context, accessTo
return nil, err return nil, err
} }
req.WithContext(ctx) req.WithContext(ctx)
req.Header.Set("User-Agent", "writefreely") req.Header.Set("User-Agent", ServerUserAgent(""))
req.Header.Set("Accept", "application/json") req.Header.Set("Accept", "application/json")
req.Header.Set("Authorization", "Bearer "+accessToken) req.Header.Set("Authorization", "Bearer "+accessToken)

@ -111,7 +111,7 @@ func (c slackOauthClient) exchangeOauthCode(ctx context.Context, code string) (*
return nil, err return nil, err
} }
req.WithContext(ctx) req.WithContext(ctx)
req.Header.Set("User-Agent", "writefreely") req.Header.Set("User-Agent", ServerUserAgent(""))
req.Header.Set("Accept", "application/json") req.Header.Set("Accept", "application/json")
req.Header.Set("Content-Type", "application/x-www-form-urlencoded") req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.SetBasicAuth(c.ClientID, c.ClientSecret) req.SetBasicAuth(c.ClientID, c.ClientSecret)
@ -140,7 +140,7 @@ func (c slackOauthClient) inspectOauthAccessToken(ctx context.Context, accessTok
return nil, err return nil, err
} }
req.WithContext(ctx) req.WithContext(ctx)
req.Header.Set("User-Agent", "writefreely") req.Header.Set("User-Agent", ServerUserAgent(""))
req.Header.Set("Accept", "application/json") req.Header.Set("Accept", "application/json")
req.Header.Set("Authorization", "Bearer "+accessToken) req.Header.Set("Authorization", "Bearer "+accessToken)

@ -62,7 +62,7 @@ func (c writeAsOauthClient) exchangeOauthCode(ctx context.Context, code string)
return nil, err return nil, err
} }
req.WithContext(ctx) req.WithContext(ctx)
req.Header.Set("User-Agent", "writefreely") req.Header.Set("User-Agent", ServerUserAgent(""))
req.Header.Set("Accept", "application/json") req.Header.Set("Accept", "application/json")
req.Header.Set("Content-Type", "application/x-www-form-urlencoded") req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.SetBasicAuth(c.ClientID, c.ClientSecret) req.SetBasicAuth(c.ClientID, c.ClientSecret)
@ -91,7 +91,7 @@ func (c writeAsOauthClient) inspectOauthAccessToken(ctx context.Context, accessT
return nil, err return nil, err
} }
req.WithContext(ctx) req.WithContext(ctx)
req.Header.Set("User-Agent", "writefreely") req.Header.Set("User-Agent", ServerUserAgent(""))
req.Header.Set("Accept", "application/json") req.Header.Set("Accept", "application/json")
req.Header.Set("Authorization", "Bearer "+accessToken) req.Header.Set("Authorization", "Bearer "+accessToken)

Loading…
Cancel
Save