Skip to content

Commit

Permalink
fix: cleanup PR
Browse files Browse the repository at this point in the history
  • Loading branch information
joel authored and joel committed Mar 6, 2024
1 parent ea0c72c commit f665e51
Show file tree
Hide file tree
Showing 6 changed files with 25 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
6 changes: 5 additions & 1 deletion internal/api/pkce.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,15 @@ func addFlowPrefixToToken(token string, flowType models.FlowType) string {

func issueAuthCode(tx *storage.Connection, user *models.User, authenticationMethod models.AuthenticationMethod) (string, error) {
flowState, err := models.FindFlowStateByUserID(tx, user.ID.String(), authenticationMethod)
if models.IsNotFoundError(err) {
if err != nil && models.IsNotFoundError(err) {
return "", badRequestError("No valid flow state found for user.")
} else if err != nil {
return "", err
}
if err := flowState.RecordIssuedTime(tx); err != nil {
return "", err
}

return flowState.AuthCode, nil
}

Expand Down
3 changes: 1 addition & 2 deletions internal/api/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,8 +246,7 @@ func (a *API) PKCE(ctx context.Context, w http.ResponseWriter, r *http.Request)
} else if err != nil {
return err
}
// We exempt Magic Links from the requirement and fallback to
if flowState.AuthenticationMethod != models.MagicLink.String() && flowState.IsExpired(a.config.External.FlowStateExpiryDuration) {
if flowState.IsExpired(a.config.External.FlowStateExpiryDuration) {
return forbiddenError("invalid flow state, flow state has expired")
}

Expand Down
2 changes: 2 additions & 0 deletions internal/api/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,11 +181,13 @@ 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
}

if terr = a.setCookieTokens(config, token, false, w); terr != nil {
return internalServerError("Failed to set JWT cookie. %s", 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"`
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 @@ -152,5 +153,17 @@ 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.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
}
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 f665e51

Please sign in to comment.