Skip to content

Commit 87207db

Browse files
authored
Merge pull request #13 from devtron-labs/auth-fix
fix: error handling in verifyAppState (/auth/callback API)
2 parents 30a0275 + 178a13f commit 87207db

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

authenticator/client/oidcClient.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"net/http"
2727
"net/url"
2828
"path"
29+
"sync"
2930
"time"
3031
)
3132

@@ -65,8 +66,8 @@ func getOidcClient(dexServerAddress string, settings *oidc.Settings, userVerifie
6566
},
6667
}
6768
dexProxy := oidc.NewDexHTTPReverseProxy(dexServerAddress, dexClient.Transport)
68-
cahecStore := &oidc.Cache{OidcState: map[string]*oidc.OIDCState{}}
69-
oidcClient, err := oidc.NewClientApp(settings, cahecStore, "/", userVerifier, RedirectUrlSanitiser)
69+
cacheStore := &oidc.Cache{OidcState: sync.Map{}}
70+
oidcClient, err := oidc.NewClientApp(settings, cacheStore, "/", userVerifier, RedirectUrlSanitiser)
7071
if err != nil {
7172
return nil, nil, err
7273
}

authenticator/oidc/oidc.go

+15-4
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232
"path"
3333
"regexp"
3434
"strings"
35+
"sync"
3536
"time"
3637

3738
gooidc "github.com/coreos/go-oidc/v3/oidc"
@@ -69,16 +70,23 @@ type OIDCStateStorage interface {
6970
}
7071

7172
type Cache struct {
72-
OidcState map[string]*OIDCState
73+
OidcState sync.Map
7374
}
7475

7576
func (c *Cache) GetOIDCState(key string) (*OIDCState, error) {
76-
state := c.OidcState[key]
77+
value, exists := c.OidcState.Load(key)
78+
if !exists {
79+
return nil, ErrCacheMiss
80+
}
81+
state, ok := value.(*OIDCState)
82+
if !ok || state == nil {
83+
return nil, ErrInvalidState
84+
}
7785
return state, nil
7886
}
7987

8088
func (c *Cache) SetOIDCState(key string, state *OIDCState) error {
81-
c.OidcState[key] = state
89+
c.OidcState.Store(key, state)
8290
return nil
8391
}
8492

@@ -287,12 +295,15 @@ func (a *ClientApp) generateAppState(returnURL string) string {
287295
}
288296

289297
var ErrCacheMiss = errors.New("cache: key is missing")
298+
var ErrInvalidState = errors.New("invalid app state")
290299

291300
func (a *ClientApp) verifyAppState(state string) (*OIDCState, error) {
292301
res, err := a.cache.GetOIDCState(state)
293302
if err != nil {
294-
if err == ErrCacheMiss {
303+
if errors.Is(err, ErrCacheMiss) {
295304
return nil, fmt.Errorf("unknown app state %s", state)
305+
} else if errors.Is(err, ErrInvalidState) {
306+
return nil, fmt.Errorf("invalid app state %s", state)
296307
} else {
297308
return nil, fmt.Errorf("failed to verify app state %s: %v", state, err)
298309
}

0 commit comments

Comments
 (0)