-
Notifications
You must be signed in to change notification settings - Fork 688
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for jwt secret creation of each user upon user login (#…
…4719) * Added support for jwt secret creation of each user upon logic Signed-off-by: Saranya-jena <saranya.jena@harness.io> * Fixed imports Signed-off-by: Saranya-jena <saranya.jena@harness.io> * Add fixes in dex service Signed-off-by: Saranya-jena <saranya.jena@harness.io> * Fixed UTs Signed-off-by: Saranya-jena <saranya.jena@harness.io> * resolved comments Signed-off-by: Saranya-jena <saranya.jena@harness.io> * updated logic Signed-off-by: Saranya-jena <saranya.jena@harness.io> * fixed UTs and removed unecessary test cases Signed-off-by: Saranya-jena <saranya.jena@harness.io> * fixed imports Signed-off-by: Saranya-jena <saranya.jena@harness.io> * fixed imports Signed-off-by: Saranya-jena <saranya.jena@harness.io> * resolved comments Signed-off-by: Saranya-jena <saranya.jena@harness.io> * fixed imports Signed-off-by: Saranya-jena <saranya.jena@harness.io> * resolved comments Signed-off-by: Saranya-jena <saranya.jena@harness.io> * added server endpoint in allowed origins Signed-off-by: Saranya-jena <saranya.jena@harness.io> * fixed imports Signed-off-by: Saranya-jena <saranya.jena@harness.io> * minor chnages Signed-off-by: Saranya-jena <saranya.jena@harness.io> * minor chnages Signed-off-by: Saranya-jena <saranya.jena@harness.io> * fixed imports Signed-off-by: Saranya-jena <saranya.jena@harness.io> --------- Signed-off-by: Saranya-jena <saranya.jena@harness.io>
- Loading branch information
1 parent
fb46bb9
commit 9d58d8b
Showing
38 changed files
with
455 additions
and
270 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package response | ||
|
||
import ( | ||
"encoding/base64" | ||
|
||
"github.com/litmuschaos/litmus/chaoscenter/authentication/pkg/authConfig" | ||
"github.com/litmuschaos/litmus/chaoscenter/authentication/pkg/services" | ||
"github.com/litmuschaos/litmus/chaoscenter/authentication/pkg/utils" | ||
log "github.com/sirupsen/logrus" | ||
"go.mongodb.org/mongo-driver/mongo" | ||
) | ||
|
||
func AddSalt(service services.ApplicationService) error { | ||
// generate salt and add/update to user collection | ||
// pass the salt in the below func which will act as jwt secret | ||
getSalt, err := service.GetConfig("salt") | ||
if err != nil && err != mongo.ErrNoDocuments { | ||
log.Error(err) | ||
return err | ||
} | ||
if getSalt != nil { | ||
return nil | ||
} | ||
|
||
salt, err := utils.RandomString(6) | ||
if err != nil { | ||
log.Error(err) | ||
return err | ||
} | ||
encodedSalt := base64.StdEncoding.EncodeToString([]byte(salt)) | ||
|
||
config := authConfig.AuthConfig{ | ||
Key: "salt", | ||
Value: encodedSalt, | ||
} | ||
|
||
err = service.CreateConfig(config) | ||
if err != nil { | ||
log.Error(err) | ||
return err | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package authConfig | ||
|
||
import ( | ||
"context" | ||
"encoding/base64" | ||
|
||
"go.mongodb.org/mongo-driver/bson" | ||
"go.mongodb.org/mongo-driver/mongo" | ||
) | ||
|
||
type Repository interface { | ||
CreateConfig(config AuthConfig) error | ||
GetConfig(key string) (*AuthConfig, error) | ||
UpdateConfig(ctx context.Context, key string, value interface{}) error | ||
} | ||
|
||
type repository struct { | ||
Collection *mongo.Collection | ||
} | ||
|
||
func (r repository) CreateConfig(config AuthConfig) error { | ||
_, err := r.Collection.InsertOne(context.Background(), config) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (r repository) GetConfig(key string) (*AuthConfig, error) { | ||
results := r.Collection.FindOne(context.Background(), bson.D{ | ||
{"key", key}, | ||
}) | ||
|
||
var config AuthConfig | ||
err := results.Decode(&config) | ||
if err != nil { | ||
return nil, err | ||
} | ||
decodedValue, err := base64.URLEncoding.DecodeString(config.Value) | ||
if err != nil { | ||
return nil, err | ||
} | ||
config.Value = string(decodedValue) | ||
return &config, nil | ||
} | ||
|
||
func (r repository) UpdateConfig(ctx context.Context, key string, value interface{}) error { | ||
query := bson.D{ | ||
{"key", key}, | ||
} | ||
update := bson.D{{"$set", bson.D{{ | ||
"value", value}}, | ||
}} | ||
_, err := r.Collection.UpdateOne(ctx, query, update) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
// NewAuthConfigRepo creates a new instance of this repository | ||
func NewAuthConfigRepo(collection *mongo.Collection) Repository { | ||
return &repository{ | ||
Collection: collection, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package authConfig | ||
|
||
type AuthConfig struct { | ||
Key string `bson:"key"` | ||
Value string `bson:"value"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
chaoscenter/authentication/pkg/services/auth_config_service.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package services | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/litmuschaos/litmus/chaoscenter/authentication/pkg/authConfig" | ||
) | ||
|
||
type authConfigService interface { | ||
CreateConfig(config authConfig.AuthConfig) error | ||
GetConfig(key string) (*authConfig.AuthConfig, error) | ||
UpdateConfig(ctx context.Context, key string, value interface{}) error | ||
} | ||
|
||
func (a applicationService) CreateConfig(config authConfig.AuthConfig) error { | ||
return a.authConfigRepo.CreateConfig(config) | ||
} | ||
|
||
func (a applicationService) GetConfig(key string) (*authConfig.AuthConfig, error) { | ||
return a.authConfigRepo.GetConfig(key) | ||
} | ||
|
||
func (a applicationService) UpdateConfig(ctx context.Context, key string, value interface{}) error { | ||
return a.authConfigRepo.UpdateConfig(ctx, key, value) | ||
} |
Oops, something went wrong.