refact(authmethods): Add top level token type to Authenticate (#2010)
Other parts of the system make use of a "type" field, while the AuthenticateResponse sets a "token_type" on the response attributes directly. Rename this field to "type", and move it up to the response level, to be more consistent with other RPCs. Also adds a new field to the AuthenticateRequest, "type" which will replace the "token_type" field over time.
This commit is contained in:
1 parent
bf9e7874b1
commit
80b2c9230f
15 files changed
+246
-171
No files matched your search
@@ -32,7 +32,7 @@ func TestAuthenticationHandler(t *testing.T) {
|
||||
"password": "password123",
|
||||
},
|
||||
}
|
||||
// No token_type defined means "token" type
|
||||
// No "type" defined means "token" type
|
||||
b, err := json.Marshal(request)
|
||||
require.NoError(t, err)
|
||||
|
||||
@@ -54,7 +54,7 @@ func TestAuthenticationHandler(t *testing.T) {
|
||||
assert.Truef(t, strings.HasPrefix(tok, pubId), "Token: %q, Id: %q", tok, pubId)
|
||||
|
||||
// Set the token type to cookie and make sure the body does not contain the token anymore.
|
||||
request["token_type"] = "cookie"
|
||||
request["type"] = "cookie"
|
||||
b, err = json.Marshal(request)
|
||||
require.NoError(t, err)
|
||||
resp, err = http.Post(fmt.Sprintf("%s/v1/auth-methods/ampw_1234567890:authenticate", c.ApiAddrs()[0]), "application/json", bytes.NewReader(b))
|
||||
|
||||
@@ -44,7 +44,7 @@ const (
|
||||
typeField = "type"
|
||||
attributesField = "attributes"
|
||||
authMethodIdField = "auth_method_id"
|
||||
tokenTypeField = "token_type"
|
||||
tokenTypeField = "type"
|
||||
isPrimaryField = "is_primary"
|
||||
)
|
||||
|
||||
@@ -1205,7 +1205,15 @@ func (s Service) convertToAuthenticateResponse(ctx context.Context, req *pbs.Aut
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
retAttrs.GetFields()[tokenTypeField] = structpb.NewStringValue(req.GetTokenType())
|
||||
tokenType := req.GetType()
|
||||
if tokenType == "" {
|
||||
// Fall back to deprecated field if type is not set
|
||||
tokenType = req.GetTokenType()
|
||||
}
|
||||
|
||||
return &pbs.AuthenticateResponse{Command: req.GetCommand(), Attributes: retAttrs}, nil
|
||||
return &pbs.AuthenticateResponse{
|
||||
Command: req.GetCommand(),
|
||||
Attributes: retAttrs,
|
||||
Type: tokenType,
|
||||
}, nil
|
||||
}
|
||||
@@ -389,7 +389,12 @@ func validateAuthenticateOidcRequest(req *pbs.AuthenticateRequest) error {
|
||||
}
|
||||
|
||||
case tokenCommand:
|
||||
tType := strings.ToLower(strings.TrimSpace(req.GetTokenType()))
|
||||
tokenType := req.GetType()
|
||||
if tokenType == "" {
|
||||
// Fall back to deprecated field if type is not set
|
||||
tokenType = req.GetTokenType()
|
||||
}
|
||||
tType := strings.ToLower(strings.TrimSpace(tokenType))
|
||||
if tType != "" && tType != "token" && tType != "cookie" {
|
||||
badFields[tokenTypeField] = `The only accepted types are "token" and "cookie".`
|
||||
}
|
||||
|
||||
@@ -156,7 +156,12 @@ func validateAuthenticatePasswordRequest(req *pbs.AuthenticateRequest) error {
|
||||
if req.Command != loginCommand {
|
||||
badFields[commandField] = "Invalid command for this auth method type."
|
||||
}
|
||||
tType := strings.ToLower(strings.TrimSpace(req.GetTokenType()))
|
||||
tokenType := req.GetType()
|
||||
if tokenType == "" {
|
||||
// Fall back to deprecated field if type is not set
|
||||
tokenType = req.GetTokenType()
|
||||
}
|
||||
tType := strings.ToLower(strings.TrimSpace(tokenType))
|
||||
if tType != "" && tType != "token" && tType != "cookie" {
|
||||
badFields[tokenTypeField] = `The only accepted types are "token" and "cookie".`
|
||||
}
|
||||
|
||||
@@ -612,7 +612,7 @@ func TestAuthenticate_Password(t *testing.T) {
|
||||
assert.Equal(aToken.GetCreatedTime(), aToken.GetApproximateLastUsedTime())
|
||||
assert.Equal(acct.GetPublicId(), aToken.GetAccountId())
|
||||
assert.Equal(am.GetPublicId(), aToken.GetAuthMethodId())
|
||||
assert.Equal(tc.wantType, resp.GetAttributes().GetFields()["token_type"].GetStringValue())
|
||||
assert.Equal(tc.wantType, resp.GetType())
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,7 +21,7 @@ import (
|
||||
const (
|
||||
HttpOnlyCookieName = "wt-http-token-cookie"
|
||||
JsVisibleCookieName = "wt-js-token-cookie"
|
||||
tokenTypeField = "token_type"
|
||||
tokenTypeField = "type"
|
||||
finalRedirectUrlField = "final_redirect_url"
|
||||
statusField = "status"
|
||||
StatusCodeHeader = "x-http-code"
|
||||
@@ -93,14 +93,13 @@ func OutgoingResponseFilter(ctx context.Context, w http.ResponseWriter, m proto.
|
||||
return nil
|
||||
}
|
||||
// It's a token response
|
||||
if _, ok := fields[tokenTypeField]; ok {
|
||||
if m.GetType() != "" {
|
||||
aToken := &authtokenpb.AuthToken{}
|
||||
// We may have "token_type" if it's a token, or it may not be a token at
|
||||
// all, so ignore unknown fields
|
||||
// This may not be a token at all, so ignore unknown fields
|
||||
if err := StructToProto(m.GetAttributes(), aToken, WithDiscardUnknownFields(true)); err != nil {
|
||||
return err
|
||||
}
|
||||
tokenType := m.GetAttributes().GetFields()[tokenTypeField].GetStringValue()
|
||||
tokenType := m.GetType()
|
||||
if strings.EqualFold(tokenType, "cookie") {
|
||||
tok := aToken.GetToken()
|
||||
if tok == "" {
|
||||
|
||||
@@ -15,15 +15,13 @@ import (
|
||||
"github.com/stretchr/testify/require"
|
||||
"google.golang.org/grpc/metadata"
|
||||
"google.golang.org/protobuf/proto"
|
||||
"google.golang.org/protobuf/types/known/structpb"
|
||||
)
|
||||
|
||||
func TestOutgoingSplitCookie(t *testing.T) {
|
||||
rec := httptest.NewRecorder()
|
||||
attrs, err := ProtoToStruct(&pb.AuthToken{Token: "t_abc_1234567890"})
|
||||
require.NoError(t, err)
|
||||
attrs.GetFields()["token_type"] = structpb.NewStringValue("cookie")
|
||||
require.NoError(t, OutgoingResponseFilter(context.Background(), rec, &pbs.AuthenticateResponse{Attributes: attrs}))
|
||||
require.NoError(t, OutgoingResponseFilter(context.Background(), rec, &pbs.AuthenticateResponse{Attributes: attrs, Type: "cookie"}))
|
||||
assert.ElementsMatch(t, rec.Result().Cookies(), []*http.Cookie{
|
||||
{Name: HttpOnlyCookieName, Value: "34567890", HttpOnly: true, Raw: "wt-http-token-cookie=34567890; HttpOnly"},
|
||||
{Name: JsVisibleCookieName, Value: "t_abc_12", Raw: "wt-js-token-cookie=t_abc_12"},
|
||||
|
||||
Reference in new issue
Block a user