Skip to content

Commit

Permalink
Merge pull request #1377 from kaytu-io/fix-dex-auth
Browse files Browse the repository at this point in the history
fix: fix import cycle
  • Loading branch information
artaasadi authored Jul 29, 2024
2 parents e92b5ce + a357fab commit 757493a
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 43 deletions.
42 changes: 42 additions & 0 deletions pkg/auth/auth0/entities.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package auth0

import (
"encoding/json"
"github.com/kaytu-io/kaytu-engine/pkg/auth/db"
api2 "github.com/kaytu-io/kaytu-util/pkg/api"
"time"

Expand Down Expand Up @@ -55,6 +57,46 @@ type User struct {
Blocked bool `json:"blocked"`
}

func DbUserToApi(u *db.User) (*User, error) {
if u == nil {
return nil, nil
}
userMetadata := Metadata{}
appMetadata := Metadata{}

err := json.Unmarshal(u.UserMetadata.Bytes, &userMetadata)
if err != nil {
return nil, err
}

err = json.Unmarshal(u.AppMetadata.Bytes, &appMetadata)
if err != nil {
return nil, err
}

return &User{
Email: u.Email,
EmailVerified: u.EmailVerified,
FamilyName: u.FamilyName,
GivenName: u.GivenName,
Locale: u.Locale,
Name: u.Name,
Nickname: u.Nickname,
Picture: u.Picture,
UserId: u.UserId,
UserMetadata: userMetadata,
LastLogin: u.LastLogin,
LastIp: u.LastIp,
LoginsCount: u.LoginsCount,
AppMetadata: appMetadata,
Username: u.Username,
PhoneNumber: u.PhoneNumber,
PhoneVerified: u.PhoneVerified,
Multifactor: u.Multifactor,
Blocked: u.Blocked,
}, nil
}

type CreateUserRequest struct {
Email string `json:"email,omitempty"`
PhoneNumber string `json:"phone_number,omitempty"`
Expand Down
8 changes: 4 additions & 4 deletions pkg/auth/auth0/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (a *Service) GetUser(userID string) (*User, error) {
return nil, err
}

resp, err := user.ToApi()
resp, err := DbUserToApi(user)
if err != nil {
return nil, err
}
Expand All @@ -102,7 +102,7 @@ func (a *Service) SearchByEmail(email string) ([]User, error) {

var resp []User
for _, user := range users {
u, err := user.ToApi()
u, err := DbUserToApi(&user)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -312,7 +312,7 @@ func (a *Service) SearchUsersByWorkspace(wsID string) ([]User, error) {

var resp []User
for _, user := range users {
u, err := user.ToApi()
u, err := DbUserToApi(&user)
if err != nil {
return nil, err
}
Expand All @@ -329,7 +329,7 @@ func (a *Service) SearchUsers(wsID string, email *string, emailVerified *bool, r

var apiUsers []User
for _, user := range users {
u, err := user.ToApi()
u, err := DbUserToApi(&user)
if err != nil {
return nil, err
}
Expand Down
39 changes: 0 additions & 39 deletions pkg/auth/db/models.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package db

import (
"encoding/json"
"github.com/jackc/pgtype"
"github.com/kaytu-io/kaytu-engine/pkg/auth/auth0"
"github.com/kaytu-io/kaytu-util/pkg/api"
"gorm.io/gorm"
"time"
Expand Down Expand Up @@ -44,43 +42,6 @@ type User struct {
Blocked bool
}

func (u *User) ToApi() (*auth0.User, error) {
userMetadata := auth0.Metadata{}
appMetadata := auth0.Metadata{}

err := json.Unmarshal(u.UserMetadata.Bytes, &userMetadata)
if err != nil {
return nil, err
}

err = json.Unmarshal(u.AppMetadata.Bytes, &appMetadata)
if err != nil {
return nil, err
}

return &auth0.User{
Email: u.Email,
EmailVerified: u.EmailVerified,
FamilyName: u.FamilyName,
GivenName: u.GivenName,
Locale: u.Locale,
Name: u.Name,
Nickname: u.Nickname,
Picture: u.Picture,
UserId: u.UserId,
UserMetadata: userMetadata,
LastLogin: u.LastLogin,
LastIp: u.LastIp,
LoginsCount: u.LoginsCount,
AppMetadata: appMetadata,
Username: u.Username,
PhoneNumber: u.PhoneNumber,
PhoneVerified: u.PhoneVerified,
Multifactor: u.Multifactor,
Blocked: u.Blocked,
}, nil
}

type WorkspaceMap struct {
ID string `gorm:"primaryKey"`
Name string `gorm:"index"`
Expand Down

0 comments on commit 757493a

Please sign in to comment.