Skip to content

Commit

Permalink
is: Add possibility to overwrite the email validtion TTL
Browse files Browse the repository at this point in the history
  • Loading branch information
halimi committed Jun 20, 2024
1 parent 7c82ae5 commit 083a8f1
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
14 changes: 12 additions & 2 deletions pkg/identityserver/email_validation_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
}
Expand All @@ -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) {
Expand Down Expand Up @@ -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.
Expand Down
28 changes: 28 additions & 0 deletions pkg/ttnpb/email_validation.go
Original file line number Diff line number Diff line change
@@ -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
}
}

0 comments on commit 083a8f1

Please sign in to comment.