Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: impose expiry on auth code instead of magic link #1440

Merged
merged 4 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.AuthCodeIssuedAt = &issueTime

terr = tx.Update(flowState)
} else {
token, terr = a.issueRefreshToken(ctx, tx, user, models.OAuth, grantParams)
Expand Down
4 changes: 4 additions & 0 deletions internal/api/pkce.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ func issueAuthCode(tx *storage.Connection, user *models.User, authenticationMeth
} else if err != nil {
return "", err
}
if err := flowState.RecordAuthCodeIssuedAtTime(tx); err != nil {
return "", err
}

return flowState.AuthCode, nil
}

Expand Down
2 changes: 1 addition & 1 deletion internal/api/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,9 @@ func (a *API) verifyGet(w http.ResponseWriter, r *http.Request, params *VerifyPa
if terr := tx.Reload(user); err != nil {
return terr
}

if isImplicitFlow(flowType) {
token, terr = a.issueRefreshToken(ctx, tx, user, models.OTP, grantParams)

if terr != nil {
return terr
}
Expand Down
13 changes: 13 additions & 0 deletions 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"`
AuthCodeIssuedAt *time.Time `json:"auth_code_issued_at" db:"auth_code_issued_at"`
CreatedAt time.Time `json:"created_at" db:"created_at"`
UpdatedAt time.Time `json:"updated_at" db:"updated_at"`
}
Expand Down Expand Up @@ -152,5 +153,17 @@ func (f *FlowState) VerifyPKCE(codeVerifier string) error {
}

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

func (f *FlowState) RecordAuthCodeIssuedAtTime(tx *storage.Connection) error {
issueTime := time.Now()
f.AuthCodeIssuedAt = &issueTime
if err := tx.Update(f); err != nil {
return err
}
return nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
do $$ begin
alter table {{ index .Options "Namespace" }}.flow_state add column if not exists auth_code_issued_at timestamptz null;
end $$
Loading