Skip to content

Commit

Permalink
Enable signup to be used against arbitarity Tozny endpoints, misc rel…
Browse files Browse the repository at this point in the history
…ated changes.
  • Loading branch information
Levi Sky committed Feb 10, 2021
1 parent 04131e8 commit 8d1a19b
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 24 deletions.
29 changes: 16 additions & 13 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func GetClient(opts ClientOpts) (*Client, error) {
}

// RegisterClient creates a new client for a given InnoVault account
func RegisterClient(registrationToken string, clientName string, publicKey string, privateKey string, backup bool, apiURL string) (*ClientDetails, error) {
func RegisterClient(registrationToken string, clientName string, publicKey string, privateKey string, backup bool, apiURL string) (*ClientDetails, string, error) {
if apiURL == "" {
apiURL = defaultStorageURL
}
Expand All @@ -169,29 +169,29 @@ func RegisterClient(registrationToken string, clientName string, publicKey strin
req, err := http.NewRequest("POST", fmt.Sprintf("%s/v1/account/e3db/clients/register", apiURL), buf)

if err != nil {
return nil, err
return nil, apiURL, err
}

client := &http.Client{}

resp, err := client.Do(req)
if err != nil {
return nil, err
return nil, apiURL, err
}

defer closeResp(resp)

var details *ClientDetails
if err := json.NewDecoder(resp.Body).Decode(&details); err != nil {
closeResp(resp)
return nil, err
return nil, apiURL, err
}

backupClient := resp.Header.Get("X-Backup-Client")

if backup {
if privateKey == "" {
return nil, errors.New("Cannot back up client credentials without a private key!")
return nil, apiURL, errors.New("Cannot back up client credentials without a private key!")
}

pubBytes, _ := base64.RawURLEncoding.DecodeString(publicKey)
Expand All @@ -211,13 +211,13 @@ func RegisterClient(registrationToken string, clientName string, publicKey strin
client, err := GetClient(*config)
if err != nil {
closeResp(resp)
return nil, err
return nil, apiURL, err
}

client.Backup(context.Background(), backupClient, registrationToken)
}

return details, nil
return details, apiURL, nil
}

func (c *Client) apiURL() string {
Expand Down Expand Up @@ -782,7 +782,10 @@ type ClientConfig struct {
}

// 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) (RegisterAccountResponse, error) {
func (c *ToznySDKV3) Register(ctx context.Context, name string, email string, password string, apiURL string) (RegisterAccountResponse, error) {
if apiURL == "" {
apiURL = defaultStorageURL
}
const (
pwEncSalt = "pwEncSalt"
pwAuthSalt = "pwAuthSalt"
Expand All @@ -791,14 +794,14 @@ func (c *ToznySDKV3) Register(ctx context.Context, name string, email string, pa
)
// Boot client
bootClientConfig := e3dbClients.ClientConfig{
Host: c.APIEndpoint,
AuthNHost: c.APIEndpoint,
Host: apiURL,
AuthNHost: apiURL,
}
bootClient := accountClient.New(bootClientConfig)
var createResponse RegisterAccountResponse
var accountClientConfig = e3dbClients.ClientConfig{
Host: c.APIEndpoint,
AuthNHost: c.APIEndpoint,
Host: apiURL,
AuthNHost: apiURL,
}
var accountResponse *accountClient.CreateAccountResponse

Expand Down Expand Up @@ -866,7 +869,7 @@ func (c *ToznySDKV3) Register(ctx context.Context, name string, email string, pa
}
clientConfig := ClientConfig{
Version: 2,
APIURL: c.APIEndpoint,
APIURL: apiURL,
ClientID: accountResponse.Account.Client.ClientID,
APIKeyID: accountResponse.Account.Client.APIKeyID,
APISecret: accountResponse.Account.Client.APISecretKey,
Expand Down
6 changes: 3 additions & 3 deletions client_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ func setup() {
pubBytes2, _ := base64Decode(pub2)
privBytes2, _ := base64Decode(priv2)

clientDetails, err := RegisterClient(token, clientName, pub, "", false, apiURL)
clientDetails, _, err := RegisterClient(token, clientName, pub, "", false, apiURL)
if err != nil {
dieErr(err)
}

shareClientDetails, err := RegisterClient(token, shareClientName, pub2, "", false, apiURL)
shareClientDetails, _, err := RegisterClient(token, shareClientName, pub2, "", false, apiURL)
if err != nil {
dieErr(err)
}
Expand Down Expand Up @@ -125,7 +125,7 @@ func TestRegistration(t *testing.T) {

clientName := "test-client-" + base64Encode(randomSecretKey()[:8])

client, err := RegisterClient(token, clientName, pub, "", false, apiURL)
client, _, err := RegisterClient(token, clientName, pub, "", false, apiURL)

if err != nil {
t.Fatal(err)
Expand Down
30 changes: 24 additions & 6 deletions cmd/e3db/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -607,7 +607,7 @@ func cmdRegister(cmd *cli.Cmd) {

publicKey := base64.RawURLEncoding.EncodeToString(pub[:])

details, err := e3db.RegisterClient(*token, *email, publicKey, "", false, *apiBaseURL)
details, apiURL, err := e3db.RegisterClient(*token, *email, publicKey, "", false, *apiBaseURL)

if err != nil {
dieErr(err)
Expand All @@ -620,7 +620,7 @@ func cmdRegister(cmd *cli.Cmd) {
APISecret: details.ApiSecret,
PublicKey: pub,
PrivateKey: priv,
APIBaseURL: *apiBaseURL,
APIBaseURL: apiURL,
Logging: false,
}

Expand Down Expand Up @@ -683,6 +683,12 @@ func cmdListRealms(cmd *cli.Cmd) {
}

func cmdSignup(cmd *cli.Cmd) {
apiBaseURL := cmd.String(cli.StringOpt{
Name: "api",
Desc: "e3db api base url",
Value: "",
HideValue: true,
})
accountName := cmd.String(cli.StringArg{
Name: "NAME",
Desc: "Account display name",
Expand All @@ -706,16 +712,28 @@ func cmdSignup(cmd *cli.Cmd) {
cmd.Spec = "[OPTIONS] [NAME] [EMAIL] [PASSWORD]"

cmd.Action = func() {
sdk, err := e3db.GetSDKV3(fmt.Sprintf(e3db.ProfileInterpolationConfigFilePath, *options.Profile))
sdk := e3db.ToznySDKV3{}
ctx := context.Background()
createdAccount, err := sdk.Register(ctx, *accountName, *accountEmail, *accountPassword, *apiBaseURL)
if err != nil {
dieErr(err)
}
ctx := context.Background()
createdAccount, err := sdk.Register(ctx, *accountName, *accountEmail, *accountPassword)
accountClientConfig := createdAccount.Account.Config
accountClientInfo := &e3db.ClientOpts{
ClientID: accountClientConfig.ClientID,
ClientEmail: accountClientConfig.ClientEmail,
APIKeyID: accountClientConfig.APIKeyID,
APISecret: accountClientConfig.APISecret,
PublicKey: e3db.MakePublicKey([]byte(accountClientConfig.PublicKey)),
PrivateKey: e3db.MakePrivateKey([]byte(accountClientConfig.PrivateKey)),
APIBaseURL: accountClientConfig.APIURL,
Logging: false,
}

err = e3db.SaveConfig(*options.Profile, accountClientInfo)
if err != nil {
dieErr(err)
}
fmt.Printf("Created Account %+v\n", createdAccount)
}
}

Expand Down
4 changes: 2 additions & 2 deletions example_registration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func ExampleRegisterClient() {

// Passing all of the data above into the registration routine will create
// a new client with the system. Remember to keep your private key private!
client_info, err := e3db.RegisterClient(token, client_name, public_key, "", false, "https://api.e3db.com")
client_info, _, err := e3db.RegisterClient(token, client_name, public_key, "", false, "https://api.e3db.com")
if err != nil {
fmt.Fprintf(os.Stderr, "Unhandled error: %s\n", err)
log.Fatal(err)
Expand All @@ -70,7 +70,7 @@ func ExampleRegisterClient() {
//
// Client credentials are not backed up by default.

// client_info := e3db.RegisterClient(token, client_name, public_key, private_key, true, "https://api.e3db.com")
// client_info, _, err := e3db.RegisterClient(token, client_name, public_key, private_key, true, "https://api.e3db.com")
// if err != nil {
// fmt.Fprintf(os.Stderr, "Unhandled error: %s\n", err)
// log.Fatal(err)
Expand Down

0 comments on commit 8d1a19b

Please sign in to comment.