Skip to content

Commit

Permalink
feat: add logger google auth
Browse files Browse the repository at this point in the history
  • Loading branch information
namnhce committed Apr 2, 2024
1 parent c3bb66f commit 4ed31d1
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 23 deletions.
25 changes: 18 additions & 7 deletions pkg/controller/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"gorm.io/gorm"

"github.com/dwarvesf/fortress-api/pkg/logger"
"github.com/dwarvesf/fortress-api/pkg/model"
"github.com/dwarvesf/fortress-api/pkg/utils/authutils"
)
Expand All @@ -15,22 +16,30 @@ type AuthenticationInput struct {
RedirectURL string
}

func (r *controller) Auth(in AuthenticationInput) (*model.Employee, string, error) {
accessToken, err := r.service.Google.GetAccessToken(in.Code, in.RedirectURL)
func (c *controller) Auth(in AuthenticationInput) (*model.Employee, string, error) {
l := c.logger.Fields(logger.Fields{
"controller": "auth",
"method": "Auth",
})

accessToken, err := c.service.Google.GetAccessToken(in.Code, in.RedirectURL)
if err != nil {
l.Errorf(err, "failed to get access token")
return nil, "", err
}

// 2.2 get login user email from access token
primaryEmail := ""
if r.config.Env == "prod" {
primaryEmail, err = r.service.Google.GetGoogleEmailLegacy(accessToken)
if c.config.Env == "prod" {
primaryEmail, err = c.service.Google.GetGoogleEmailLegacy(accessToken)
if err != nil {
l.Errorf(err, "failed to get google email legacy")
return nil, "", err
}
} else {
primaryEmail, err = r.service.Google.GetGoogleEmail(accessToken)
primaryEmail, err = c.service.Google.GetGoogleEmail(accessToken)
if err != nil {
l.Errorf(err, "failed to get google email")
return nil, "", err
}
}
Expand All @@ -41,8 +50,9 @@ func (r *controller) Auth(in AuthenticationInput) (*model.Employee, string, erro
}

// 2.4 check user is active
employee, err := r.store.Employee.OneByEmail(r.repo.DB(), primaryEmail)
employee, err := c.store.Employee.OneByEmail(c.repo.DB(), primaryEmail)
if err != nil {
l.Errorf(err, "failed to employee by email")
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, "", ErrUserInactivated
}
Expand All @@ -60,8 +70,9 @@ func (r *controller) Auth(in AuthenticationInput) (*model.Employee, string, erro
Email: primaryEmail,
}

jwt, err := authutils.GenerateJWTToken(&authenticationInfo, time.Now().Add(24*365*time.Hour).Unix(), r.config.JWTSecretKey)
jwt, err := authutils.GenerateJWTToken(&authenticationInfo, time.Now().Add(24*365*time.Hour).Unix(), c.config.JWTSecretKey)
if err != nil {
l.Errorf(err, "failed to generate jwt token")
return nil, "", err
}

Expand Down
10 changes: 5 additions & 5 deletions pkg/controller/auth/create_apikey.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/dwarvesf/fortress-api/pkg/utils/authutils"
)

func (r *controller) CreateAPIKey(roleID string) (string, error) {
func (c *controller) CreateAPIKey(roleID string) (string, error) {
clientID, err := authutils.GenerateUniqueNanoID(authutils.ClientIDLength)
if err != nil {
return "", err
Expand All @@ -30,17 +30,17 @@ func (r *controller) CreateAPIKey(roleID string) (string, error) {
return "", err
}

tx, done := r.repo.NewTransaction()
tx, done := c.repo.NewTransaction()

role, err := r.store.Role.One(tx.DB(), roleIDUUID)
role, err := c.store.Role.One(tx.DB(), roleIDUUID)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return "", done(ErrRoleNotfound)
}
return "", done(err)
}

apikey, err := r.store.APIKey.Create(tx.DB(), &model.APIKey{
apikey, err := c.store.APIKey.Create(tx.DB(), &model.APIKey{
ClientID: clientID,
SecretKey: hashedKey,
Status: model.ApikeyStatusValid,
Expand All @@ -49,7 +49,7 @@ func (r *controller) CreateAPIKey(roleID string) (string, error) {
return "", done(err)
}

_, err = r.store.APIKeyRole.Create(tx.DB(), &model.APIKeyRole{
_, err = c.store.APIKeyRole.Create(tx.DB(), &model.APIKeyRole{
APIKeyID: apikey.ID,
RoleID: role.ID,
})
Expand Down
6 changes: 3 additions & 3 deletions pkg/controller/auth/me.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"github.com/dwarvesf/fortress-api/pkg/model"
)

func (r *controller) Me(userID string) (*model.Employee, []*model.Permission, error) {
e, err := r.store.Employee.One(r.repo.DB(), userID, false)
func (c *controller) Me(userID string) (*model.Employee, []*model.Permission, error) {
e, err := c.store.Employee.One(c.repo.DB(), userID, false)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, nil, ErrUserNotFound
Expand All @@ -18,7 +18,7 @@ func (r *controller) Me(userID string) (*model.Employee, []*model.Permission, er
return nil, nil, err
}

perms, err := r.store.Permission.GetByEmployeeID(r.repo.DB(), userID)
perms, err := c.store.Permission.GetByEmployeeID(c.repo.DB(), userID)
if err != nil {
return nil, nil, err
}
Expand Down
14 changes: 6 additions & 8 deletions pkg/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,13 @@ type Service struct {
func New(cfg *config.Config, store *store.Store, repo store.DBRepo) *Service {
cch := cache.New(5*time.Minute, 10*time.Minute)

authServiceCfg := &oauth2.Config{
ClientID: cfg.Google.ClientID,
ClientSecret: cfg.Google.ClientSecret,
Endpoint: google.Endpoint,
Scopes: []string{"email", "profile"},
}

googleAuthSvc, err := googleauth.New(
authServiceCfg,
&oauth2.Config{
ClientID: cfg.Google.ClientID,
ClientSecret: cfg.Google.ClientSecret,
Endpoint: google.Endpoint,
Scopes: []string{"email", "profile"},
},
)
if err != nil {
logger.L.Error(err, "failed to init google auth")
Expand Down

0 comments on commit 4ed31d1

Please sign in to comment.