Skip to content
This repository was archived by the owner on Nov 24, 2020. It is now read-only.

Commit f6f9db3

Browse files
committed
support multiple access tokens
users can now generate five access tokens
1 parent b14a5ee commit f6f9db3

File tree

5 files changed

+53
-22
lines changed

5 files changed

+53
-22
lines changed

database/common.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,10 +151,18 @@ func ValidateAccessToken(name string, accessToken string) (User, error) {
151151
if !user.Conf {
152152
continue
153153
}
154-
if user.Username == name && user.AccessToken == accessToken &&
155-
utils.Unix()-user.AccessTokenTimeout < consts.AccessTokenLife {
156-
return user, nil
154+
if user.Username == name {
155+
dummy = user
156+
break
157157
}
158158
}
159+
160+
timeNow := utils.Unix()
161+
for storedToken, timeout := range dummy.AccessToken {
162+
if storedToken == accessToken && timeNow-timeout < consts.AccessTokenLife {
163+
return dummy, nil
164+
}
165+
}
166+
159167
return dummy, errors.New("could not find user with requested credentials")
160168
}

database/user.go

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,7 @@ type User struct {
8383
// AnchorKYC contains KYC information required by AnchorUSD
8484
AnchorKYC AnchorKYCHelper
8585
// AccessToken is the access token that will be used for authenticating RPC requests made to the server
86-
AccessToken string
87-
// AccessTokenTimeout is the unix time at which the accessToken was generated
88-
AccessTokenTimeout int64
86+
AccessToken map[string]int64
8987
// Mailbox is a mailbox where admins can send you messages or updated on your invested / interested projects
9088
Mailbox []MailboxHelper
9189
// Legal is a bool which is set when the user accepts the terms and conditions
@@ -594,20 +592,45 @@ func (a *User) ImportSeed(encryptedSeed []byte, pubkey string, seedpwd string) e
594592

595593
// GenAccessToken generates a new access token for the user
596594
func (a *User) GenAccessToken() (string, error) {
597-
a.AccessToken = utils.GetRandomString(consts.AccessTokenLength)
598-
a.AccessTokenTimeout = utils.Unix()
595+
timeNow := utils.Unix()
596+
if len(a.AccessToken) == 0 {
597+
a.AccessToken = make(map[string]int64)
598+
} else {
599+
// delete expired tokens
600+
for token, timeout := range a.AccessToken {
601+
if timeNow-timeout >= consts.AccessTokenLife {
602+
delete(a.AccessToken, token)
603+
}
604+
}
605+
606+
if len(a.AccessToken) == 5 { // all 5 tokens are valid, delete oldest token
607+
min := int64(0)
608+
minToken := ""
609+
for token, timeout := range a.AccessToken {
610+
if timeout > min {
611+
min = timeout
612+
minToken = token
613+
}
614+
}
615+
delete(a.AccessToken, minToken) // delete the oldest token
616+
}
617+
}
618+
619+
token := utils.GetRandomString(consts.AccessTokenLength)
620+
a.AccessToken[token] = timeNow
599621

600622
err := a.Save()
601623
if err != nil {
602624
return "", errors.Wrap(err, "could not save user to database")
603625
}
604-
return a.AccessToken, nil
626+
return token, nil
605627
}
606628

607629
// AllLogout invalidates the user access token
608630
func (a *User) AllLogout() error {
609-
a.AccessToken = utils.GetRandomString(consts.AccessTokenLength)
610-
a.AccessTokenTimeout = utils.Unix()
631+
for token := range a.AccessToken {
632+
delete(a.AccessToken, token)
633+
}
611634
return a.Save()
612635
}
613636

go.mod

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module github.com/YaleOpenLab/openx
33
go 1.13
44

55
require (
6-
github.com/Varunram/essentials v1.0.3
6+
github.com/Varunram/essentials v1.0.4
77
github.com/bithyve/research v0.0.0-20191102090848-d238806b60bf
88
github.com/boltdb/bolt v1.3.1
99
github.com/fsnotify/fsnotify v1.4.9 // indirect
@@ -25,8 +25,8 @@ require (
2525
github.com/spf13/jwalterweatherman v1.1.0 // indirect
2626
github.com/spf13/pflag v1.0.5 // indirect
2727
github.com/spf13/viper v1.7.0
28-
github.com/stellar/go v0.0.0-20200527155254-4b6180abef10
28+
github.com/stellar/go v0.0.0-20200528062442-f08b35a3f034
2929
github.com/stellar/go-xdr v0.0.0-20200331223602-71a1e6d555f2 // indirect
3030
github.com/stretchr/objx v0.2.0 // indirect
31-
gopkg.in/ini.v1 v1.56.0 // indirect
31+
gopkg.in/ini.v1 v1.57.0 // indirect
3232
)

go.sum

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAE
3434
github.com/Shopify/sarama v1.19.0/go.mod h1:FVkBWblsNy7DGZRfXLU0O9RCGt5g3g3yEuWXgklEdEo=
3535
github.com/Shopify/toxiproxy v2.1.4+incompatible/go.mod h1:OXgGpZ6Cli1/URJOF1DMxUHB2q5Ap20/P/eIdh4G0pI=
3636
github.com/StackExchange/wmi v0.0.0-20180116203802-5d049714c4a6/go.mod h1:3eOhrUMpNV+6aFIbp5/iudMxNCF27Vw2OZgy4xEx0Fg=
37-
github.com/Varunram/essentials v1.0.3 h1:yvtIktxyyFLem7DlPVEialdhhEQTNOClYU8/hLIc2EU=
38-
github.com/Varunram/essentials v1.0.3/go.mod h1:zMk9fBDl4caEScH5ldr0qkWKRgdLMnVx9Noiz33PISs=
37+
github.com/Varunram/essentials v1.0.4 h1:vozDqHxIx1/+SwrqNgau38BFHbmuXl/RdhXktRaMACA=
38+
github.com/Varunram/essentials v1.0.4/go.mod h1:zMk9fBDl4caEScH5ldr0qkWKRgdLMnVx9Noiz33PISs=
3939
github.com/VictoriaMetrics/fastcache v1.5.7/go.mod h1:ptDBkNMQI4RtmVo8VS/XwRY6RoTu1dAWCbrk+6WsEM8=
4040
github.com/aead/siphash v1.0.1/go.mod h1:Nywa3cDsYNNK3gaciGTWPwHt0wlpNV15vwmswBAUSII=
4141
github.com/ajg/form v0.0.0-20160822230020-523a5da1a92f h1:zvClvFQwU++UpIUBGC8YmDlfhUrweEy1R1Fj1gu5iIM=
@@ -496,8 +496,8 @@ github.com/status-im/keycard-go v0.0.0-20190316090335-8537d3370df4/go.mod h1:RZL
496496
github.com/steakknife/bloomfilter v0.0.0-20180922174646-6819c0d2a570/go.mod h1:8OR4w3TdeIHIh1g6EMY5p0gVNOovcWC+1vpc7naMuAw=
497497
github.com/steakknife/hamming v0.0.0-20180906055917-c99c65617cd3/go.mod h1:hpGUWaI9xL8pRQCTXQgocU38Qw1g0Us7n5PxxTwTCYU=
498498
github.com/stellar/go v0.0.0-20200515153832-820becd8083b/go.mod h1:kfZjGLxoc7P6QAk7BOrXL1qVk7gJV3+OSk4FCf0dXsM=
499-
github.com/stellar/go v0.0.0-20200527155254-4b6180abef10 h1:oMka3ywMJQHCsZe8NtLdYe5CLWBSyS6POhUYe0ICoBM=
500-
github.com/stellar/go v0.0.0-20200527155254-4b6180abef10/go.mod h1:Q4JDgZhGw7Ytu1924PzNkwr2GsLptrzAjwC/icuXnLU=
499+
github.com/stellar/go v0.0.0-20200528062442-f08b35a3f034 h1:YbQySRDlm+qU2+khzooq+bCEteADDprTrqh2YMNehqI=
500+
github.com/stellar/go v0.0.0-20200528062442-f08b35a3f034/go.mod h1:Q4JDgZhGw7Ytu1924PzNkwr2GsLptrzAjwC/icuXnLU=
501501
github.com/stellar/go-xdr v0.0.0-20180917104419-0bc96f33a18e h1:n/hfey8pO+RYMoGXyvyzuw5pdO8IFDoyAL/g5OiCesY=
502502
github.com/stellar/go-xdr v0.0.0-20180917104419-0bc96f33a18e/go.mod h1:gpOLVzy6TVYTQ3LvHSN9RJC700FkhFCpSE82u37aNRM=
503503
github.com/stellar/go-xdr v0.0.0-20200331223602-71a1e6d555f2 h1:K9H+A+eWe8ZlnpNha+pXbEK+jtIluQp/2dKxkK8k7OE=
@@ -703,8 +703,8 @@ gopkg.in/gavv/httpexpect.v1 v1.0.0-20170111145843-40724cf1e4a0/go.mod h1:WtiW9ZA
703703
gopkg.in/gorp.v1 v1.7.1/go.mod h1:Wo3h+DBQZIxATwftsglhdD/62zRFPhGhTiu5jUJmCaw=
704704
gopkg.in/ini.v1 v1.51.0 h1:AQvPpx3LzTDM0AjnIRlVFwFFGC+npRopjZxLJj6gdno=
705705
gopkg.in/ini.v1 v1.51.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
706-
gopkg.in/ini.v1 v1.56.0 h1:DPMeDvGTM54DXbPkVIZsp19fp/I2K7zwA/itHYHKo8Y=
707-
gopkg.in/ini.v1 v1.56.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
706+
gopkg.in/ini.v1 v1.57.0 h1:9unxIsFcTt4I55uWluz+UmL95q4kdJ0buvQ1ZIqVQww=
707+
gopkg.in/ini.v1 v1.57.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
708708
gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce/go.mod h1:5AcXVHNjg+BDxry382+8OKon8SEWiKktQR07RKPsv1c=
709709
gopkg.in/olebedev/go-duktape.v3 v3.0.0-20200316214253-d7b0ff38cac9/go.mod h1:uAJfkITjFhyEEuUfm7bsmCZRbW5WRq8s9EY8HZ6hCns=
710710
gopkg.in/resty.v1 v1.12.0/go.mod h1:mDo4pnntr5jdWRML875a/NmxYqAlA73dVijT2AXvQQo=

test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@ func main() {
104104
admin.Index = 1
105105
admin.Username = "admin"
106106
admin.Pwhash = utils.SHA3hash("password")
107-
admin.AccessToken = "pmkjMEnyeUpdTyhdHElkBExEKeLIlYft"
108-
admin.AccessTokenTimeout = utils.Unix() + 1000000
107+
admin.AccessToken = make(map[string]int64)
108+
admin.AccessToken["pmkjMEnyeUpdTyhdHElkBExEKeLIlYft"] = utils.Unix() + 10000000000
109109
admin.Admin = true
110110
admin.Conf = true
111111
err = admin.Save()

0 commit comments

Comments
 (0)