Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor API Identity create endpoint for legibility #122

Merged
merged 1 commit into from
Sep 13, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 36 additions & 25 deletions internal/hermes-api/service/identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,54 +52,65 @@ type Identityer interface {
limit int32, appID string) ([]*api.Identity, int32, error)
}

// CreateIdentity creates an identity.
func (ai *AppIdentity) CreateIdentity(
ctx context.Context, req *api.CreateIdentityRequest,
) (*api.CreateIdentityResponse, error) {
logger := hlog.FromContext(ctx)
sess, ok := session.FromContext(ctx)
if !ok || sess.Role < api.Role_AUTHENTICATOR {
return nil, errPerm(api.Role_AUTHENTICATOR)
}

// Validate notification methods and apply plan limits.
switch m := req.Identity.MethodOneof.(type) {
// validatePlan validates notification methods and apply plan limits.
func (ai *AppIdentity) validatePlan(
ctx context.Context, orgPlan api.Plan, i *api.Identity,
) error {
switch m := i.MethodOneof.(type) {
case *api.Identity_SmsMethod:
if sess.OrgPlan < api.Plan_PRO {
return nil, errPlan(api.Plan_PRO)
if orgPlan < api.Plan_PRO {
return errPlan(api.Plan_PRO)
}

if !rePhone.MatchString(m.SmsMethod.Phone) {
return nil, status.Error(codes.InvalidArgument,
return status.Error(codes.InvalidArgument,
"invalid E.164 phone number")
}

if err := ai.notify.ValidateSMS(ctx, m.SmsMethod.Phone); err != nil {
return nil, errToStatus(err)
return errToStatus(err)
}
case *api.Identity_PushoverMethod:
if sess.OrgPlan < api.Plan_PRO {
return nil, errPlan(api.Plan_PRO)
if orgPlan < api.Plan_PRO {
return errPlan(api.Plan_PRO)
}

if err := ai.notify.ValidatePushover(
m.PushoverMethod.PushoverKey); err != nil {
return nil, errToStatus(err)
return errToStatus(err)
}
case *api.Identity_EmailMethod:
if sess.OrgPlan < api.Plan_PRO {
return nil, errPlan(api.Plan_PRO)
if orgPlan < api.Plan_PRO {
return errPlan(api.Plan_PRO)
}
case *api.Identity_BackupCodesMethod:
if sess.OrgPlan < api.Plan_PRO {
return nil, errPlan(api.Plan_PRO)
if orgPlan < api.Plan_PRO {
return errPlan(api.Plan_PRO)
}
case *api.Identity_SecurityQuestionsMethod:
if sess.OrgPlan < api.Plan_PRO {
return nil, errPlan(api.Plan_PRO)
if orgPlan < api.Plan_PRO {
return errPlan(api.Plan_PRO)
}
}

return nil
}

// CreateIdentity creates an identity.
func (ai *AppIdentity) CreateIdentity(
ctx context.Context, req *api.CreateIdentityRequest,
) (*api.CreateIdentityResponse, error) {
logger := hlog.FromContext(ctx)
sess, ok := session.FromContext(ctx)
if !ok || sess.Role < api.Role_AUTHENTICATOR {
return nil, errPerm(api.Role_AUTHENTICATOR)
}

// Validate notification methods and apply plan limits.
if err := ai.validatePlan(ctx, sess.OrgPlan, req.Identity); err != nil {
return nil, err
}

req.Identity.OrgId = sess.OrgID

identity, otp, retSecret, err := ai.identDAO.Create(ctx, req.Identity)
Expand Down