-
-
Notifications
You must be signed in to change notification settings - Fork 963
feat: initial cstoken support #1102
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
gusquadri
wants to merge
7
commits into
tulir:main
Choose a base branch
from
gusquadri:cstoken-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
472d91d
feat: cstoken initial support
gusquadri 0330670
fix: copyright header
gusquadri 32137e4
fix: jid handling in generatecstoken
gusquadri 27eb66f
fix: simplify NCT salt handling in storeNCTSalt and dispatchAppState
gusquadri 6acfbf3
Merge remote-tracking branch 'upstream/main' into cstoken-support
gusquadri 82782d1
fix: streamline NCT salt handling in storeNCTSalt and DownloadHistory…
gusquadri 82b5241
fix: explicit handle SyncdMutation_REMOVE
gusquadri File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| // Copyright (c) 2026 Tulir Asokan | ||
| // | ||
| // This Source Code Form is subject to the terms of the Mozilla Public | ||
| // License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| // file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
|
||
| package whatsmeow | ||
|
|
||
| import ( | ||
| "context" | ||
| "crypto/hmac" | ||
| "crypto/sha256" | ||
|
|
||
| "go.mau.fi/whatsmeow/types" | ||
| ) | ||
|
|
||
| func shouldSendCsToken(jid types.JID) bool { | ||
| jid = jid.ToNonAD() | ||
| return (jid.Server == types.DefaultUserServer || jid.Server == types.HiddenUserServer) && | ||
| jid.User != types.PSAJID.User && | ||
| !jid.IsBot() | ||
| } | ||
|
|
||
| // derives a cstoken for the given JID using HMAC-SHA256(nctSalt, recipientLID). | ||
| func (cli *Client) generateCsToken(ctx context.Context, jid types.JID) []byte { | ||
| if !shouldSendCsToken(jid) { | ||
| return nil | ||
| } | ||
| if cli.Store == nil || cli.Store.NCTSalt == nil { | ||
| return nil | ||
| } | ||
| salt, err := cli.Store.NCTSalt.GetNCTSalt(ctx) | ||
| if err != nil { | ||
| cli.Log.Debugf("Failed to load NCT salt for cstoken: %v", err) | ||
| return nil | ||
| } | ||
| if len(salt) == 0 { | ||
| return nil | ||
| } | ||
| var recipientLID types.JID | ||
| switch jid.Server { | ||
| case types.HiddenUserServer: | ||
| recipientLID = jid.ToNonAD() | ||
| case types.DefaultUserServer: | ||
| if cli.Store == nil || cli.Store.LIDs == nil { | ||
| return nil | ||
| } | ||
| pn := jid.ToNonAD() | ||
| lid, err := cli.Store.LIDs.GetLIDForPN(ctx, pn) | ||
| if err != nil { | ||
| cli.Log.Debugf("Failed to resolve LID for cstoken JID %s: %v", pn, err) | ||
| return nil | ||
| } | ||
| if lid.IsEmpty() { | ||
| return nil | ||
| } | ||
| recipientLID = lid.ToNonAD() | ||
| default: | ||
| return nil | ||
| } | ||
|
|
||
| if recipientLID.Server != types.HiddenUserServer { | ||
| return nil | ||
| } | ||
|
|
||
| h := hmac.New(sha256.New, salt) | ||
| h.Write([]byte(recipientLID.String())) | ||
| return h.Sum(nil) | ||
| } | ||
|
|
||
| func (cli *Client) storeNCTSalt(ctx context.Context, salt []byte) error { | ||
| if cli.Store == nil || cli.Store.NCTSalt == nil { | ||
| return nil | ||
| } | ||
| if len(salt) == 0 { | ||
| return nil | ||
| } | ||
| return cli.Store.NCTSalt.PutNCTSalt(ctx, salt) | ||
| } | ||
|
|
||
| func (cli *Client) clearNCTSalt(ctx context.Context) error { | ||
| if cli.Store == nil || cli.Store.NCTSalt == nil { | ||
| return nil | ||
| } | ||
| return cli.Store.NCTSalt.DeleteNCTSalt(ctx) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| -- v14 (compatible with v8+): Add NCT salt table for cstoken derivation | ||
| CREATE TABLE whatsmeow_nct_salt ( | ||
| our_jid TEXT PRIMARY KEY, | ||
| salt bytea NOT NULL, | ||
| FOREIGN KEY (our_jid) REFERENCES whatsmeow_device(jid) ON DELETE CASCADE ON UPDATE CASCADE | ||
| ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.