Skip to content

Commit

Permalink
Merge pull request #33 from gen-mind/develop
Browse files Browse the repository at this point in the history
develop
  • Loading branch information
apaladiychuk authored Apr 16, 2024
2 parents d77f4ad + 740bddf commit 0d1f61b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 36 deletions.
37 changes: 18 additions & 19 deletions backend/api/handler/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"cognix.ch/api/v2/core/parameters"
"cognix.ch/api/v2/core/security"
"cognix.ch/api/v2/core/server"
"cognix.ch/api/v2/core/storage"
"cognix.ch/api/v2/core/utils"
"encoding/base64"
"encoding/json"
Expand All @@ -21,19 +20,19 @@ type AuthHandler struct {
oauthClient oauth.Proxy
jwtService security.JWTService
authBL bll.AuthBL
storage storage.Storage
//storage storage.Storage
}

func NewAuthHandler(oauthClient oauth.Proxy,
jwtService security.JWTService,
authBL bll.AuthBL,
storage storage.Storage,
//storage storage.Storage,

) *AuthHandler {
return &AuthHandler{oauthClient: oauthClient,
jwtService: jwtService,
authBL: authBL,
storage: storage,
//storage: storage,
}
}

Expand Down Expand Up @@ -184,20 +183,20 @@ func (h *AuthHandler) Invite(c *gin.Context, identity *security.Identity) error
// @Success 200 {object} string
// @Router /auth/google/invite [get]
func (h *AuthHandler) JoinToTenant(c *gin.Context) error {
param := c.Query("state")

key, err := base64.URLEncoding.DecodeString(param)
if err != nil {
return utils.InvalidInput.Wrap(err, "wrong state")
}
value, err := h.storage.Pull(string(key))

state := base64.URLEncoding.EncodeToString(value)

url, err := h.oauthClient.Login(c.Request.Context(), state)
if err != nil {
return err
}
c.Redirect(http.StatusFound, url)
//param := c.Query("state")
//
//key, err := base64.URLEncoding.DecodeString(param)
//if err != nil {
// return utils.InvalidInput.Wrap(err, "wrong state")
//}
////value, err := h.storage.Pull(string(key))
//
//state := base64.URLEncoding.EncodeToString("value")
//
//url, err := h.oauthClient.Login(c.Request.Context(), state)
//if err != nil {
// return err
//}
//c.Redirect(http.StatusFound, url)
return nil
}
2 changes: 1 addition & 1 deletion backend/api/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ var Module = fx.Options(
NewRouter,
newGoogleOauthProvider,
newJWTService,
newStorage,
//newStorage,
ai.NewBuilder,
server.NewAuthMiddleware,
handler.NewAuthHandler,
Expand Down
28 changes: 12 additions & 16 deletions backend/core/bll/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@ import (
"cognix.ch/api/v2/core/parameters"
"cognix.ch/api/v2/core/repository"
"cognix.ch/api/v2/core/security"
"cognix.ch/api/v2/core/storage"
"cognix.ch/api/v2/core/utils"
"context"
"encoding/base64"
"encoding/json"
"fmt"
"github.com/google/uuid"
)
Expand All @@ -26,16 +24,14 @@ type (
authBL struct {
userRepo repository.UserRepository
redirectURL string
storage storage.Storage
//storage storage.Storage
}
)

func NewAuthBL(userRepo repository.UserRepository,
storage storage.Storage,
cfg *Config) AuthBL {
return &authBL{
userRepo: userRepo,
storage: storage,
redirectURL: cfg.RedirectURL,
}
}
Expand Down Expand Up @@ -83,18 +79,18 @@ func (a *authBL) Invite(ctx context.Context, identity *security.Identity, param
if exists {
return "", utils.InvalidInput.New("user already registered.")
}
buf, err := json.Marshal(parameters.OAuthParam{Action: oauth.InviteState,
Role: param.Role,
Email: param.Email,
TenantID: identity.User.TenantID.String(),
})
if err != nil {
return "", utils.Internal.Wrap(err, "can not marshal payload")
}
//buf, err := json.Marshal(parameters.OAuthParam{Action: oauth.InviteState,
// Role: param.Role,
// Email: param.Email,
// TenantID: identity.User.TenantID.String(),
//})
//if err != nil {
// return "", utils.Internal.Wrap(err, "can not marshal payload")
//}
key := uuid.New()
if err = a.storage.Save(key.String(), buf); err != nil {
return "", err
}
//if err = a.storage.Save(key.String(), buf); err != nil {
// return "", err
//}
state := base64.URLEncoding.EncodeToString([]byte(key.String()))

return fmt.Sprintf("%s/auth/google/invite?state=%s", a.redirectURL, state), nil
Expand Down

0 comments on commit 0d1f61b

Please sign in to comment.