Skip to content

Commit

Permalink
fix: otp
Browse files Browse the repository at this point in the history
  • Loading branch information
isaqueveras committed Jul 29, 2023
1 parent 50479e8 commit 5dfdcc7
Show file tree
Hide file tree
Showing 10 changed files with 117 additions and 121 deletions.
33 changes: 28 additions & 5 deletions application/auth/otp_business.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,17 @@ func Configure(ctx context.Context, userID *uuid.UUID) (err error) {
dst := make([]byte, base32.StdEncoding.EncodedLen(len(data)))
base32.StdEncoding.Encode(dst, data)

repo := auth.NewOTPRepository(tx)
if err = repo.Configure(userID, utils.Pointer(string(dst))); err != nil {
repo := auth.NewOTPRepo(tx, userID)
if err = repo.SetToken(utils.Pointer(string(dst))); err != nil {
return oops.Err(err)
}

repoFlag := auth.NewFlagRepo(tx)
if err = repoFlag.Set(userID, utils.Pointer(domain.FlagOTPEnable)); err != nil {
return oops.Err(err)
}

if err = repoFlag.Set(userID, utils.Pointer(domain.FlagOTPSetup)); err != nil {
return oops.Err(err)
}

Expand All @@ -49,8 +58,22 @@ func Unconfigure(ctx context.Context, userID *uuid.UUID) (err error) {
}
defer tx.Rollback()

repository := auth.NewOTPRepository(tx)
if err = repository.Unconfigure(userID); err != nil {
repoFlag := auth.NewFlagRepo(tx)
flag, err := repoFlag.Get(userID)
if err != nil {
return oops.Err(err)
}

if err = repoFlag.Set(userID, utils.Pointer((domain.Flag(*flag))&(^domain.FlagOTPEnable))); err != nil {
return oops.Err(err)
}

if err = repoFlag.Set(userID, utils.Pointer((domain.Flag(*flag))&(^domain.FlagOTPSetup))); err != nil {
return oops.Err(err)
}

repoOTP := auth.NewOTPRepo(tx, userID)
if err = repoOTP.SetToken(nil); err != nil {
return oops.Err(err)
}

Expand All @@ -70,7 +93,7 @@ func GetQrCode(ctx context.Context, userID *uuid.UUID) (res *domain.QRCode, err
defer tx.Rollback()

var userName, token *string
if userName, token, err = auth.NewOTPRepository(tx).GetToken(userID); err != nil {
if userName, token, err = auth.NewOTPRepo(tx, userID).GetToken(); err != nil {
return nil, oops.Err(err)
}

Expand Down
10 changes: 5 additions & 5 deletions domain/auth/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@ type ISession interface {
Delete(sessionID *uuid.UUID) error
}

// IRole define an interface for data layer access methods
type IRole interface {
// IFlag define an interface for data layer access methods
type IFlag interface {
Get(userID *uuid.UUID) (*int64, error)
Set(userID *uuid.UUID, flag *Flag) error
}

// IOTP define an interface for data layer access methods
type IOTP interface {
GetToken(userID *uuid.UUID) (*string, *string, error)
Configure(userID *uuid.UUID, secret *string) error
Unconfigure(userID *uuid.UUID) error
GetToken() (*string, *string, error)
SetToken(secret *string) error
}

// IUser define an interface for data layer access methods
Expand Down
28 changes: 28 additions & 0 deletions infrastructure/persistencie/auth/flag_repository.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright (c) 2023 Isaque Veras
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.

package auth

import (
"github.com/google/uuid"
"github.com/isaqueveras/powersso/database/postgres"
domain "github.com/isaqueveras/powersso/domain/auth"
infra "github.com/isaqueveras/powersso/infrastructure/persistencie/auth/postgres"
)

var _ domain.IFlag = (*repoFlag)(nil)

type repoFlag struct{ pg *infra.PGFlag }

func NewFlagRepo(tx *postgres.Transaction) domain.IFlag {
return &repoFlag{pg: &infra.PGFlag{DB: tx}}
}

func (r *repoFlag) Set(userID *uuid.UUID, flag *domain.Flag) error {
return r.pg.Set(userID, flag)
}

func (r *repoFlag) Get(userID *uuid.UUID) (*int64, error) {
return r.pg.Get(userID)
}
26 changes: 8 additions & 18 deletions infrastructure/persistencie/auth/otp_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,23 @@ package auth

import (
"github.com/google/uuid"
pg "github.com/isaqueveras/powersso/database/postgres"
"github.com/isaqueveras/powersso/database/postgres"
domain "github.com/isaqueveras/powersso/domain/auth"
infra "github.com/isaqueveras/powersso/infrastructure/persistencie/auth/postgres"
)

var _ domain.IOTP = (*repoOTP)(nil)

type repoOTP struct {
pg *infra.PGOTP
}

// NewOTPRepository creates a new repository
func NewOTPRepository(tx *pg.Transaction) domain.IOTP {
return &repoOTP{pg: &infra.PGOTP{DB: tx}}
}
type repoOTP struct{ pg *infra.PGOTP }

// GetToken
func (r *repoOTP) GetToken(userID *uuid.UUID) (*string, *string, error) {
return r.pg.GetToken(userID)
func NewOTPRepo(tx *postgres.Transaction, userID *uuid.UUID) domain.IOTP {
return &repoOTP{pg: &infra.PGOTP{DB: tx, UserID: userID}}
}

// Configure
func (r *repoOTP) Configure(userID *uuid.UUID, secret *string) error {
return r.pg.Configure(userID, secret)
func (r *repoOTP) GetToken() (*string, *string, error) {
return r.pg.GetToken()
}

// Unconfigure
func (r *repoOTP) Unconfigure(userID *uuid.UUID) error {
return r.pg.Unconfigure(userID)
func (r *repoOTP) SetToken(secret *string) error {
return r.pg.SetToken(secret)
}
6 changes: 4 additions & 2 deletions infrastructure/persistencie/auth/postgres/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,13 @@ func (pg *PGAuth) AddAttempts(userID *uuid.UUID) (err error) {
func (pg *PGAuth) LoginSteps(email *string) (steps *auth.Steps, err error) {
steps = new(auth.Steps)
if err = pg.DB.Builder.
Select("COALESCE(otp AND otp_setup, FALSE), first_name").
Select("first_name").
Column("(flag&?) <> 0 AND (flag&?) <> 0",
auth.FlagOTPEnable, auth.FlagOTPSetup).
From("users").
Where("email = ?", email).
Limit(1).
Scan(&steps.OTP, &steps.Name); err != nil && err != sql.ErrNoRows {
Scan(&steps.Name, &steps.OTP); err != nil && err != sql.ErrNoRows {
return nil, oops.Err(err)
}

Expand Down
32 changes: 32 additions & 0 deletions infrastructure/persistencie/auth/postgres/flag.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package postgres

import (
"github.com/google/uuid"
"github.com/isaqueveras/powersso/database/postgres"
"github.com/isaqueveras/powersso/domain/auth"
"github.com/isaqueveras/powersso/oops"
)

type PGFlag struct{ DB *postgres.Transaction }

func (pg *PGFlag) Set(userID *uuid.UUID, flag *auth.Flag) error {
if _, err := pg.DB.Builder.
Update("users").
Set("flag", flag).
Where("id = ?::UUID", userID).
Exec(); err != nil {
return oops.Err(err)
}
return nil
}

func (pg *PGFlag) Get(userID *uuid.UUID) (flag *int64, err error) {
if err = pg.DB.Builder.
Select("flag").
From("users").
Where("id = ?::UUID", userID).
Scan(&flag); err != nil {
return nil, oops.Err(err)
}
return
}
40 changes: 10 additions & 30 deletions infrastructure/persistencie/auth/postgres/otp.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,55 +5,35 @@
package postgres

import (
"database/sql"

"github.com/Masterminds/squirrel"
"github.com/google/uuid"
pg "github.com/isaqueveras/powersso/database/postgres"
"github.com/isaqueveras/powersso/database/postgres"
"github.com/isaqueveras/powersso/oops"
)

// PGOTP is the implementation of transaction for the otp repository
type PGOTP struct {
DB *pg.Transaction
DB *postgres.Transaction
UserID *uuid.UUID
}

// GetToken fetch the token of a user's otp
func (pg *PGOTP) GetToken(userID *uuid.UUID) (userName, token *string, err error) {
func (pg *PGOTP) GetToken() (userName, token *string, err error) {
if err = pg.DB.Builder.
Select("CONCAT('(',first_name,' ',last_name,')') AS user_name, otp_token").
Select("CONCAT('(',first_name,' ',last_name,')'), otp").
From("public.users").
Where("id = ?::UUID", userID).
Where("id = ?::UUID AND otp NOTNULL", pg.UserID).
QueryRow().
Scan(&userName, &token); err != nil {
return nil, nil, oops.Err(err)
}
return
}

// Configure configure otp for a user
func (pg *PGOTP) Configure(userID *uuid.UUID, secret *string) (err error) {
if _, err = pg.DB.Builder.
Update("users").
Set("otp_token", secret).
Set("otp", true).
Set("otp_setup", true).
Set("updated_at", squirrel.Expr("NOW()")).
Where("id = ?", userID).
Exec(); err != nil && err != sql.ErrNoRows {
return oops.Err(err)
}
return
}

// Unconfigure unconfigure otp for a user
func (pg *PGOTP) Unconfigure(userID *uuid.UUID) (err error) {
func (pg *PGOTP) SetToken(secret *string) (err error) {
if _, err = pg.DB.Builder.
Update("users").
Set("otp", false).
Set("updated_at", squirrel.Expr("NOW()")).
Where("id = ?", userID).
Exec(); err != nil && err != sql.ErrNoRows {
Set("otp", secret).
Where("id = ?", pg.UserID).
Exec(); err != nil {
return oops.Err(err)
}
return
Expand Down
27 changes: 0 additions & 27 deletions infrastructure/persistencie/auth/postgres/role.go

This file was deleted.

8 changes: 2 additions & 6 deletions infrastructure/persistencie/auth/postgres/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,15 @@ import (

"github.com/Masterminds/squirrel"
"github.com/google/uuid"
pg "github.com/isaqueveras/powersso/database/postgres"
"github.com/isaqueveras/powersso/database/postgres"
domain "github.com/isaqueveras/powersso/domain/auth"
"github.com/isaqueveras/powersso/oops"
)

// PGUser is the implementation of transaction for the user repository
type PGUser struct {
DB *pg.Transaction
DB *postgres.Transaction
}

// Exist check if the user exists by email in the database
func (pg *PGUser) Exist(email *string) (err error) {
var exists *bool
if err = pg.DB.Builder.
Expand All @@ -36,7 +34,6 @@ func (pg *PGUser) Exist(email *string) (err error) {
return
}

// Get get the user from the database
func (pg *PGUser) Get(data *domain.User) (err error) {
cond := squirrel.Eq{"id": data.ID}
if data.Email != nil {
Expand All @@ -61,7 +58,6 @@ func (pg *PGUser) Get(data *domain.User) (err error) {
return
}

// Disable disable user in database
func (pg *PGUser) Disable(userUUID *uuid.UUID) (err error) {
if err = pg.DB.Builder.
Update("users").
Expand Down
28 changes: 0 additions & 28 deletions infrastructure/persistencie/auth/role_repository.go

This file was deleted.

0 comments on commit 5dfdcc7

Please sign in to comment.