Skip to content

Commit

Permalink
#70 generating random client secret
Browse files Browse the repository at this point in the history
  • Loading branch information
drypa committed Oct 21, 2023
1 parent da91234 commit 5b513a0
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion backend/users/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ package users

import (
"context"
"crypto/rand"
"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"
Expand Down Expand Up @@ -95,8 +98,14 @@ func (p Processor) RegisterUser(ctx context.Context, in *api.UserRegistrationReq
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: "", //TODO: generate random(or not) secret
ClientSecret: *secret,
SessionId: "",
RefreshToken: "",
Update: nil,
Expand All @@ -117,6 +126,16 @@ func (p Processor) RegisterUser(ctx context.Context, in *api.UserRegistrationReq
return &api.UserRegistrationResponse{UserId: userId}, nil
}

func generateRandomSecret() (*string, error) {
buf := make([]byte, 20)
_, err := rand.Read(buf)
if err != nil {
return nil, err
}
encoded := base64.StdEncoding.EncodeToString(buf)
return &encoded, nil
}

func (p Processor) addNewUser(ctx context.Context, telegramId int) (*User, error) {
u := User{
TelegramId: telegramId,
Expand Down

0 comments on commit 5b513a0

Please sign in to comment.