Skip to content

Commit

Permalink
fix: att issued at column
Browse files Browse the repository at this point in the history
  • Loading branch information
joel authored and joel committed Mar 6, 2024
1 parent 8f81e91 commit 1bdac5c
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 3 deletions.
3 changes: 3 additions & 0 deletions internal/api/external.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,9 @@ func (a *API) internalExternalProviderCallback(w http.ResponseWriter, r *http.Re
flowState.ProviderAccessToken = providerAccessToken
flowState.ProviderRefreshToken = providerRefreshToken
flowState.UserID = &(user.ID)
issueTime := time.Now()
flowState.IssuedAt = &issueTime

terr = tx.Update(flowState)
} else {
token, terr = a.issueRefreshToken(ctx, tx, user, models.OAuth, grantParams)
Expand Down
3 changes: 1 addition & 2 deletions internal/api/pkce.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ func issueAuthCode(tx *storage.Connection, user *models.User, authenticationMeth
} else if err != nil {
return "", err
}
// No op action to mark that auth code has been issued
if err := tx.Update(flowState); err != nil {
if err := flowState.RecordIssuedTime(tx); err != nil {
return "", err
}

Expand Down
12 changes: 11 additions & 1 deletion internal/models/flow_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type FlowState struct {
ProviderType string `json:"provider_type" db:"provider_type"`
ProviderAccessToken string `json:"provider_access_token" db:"provider_access_token"`
ProviderRefreshToken string `json:"provider_refresh_token" db:"provider_refresh_token"`
IssuedAt *time.Time `json:"issued_at" db:"issued_at"`
CreatedAt time.Time `json:"created_at" db:"created_at"`
UpdatedAt time.Time `json:"updated_at" db:"updated_at"`
}
Expand Down Expand Up @@ -153,7 +154,16 @@ func (f *FlowState) VerifyPKCE(codeVerifier string) error {

func (f *FlowState) IsExpired(expiryDuration time.Duration) bool {
if f.AuthenticationMethod == MagicLink.String() {
return time.Now().After(f.UpdatedAt.Add(expiryDuration))
return time.Now().After(f.IssuedAt.Add(expiryDuration))
}
return time.Now().After(f.CreatedAt.Add(expiryDuration))
}

func (f *FlowState) RecordIssuedTime(tx *storage.Connection) error {
issueTime := time.Now()
f.IssuedAt = &issueTime
if err := tx.Update(f); err != nil {
return err
}
return nil
}
1 change: 1 addition & 0 deletions migrations/20240221100230_add_issued_at_to_flow_state.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
alter table {{ index .Options "Namespace" }}.flow_state add column if not exists issued_at timestamptz null;

0 comments on commit 1bdac5c

Please sign in to comment.