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/authtokens"
|
2020-07-14 14:32:47 -07:00
|
|
|
)
|
|
|
|
|
|
2020-09-24 14:34:58 -07:00
|
|
|
func (c *Client) Authenticate(ctx context.Context, authMethodId string, credentials map[string]interface{}, opt ...Option) (*authtokens.AuthTokenReadResult, error) {
|
2020-08-07 12:55:02 -04:00
|
|
|
if c.client == nil {
|
2020-09-24 14:34:58 -07:00
|
|
|
return nil, fmt.Errorf("nil client in Authenticate request")
|
2020-07-14 14:32:47 -07:00
|
|
|
}
|
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{}{
|
2020-09-08 13:27:51 -07:00
|
|
|
"credentials": credentials,
|
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 {
|
2020-09-24 14:34:58 -07:00
|
|
|
return nil, fmt.Errorf("error creating Authenticate request: %w", err)
|
2020-07-14 14:32:47 -07:00
|
|
|
}
|
|
|
|
|
|
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 {
|
2020-09-24 14:34:58 -07:00
|
|
|
return nil, fmt.Errorf("error performing client request during Authenticate call: %w", err)
|
2020-07-14 14:32:47 -07:00
|
|
|
}
|
|
|
|
|
|
2020-09-15 01:08:21 -04:00
|
|
|
target := new(authtokens.AuthTokenReadResult)
|
|
|
|
|
target.Item = new(authtokens.AuthToken)
|
|
|
|
|
apiErr, err := resp.Decode(target.Item)
|
2020-07-14 14:32:47 -07:00
|
|
|
if err != nil {
|
2020-09-24 14:34:58 -07:00
|
|
|
return nil, fmt.Errorf("error decoding Authenticate response: %w", err)
|
|
|
|
|
}
|
|
|
|
|
if apiErr != nil {
|
|
|
|
|
return nil, apiErr
|
2020-07-14 14:32:47 -07:00
|
|
|
}
|
|
|
|
|
|
2020-09-24 14:34:58 -07:00
|
|
|
return target, nil
|
2020-07-14 14:32:47 -07:00
|
|
|
}
|