Revamp authenticate (#1025)

This commit is contained in:
Jeff Mitchell authored and GitHub committed 2021-03-24 16:07:43 -04:00
1 parent aa570f4e48
commit 6605b4c9f8
14 files changed
+276 -207

No files matched your search

@@ -258,8 +258,6 @@ func (s Service) DeleteAuthMethod(ctx context.Context, req *pbs.DeleteAuthMethod
}
// Authenticate implements the interface pbs.AuthenticationServiceServer.
//
// Deprecated in favor of AuthenticateLogin
func (s Service) Authenticate(ctx context.Context, req *pbs.AuthenticateRequest) (*pbs.AuthenticateResponse, error) {
if err := validateAuthenticateRequest(req); err != nil {
return nil, err
@@ -268,8 +266,8 @@ func (s Service) Authenticate(ctx context.Context, req *pbs.AuthenticateRequest)
if authResults.Error != nil {
return nil, authResults.Error
}
creds := req.GetCredentials().GetFields()
tok, err := s.authenticateWithRepo(ctx, authResults.Scope.GetId(), req.GetAuthMethodId(), creds[loginNameKey].GetStringValue(), creds[pwKey].GetStringValue())
attrs := req.GetAttributes().GetFields()
tok, err := s.authenticateWithRepo(ctx, authResults.Scope.GetId(), req.GetAuthMethodId(), attrs[loginNameKey].GetStringValue(), attrs[pwKey].GetStringValue())
if err != nil {
return nil, err
}
@@ -281,7 +279,7 @@ func (s Service) Authenticate(ctx context.Context, req *pbs.AuthenticateRequest)
return &pbs.AuthenticateResponse{Item: tok, TokenType: req.GetTokenType()}, nil
}
// AuthenticateLogin implements the interface pbs.AuthenticationServiceServer.
// Deprecated: use Authenticate
func (s Service) AuthenticateLogin(ctx context.Context, req *pbs.AuthenticateLoginRequest) (*pbs.AuthenticateLoginResponse, error) {
if err := validateAuthenticateLoginRequest(req); err != nil {
return nil, err
@@ -633,7 +631,6 @@ func validateListRequest(req *pbs.ListAuthMethodsRequest) error {
return nil
}
// Deprecated; remove when Authenticate is removedLogin
func validateAuthenticateRequest(req *pbs.AuthenticateRequest) error {
badFields := make(map[string]string)
if strings.TrimSpace(req.GetAuthMethodId()) == "" {
@@ -641,16 +638,23 @@ func validateAuthenticateRequest(req *pbs.AuthenticateRequest) error {
} else if !handlers.ValidId(password.AuthMethodPrefix, req.GetAuthMethodId()) {
badFields["auth_method_id"] = "Invalid formatted identifier."
}
// TODO: Update this when we enable different auth method types.
if req.GetCredentials() == nil {
badFields["credentials"] = "This is a required field."
if req.GetAttributes() == nil && req.GetCredentials() != nil {
// TODO: Eventually, remove this
req.Attributes = req.Credentials
}
creds := req.GetCredentials().GetFields()
if _, ok := creds[loginNameKey]; !ok {
badFields["credentials.login_name"] = "This is a required field."
if req.GetCommand() == "" {
// TODO: Eventually, require a command. For now, fall back to "login" for backwards compat.
req.Command = "login"
}
if _, ok := creds[pwKey]; !ok {
badFields["credentials.password"] = "This is a required field."
if req.Command != "login" {
badFields["command"] = "Invalid command for this auth method type."
}
attrs := req.GetAttributes().GetFields()
if _, ok := attrs[loginNameKey]; !ok {
badFields["attributes.login_name"] = "This is a required field."
}
if _, ok := attrs[pwKey]; !ok {
badFields["attributes.password"] = "This is a required field."
}
tType := strings.ToLower(strings.TrimSpace(req.GetTokenType()))
if tType != "" && tType != "token" && tType != "cookie" {
@@ -662,6 +666,7 @@ func validateAuthenticateRequest(req *pbs.AuthenticateRequest) error {
return nil
}
// Deprecated; remove when AuthenticateLogin is removed
func validateAuthenticateLoginRequest(req *pbs.AuthenticateLoginRequest) error {
badFields := make(map[string]string)
if strings.TrimSpace(req.GetAuthMethodId()) == "" {
@@ -7,7 +7,6 @@ import (
"strings"
"testing"
"github.com/golang/protobuf/ptypes"
"github.com/google/go-cmp/cmp"
"github.com/hashicorp/boundary/internal/auth"
"github.com/hashicorp/boundary/internal/auth/password"
@@ -25,6 +24,7 @@ import (
"google.golang.org/grpc/codes"
"google.golang.org/protobuf/testing/protocmp"
"google.golang.org/protobuf/types/known/structpb"
"google.golang.org/protobuf/types/known/timestamppb"
"google.golang.org/protobuf/types/known/wrapperspb"
"github.com/stretchr/testify/assert"
@@ -377,8 +377,7 @@ func TestCreate(t *testing.T) {
o, _ := iam.TestScopes(t, iamRepo)
defaultAm := password.TestAuthMethods(t, conn, o.GetPublicId(), 1)[0]
defaultCreated, err := ptypes.Timestamp(defaultAm.GetCreateTime().GetTimestamp())
require.NoError(t, err, "Error converting proto to timestamp.")
defaultCreated := defaultAm.GetCreateTime().GetTimestamp()
cases := []struct {
name string
@@ -458,7 +457,7 @@ func TestCreate(t *testing.T) {
name: "Can't specify Created Time",
req: &pbs.CreateAuthMethodRequest{Item: &pb.AuthMethod{
ScopeId: o.GetPublicId(),
CreatedTime: ptypes.TimestampNow(),
CreatedTime: timestamppb.Now(),
}},
res: nil,
err: handlers.ApiErrorWithCode(codes.InvalidArgument),
@@ -467,7 +466,7 @@ func TestCreate(t *testing.T) {
name: "Can't specify Update Time",
req: &pbs.CreateAuthMethodRequest{Item: &pb.AuthMethod{
ScopeId: o.GetPublicId(),
UpdatedTime: ptypes.TimestampNow(),
UpdatedTime: timestamppb.Now(),
Type: "password",
}},
res: nil,
@@ -477,7 +476,7 @@ func TestCreate(t *testing.T) {
name: "Can't specify Update Time",
req: &pbs.CreateAuthMethodRequest{Item: &pb.AuthMethod{
ScopeId: o.GetPublicId(),
UpdatedTime: ptypes.TimestampNow(),
UpdatedTime: timestamppb.Now(),
Type: "password",
}},
res: nil,
@@ -527,13 +526,11 @@ func TestCreate(t *testing.T) {
if got != nil {
assert.Contains(got.GetUri(), tc.res.Uri)
assert.True(strings.HasPrefix(got.GetItem().GetId(), password.AuthMethodPrefix+"_"))
gotCreateTime, err := ptypes.Timestamp(got.GetItem().GetCreatedTime())
require.NoError(err, "Error converting proto to timestamp.")
gotUpdateTime, err := ptypes.Timestamp(got.GetItem().GetUpdatedTime())
require.NoError(err, "Error converting proto to timestamp.")
gotCreateTime := got.GetItem().GetCreatedTime()
gotUpdateTime := got.GetItem().GetUpdatedTime()
// Verify it is a auth_method created after the test setup's default auth_method
assert.True(gotCreateTime.After(defaultCreated), "New auth_method should have been created after default auth_method. Was created %v, which is after %v", gotCreateTime, defaultCreated)
assert.True(gotUpdateTime.After(defaultCreated), "New auth_method should have been updated after default auth_method. Was updated %v, which is after %v", gotUpdateTime, defaultCreated)
assert.True(gotCreateTime.AsTime().After(defaultCreated.AsTime()), "New auth_method should have been created after default auth_method. Was created %v, which is after %v", gotCreateTime, defaultCreated)
assert.True(gotUpdateTime.AsTime().After(defaultCreated.AsTime()), "New auth_method should have been updated after default auth_method. Was updated %v, which is after %v", gotUpdateTime, defaultCreated)
// Clear all values which are hard to compare against.
got.Uri, tc.res.Uri = "", ""
@@ -806,7 +803,7 @@ func TestUpdate(t *testing.T) {
Paths: []string{"created_time"},
},
Item: &pb.AuthMethod{
CreatedTime: ptypes.TimestampNow(),
CreatedTime: timestamppb.Now(),
},
},
res: nil,
@@ -819,7 +816,7 @@ func TestUpdate(t *testing.T) {
Paths: []string{"updated_time"},
},
Item: &pb.AuthMethod{
UpdatedTime: ptypes.TimestampNow(),
UpdatedTime: timestamppb.Now(),
},
},
res: nil,
@@ -930,12 +927,9 @@ func TestUpdate(t *testing.T) {
if got != nil {
assert.NotNilf(tc.res, "Expected UpdateAuthMethod response to be nil, but was %v", got)
gotUpdateTime, err := ptypes.Timestamp(got.GetItem().GetUpdatedTime())
require.NoError(err, "Error converting proto to timestamp")
created, err := ptypes.Timestamp(am.GetCreatedTime())
require.NoError(err, "Error converting proto to timestamp")
created := am.GetCreatedTime().AsTime()
gotUpdateTime := got.GetItem().GetUpdatedTime().AsTime()
// Verify it is a auth_method updated after it was created
assert.True(gotUpdateTime.After(created), "Updated auth_method should have been updated after it's creation. Was updated %v, which is after %v", gotUpdateTime, created)
+2 -2
View File
@@ -30,14 +30,14 @@ func TestAuthenticationMulti(t *testing.T) {
defer c2.Shutdown()
auth := authmethods.NewClient(c1.Client())
token1Result, err := auth.Authenticate(c1.Context(), c1.Server().DevAuthMethodId, map[string]interface{}{"login_name": c1.Server().DevLoginName, "password": c1.Server().DevPassword})
token1Result, err := auth.Authenticate(c1.Context(), c1.Server().DevAuthMethodId, "login", map[string]interface{}{"login_name": c1.Server().DevLoginName, "password": c1.Server().DevPassword})
require.Nil(err)
token1 := token1Result.Item
require.NotNil(token1)
time.Sleep(5 * time.Second)
auth = authmethods.NewClient(c2.Client())
token2Result, err := auth.Authenticate(c2.Context(), c2.Server().DevAuthMethodId, map[string]interface{}{"login_name": c2.Server().DevLoginName, "password": c2.Server().DevPassword})
token2Result, err := auth.Authenticate(c2.Context(), c2.Server().DevAuthMethodId, "login", map[string]interface{}{"login_name": c2.Server().DevLoginName, "password": c2.Server().DevPassword})
require.Nil(err)
token2 := token2Result.Item
require.NotNil(token2)
+2
View File
@@ -131,6 +131,7 @@ func (tc *TestController) Token() *authtokens.AuthToken {
token, err := authmethods.NewClient(tc.Client()).Authenticate(
tc.Context(),
tc.b.DevAuthMethodId,
"login",
map[string]interface{}{
"login_name": tc.b.DevLoginName,
"password": tc.b.DevPassword,
@@ -151,6 +152,7 @@ func (tc *TestController) UnprivilegedToken() *authtokens.AuthToken {
token, err := authmethods.NewClient(tc.Client()).Authenticate(
tc.Context(),
tc.b.DevAuthMethodId,
"login",
map[string]interface{}{
"login_name": tc.b.DevUnprivilegedLoginName,
"password": tc.b.DevUnprivilegedPassword,