From 083a8f1f329f262c9facf5ad241017fa3b3b56b5 Mon Sep 17 00:00:00 2001 From: Imre Halasz Date: Thu, 20 Jun 2024 10:43:10 +0200 Subject: [PATCH] is: Add possibility to overwrite the email validtion TTL --- .../email_validation_registry.go | 14 ++++++++-- pkg/ttnpb/email_validation.go | 28 +++++++++++++++++++ 2 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 pkg/ttnpb/email_validation.go diff --git a/pkg/identityserver/email_validation_registry.go b/pkg/identityserver/email_validation_registry.go index eb1d11f013..6f6f171c10 100644 --- a/pkg/identityserver/email_validation_registry.go +++ b/pkg/identityserver/email_validation_registry.go @@ -42,7 +42,7 @@ type emailValidationRegistry struct { } func (is *IdentityServer) refreshEmailValidation( - ctx context.Context, usrID *ttnpb.UserIdentifiers, + ctx context.Context, usrID *ttnpb.UserIdentifiers, opts ...ttnpb.EmailValidationOverwrite, ) (*ttnpb.EmailValidation, error) { ttl := is.configFromContext(ctx).UserRegistration.ContactInfoValidation.TokenTTL expires := time.Now().Add(ttl) @@ -55,6 +55,11 @@ func (is *IdentityServer) refreshEmailValidation( return err } validation.ExpiresAt = timestamppb.New(expires) + + for _, opt := range opts { + opt(validation) + } + if err = st.RefreshEmailValidation(ctx, validation); err != nil { return err } @@ -67,7 +72,7 @@ func (is *IdentityServer) refreshEmailValidation( } func (is *IdentityServer) requestEmailValidation( - ctx context.Context, usrID *ttnpb.UserIdentifiers, + ctx context.Context, usrID *ttnpb.UserIdentifiers, opts ...ttnpb.EmailValidationOverwrite, ) (*ttnpb.EmailValidation, error) { var validation *ttnpb.EmailValidation err := is.store.Transact(ctx, func(ctx context.Context, st store.Store) (err error) { @@ -98,6 +103,11 @@ func (is *IdentityServer) requestEmailValidation( time.Now().Add(is.configFromContext(ctx).UserRegistration.ContactInfoValidation.TokenTTL), ), } + + for _, opt := range opts { + opt(validation) + } + validation, err = st.CreateEmailValidation(ctx, validation) if err != nil { // Only one validation can exist at a time, so if it already exists, return a forbidden error. diff --git a/pkg/ttnpb/email_validation.go b/pkg/ttnpb/email_validation.go new file mode 100644 index 0000000000..5343c44feb --- /dev/null +++ b/pkg/ttnpb/email_validation.go @@ -0,0 +1,28 @@ +// Copyright © 2024 The Things Network Foundation, The Things Industries B.V. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package ttnpb + +import "google.golang.org/protobuf/types/known/timestamppb" + +// EmailValidationOverwrite is a function to overwrite the EmailValidation. +type EmailValidationOverwrite func(*EmailValidation) *EmailValidation + +// EmailValidationWithExpiresAt overwrites the EmailValidation ExpiresAt field. +func EmailValidationWithExpiresAt(expiresAt *timestamppb.Timestamp) EmailValidationOverwrite { + return func(validation *EmailValidation) *EmailValidation { + validation.ExpiresAt = expiresAt + return validation + } +}