Files
threshold/api/authmethods/authenticate.go
T

42 lines
1.1 KiB
Go
Raw Normal View History

2020-08-07 12:55:02 -04:00
package authmethods
import (
"context"
"fmt"
2020-08-10 17:15:02 -04:00
"github.com/hashicorp/boundary/api/authtokens"
)
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-22 00:14:28 -04:00
2020-08-07 12:55:02 -04:00
_, apiOpts := getOpts(opt...)
reqBody := map[string]interface{}{
"credentials": credentials,
}
2020-08-07 12:55:02 -04:00
req, err := c.client.NewRequest(ctx, "POST", fmt.Sprintf("auth-methods/%s:authenticate", authMethodId), reqBody, apiOpts...)
if err != nil {
2020-09-24 14:34:58 -07:00
return nil, fmt.Errorf("error creating Authenticate request: %w", err)
}
2020-08-07 12:55:02 -04:00
resp, err := c.client.Do(req)
if err != nil {
2020-09-24 14:34:58 -07:00
return nil, fmt.Errorf("error performing client request during Authenticate call: %w", err)
}
target := new(authtokens.AuthTokenReadResult)
target.Item = new(authtokens.AuthToken)
apiErr, err := resp.Decode(target.Item)
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-09-24 14:34:58 -07:00
return target, nil
}