diff --git a/backend/main.go b/backend/main.go index 166548f..5b67e46 100644 --- a/backend/main.go +++ b/backend/main.go @@ -36,6 +36,7 @@ var mongoUser = os.Getenv("MONGO_LOGIN") var mongoSecret = os.Getenv("MONGO_SECRET") var openUrl = os.Getenv("OPEN_URL") var templatePath = os.Getenv("TEMPLATES_PATH") +var clientSecret = os.Getenv("CLIENT_SECRET") func main() { log.SetOutput(os.Stdout) @@ -82,7 +83,7 @@ func main() { if err != nil { log.Fatalf("failed to load TLS keys: %v", err) } - var accountProcessor internal.AccountProcessor = users.NewProcessor(&userRepository, generator, nalogruClient, deviceService) + var accountProcessor internal.AccountProcessor = users.NewProcessor(&userRepository, generator, nalogruClient, deviceService, clientSecret) r := render.New(templatePath) var receiptProcessor internal.ReceiptProcessor = receipts.NewProcessor(&receiptRepository, r) diff --git a/backend/users/processor.go b/backend/users/processor.go index fbb2889..02cd263 100644 --- a/backend/users/processor.go +++ b/backend/users/processor.go @@ -6,7 +6,6 @@ import ( "encoding/base64" api "github.com/drypa/ReceiptCollector/api/inside" "google.golang.org/grpc" - "log" "receipt_collector/device" "receipt_collector/nalogru" nalogDevice "receipt_collector/nalogru/device" @@ -17,13 +16,19 @@ import ( type Processor struct { repository *Repository linkGenerator LinkGenerator - d *device.Service + deviceService *device.Service nalogClient *nalogru.Client + clientSecret string } // NewProcessor constructs Processor. -func NewProcessor(repository *Repository, linkGenerator LinkGenerator, nalogClient *nalogru.Client, d *device.Service) *Processor { - return &Processor{repository: repository, linkGenerator: linkGenerator, nalogClient: nalogClient, d: d} +func NewProcessor(repository *Repository, linkGenerator LinkGenerator, nalogClient *nalogru.Client, d *device.Service, secret string) *Processor { + return &Processor{ + repository: repository, + linkGenerator: linkGenerator, + nalogClient: nalogClient, + deviceService: d, + clientSecret: secret} } // GetLoginLink returns login link for user in request. @@ -93,19 +98,13 @@ func (p Processor) RegisterUser(ctx context.Context, in *api.UserRegistrationReq } } userId := user.Id.Hex() - d, err := p.d.GetByUserId(ctx, userId) + d, err := p.deviceService.GetByUserId(ctx, userId) if err != nil { return nil, err } if d == nil { - secret, err := generateRandomSecret() - if err != nil { - log.Printf("error while generating random string: %v", err) - return nil, err - } - d := &nalogDevice.Device{ - ClientSecret: *secret, + ClientSecret: p.clientSecret, SessionId: "", RefreshToken: "", Update: nil, @@ -113,10 +112,10 @@ func (p Processor) RegisterUser(ctx context.Context, in *api.UserRegistrationReq Phone: in.PhoneNumber, } d.Update = func(sessionId string, refreshToken string) error { - return p.d.Update(ctx, d, sessionId, refreshToken) + return p.deviceService.Update(ctx, d, sessionId, refreshToken) } - err = p.d.Add(ctx, d) + err = p.deviceService.Add(ctx, d) if err != nil { return nil, err }