Skip to content

Commit

Permalink
feat: add mfa indexes (#745)
Browse files Browse the repository at this point in the history
* feat: add mfa indexes

* Update models/amr.go

Co-authored-by: Stojan Dimitrovski <sdimitrovski@gmail.com>

* Update migrations/20221011041400_add_mfa_indexes.up.sql

Co-authored-by: Stojan Dimitrovski <sdimitrovski@gmail.com>

Co-authored-by: joel@joellee.org <joel@joellee.org>
Co-authored-by: Stojan Dimitrovski <sdimitrovski@gmail.com>
  • Loading branch information
3 people committed Oct 11, 2022
1 parent 5521df1 commit 243364b
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.

This file was deleted.

9 changes: 9 additions & 0 deletions migrations/20221011041400_add_mfa_indexes.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
create index if not exists refresh_token_session_id on {{ index .Options "Namespace" }}.refresh_tokens using btree(session_id);

alter table {{ index .Options "Namespace" }}.mfa_amr_claims
add column if not exists id uuid not null,
add constraint amr_id_pk primary key(id);

create index if not exists user_id_created_at_idx on {{ index .Options "Namespace" }}.sessions (user_id, created_at);
create index if not exists factor_id_created_at_idx on {{ index .Options "Namespace" }}.mfa_factors (user_id, created_at);
-- Add Index which handles the clause below
10 changes: 9 additions & 1 deletion models/amr.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import (
"github.com/gobuffalo/pop/v5"
"github.com/gofrs/uuid"
"github.com/netlify/gotrue/storage"
"github.com/pkg/errors"
)

type AMRClaim struct {
ID uuid.UUID `json:"id" db:"id"`
SessionID uuid.UUID `json:"session_id" db:"session_id"`
CreatedAt time.Time `json:"created_at" db:"created_at"`
UpdatedAt time.Time `json:"updated_at" db:"updated_at"`
Expand All @@ -21,7 +23,13 @@ func (AMRClaim) TableName() string {
}

func AddClaimToSession(tx *storage.Connection, session *Session, authenticationMethod AuthenticationMethod) error {
id, err := uuid.NewV4()
if err != nil {
return errors.Wrap(err, "Error generating unique claim id")
}
currentTime := time.Now()
return tx.RawQuery("INSERT INTO "+(&pop.Model{Value: AMRClaim{}}).TableName()+
"(session_id, created_at, updated_at, authentication_method) values(?, ?, ?, ?) "+"ON CONFLICT ON CONSTRAINT mfa_amr_claims_session_id_authentication_method_pkey "+"DO UPDATE SET updated_at = ?;", session.ID, currentTime, currentTime, authenticationMethod.String(), currentTime).Exec()
`(id, session_id, created_at, updated_at, authentication_method) values (?, ?, ?, ?, ?)
ON CONFLICT ON CONSTRAINT mfa_amr_claims_session_id_authentication_method_pkey
DO UPDATE SET updated_at = ?;`, id, session.ID, currentTime, currentTime, authenticationMethod.String(), currentTime).Exec()
}

0 comments on commit 243364b

Please sign in to comment.