Skip to content

Commit

Permalink
crypto/machine: add function to download and store latest key backup
Browse files Browse the repository at this point in the history
Signed-off-by: Sumner Evans <sumner@beeper.com>
  • Loading branch information
sumnerevans committed Jan 11, 2024
1 parent c60474b commit 28b408f
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions crypto/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -688,3 +688,43 @@ func (mach *OlmMachine) ExpiredKeyDeleteLoop(ctx context.Context) {
}
}
}

func (mach *OlmMachine) DownloadAndStoreLatestKeyBackup(ctx context.Context) error {
log := mach.machOrContextLog(ctx).With().
Str("action", "download and store latest key backup").
Logger()
versionInfo, err := mach.Client.GetKeyBackupLatestVersion(ctx)
if err != nil {
return err
}

if versionInfo.Algorithm != id.KeyBackupAlgorithmCurve25519AESSHA2 {
return fmt.Errorf("unsupported key backup algorithm: %s", versionInfo.Algorithm)
}

log = log.With().
Int("count", versionInfo.Count).
Str("etag", versionInfo.ETag).
Str("key_backup_version", versionInfo.Version).
Logger()

if versionInfo.Count == 0 {
log.Debug().Msg("No keys found in key backup")
return nil
}

keys, err := mach.Client.GetKeyBackup(ctx, versionInfo.Version)
if err != nil {
return err
}

for roomID, backup := range keys.Rooms {
fmt.Printf("Sessions for %s\n", roomID)
for sessionID, keyBackupData := range backup.Sessions {
// TODO
fmt.Printf("\t%s: %v\n", sessionID, keyBackupData)
}
}

return nil
}

0 comments on commit 28b408f

Please sign in to comment.