-
Notifications
You must be signed in to change notification settings - Fork 975
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: transient_payload lost in API flow with session token exchange code (PS-482) #4112
Open
splaunov
wants to merge
1
commit into
ory:master
Choose a base branch
from
monta-app:feature/fix-ps-482
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package flow | ||
|
||
import ( | ||
"github.com/ory/x/sqlxx" | ||
"github.com/tidwall/gjson" | ||
"github.com/tidwall/sjson" | ||
) | ||
|
||
const internalContextTransientPayloadPath = "transient_payload" | ||
|
||
func SetTransientPayloadIntoInternalContext(flow InternalContexter, transientPayload sqlxx.JSONRawMessage) error { | ||
if flow.GetInternalContext() == nil { | ||
flow.EnsureInternalContext() | ||
} | ||
bytes, err := sjson.SetBytes( | ||
flow.GetInternalContext(), | ||
internalContextTransientPayloadPath, | ||
transientPayload, | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
flow.SetInternalContext(bytes) | ||
|
||
return nil | ||
} | ||
|
||
func GetTransientPayloadFromInternalContext(flow InternalContexter) (sqlxx.JSONRawMessage, error) { | ||
if flow.GetInternalContext() == nil { | ||
flow.EnsureInternalContext() | ||
} | ||
raw := gjson.GetBytes(flow.GetInternalContext(), internalContextTransientPayloadPath) | ||
if !raw.IsObject() { | ||
return nil, nil | ||
} | ||
|
||
return sqlxx.JSONRawMessage(raw.Raw), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -164,6 +164,12 @@ func (s *Strategy) Register(w http.ResponseWriter, r *http.Request, f *registrat | |
f.TransientPayload = p.TransientPayload | ||
f.IDToken = p.IDToken | ||
f.RawIDTokenNonce = p.IDTokenNonce | ||
if err := flow.SetTransientPayloadIntoInternalContext(f, sqlxx.JSONRawMessage(p.TransientPayload)); err != nil { | ||
return s.handleError(w, r, f, pid, nil, err) | ||
} | ||
if err := s.d.RegistrationFlowPersister().UpdateRegistrationFlow(ctx, f); err != nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this database write really needed? |
||
return s.handleError(w, r, f, pid, nil, err) | ||
} | ||
|
||
if !strings.EqualFold(strings.ToLower(p.Method), s.SettingsStrategyID()) && p.Method != "" { | ||
// the user is sending a method that is not oidc, but the payload includes a provider | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -187,11 +187,13 @@ func TestStrategy(t *testing.T) { | |
return res, body | ||
} | ||
|
||
makeAPICodeFlowRequest := func(t *testing.T, provider, action string) (returnToURL *url.URL) { | ||
res, err := testhelpers.NewDebugClient(t).Post(action, "application/json", strings.NewReader(fmt.Sprintf(`{ | ||
"method": "oidc", | ||
"provider": %q | ||
}`, provider))) | ||
makeAPICodeFlowRequest := func(t *testing.T, provider, action string, transientPayload string) (returnToURL *url.URL) { | ||
res, err := testhelpers.NewDebugClient(t).Post(action, "application/json", | ||
strings.NewReader(fmt.Sprintf(`{ | ||
"method": "oidc", | ||
"provider": %q, | ||
"transient_payload": %q | ||
}`, provider, transientPayload))) | ||
require.NoError(t, err) | ||
require.Equal(t, http.StatusUnprocessableEntity, res.StatusCode) | ||
var changeLocation flow.BrowserLocationChangeRequiredError | ||
|
@@ -650,14 +652,25 @@ func TestStrategy(t *testing.T) { | |
}) | ||
|
||
t.Run("suite=API with session token exchange code", func(t *testing.T) { | ||
postRegistrationWebhook := hooktest.NewServer() | ||
t.Cleanup(postRegistrationWebhook.Close) | ||
postRegistrationWebhook.SetConfig(t, conf.GetProvider(ctx), | ||
config.HookStrategyKey(config.ViperKeySelfServiceRegistrationAfter, identity.CredentialsTypeOIDC.String())) | ||
|
||
postLoginWebhook := hooktest.NewServer() | ||
t.Cleanup(postLoginWebhook.Close) | ||
postLoginWebhook.SetConfig(t, conf.GetProvider(ctx), | ||
config.HookStrategyKey(config.ViperKeySelfServiceLoginAfter, config.HookGlobal)) | ||
|
||
scope = []string{"openid"} | ||
transientPayload := `{"data": "registration"}` | ||
|
||
loginOrRegister := func(t *testing.T, flowID uuid.UUID, code string) { | ||
_, err := exchangeCodeForToken(t, sessiontokenexchange.Codes{InitCode: code}) | ||
require.Error(t, err) | ||
|
||
action := assertFormValues(t, flowID, "valid") | ||
returnToURL := makeAPICodeFlowRequest(t, "valid", action) | ||
returnToURL := makeAPICodeFlowRequest(t, "valid", action, transientPayload) | ||
returnToCode := returnToURL.Query().Get("code") | ||
assert.NotEmpty(t, code, "code query param was empty in the return_to URL") | ||
|
||
|
@@ -673,27 +686,39 @@ func TestStrategy(t *testing.T) { | |
performRegistration := func(t *testing.T) { | ||
f := newAPIRegistrationFlow(t, returnTS.URL+"?return_session_token_exchange_code=true&return_to=/app_code", 1*time.Minute) | ||
loginOrRegister(t, f.ID, f.SessionTokenExchangeCode) | ||
postRegistrationWebhook.AssertTransientPayload(t, transientPayload) | ||
} | ||
startRegistrationButLogin := func(t *testing.T) { | ||
f := newAPIRegistrationFlow(t, returnTS.URL+"?return_session_token_exchange_code=true&return_to=/app_code", 1*time.Minute) | ||
loginOrRegister(t, f.ID, f.SessionTokenExchangeCode) | ||
postLoginWebhook.AssertTransientPayload(t, transientPayload) | ||
} | ||
performLogin := func(t *testing.T) { | ||
f := newAPILoginFlow(t, returnTS.URL+"?return_session_token_exchange_code=true&return_to=/app_code", 1*time.Minute) | ||
loginOrRegister(t, f.ID, f.SessionTokenExchangeCode) | ||
postLoginWebhook.AssertTransientPayload(t, transientPayload) | ||
} | ||
startLoginButRegister := func(t *testing.T) { | ||
f := newAPILoginFlow(t, returnTS.URL+"?return_session_token_exchange_code=true&return_to=/app_code", 1*time.Minute) | ||
loginOrRegister(t, f.ID, f.SessionTokenExchangeCode) | ||
postRegistrationWebhook.AssertTransientPayload(t, transientPayload) | ||
} | ||
|
||
for _, tc := range []struct { | ||
name string | ||
first, then func(*testing.T) | ||
}{{ | ||
name: "login-twice", | ||
first: performLogin, then: performLogin, | ||
first: startLoginButRegister, then: performLogin, | ||
}, { | ||
name: "login-then-register", | ||
first: performLogin, then: performRegistration, | ||
first: startLoginButRegister, then: startRegistrationButLogin, | ||
}, { | ||
name: "register-then-login", | ||
first: performRegistration, then: performLogin, | ||
}, { | ||
name: "register-twice", | ||
first: performRegistration, then: performRegistration, | ||
first: performRegistration, then: startRegistrationButLogin, | ||
}} { | ||
t.Run("case="+tc.name, func(t *testing.T) { | ||
subject = tc.name + "[email protected]" | ||
|
@@ -718,7 +743,7 @@ func TestStrategy(t *testing.T) { | |
require.Error(t, err) | ||
|
||
action := assertFormValues(t, f.ID, "valid") | ||
returnToURL := makeAPICodeFlowRequest(t, "valid", action) | ||
returnToURL := makeAPICodeFlowRequest(t, "valid", action, "{}") | ||
returnedFlow := returnToURL.Query().Get("flow") | ||
|
||
require.NotEmpty(t, returnedFlow, "flow query param was empty in the return_to URL") | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Error is always nil, remove it?