-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathcstoken.go
More file actions
86 lines (77 loc) · 2.02 KB
/
Copy pathcstoken.go
File metadata and controls
86 lines (77 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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)
}