File tree Expand file tree Collapse file tree 6 files changed +60
-5
lines changed Expand file tree Collapse file tree 6 files changed +60
-5
lines changed Original file line number Diff line number Diff line change 4
4
"bytes"
5
5
"encoding/json"
6
6
"fmt"
7
- "github.com/segmentio/ksuid "
7
+ "github.com/xmidt-org/webpa-common/device/sessionid "
8
8
"sync/atomic"
9
9
"time"
10
10
@@ -185,7 +185,7 @@ func newDevice(o deviceOptions) *device {
185
185
transactions : NewTransactions (),
186
186
partnerIDs : partnerIDs ,
187
187
satClientID : o .SatClientID ,
188
- sessionID : ksuid . New (). String (),
188
+ sessionID : sessionid . GenerateID (),
189
189
trust : o .Trust ,
190
190
}
191
191
}
Original file line number Diff line number Diff line change @@ -120,3 +120,4 @@ func TestDeviceSessionID(t *testing.T) {
120
120
assert .Equal (sessionOne .ID (), sessionTwo .ID ())
121
121
assert .NotEqual (sessionOne .SessionID (), sessionTwo .SessionID ())
122
122
}
123
+
Original file line number Diff line number Diff line change
1
+ package sessionid
2
+
3
+ import (
4
+ "encoding/base64"
5
+ "encoding/binary"
6
+ "errors"
7
+ "math/rand"
8
+ "time"
9
+ )
10
+
11
+ func GenerateID () string {
12
+ return GenerateIDWithTime (time .Now ())
13
+ }
14
+
15
+ func GenerateIDWithTime (t time.Time ) string {
16
+ var buffer [16 ]byte
17
+ rand .Read (buffer [:])
18
+ ts := uint32 (t .Unix ())
19
+ binary .BigEndian .PutUint32 (buffer [:4 ], ts )
20
+ return base64 .RawURLEncoding .EncodeToString (buffer [:])
21
+ }
22
+
23
+ func ParseID (id string ) (time.Time , error ) {
24
+ buffer , err := base64 .RawURLEncoding .DecodeString (id )
25
+ if err != nil {
26
+ return time.Time {}, err
27
+ }
28
+ if len (buffer ) != 16 {
29
+ return time.Time {}, errors .New ("byte array is wrong length" )
30
+ }
31
+ ts := binary .BigEndian .Uint32 (buffer [:4 ])
32
+ return time .Unix (int64 (ts ), 0 ), nil
33
+ }
Original file line number Diff line number Diff line change
1
+ package sessionid
2
+
3
+ import (
4
+ "github.com/stretchr/testify/assert"
5
+ "testing"
6
+ "time"
7
+ )
8
+
9
+ func TestGenerateID (t * testing.T ) {
10
+ assert := assert .New (t )
11
+ now := time .Now ()
12
+ id := GenerateIDWithTime (now )
13
+ assert .NotEmpty (id )
14
+
15
+ ts , err := ParseID (id )
16
+ assert .NoError (err )
17
+ assert .Equal (ts .Unix (), now .Unix ())
18
+
19
+ _ , err = ParseID ("" )
20
+ assert .Error (err )
21
+
22
+ _ , err = ParseID ("XjS1ECGCZU8WP18PmmIdc" )
23
+ assert .Error (err )
24
+ }
Original file line number Diff line number Diff line change @@ -39,7 +39,6 @@ require (
39
39
github.com/prometheus/client_golang v0.9.2
40
40
github.com/rubyist/circuitbreaker v2.2.0+incompatible
41
41
github.com/samuel/go-zookeeper v0.0.0-20180130194729-c4fab1ac1bec
42
- github.com/segmentio/ksuid v1.0.2
43
42
github.com/smartystreets/goconvey v0.0.0-20190731233626-505e41936337 // indirect
44
43
github.com/spaolacci/murmur3 v0.0.0-20150829172844-0d12bf811670 // indirect
45
44
github.com/spf13/pflag v1.0.3
Original file line number Diff line number Diff line change @@ -153,8 +153,6 @@ github.com/samuel/go-zookeeper v0.0.0-20180130194729-c4fab1ac1bec h1:6ncX5ko6B9L
153
153
github.com/samuel/go-zookeeper v0.0.0-20180130194729-c4fab1ac1bec /go.mod h1:gi+0XIa01GRL2eRQVjQkKGqKF3SF9vZR/HnPullcV2E =
154
154
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529 h1:nn5Wsu0esKSJiIVhscUtVbo7ada43DJhG55ua/hjS5I =
155
155
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529 /go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc =
156
- github.com/segmentio/ksuid v1.0.2 h1:9yBfKyw4ECGTdALaF09Snw3sLJmYIX6AbPJrAy6MrDc =
157
- github.com/segmentio/ksuid v1.0.2 /go.mod h1:BXuJDr2byAiHuQaQtSKoXh1J0YmUDurywOXgB2w+OSU =
158
156
github.com/sirupsen/logrus v1.2.0 /go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo =
159
157
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykElWQ6/NYmHa3jpm/yHnI4xSofP+UP6SpjHcSeM =
160
158
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d /go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc =
You can’t perform that action at this time.
0 commit comments