github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/pkg/modules/publisher/anonymous_publisher.go (about)

     1  package publisher
     2  
     3  import (
     4  	"context"
     5  	"time"
     6  
     7  	"github.com/pkg/errors"
     8  
     9  	confid "github.com/machinefi/w3bstream/pkg/depends/conf/id"
    10  	"github.com/machinefi/w3bstream/pkg/depends/conf/jwt"
    11  	"github.com/machinefi/w3bstream/pkg/depends/kit/httptransport"
    12  	"github.com/machinefi/w3bstream/pkg/depends/kit/logr"
    13  	"github.com/machinefi/w3bstream/pkg/depends/kit/sqlx"
    14  	"github.com/machinefi/w3bstream/pkg/depends/kit/sqlx/builder"
    15  	"github.com/machinefi/w3bstream/pkg/depends/kit/sqlx/datatypes"
    16  	"github.com/machinefi/w3bstream/pkg/enums"
    17  	"github.com/machinefi/w3bstream/pkg/errors/status"
    18  	"github.com/machinefi/w3bstream/pkg/models"
    19  	"github.com/machinefi/w3bstream/pkg/modules/access_key"
    20  	"github.com/machinefi/w3bstream/pkg/types"
    21  )
    22  
    23  func CreateAnonymousPublisher(ctx context.Context) (*models.Publisher, error) {
    24  	ctx, l := logr.Start(ctx, "publisher.CreateAnonymousPublisher")
    25  	defer l.End()
    26  
    27  	var (
    28  		d   = types.MustMgrDBExecutorFromContext(ctx)
    29  		prj = types.MustProjectFromContext(ctx)
    30  		idg = confid.MustSFIDGeneratorFromContext(ctx)
    31  
    32  		pub *models.Publisher
    33  		tok *access_key.CreateRsp
    34  		err error
    35  	)
    36  
    37  	id := idg.MustGenSFID()
    38  	tok, err = access_key.Create(ctx, &access_key.CreateReq{
    39  		IdentityID:   id,
    40  		IdentityType: enums.ACCESS_KEY_IDENTITY_TYPE__PUBLISHER,
    41  		CreateReqBase: access_key.CreateReqBase{
    42  			Name: "pub_" + id.String(),
    43  			Desc: "pub_" + id.String(),
    44  			Privileges: access_key.GroupAccessPrivileges{{
    45  				Name: enums.ApiGroupEvent,
    46  				Perm: enums.ACCESS_PERMISSION__READ_WRITE,
    47  			}},
    48  		},
    49  	})
    50  	if err != nil {
    51  		return nil, err
    52  	}
    53  
    54  	pub = &models.Publisher{
    55  		PrimaryID:    datatypes.PrimaryID{ID: 0},
    56  		RelProject:   models.RelProject{ProjectID: prj.ProjectID},
    57  		RelPublisher: models.RelPublisher{PublisherID: tok.IdentityID},
    58  		PublisherInfo: models.PublisherInfo{
    59  			Name:  "anonymous",
    60  			Key:   "anonymous",
    61  			Token: tok.AccessKey,
    62  		},
    63  	}
    64  	pub.CreatedAt.Set(time.Now())
    65  	pub.UpdatedAt.Set(time.Now())
    66  
    67  	found := false
    68  	err = sqlx.NewTasks(d).With(
    69  		func(d sqlx.DBExecutor) (err error) {
    70  			prj.Public = datatypes.TRUE
    71  			if err := prj.UpdateByName(d); err != nil {
    72  				if sqlx.DBErr(err).IsConflict() {
    73  					return status.ProjectNameConflict
    74  				}
    75  				return status.DatabaseError.StatusErr().WithDesc(err.Error())
    76  			}
    77  			return nil
    78  		},
    79  		func(d sqlx.DBExecutor) (err error) {
    80  			if err := pub.FetchByID(d); err != nil {
    81  				if sqlx.DBErr(err).IsNotFound() {
    82  					found = false
    83  					return nil
    84  				}
    85  				return status.DatabaseError.StatusErr().WithDesc(err.Error())
    86  			}
    87  			found = true
    88  			return nil
    89  		},
    90  		func(d sqlx.DBExecutor) error {
    91  			if found {
    92  				return nil
    93  			}
    94  
    95  			if _, err = d.Exec(
    96  				builder.Insert().Into(d.T(pub)).Values(builder.Cols(
    97  					pub.ColID().Name, pub.ColProjectID().Name, pub.ColPublisherID().Name, pub.ColName().Name,
    98  					pub.ColKey().Name, pub.ColToken().Name, pub.ColCreatedAt().Name, pub.ColUpdatedAt().Name),
    99  					pub.ID, pub.ProjectID, pub.PublisherID, pub.Name, pub.Key, pub.Token, pub.CreatedAt, pub.UpdatedAt),
   100  			); err != nil {
   101  				return status.DatabaseError.StatusErr().WithDesc(err.Error())
   102  			}
   103  			return nil
   104  		},
   105  	).Do()
   106  
   107  	if err != nil {
   108  		l.Warn(errors.Wrap(err, "insert anonymous publisher"))
   109  		return nil, err
   110  	}
   111  
   112  	if jwt.WithAnonymousPublisherFn == nil {
   113  		jwt.SetWithAnonymousPublisherFn(SetAnonymousPublisher)
   114  	}
   115  
   116  	return pub, nil
   117  }
   118  
   119  func SetAnonymousPublisher(ctx context.Context, tok string) (string, error) {
   120  	var (
   121  		d = types.MustMgrDBExecutorFromContext(ctx)
   122  
   123  		pub = &models.Publisher{PrimaryID: datatypes.PrimaryID{ID: 0}}
   124  	)
   125  
   126  	opId := httptransport.OperationIDFromContext(ctx)
   127  
   128  	if tok == "" && opId == "HandleEvent" {
   129  		if err := pub.FetchByID(d); err != nil {
   130  			return "", status.PublisherNotFound
   131  		}
   132  		tok = pub.Token
   133  	}
   134  
   135  	return tok, nil
   136  }