Add nda to trust center
Signed-off-by: Sacha Al Himdani <sacha@getprobo.com>
This commit is contained in:
@@ -16,6 +16,7 @@ package trust
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/mail"
|
||||
@@ -23,8 +24,6 @@ import (
|
||||
|
||||
"github.com/getprobo/probo/pkg/coredata"
|
||||
"github.com/getprobo/probo/pkg/gid"
|
||||
"github.com/getprobo/probo/pkg/probo"
|
||||
"github.com/getprobo/probo/pkg/statelesstoken"
|
||||
"github.com/getprobo/probo/pkg/usrmgr"
|
||||
"go.gearno.de/kit/pg"
|
||||
)
|
||||
@@ -48,20 +47,12 @@ const (
|
||||
|
||||
func (s TrustCenterAccessService) ValidateToken(
|
||||
ctx context.Context,
|
||||
tokenString string,
|
||||
) (*probo.TrustCenterAccessData, error) {
|
||||
token, err := statelesstoken.ValidateToken[probo.TrustCenterAccessData](
|
||||
s.svc.tokenSecret,
|
||||
TokenTypeTrustCenterAccess,
|
||||
tokenString,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot validate trust center access token: %w", err)
|
||||
}
|
||||
|
||||
access := &coredata.TrustCenterAccess{}
|
||||
err = s.svc.pg.WithConn(ctx, func(conn pg.Conn) error {
|
||||
err := access.LoadByTrustCenterIDAndEmail(ctx, conn, s.svc.scope, token.Data.TrustCenterID, token.Data.Email)
|
||||
trustCenterID gid.GID,
|
||||
email string,
|
||||
) error {
|
||||
return s.svc.pg.WithConn(ctx, func(conn pg.Conn) error {
|
||||
access := &coredata.TrustCenterAccess{}
|
||||
err := access.LoadByTrustCenterIDAndEmail(ctx, conn, s.svc.scope, trustCenterID, email)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot load trust center access: %w", err)
|
||||
}
|
||||
@@ -72,12 +63,6 @@ func (s TrustCenterAccessService) ValidateToken(
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &token.Data, nil
|
||||
}
|
||||
|
||||
func (s TrustCenterAccessService) Create(
|
||||
@@ -115,14 +100,15 @@ func (s TrustCenterAccessService) Create(
|
||||
}
|
||||
|
||||
access = &coredata.TrustCenterAccess{
|
||||
ID: gid.New(s.svc.scope.GetTenantID(), coredata.TrustCenterAccessEntityType),
|
||||
TenantID: s.svc.scope.GetTenantID(),
|
||||
TrustCenterID: req.TrustCenterID,
|
||||
Email: req.Email,
|
||||
Name: req.Name,
|
||||
Active: false,
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
ID: gid.New(s.svc.scope.GetTenantID(), coredata.TrustCenterAccessEntityType),
|
||||
TenantID: s.svc.scope.GetTenantID(),
|
||||
TrustCenterID: req.TrustCenterID,
|
||||
Email: req.Email,
|
||||
Name: req.Name,
|
||||
Active: false,
|
||||
HasAcceptedNonDisclosureAgreement: false,
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
}
|
||||
|
||||
if err := access.Insert(ctx, tx, s.svc.scope); err != nil {
|
||||
@@ -137,3 +123,48 @@ func (s TrustCenterAccessService) Create(
|
||||
|
||||
return access, nil
|
||||
}
|
||||
|
||||
func (s TrustCenterAccessService) HasAcceptedNonDisclosureAgreement(ctx context.Context, trustCenterID gid.GID, email string) (bool, error) {
|
||||
access := &coredata.TrustCenterAccess{}
|
||||
err := s.svc.pg.WithConn(ctx, func(conn pg.Conn) error {
|
||||
err := access.LoadByTrustCenterIDAndEmail(ctx, conn, s.svc.scope, trustCenterID, email)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot load trust center access: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
return access.HasAcceptedNonDisclosureAgreement, nil
|
||||
}
|
||||
|
||||
func (s TrustCenterAccessService) AcceptNonDisclosureAgreement(ctx context.Context, trustCenterID gid.GID, email string) error {
|
||||
return s.svc.pg.WithTx(ctx, func(tx pg.Conn) error {
|
||||
access := &coredata.TrustCenterAccess{}
|
||||
if err := access.LoadByTrustCenterIDAndEmail(ctx, tx, s.svc.scope, trustCenterID, email); err != nil {
|
||||
return fmt.Errorf("cannot load trust center access: %w", err)
|
||||
}
|
||||
|
||||
acceptationLogs, err := json.Marshal(map[string]string{
|
||||
"email": email,
|
||||
"timestamp": time.Now().Format(time.RFC3339),
|
||||
"ip": ctx.Value(coredata.ContextKeyIPAddress).(string),
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot marshal non disclosure agreement acceptation logs: %w", err)
|
||||
}
|
||||
|
||||
access.HasAcceptedNonDisclosureAgreement = true
|
||||
access.UpdatedAt = time.Now()
|
||||
access.HasAcceptedNonDisclosureAgreementMetadata = acceptationLogs
|
||||
if err := access.Update(ctx, tx, s.svc.scope); err != nil {
|
||||
return fmt.Errorf("cannot update trust center access: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
@@ -16,10 +16,14 @@ package trust
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"fmt"
|
||||
"net/url"
|
||||
"time"
|
||||
|
||||
"github.com/aws/aws-sdk-go-v2/aws"
|
||||
"github.com/aws/aws-sdk-go-v2/service/s3"
|
||||
"github.com/getprobo/probo/pkg/coredata"
|
||||
"github.com/getprobo/probo/pkg/gid"
|
||||
"go.gearno.de/kit/pg"
|
||||
)
|
||||
|
||||
@@ -51,3 +55,88 @@ func (s TrustCenterService) GetBySlug(
|
||||
|
||||
return trustCenter, nil
|
||||
}
|
||||
|
||||
func (s TrustCenterService) Get(
|
||||
ctx context.Context,
|
||||
trustCenterID gid.GID,
|
||||
) (*coredata.TrustCenter, *coredata.File, error) {
|
||||
var trustCenter *coredata.TrustCenter
|
||||
var file *coredata.File
|
||||
|
||||
err := s.svc.pg.WithConn(
|
||||
ctx,
|
||||
func(conn pg.Conn) error {
|
||||
trustCenter = &coredata.TrustCenter{}
|
||||
if err := trustCenter.LoadByID(ctx, conn, s.svc.scope, trustCenterID); err != nil {
|
||||
return fmt.Errorf("cannot load trust center: %w", err)
|
||||
}
|
||||
|
||||
if trustCenter.NonDisclosureAgreementFileID != nil {
|
||||
file = &coredata.File{}
|
||||
if err := file.LoadByID(ctx, conn, s.svc.scope, *trustCenter.NonDisclosureAgreementFileID); err != nil {
|
||||
return fmt.Errorf("cannot load file: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("cannot load trust center: %w", err)
|
||||
}
|
||||
|
||||
return trustCenter, file, nil
|
||||
}
|
||||
|
||||
func (s TrustCenterService) GenerateNDAFileURL(
|
||||
ctx context.Context,
|
||||
trustCenterID gid.GID,
|
||||
expiresIn time.Duration,
|
||||
) (string, error) {
|
||||
var file *coredata.File
|
||||
|
||||
err := s.svc.pg.WithConn(
|
||||
ctx,
|
||||
func(conn pg.Conn) error {
|
||||
trustCenter := &coredata.TrustCenter{}
|
||||
if err := trustCenter.LoadByID(ctx, conn, s.svc.scope, trustCenterID); err != nil {
|
||||
return fmt.Errorf("cannot load trust center: %w", err)
|
||||
}
|
||||
|
||||
if trustCenter.NonDisclosureAgreementFileID == nil {
|
||||
return fmt.Errorf("no NDA file found")
|
||||
}
|
||||
|
||||
file = &coredata.File{}
|
||||
if err := file.LoadByID(ctx, conn, s.svc.scope, *trustCenter.NonDisclosureAgreementFileID); err != nil {
|
||||
return fmt.Errorf("cannot load file: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
presignClient := s3.NewPresignClient(s.svc.s3)
|
||||
|
||||
encodedFilename := url.QueryEscape(file.FileName)
|
||||
contentDisposition := fmt.Sprintf("attachment; filename=\"%s\"; filename*=UTF-8''%s",
|
||||
encodedFilename, encodedFilename)
|
||||
|
||||
presignedReq, err := presignClient.PresignGetObject(ctx, &s3.GetObjectInput{
|
||||
Bucket: aws.String(s.svc.bucket),
|
||||
Key: aws.String(file.FileKey),
|
||||
ResponseCacheControl: aws.String("max-age=3600, public"),
|
||||
ResponseContentDisposition: aws.String(contentDisposition),
|
||||
}, func(opts *s3.PresignOptions) {
|
||||
opts.Expires = expiresIn
|
||||
})
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("cannot presign GetObject request: %w", err)
|
||||
}
|
||||
|
||||
return presignedReq.URL, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user