mirror of
https://github.com/zitadel/zitadel.git
synced 2025-08-12 07:37:31 +00:00
fix: rename OTP to TOTP in v2 alpha user api
This change renames the v2 user OTP registration endpoints and objects to TOTP. Also the v2 related code paths have been renamed to TOTP. This change was discussed during the sprint review.
This commit is contained in:
@@ -1,38 +0,0 @@
|
||||
package user
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/zitadel/zitadel/internal/api/authz"
|
||||
"github.com/zitadel/zitadel/internal/api/grpc/object/v2"
|
||||
"github.com/zitadel/zitadel/internal/domain"
|
||||
user "github.com/zitadel/zitadel/pkg/grpc/user/v2alpha"
|
||||
)
|
||||
|
||||
func (s *Server) RegisterOTP(ctx context.Context, req *user.RegisterOTPRequest) (*user.RegisterOTPResponse, error) {
|
||||
return otpDetailsToPb(
|
||||
s.command.AddUserOTP(ctx, req.GetUserId(), authz.GetCtxData(ctx).ResourceOwner),
|
||||
)
|
||||
|
||||
}
|
||||
|
||||
func otpDetailsToPb(otp *domain.OTPv2, err error) (*user.RegisterOTPResponse, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &user.RegisterOTPResponse{
|
||||
Details: object.DomainToDetailsPb(otp.ObjectDetails),
|
||||
Uri: otp.URI,
|
||||
Secret: otp.Secret,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *Server) VerifyOTPRegistration(ctx context.Context, req *user.VerifyOTPRegistrationRequest) (*user.VerifyOTPRegistrationResponse, error) {
|
||||
objectDetails, err := s.command.CheckUserOTP(ctx, req.GetUserId(), req.GetCode(), authz.GetCtxData(ctx).ResourceOwner)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &user.VerifyOTPRegistrationResponse{
|
||||
Details: object.DomainToDetailsPb(objectDetails),
|
||||
}, nil
|
||||
}
|
38
internal/api/grpc/user/v2/totp.go
Normal file
38
internal/api/grpc/user/v2/totp.go
Normal file
@@ -0,0 +1,38 @@
|
||||
package user
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/zitadel/zitadel/internal/api/authz"
|
||||
"github.com/zitadel/zitadel/internal/api/grpc/object/v2"
|
||||
"github.com/zitadel/zitadel/internal/domain"
|
||||
user "github.com/zitadel/zitadel/pkg/grpc/user/v2alpha"
|
||||
)
|
||||
|
||||
func (s *Server) RegisterTOTP(ctx context.Context, req *user.RegisterTOTPRequest) (*user.RegisterTOTPResponse, error) {
|
||||
return totpDetailsToPb(
|
||||
s.command.AddUserTOTP(ctx, req.GetUserId(), authz.GetCtxData(ctx).ResourceOwner),
|
||||
)
|
||||
|
||||
}
|
||||
|
||||
func totpDetailsToPb(totp *domain.TOTP, err error) (*user.RegisterTOTPResponse, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &user.RegisterTOTPResponse{
|
||||
Details: object.DomainToDetailsPb(totp.ObjectDetails),
|
||||
Uri: totp.URI,
|
||||
Secret: totp.Secret,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *Server) VerifyTOTPRegistration(ctx context.Context, req *user.VerifyTOTPRegistrationRequest) (*user.VerifyTOTPRegistrationResponse, error) {
|
||||
objectDetails, err := s.command.CheckUserTOTP(ctx, req.GetUserId(), req.GetCode(), authz.GetCtxData(ctx).ResourceOwner)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &user.VerifyTOTPRegistrationResponse{
|
||||
Details: object.DomainToDetailsPb(objectDetails),
|
||||
}, nil
|
||||
}
|
@@ -13,24 +13,24 @@ import (
|
||||
user "github.com/zitadel/zitadel/pkg/grpc/user/v2alpha"
|
||||
)
|
||||
|
||||
func TestServer_RegisterOTP(t *testing.T) {
|
||||
func TestServer_RegisterTOTP(t *testing.T) {
|
||||
// userID := Tester.CreateHumanUser(CTX).GetUserId()
|
||||
|
||||
type args struct {
|
||||
ctx context.Context
|
||||
req *user.RegisterOTPRequest
|
||||
req *user.RegisterTOTPRequest
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want *user.RegisterOTPResponse
|
||||
want *user.RegisterTOTPResponse
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "missing user id",
|
||||
args: args{
|
||||
ctx: CTX,
|
||||
req: &user.RegisterOTPRequest{},
|
||||
req: &user.RegisterTOTPRequest{},
|
||||
},
|
||||
wantErr: true,
|
||||
},
|
||||
@@ -38,7 +38,7 @@ func TestServer_RegisterOTP(t *testing.T) {
|
||||
name: "user mismatch",
|
||||
args: args{
|
||||
ctx: CTX,
|
||||
req: &user.RegisterOTPRequest{
|
||||
req: &user.RegisterTOTPRequest{
|
||||
UserId: "wrong",
|
||||
},
|
||||
},
|
||||
@@ -50,11 +50,11 @@ func TestServer_RegisterOTP(t *testing.T) {
|
||||
name: "human user",
|
||||
args: args{
|
||||
ctx: CTX,
|
||||
req: &user.RegisterOTPRequest{
|
||||
req: &user.RegisterTOTPRequest{
|
||||
UserId: userID,
|
||||
},
|
||||
},
|
||||
want: &user.RegisterOTPResponse{
|
||||
want: &user.RegisterTOTPResponse{
|
||||
Details: &object.Details{
|
||||
ResourceOwner: Tester.Organisation.ID,
|
||||
},
|
||||
@@ -64,7 +64,7 @@ func TestServer_RegisterOTP(t *testing.T) {
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got, err := Client.RegisterOTP(tt.args.ctx, tt.args.req)
|
||||
got, err := Client.RegisterTOTP(tt.args.ctx, tt.args.req)
|
||||
if tt.wantErr {
|
||||
require.Error(t, err)
|
||||
return
|
||||
@@ -78,11 +78,11 @@ func TestServer_RegisterOTP(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestServer_VerifyOTPRegistration(t *testing.T) {
|
||||
func TestServer_VerifyTOTPRegistration(t *testing.T) {
|
||||
userID := Tester.CreateHumanUser(CTX).GetUserId()
|
||||
|
||||
/* TODO: after we are able to obtain a Bearer token for a human user
|
||||
reg, err := Client.RegisterOTP(CTX, &user.RegisterOTPRequest{
|
||||
reg, err := Client.RegisterTOTP(CTX, &user.RegisterTOTPRequest{
|
||||
UserId: userID,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
@@ -92,19 +92,19 @@ func TestServer_VerifyOTPRegistration(t *testing.T) {
|
||||
|
||||
type args struct {
|
||||
ctx context.Context
|
||||
req *user.VerifyOTPRegistrationRequest
|
||||
req *user.VerifyTOTPRegistrationRequest
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want *user.VerifyOTPRegistrationResponse
|
||||
want *user.VerifyTOTPRegistrationResponse
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "user mismatch",
|
||||
args: args{
|
||||
ctx: CTX,
|
||||
req: &user.VerifyOTPRegistrationRequest{
|
||||
req: &user.VerifyTOTPRegistrationRequest{
|
||||
UserId: "wrong",
|
||||
},
|
||||
},
|
||||
@@ -114,7 +114,7 @@ func TestServer_VerifyOTPRegistration(t *testing.T) {
|
||||
name: "wrong code",
|
||||
args: args{
|
||||
ctx: CTX,
|
||||
req: &user.VerifyOTPRegistrationRequest{
|
||||
req: &user.VerifyTOTPRegistrationRequest{
|
||||
UserId: userID,
|
||||
Code: "123",
|
||||
},
|
||||
@@ -127,12 +127,12 @@ func TestServer_VerifyOTPRegistration(t *testing.T) {
|
||||
name: "success",
|
||||
args: args{
|
||||
ctx: CTX,
|
||||
req: &user.VerifyOTPRegistrationRequest{
|
||||
req: &user.VerifyTOTPRegistrationRequest{
|
||||
UserId: userID,
|
||||
Code: code,
|
||||
},
|
||||
},
|
||||
want: &user.VerifyOTPRegistrationResponse{
|
||||
want: &user.VerifyTOTPRegistrationResponse{
|
||||
Details: &object.Details{
|
||||
ResourceOwner: Tester.Organisation.ResourceOwner,
|
||||
},
|
||||
@@ -142,7 +142,7 @@ func TestServer_VerifyOTPRegistration(t *testing.T) {
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got, err := Client.VerifyOTPRegistration(tt.args.ctx, tt.args.req)
|
||||
got, err := Client.VerifyTOTPRegistration(tt.args.ctx, tt.args.req)
|
||||
if tt.wantErr {
|
||||
require.Error(t, err)
|
||||
return
|
@@ -14,15 +14,15 @@ import (
|
||||
user "github.com/zitadel/zitadel/pkg/grpc/user/v2alpha"
|
||||
)
|
||||
|
||||
func Test_otpDetailsToPb(t *testing.T) {
|
||||
func Test_totpDetailsToPb(t *testing.T) {
|
||||
type args struct {
|
||||
otp *domain.OTPv2
|
||||
otp *domain.TOTP
|
||||
err error
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want *user.RegisterOTPResponse
|
||||
want *user.RegisterTOTPResponse
|
||||
wantErr error
|
||||
}{
|
||||
{
|
||||
@@ -35,7 +35,7 @@ func Test_otpDetailsToPb(t *testing.T) {
|
||||
{
|
||||
name: "success",
|
||||
args: args{
|
||||
otp: &domain.OTPv2{
|
||||
otp: &domain.TOTP{
|
||||
ObjectDetails: &domain.ObjectDetails{
|
||||
Sequence: 123,
|
||||
EventDate: time.Unix(456, 789),
|
||||
@@ -45,7 +45,7 @@ func Test_otpDetailsToPb(t *testing.T) {
|
||||
URI: "URI",
|
||||
},
|
||||
},
|
||||
want: &user.RegisterOTPResponse{
|
||||
want: &user.RegisterTOTPResponse{
|
||||
Details: &object.Details{
|
||||
Sequence: 123,
|
||||
ChangeDate: ×tamppb.Timestamp{
|
||||
@@ -61,10 +61,10 @@ func Test_otpDetailsToPb(t *testing.T) {
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got, err := otpDetailsToPb(tt.args.otp, tt.args.err)
|
||||
got, err := totpDetailsToPb(tt.args.otp, tt.args.err)
|
||||
require.ErrorIs(t, err, tt.wantErr)
|
||||
if !proto.Equal(tt.want, got) {
|
||||
t.Errorf("RegisterOTPResponse =\n%v\nwant\n%v", got, tt.want)
|
||||
t.Errorf("RegisterTOTPResponse =\n%v\nwant\n%v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
Reference in New Issue
Block a user