Files
threshold/api/authmethods/authenticate.go
T

48 lines
1.3 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"
"github.com/hashicorp/boundary/api/authtokens"
)
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
// way to handle different auth method types.
func (c *Client) Authenticate(ctx context.Context, authMethodId, loginName, password string, opt ...Option) (*authtokens.AuthToken, *api.Error, error) {
2020-08-07 12:55:02 -04:00
if c.client == nil {
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...)
reqBody := map[string]interface{}{
"credentials": map[string]string{
"login_name": loginName,
2020-08-11 17:25:00 -04:00
"password": password,
},
}
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 {
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)
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)
}
return target, apiErr, nil
}