Skip to content

Commit ae34fc7

Browse files
committed
fix: update_env resolver
1 parent 2a5d5d4 commit ae34fc7

File tree

5 files changed

+109
-15
lines changed

5 files changed

+109
-15
lines changed

server/memorystore/providers/inmemory/store.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,7 @@ func (c *provider) DeleteAllUserSessions(userId string) error {
3535
constants.AuthRecipeMethodGoogle,
3636
constants.AuthRecipeMethodLinkedIn,
3737
}
38-
if os.Getenv("ENV") != constants.TestEnv {
39-
c.mutex.Lock()
40-
defer c.mutex.Unlock()
41-
}
38+
4239
for _, namespace := range namespaces {
4340
c.sessionStore.RemoveAll(namespace + ":" + userId)
4441
}
@@ -47,14 +44,16 @@ func (c *provider) DeleteAllUserSessions(userId string) error {
4744

4845
// DeleteUserSession deletes the user session from the in-memory store.
4946
func (c *provider) DeleteUserSession(userId, sessionToken string) error {
50-
if os.Getenv("ENV") != constants.TestEnv {
51-
c.mutex.Lock()
52-
defer c.mutex.Unlock()
53-
}
5447
c.sessionStore.Remove(userId, sessionToken)
5548
return nil
5649
}
5750

51+
// DeleteSessionForNamespace to delete session for a given namespace example google,github
52+
func (c *provider) DeleteSessionForNamespace(namespace string) error {
53+
c.sessionStore.RemoveByNamespace(namespace)
54+
return nil
55+
}
56+
5857
// SetState sets the state in the in-memory store.
5958
func (c *provider) SetState(key, state string) error {
6059
if os.Getenv("ENV") != constants.TestEnv {

server/memorystore/providers/inmemory/stores/session_store.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package stores
22

33
import (
44
"os"
5+
"strings"
56
"sync"
67

78
"github.com/authorizerdev/authorizer/server/constants"
@@ -65,3 +66,18 @@ func (s *SessionStore) GetAll(key string) map[string]string {
6566
}
6667
return s.store[key]
6768
}
69+
70+
// RemoveByNamespace to delete session for a given namespace example google,github
71+
func (s *SessionStore) RemoveByNamespace(namespace string) error {
72+
if os.Getenv("ENV") != constants.TestEnv {
73+
s.mutex.Lock()
74+
defer s.mutex.Unlock()
75+
}
76+
77+
for key := range s.store {
78+
if strings.Contains(key, namespace+":") {
79+
delete(s.store, key)
80+
}
81+
}
82+
return nil
83+
}

server/memorystore/providers/providers.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ type Provider interface {
1212
DeleteUserSession(userId, key string) error
1313
// DeleteAllSessions deletes all the sessions from the session store
1414
DeleteAllUserSessions(userId string) error
15+
// DeleteSessionForNamespace deletes the session for a given namespace
16+
DeleteSessionForNamespace(namespace string) error
1517

1618
// SetState sets the login state (key, value form) in the session store
1719
SetState(key, state string) error

server/memorystore/providers/redis/store.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,32 @@ func (c *provider) DeleteAllUserSessions(userID string) error {
8282
return nil
8383
}
8484

85+
// DeleteSessionForNamespace to delete session for a given namespace example google,github
86+
func (c *provider) DeleteSessionForNamespace(namespace string) error {
87+
var cursor uint64
88+
for {
89+
keys := []string{}
90+
keys, cursor, err := c.store.Scan(c.ctx, cursor, namespace+":*", 0).Result()
91+
if err != nil {
92+
log.Debugf("Error scanning keys for %s namespace: %s", namespace, err.Error())
93+
return err
94+
}
95+
96+
for _, key := range keys {
97+
err := c.store.Del(c.ctx, key).Err()
98+
if err != nil {
99+
log.Debugf("Error deleting sessions for %s namespace: %s", namespace, err.Error())
100+
return err
101+
}
102+
}
103+
if cursor == 0 { // no more keys
104+
break
105+
}
106+
}
107+
108+
return nil
109+
}
110+
85111
// SetState sets the state in redis store.
86112
func (c *provider) SetState(key, value string) error {
87113
err := c.store.Set(c.ctx, stateStorePrefix+key, value, 0).Err()

server/resolvers/update_env.go

Lines changed: 58 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,54 @@ import (
2121
"github.com/authorizerdev/authorizer/server/utils"
2222
)
2323

24+
// check if login methods have been disabled
25+
// remove the session tokens for those methods
26+
func clearSessionIfRequired(currentData, updatedData map[string]interface{}) {
27+
isCurrentBasicAuthEnabled := !currentData[constants.EnvKeyDisableBasicAuthentication].(bool)
28+
isCurrentMagicLinkLoginEnabled := !currentData[constants.EnvKeyDisableMagicLinkLogin].(bool)
29+
isCurrentAppleLoginEnabled := currentData[constants.EnvKeyAppleClientID] != nil && currentData[constants.EnvKeyAppleClientSecret] != nil && currentData[constants.EnvKeyAppleClientID].(string) != "" && currentData[constants.EnvKeyAppleClientSecret].(string) != ""
30+
isCurrentFacebookLoginEnabled := currentData[constants.EnvKeyFacebookClientID] != nil && currentData[constants.EnvKeyFacebookClientSecret] != nil && currentData[constants.EnvKeyFacebookClientID].(string) != "" && currentData[constants.EnvKeyFacebookClientSecret].(string) != ""
31+
isCurrentGoogleLoginEnabled := currentData[constants.EnvKeyGoogleClientID] != nil && currentData[constants.EnvKeyGoogleClientSecret] != nil && currentData[constants.EnvKeyGoogleClientID].(string) != "" && currentData[constants.EnvKeyGoogleClientSecret].(string) != ""
32+
isCurrentGithubLoginEnabled := currentData[constants.EnvKeyGithubClientID] != nil && currentData[constants.EnvKeyGithubClientSecret] != nil && currentData[constants.EnvKeyGithubClientID].(string) != "" && currentData[constants.EnvKeyGithubClientSecret].(string) != ""
33+
isCurrentLinkedInLoginEnabled := currentData[constants.EnvKeyLinkedInClientID] != nil && currentData[constants.EnvKeyLinkedInClientSecret] != nil && currentData[constants.EnvKeyLinkedInClientID].(string) != "" && currentData[constants.EnvKeyLinkedInClientSecret].(string) != ""
34+
35+
isUpdatedBasicAuthEnabled := !updatedData[constants.EnvKeyDisableBasicAuthentication].(bool)
36+
isUpdatedMagicLinkLoginEnabled := !updatedData[constants.EnvKeyDisableMagicLinkLogin].(bool)
37+
isUpdatedAppleLoginEnabled := updatedData[constants.EnvKeyAppleClientID] != nil && updatedData[constants.EnvKeyAppleClientSecret] != nil && updatedData[constants.EnvKeyAppleClientID].(string) != "" && updatedData[constants.EnvKeyAppleClientSecret].(string) != ""
38+
isUpdatedFacebookLoginEnabled := updatedData[constants.EnvKeyFacebookClientID] != nil && updatedData[constants.EnvKeyFacebookClientSecret] != nil && updatedData[constants.EnvKeyFacebookClientID].(string) != "" && updatedData[constants.EnvKeyFacebookClientSecret].(string) != ""
39+
isUpdatedGoogleLoginEnabled := updatedData[constants.EnvKeyGoogleClientID] != nil && updatedData[constants.EnvKeyGoogleClientSecret] != nil && updatedData[constants.EnvKeyGoogleClientID].(string) != "" && updatedData[constants.EnvKeyGoogleClientSecret].(string) != ""
40+
isUpdatedGithubLoginEnabled := updatedData[constants.EnvKeyGithubClientID] != nil && updatedData[constants.EnvKeyGithubClientSecret] != nil && updatedData[constants.EnvKeyGithubClientID].(string) != "" && updatedData[constants.EnvKeyGithubClientSecret].(string) != ""
41+
isUpdatedLinkedInLoginEnabled := updatedData[constants.EnvKeyLinkedInClientID] != nil && updatedData[constants.EnvKeyLinkedInClientSecret] != nil && updatedData[constants.EnvKeyLinkedInClientID].(string) != "" && updatedData[constants.EnvKeyLinkedInClientSecret].(string) != ""
42+
43+
if isCurrentBasicAuthEnabled && !isUpdatedBasicAuthEnabled {
44+
memorystore.Provider.DeleteSessionForNamespace(constants.AuthRecipeMethodBasicAuth)
45+
}
46+
47+
if isCurrentMagicLinkLoginEnabled && !isUpdatedMagicLinkLoginEnabled {
48+
memorystore.Provider.DeleteSessionForNamespace(constants.AuthRecipeMethodMagicLinkLogin)
49+
}
50+
51+
if isCurrentAppleLoginEnabled && !isUpdatedAppleLoginEnabled {
52+
memorystore.Provider.DeleteSessionForNamespace(constants.AuthRecipeMethodApple)
53+
}
54+
55+
if isCurrentFacebookLoginEnabled && !isUpdatedFacebookLoginEnabled {
56+
memorystore.Provider.DeleteSessionForNamespace(constants.AuthRecipeMethodFacebook)
57+
}
58+
59+
if isCurrentGoogleLoginEnabled && !isUpdatedGoogleLoginEnabled {
60+
memorystore.Provider.DeleteSessionForNamespace(constants.AuthRecipeMethodGoogle)
61+
}
62+
63+
if isCurrentGithubLoginEnabled && !isUpdatedGithubLoginEnabled {
64+
memorystore.Provider.DeleteSessionForNamespace(constants.AuthRecipeMethodGithub)
65+
}
66+
67+
if isCurrentLinkedInLoginEnabled && !isUpdatedLinkedInLoginEnabled {
68+
memorystore.Provider.DeleteSessionForNamespace(constants.AuthRecipeMethodLinkedIn)
69+
}
70+
}
71+
2472
// UpdateEnvResolver is a resolver for update config mutation
2573
// This is admin only mutation
2674
func UpdateEnvResolver(ctx context.Context, params model.UpdateEnvInput) (*model.Response, error) {
@@ -37,12 +85,19 @@ func UpdateEnvResolver(ctx context.Context, params model.UpdateEnvInput) (*model
3785
return res, fmt.Errorf("unauthorized")
3886
}
3987

40-
updatedData, err := memorystore.Provider.GetEnvStore()
88+
currentData, err := memorystore.Provider.GetEnvStore()
4189
if err != nil {
4290
log.Debug("Failed to get env store: ", err)
4391
return res, err
4492
}
4593

94+
// clone currentData in new var
95+
// that will be updated based on the req
96+
updatedData := make(map[string]interface{})
97+
for key, val := range currentData {
98+
updatedData[key] = val
99+
}
100+
46101
isJWTUpdated := false
47102
algo := updatedData[constants.EnvKeyJwtType].(string)
48103
if params.JwtType != nil {
@@ -210,6 +265,8 @@ func UpdateEnvResolver(ctx context.Context, params model.UpdateEnvInput) (*model
210265
}
211266
}
212267

268+
go clearSessionIfRequired(currentData, updatedData)
269+
213270
// Update local store
214271
memorystore.Provider.UpdateEnvStore(updatedData)
215272
jwk, err := crypto.GenerateJWKBasedOnEnv()
@@ -224,12 +281,6 @@ func UpdateEnvResolver(ctx context.Context, params model.UpdateEnvInput) (*model
224281
return res, err
225282
}
226283

227-
// TODO check how to update session store based on env change.
228-
// err = sessionstore.InitSession()
229-
// if err != nil {
230-
// log.Debug("Failed to init session store: ", err)
231-
// return res, err
232-
// }
233284
err = oauth.InitOAuth()
234285
if err != nil {
235286
return res, err

0 commit comments

Comments
 (0)