Files
threshold/api/scopes/authenticate.go
T

53 lines
1.4 KiB
Go
Raw Normal View History

package scopes
import (
"context"
"fmt"
"github.com/hashicorp/watchtower/api"
"github.com/hashicorp/watchtower/api/authtokens"
)
// TODO: This will need to be changed when we add Auth Method API to watchtower. We'll also need a better
// way to handle different auth method types.
2020-07-22 00:14:28 -04:00
func (s Scope) Authenticate(ctx context.Context, authMethodId, name, password string) (*authtokens.AuthToken, *api.Error, error) {
if s.Client == nil {
return nil, nil, fmt.Errorf("nil client in Authenticate request")
}
2020-07-22 00:14:28 -04:00
var opts []api.Option
if s.Scope.Id != "" {
// If it's explicitly set here, override anything that might be in the
// client
2020-07-22 00:14:28 -04:00
opts = append(opts, api.WithScopeId(s.Scope.Id))
}
reqBody := map[string]interface{}{
"credentials": map[string]string{
"name": name,
"password": password,
},
}
2020-07-22 00:14:28 -04:00
req, err := s.Client.NewRequest(ctx, "POST", fmt.Sprintf("auth-methods/%s:authenticate", authMethodId), reqBody, opts...)
if err != nil {
return nil, nil, fmt.Errorf("error creating Authenticate request: %w", err)
}
resp, err := s.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 != "" {
s.Client.SetToken(target.Token)
}
return target, apiErr, nil
}