2020-08-07 12:55:02 -04:00
|
|
|
package authmethods
|
2020-07-14 14:32:47 -07:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"fmt"
|
|
|
|
|
|
2020-08-10 17:15:02 -04:00
|
|
|
"github.com/hashicorp/boundary/api"
|
|
|
|
|
"github.com/hashicorp/boundary/api/authtokens"
|
2020-07-14 14:32:47 -07:00
|
|
|
)
|
|
|
|
|
|
2020-08-10 17:15:02 -04:00
|
|
|
// TODO: This will need to be changed when we add Auth Method API to boundary. We'll also need a better
|
2020-07-14 14:32:47 -07:00
|
|
|
// way to handle different auth method types.
|
2020-08-12 21:57:44 -04:00
|
|
|
func (c *AuthMethodsClient) Authenticate(ctx context.Context, authMethodId, name, password string, opt ...Option) (*authtokens.AuthToken, *api.Error, error) {
|
2020-08-07 12:55:02 -04:00
|
|
|
if c.client == nil {
|
2020-07-14 14:32:47 -07:00
|
|
|
return nil, nil, fmt.Errorf("nil client in Authenticate request")
|
|
|
|
|
}
|
2020-07-22 00:14:28 -04:00
|
|
|
|
2020-08-07 12:55:02 -04:00
|
|
|
_, apiOpts := getOpts(opt...)
|
2020-07-14 14:32:47 -07:00
|
|
|
|
|
|
|
|
reqBody := map[string]interface{}{
|
|
|
|
|
"credentials": map[string]string{
|
2020-08-11 17:25:00 -04:00
|
|
|
"login_name": name,
|
|
|
|
|
"password": password,
|
2020-07-14 14:32:47 -07:00
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-07 12:55:02 -04:00
|
|
|
req, err := c.client.NewRequest(ctx, "POST", fmt.Sprintf("auth-methods/%s:authenticate", authMethodId), reqBody, apiOpts...)
|
2020-07-14 14:32:47 -07:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, nil, fmt.Errorf("error creating Authenticate request: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-07 12:55:02 -04:00
|
|
|
resp, err := c.client.Do(req)
|
2020-07-14 14:32:47 -07:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, nil, fmt.Errorf("error performing client request during Authenticate call: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
target := new(authtokens.AuthToken)
|
|
|
|
|
apiErr, err := resp.Decode(target)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, nil, fmt.Errorf("error decoding Authenticate response: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if target.Token != "" {
|
2020-08-07 12:55:02 -04:00
|
|
|
c.client.SetToken(target.Token)
|
2020-07-14 14:32:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return target, apiErr, nil
|
|
|
|
|
}
|