Add a new configuration option: UserAgent

This commit is contained in:
Chun-Hung Tseng
2023-07-01 10:08:07 +02:00
parent a4e81165f9
commit b675a36304
3 changed files with 9 additions and 2 deletions

View File

@@ -5,6 +5,7 @@ import "os"
type Config struct {
/* Constants */
AppVersion string
UserAgent string
/* Login */
FirstLoginCredential *FirstLoginCredentialData
@@ -37,6 +38,7 @@ type ReusableCredentialData struct {
func NewConfigWithDefaultValues() *Config {
return &Config{
AppVersion: "",
UserAgent: "",
FirstLoginCredential: &FirstLoginCredentialData{
Username: "",
@@ -62,6 +64,8 @@ func NewConfigWithDefaultValues() *Config {
func NewConfigForIntegrationTests() *Config {
appVersion := os.Getenv("PROTON_API_BRIDGE_APP_VERSION")
userAgent := os.Getenv("PROTON_API_BRIDGE_USER_AGENT")
username := os.Getenv("PROTON_API_BRIDGE_TEST_USERNAME")
password := os.Getenv("PROTON_API_BRIDGE_TEST_PASSWORD")
twoFA := os.Getenv("PROTON_API_BRIDGE_TEST_TWOFA")
@@ -79,6 +83,7 @@ func NewConfigForIntegrationTests() *Config {
return &Config{
AppVersion: appVersion,
UserAgent: userAgent,
FirstLoginCredential: &FirstLoginCredentialData{
Username: username,

View File

@@ -4,10 +4,11 @@ import (
"github.com/henrybear327/go-proton-api"
)
func getProtonManager(appVersion string) *proton.Manager {
func getProtonManager(appVersion string, userAgent string) *proton.Manager {
/* Notes on API calls: if the app version is not specified, the api calls will be rejected. */
options := []proton.Option{
proton.WithAppVersion(appVersion),
proton.WithUserAgent(userAgent),
}
m := proton.New(options...)

View File

@@ -48,7 +48,7 @@ func Login(ctx context.Context, config *Config) (*proton.Manager, *proton.Client
var addr []proton.Address
// get manager
m := getProtonManager(config.AppVersion)
m := getProtonManager(config.AppVersion, config.UserAgent)
if config.UseReusableLogin {
c = m.NewClient(config.ReusableCredential.UID, config.ReusableCredential.AccessToken, config.ReusableCredential.RefreshToken)
@@ -81,6 +81,7 @@ func Login(ctx context.Context, config *Config) (*proton.Manager, *proton.Client
if err != nil {
return nil, nil, nil, nil, nil, err
}
// log.Printf("Available scopes %#v", auth.Scope)
if auth.TwoFA.Enabled&proton.HasTOTP != 0 {
if config.FirstLoginCredential.TwoFA != "" {