@@ -147,6 +147,7 @@ var (
147147 errWsNoToken = errors .New ("ws: missing token" )
148148 errWsNoCid = errors .New ("ws: missing cid" )
149149 errWsNoDid = errors .New ("ws: missing device id" )
150+ errWsDidMismatch = errors .New ("ws: device id mismatch" )
150151 errWsNoResponse = errors .New ("ws: no response" )
151152 errWsNoLocHash = errors .New ("ws: no loc hash" )
152153 errWsNoServerList = errors .New ("ws: no server list" )
@@ -1837,12 +1838,12 @@ func (a *WsClient) kid() string {
18371838
18381839func trunc8 (s string ) string {
18391840 if len (s ) <= 8 {
1840- return s [:4 ]
1841+ return s [:3 ]
18411842 }
18421843 if len (s ) <= 16 {
1843- return s [:4 ] + ".." + s [len (s )- 4 :]
1844+ return s [:2 ] + ".." + s [len (s )- 2 :]
18441845 }
1845- return s [:8 ] + ".." + s [len (s )- 8 :]
1846+ return s [:4 ] + ".." + s [len (s )- 4 :]
18461847}
18471848
18481849func newWsGw (c * WsWgConfig , h * http.Client , o x.RpnOps ) (* WsClient , error ) {
@@ -1862,6 +1863,20 @@ func newWsGw(c *WsWgConfig, h *http.Client, o x.RpnOps) (*WsClient, error) {
18621863 return a , nil
18631864}
18641865
1866+ // setDidIfNeeded assigns did to ent.Did. If ent.Did is already set and does not match
1867+ // the incoming did, errWsDidMismatch is returned and ent is left unchanged.
1868+ func setDidIfNeeded (ent * WsEntitlement , did string ) error {
1869+ if ent == nil {
1870+ return errWsNoEntitlement
1871+ }
1872+ if existing := ent .Did ; len (existing ) > 0 && existing != did {
1873+ log .E ("ws: did mismatch: existing %s, incoming %s" , trunc8 (existing ), trunc8 (did ))
1874+ return errWsDidMismatch
1875+ }
1876+ ent .Did = did
1877+ return nil
1878+ }
1879+
18651880func (w * BaseClient ) MakeWsWg (entitlement []byte , did string , ops x.RpnOps ) (* WsClient , error ) {
18661881 if len (entitlement ) <= 0 {
18671882 return nil , errWsNoEntitlement
@@ -1876,7 +1891,10 @@ func (w *BaseClient) MakeWsWg(entitlement []byte, did string, ops x.RpnOps) (*Ws
18761891 return nil , err
18771892 }
18781893
1879- (& ent ).Did = did
1894+ // TODO: if ent already has did set; then err on mismatch?
1895+ if err := setDidIfNeeded (& ent , did ); err != nil {
1896+ return nil , err
1897+ }
18801898 return makeWsWg (& w .h2 , & ent , ops )
18811899}
18821900
@@ -1924,14 +1942,18 @@ func (w *BaseClient) MakeWsEntitlement(entitlementOrStateJson []byte, did string
19241942 var ent WsEntitlement
19251943 err1 := json .Unmarshal (entitlementOrStateJson , & ent )
19261944 if err1 == nil {
1927- (& ent ).Did = did
1945+ if err := setDidIfNeeded (& ent , did ); err != nil {
1946+ return nil , err
1947+ }
19281948 return & ent , nil
19291949 }
19301950 var existingConf WsWgConfig
19311951 err2 := json .Unmarshal (entitlementOrStateJson , & existingConf )
19321952 if err2 == nil && existingConf .Entitlement != nil && len (existingConf .Entitlement .SessionToken ) > 0 {
19331953 ent := existingConf .Entitlement
1934- (ent ).Did = did
1954+ if err := setDidIfNeeded (ent , did ); err != nil {
1955+ return nil , err
1956+ }
19351957 return ent , nil
19361958 }
19371959 return nil , core .JoinErr (err1 , err2 )
@@ -1957,7 +1979,9 @@ func (w *BaseClient) MakeWsWgFrom(entitlementOrWsConfigJson []byte, did string,
19571979 sz , hasEnt , hasTok , err )
19581980 return w .MakeWsWg (entitlementOrWsConfigJson , did , ops )
19591981 }
1960- (existingConf .Entitlement ).Did = did
1982+ if err := setDidIfNeeded (existingConf .Entitlement , did ); err != nil {
1983+ return nil , err
1984+ }
19611985 return w .makeWsWgFrom (& existingConf , ops )
19621986}
19631987
0 commit comments