Skip to content

Commit

Permalink
Track complete auth only for unique state
Browse files Browse the repository at this point in the history
  • Loading branch information
berejant committed Oct 16, 2023
1 parent e3c231a commit 0cefd5c
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 1 deletion.
9 changes: 8 additions & 1 deletion ApiController.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ type ApiController struct {
apiClientFactory func(token string) kneu.ApiClientInterface

oauthRedirectUrl string

countCache *countCache
}

type AuthOptionsClaims struct {
Expand Down Expand Up @@ -101,7 +103,6 @@ func (controller *ApiController) getAuthUrl(c *gin.Context) {
}

func (controller *ApiController) completeAuth(c *gin.Context) {
completeAuthRequestsTotal.Inc()

var authOptionsClaims AuthOptionsClaims
var tokenResponse kneu.OauthTokenResponse
Expand All @@ -110,6 +111,12 @@ func (controller *ApiController) completeAuth(c *gin.Context) {
code := c.Query("code")
state := c.Query("state")

stateReceivedCount := controller.countCache.Get(&state)
controller.countCache.Set(&state, stateReceivedCount+1)
if stateReceivedCount == 0 {
completeAuthRequestsTotal.Inc()
}

authOptionsClaims, err := controller.parseState(state)
if err == nil {
tokenResponse, err = controller.oauthClient.GetOauthToken(controller.oauthRedirectUrl, code)
Expand Down
5 changes: 5 additions & 0 deletions ApiController_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ func TestCompleteAuth(t *testing.T) {
assert.Equal(t, tokenResponse.AccessToken, token)
return apiClient
},
countCache: NewCountCache(1),
}

router := (controller).setupRouter()
Expand Down Expand Up @@ -288,6 +289,7 @@ func TestCompleteAuth(t *testing.T) {
assert.Equal(t, tokenResponse.AccessToken, token)
return apiClient
},
countCache: NewCountCache(1),
}

router := (controller).setupRouter()
Expand Down Expand Up @@ -334,6 +336,7 @@ func TestCompleteAuth(t *testing.T) {
out: &bytes.Buffer{},
config: config,
oauthClient: oauthClient,
countCache: NewCountCache(1),
}

router := (controller).setupRouter()
Expand Down Expand Up @@ -410,6 +413,7 @@ func TestCompleteAuth(t *testing.T) {
assert.Equal(t, tokenResponse.AccessToken, token)
return apiClient
},
countCache: NewCountCache(1),
}

router := (controller).setupRouter()
Expand Down Expand Up @@ -460,6 +464,7 @@ func TestCompleteAuth(t *testing.T) {
assert.Equal(t, tokenResponse.AccessToken, token)
return apiClient
},
countCache: NewCountCache(1),
}

router := (controller).setupRouter()
Expand Down
2 changes: 2 additions & 0 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ func runApp(out io.Writer, listenAndServe func(string, http.Handler) error) erro
AccessToken: token,
}
},

countCache: NewCountCache(1),
}

apiController.apiClientFactory("test")
Expand Down
27 changes: 27 additions & 0 deletions countCache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package main

import (
"github.com/VictoriaMetrics/fastcache"
)

type countCache struct {
*fastcache.Cache
}

func (cache *countCache) Set(key *string, count uint8) {
cache.Cache.Set([]byte(*key), []byte{count})
}

func (cache *countCache) Get(key *string) uint8 {
countBytes, exists := cache.Cache.HasGet([]byte{}, []byte(*key))
if exists {
return countBytes[0]
}
return 0
}

func NewCountCache(maxBytes int) *countCache {
return &countCache{
Cache: fastcache.New(maxBytes),
}
}
29 changes: 29 additions & 0 deletions countCache_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package main

import (
"github.com/stretchr/testify/assert"
"testing"
)

func TestCountCacheGetSet(t *testing.T) {
cache := NewCountCache(1)

t.Run("Valid time get-set", func(t *testing.T) {
key := "key-1"
expectedCount := uint8(6)

cache.Set(&key, expectedCount)
acualCount := cache.Get(&key)

assert.Equal(t, expectedCount, acualCount, "Timestamp value is not expected")
})

t.Run("Get not not exist value", func(t *testing.T) {
key := "key-20"
expectedValue := uint8(0)

actualValue := cache.Get(&key)

assert.Equal(t, expectedValue, actualValue, "Timestamp value is not expected")
})
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@ require (
)

require (
github.com/VictoriaMetrics/fastcache v1.12.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-playground/locales v0.14.0 // indirect
github.com/go-playground/universal-translator v0.18.0 // indirect
github.com/go-playground/validator/v10 v10.11.1 // indirect
github.com/goccy/go-json v0.9.11 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.15.9 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
Expand Down
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
github.com/VictoriaMetrics/fastcache v1.12.1 h1:i0mICQuojGDL3KblA7wUNlY5lOK6a4bwt3uRKnkZU40=
github.com/VictoriaMetrics/fastcache v1.12.1/go.mod h1:tX04vaqcNoQeGLD+ra5pU5sWkuxnzWhEzLwhP9w653o=
github.com/VictoriaMetrics/metrics v1.24.0 h1:ILavebReOjYctAGY5QU2F9X0MYvkcrG3aEn2RKa1Zkw=
github.com/VictoriaMetrics/metrics v1.24.0/go.mod h1:eFT25kvsTidQFHb6U0oa0rTrDRdz4xTYjpL8+UPohys=
github.com/allegro/bigcache v1.2.1-0.20190218064605-e24eb225f156/go.mod h1:Cb/ax3seSYIx7SuZdm2G2xzfwmv3TPSk2ucNfQESPXM=
github.com/berejant/go-kneu v0.1.7 h1:GGpNh11jydXqdD7umoz9Vg0qjq6HDUNXffRu4K5Rtas=
github.com/berejant/go-kneu v0.1.7/go.mod h1:WzeT6vKANsglUcHsu5KjZcVq8orZsEwVE+QDYK8DIQA=
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
Expand All @@ -23,6 +28,8 @@ github.com/goccy/go-json v0.9.11/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MG
github.com/golang-jwt/jwt/v5 v5.0.0-rc.1 h1:tDQ1LjKga657layZ4JLsRdxgvupebc0xuPwRNuTfUgs=
github.com/golang-jwt/jwt/v5 v5.0.0-rc.1/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM=
github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
Expand Down Expand Up @@ -102,6 +109,7 @@ golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20210806184541-e5e7981a1069/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.7.0 h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU=
golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
Expand Down

0 comments on commit 0cefd5c

Please sign in to comment.