2023-06-22 10:06:32 +00:00
|
|
|
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"
|
2023-09-13 12:43:01 +00:00
|
|
|
user "github.com/zitadel/zitadel/pkg/grpc/user/v2beta"
|
2023-06-22 10:06:32 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (s *Server) RegisterTOTP(ctx context.Context, req *user.RegisterTOTPRequest) (*user.RegisterTOTPResponse, error) {
|
|
|
|
return totpDetailsToPb(
|
2023-10-12 08:00:36 +00:00
|
|
|
s.command.AddUserTOTP(ctx, req.GetUserId(), authz.GetCtxData(ctx).OrgID),
|
2023-06-22 10:06:32 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2023-10-12 08:00:36 +00:00
|
|
|
objectDetails, err := s.command.CheckUserTOTP(ctx, req.GetUserId(), req.GetCode(), authz.GetCtxData(ctx).OrgID)
|
2023-06-22 10:06:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &user.VerifyTOTPRegistrationResponse{
|
|
|
|
Details: object.DomainToDetailsPb(objectDetails),
|
|
|
|
}, nil
|
|
|
|
}
|