Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SM-1371] Add GeneratePassword to the Go SDK #992

Merged
merged 11 commits into from
Aug 23, 2024
7 changes: 7 additions & 0 deletions languages/go/bitwarden_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type BitwardenClientInterface interface {
AccessTokenLogin(accessToken string, stateFile *string) error
Projects() ProjectsInterface
Secrets() SecretsInterface
Generators() GeneratorsInterface
Close()
}

Expand All @@ -19,6 +20,7 @@ type BitwardenClient struct {
commandRunner CommandRunnerInterface
projects ProjectsInterface
secrets SecretsInterface
generators GeneratorsInterface
}

func NewBitwardenClient(apiURL *string, identityURL *string) (BitwardenClientInterface, error) {
Expand Down Expand Up @@ -49,6 +51,7 @@ func NewBitwardenClient(apiURL *string, identityURL *string) (BitwardenClientInt
commandRunner: runner,
projects: NewProjects(runner),
secrets: NewSecrets(runner),
generators: NewGenerators(runner),
}, nil
}

Expand All @@ -73,6 +76,10 @@ func (c *BitwardenClient) Secrets() SecretsInterface {
return c.secrets
}

func (c *BitwardenClient) Generators() GeneratorsInterface {
return c.generators
}

func (c *BitwardenClient) Close() {
c.lib.FreeMem(c.client)
}
21 changes: 21 additions & 0 deletions languages/go/example/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,5 +112,26 @@ func main() {

fmt.Println(string(jsonSecrets))

// Generate a password which can be used as a secret value
request := sdk.PasswordGeneratorRequest{
AvoidAmbiguous: true,
Length: 64,
Lowercase: true,
MinLowercase: new(int64),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

new(int64) is 0 ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, just putting it in there as an example

MinNumber: new(int64),
MinSpecial: new(int64),
MinUppercase: new(int64),
Numbers: true,
Special: true,
Uppercase: true,
}
password, err := bitwardenClient.Generators().GeneratePassword(request)

if err != nil {
panic(err)
}

fmt.Println(*password)

defer bitwardenClient.Close()
}
35 changes: 35 additions & 0 deletions languages/go/generators.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package sdk

type GeneratorsInterface interface {
GeneratePassword(request PasswordGeneratorRequest) (*string, error)
}

type Generators struct {
CommandRunner CommandRunnerInterface
}

func NewGenerators(commandRunner CommandRunnerInterface) *Generators {
return &Generators{CommandRunner: commandRunner}
}

func (s *Generators) executeCommand(command Command, target interface{}) error {
responseStr, err := s.CommandRunner.RunCommand(command)
if err != nil {
return err
}
return checkSuccessAndError(responseStr, target)
}

func (s *Generators) GeneratePassword(request PasswordGeneratorRequest) (*string, error) {
command := Command{
Generators: &GeneratorsCommand{
GeneratePassword: request,
},
}

var response string
if err := s.executeCommand(command, &response); err != nil {
return nil, err
}
return &response, nil
}
Loading