diff --git a/client.go b/client.go index e97e3d2..68dc9c6 100644 --- a/client.go +++ b/client.go @@ -659,6 +659,7 @@ type ToznySDKV3 struct { APIEndpoint string // Tozny server defined globally unique id for this Client. ClientID string + config e3dbClients.ClientConfig } // ToznySDKConfig wraps parameters needed to configure a ToznySDK @@ -686,6 +687,7 @@ func NewToznySDKV3(config ToznySDKConfig) (*ToznySDKV3, error) { AccountPassword: config.AccountPassword, APIEndpoint: config.APIEndpoint, ClientID: config.ClientID, + config: config.ClientConfig, }, nil } @@ -738,6 +740,7 @@ type LoginActionData = map[string]string type IdentitySessionIntermediateResponse = identityClient.IdentitySessionRequestResponse +// TozIDLoginRequest is used to login to a TozID account to get a ToznySDKV3 or active TozID session (future plan) type TozIDLoginRequest struct { Username string Password string @@ -765,7 +768,7 @@ func GetSDKV3ForTozIDUser(login TozIDLoginRequest) (*ToznySDKV3, error) { realmInfo, err := anonymousClient.RealmInfo(ctx, login.RealmName) if err != nil { // TODO: better error message for failure to get realmInfo - return nil, err + return nil, fmt.Errorf("GetSDKV3ForTozIDUser: failed to get realm infor with error %w", err) } noteName, encryptionKeys, signingKeys, err := e3dbClients.DeriveIdentityCredentials(username, login.Password, realmInfo.Name, "") if err != nil { @@ -831,10 +834,10 @@ func GetSDKV3ForTozIDUser(login TozIDLoginRequest) (*ToznySDKV3, error) { reader = &buf } request, err := http.NewRequest("POST", sessionResponse.ActionURL, reader) - request.Header.Set("Content-Type", sessionResponse.ContentType) if err != nil { return nil, err } + request.Header.Set("Content-Type", sessionResponse.ContentType) err = e3dbClients.MakeSignedServiceCall(ctx, &http.Client{}, request, signingKeys, "", &sessionResponse) if err != nil { return nil, err @@ -925,6 +928,27 @@ type ClientConfig struct { PrivateSigningKey string `json:"private_signing_key"` } +// StoreConfigFile stores a ToznySDKV3 config file at the specified path, returning an error if any +func (c *ToznySDKV3) StoreConfigFile(path string) (error) { + config := ToznySDKJSONConfig{ + ConfigFile: ConfigFile{ + Version: 2, + APIBaseURL: c.APIEndpoint, + APIKeyID: c.config.APIKey, + APISecret: c.config.APISecret, + ClientID: c.config.ClientID, + ClientEmail: "", + PublicKey: c.config.EncryptionKeys.Public.Material, + PrivateKey: c.config.EncryptionKeys.Private.Material, + }, + PublicSigningKey: c.config.SigningKeys.Public.Material, + PrivateSigningKey: c.config.SigningKeys.Private.Material, + AccountUsername: c.AccountUsername, + AccountPassword: c.AccountPassword, + } + return saveJson(path, config) +} + // Register attempts to create a valid TozStore account returning the root client config for the created account and error (if any). func (c *ToznySDKV3) Register(ctx context.Context, name string, email string, password string, apiURL string) (RegisterAccountResponse, error) { if apiURL == "" { diff --git a/config.go b/config.go index b352611..788ba69 100644 --- a/config.go +++ b/config.go @@ -109,7 +109,7 @@ func loadConfig(configPath string) (*ClientOpts, error) { }, nil } -func saveConfig(configPath string, opts *ClientOpts) error { +func saveJson(configPath string, obj interface{}) error { configFullPath, err := homedir.Expand(configPath) if err != nil { return err @@ -126,6 +126,15 @@ func saveConfig(configPath string, opts *ClientOpts) error { } defer configFd.Close() + + if err = json.NewEncoder(configFd).Encode(&obj); err != nil { + return err + } + + return nil +} + +func saveConfig(configPath string, opts *ClientOpts) error { configObj := configFile{ Version: 1, ClientID: opts.ClientID, @@ -137,11 +146,7 @@ func saveConfig(configPath string, opts *ClientOpts) error { PrivateKey: encodePrivateKey(opts.PrivateKey), } - if err = json.NewEncoder(configFd).Encode(&configObj); err != nil { - return err - } - - return nil + return saveJson(configPath, configObj) } func fileExists(name string) (bool, error) { @@ -214,3 +219,7 @@ func LoadConfigFile(configPath string) (ToznySDKJSONConfig, error) { } return config, nil } + +func StoreConfigFile(configPath string, config ToznySDKJSONConfig) (error) { + return saveJson(configPath, config) +} diff --git a/go.mod b/go.mod index 1f6ecd3..2e58b23 100644 --- a/go.mod +++ b/go.mod @@ -7,7 +7,7 @@ require ( github.com/jawher/mow.cli v1.0.4 github.com/mitchellh/go-homedir v1.0.0 github.com/stretchr/testify v1.6.1 // indirect - github.com/tozny/e3db-clients-go v0.0.144-0.20210428154208-cc5c7c2fe4ee + github.com/tozny/e3db-clients-go v0.0.144 golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2 golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45 )