Skip to content

Commit

Permalink
Add ToznySDKV3 config storage
Browse files Browse the repository at this point in the history
  • Loading branch information
Eli Fabens committed Apr 28, 2021
1 parent c19758c commit cb9782d
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 9 deletions.
28 changes: 26 additions & 2 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -686,6 +687,7 @@ func NewToznySDKV3(config ToznySDKConfig) (*ToznySDKV3, error) {
AccountPassword: config.AccountPassword,
APIEndpoint: config.APIEndpoint,
ClientID: config.ClientID,
config: config.ClientConfig,
}, nil
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 == "" {
Expand Down
21 changes: 15 additions & 6 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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) {
Expand Down Expand Up @@ -214,3 +219,7 @@ func LoadConfigFile(configPath string) (ToznySDKJSONConfig, error) {
}
return config, nil
}

func StoreConfigFile(configPath string, config ToznySDKJSONConfig) (error) {
return saveJson(configPath, config)
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down

0 comments on commit cb9782d

Please sign in to comment.