Skip to content

Commit

Permalink
bridge/crypto: add ShareOneTimeKeys function to CryptoHelper
Browse files Browse the repository at this point in the history
Signed-off-by: Sumner Evans <sumner@beeper.com>
  • Loading branch information
sumnerevans committed Aug 10, 2023
1 parent 16a756d commit 57bfd82
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 2 deletions.
2 changes: 2 additions & 0 deletions bridge/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package bridge

import (
"context"
"encoding/json"
"errors"
"fmt"
Expand Down Expand Up @@ -219,6 +220,7 @@ type Crypto interface {
Stop()
Reset(startAfterReset bool)
Client() *mautrix.Client
ShareOneTimeKeys(context.Context) error
}

func (br *Bridge) GenerateRegistration() {
Expand Down
5 changes: 5 additions & 0 deletions bridge/crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,11 @@ func (helper *CryptoHelper) HandleMemberEvent(evt *event.Event) {
helper.mach.HandleMemberEvent(0, evt)
}

// ShareOneTimeKeys uploads the given number of one-time-keys to the server.
func (helper *CryptoHelper) ShareOneTimeKeys(ctx context.Context) error {
return helper.mach.ShareKeys(ctx, -1)
}

type cryptoSyncer struct {
*crypto.OlmMachine
}
Expand Down
4 changes: 2 additions & 2 deletions crypto/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -622,8 +622,8 @@ func (mach *OlmMachine) ShareKeys(ctx context.Context, currentOTKCount int) erro
start := time.Now()
mach.otkUploadLock.Lock()
defer mach.otkUploadLock.Unlock()
if mach.lastOTKUpload.Add(1 * time.Minute).After(start) {
log.Debug().Msg("Checking OTK count from server due to suspiciously close share keys requests")
if mach.lastOTKUpload.Add(1*time.Minute).After(start) || currentOTKCount < 0 {
log.Debug().Msg("Checking OTK count from server due to suspiciously close share keys requests or negative OTK count")
resp, err := mach.Client.UploadKeys(&mautrix.ReqUploadKeys{})
if err != nil {
return fmt.Errorf("failed to check current OTK counts: %w", err)
Expand Down

0 comments on commit 57bfd82

Please sign in to comment.