Skip to content

Initiate login flow from oauth before redirecting to gate#624

Merged
senthalan merged 2 commits intoasgardeo:mainfrom
senthalan:improve-code-flow-engine
Nov 1, 2025
Merged

Initiate login flow from oauth before redirecting to gate#624
senthalan merged 2 commits intoasgardeo:mainfrom
senthalan:improve-code-flow-engine

Conversation

@senthalan
Copy link
Contributor

@senthalan senthalan commented Oct 30, 2025

This pull request introduces a new flow initialization mechanism and integrates it into the OAuth authorization process. The changes allow the OAuth authorization handler to pre-initialize a flow with runtime data and store the resulting flowId in the session, improving extensibility for future authentication flows. The update also includes dependency injection improvements, new error handling, and test adjustments.

Architecture Flow

┌─────────────────────────────────────────────────────────────┐
│ OAuth Authorization Request                                 │
│ GET /oauth2/authorize?client_id=X&scope=openid profile      │
└──────────────────┬──────────────────────────────────────────┘
                   │
                   ▼
┌─────────────────────────────────────────────────────────────┐
│ OAuth Handler: handleInitialAuthorizationRequest()          │
│ • Validates OAuth parameters                                │
│ • Initiates flow: flowExecService.InitiateFlow({            │
│     ApplicationID: appID,                                   │
│     FlowType: "AUTHENTICATION",                             │
│     RuntimeData: {"oauth_scope": "openid profile"}          │
│   })                                                        │
│ • Stores: SessionData{OAuthParams: ...}                     │
└──────────────────┬──────────────────────────────────────────┘
                   │
                   ▼
┌─────────────────────────────────────────────────────────────┐
│ Flow Engine: InitiateFlow()                                 │
│ • Generates UUID for flowID                                 │
│ • Creates EngineContext with RuntimeData                    │
│ • Stores context to DB (CurrentNode = nil)                  │
│ • Returns flowID                                            │
└──────────────────┬──────────────────────────────────────────┘
                   │
                   ▼
┌─────────────────────────────────────────────────────────────┐
│ Redirect to Gate App                                        │
│ ?sessionDataKey=xxx                                         │
│ &flowId=flow-456          ← Changed parameter               │
└──────────────────┬──────────────────────────────────────────┘
                   │
                   ▼
┌─────────────────────────────────────────────────────────────┐
│ Gate App                                                    │
│ • Can use flowId to call POST /flow/execute directly        │
└──────────────────┬──────────────────────────────────────────┘
                   │
                   ▼
┌─────────────────────────────────────────────────────────────┐
│ Flow Execution: POST /flow/execute                          │
│ • Loads context from DB using flowId                        │
│ • Detects CurrentNode == nil → starts from beginning        │
│ • OAuth scope available in RuntimeData["oauth_scope"]       │
└─────────────────────────────────────────────────────────────┘

Depends on asgardeo/javascript#229 for the Gate App to use the new flowId param

Breaking change is for the deployments which hosts the Gate Application hosted outside(Currently, the oauth2/authorize will redirect the Gate with the applicationId, with this change the redirection from the oauth2/authorize will redirect the Gate with the flowId). In those deployments, the hosted applications asgardeo/react sdk needs to be updated to 0.5.33.

Copilot AI review requested due to automatic review settings October 30, 2025 18:57
@senthalan senthalan added Type/Improvement breaking change The feature/ improvement will alter the existing behaviour labels Oct 30, 2025
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR refactors the OAuth authorization flow to use a pre-initialized flow ID instead of passing the application ID. The flow is now initiated before redirecting to the authentication page, providing better separation of concerns between OAuth authorization and flow execution.

Key changes:

  • Added InitiateFlow() method to FlowExecServiceInterface to pre-initialize flows without execution
  • Replaced applicationId parameter with flowId throughout the authorization flow
  • Updated integration tests to extract and validate the flowId instead of applicationId

Reviewed Changes

Copilot reviewed 17 out of 17 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
backend/internal/flow/flowexec/service.go Added InitiateFlow() method to pre-initialize flows with runtime data
backend/internal/flow/common/model/flowmodel.go Added FlowInitContext struct for flow initialization
backend/internal/flow/common/constants/errorconstants.go Added error constants for flow initialization
backend/internal/oauth/oauth2/authz/handler.go Updated to initiate flow and pass flowId instead of applicationId
backend/internal/oauth/oauth2/authz/session_store.go Added FlowID field to SessionData
backend/internal/oauth/oauth2/constants/constants.go Added FlowID constant
backend/internal/oauth/oauth2/authz/init.go Updated initialization to inject flowExecService
backend/internal/oauth/oauth2/granthandlers/init.go Updated to pass flowExecService to authz initialization
backend/internal/oauth/init.go Updated to accept and pass flowExecService
backend/cmd/server/servicemanager.go Updated to capture and pass flowExecService
tests/integration/oauth/authz/utils.go Updated test helpers to use flowId instead of applicationId
tests/integration/oauth/authz/authz_test.go Updated tests to validate flowId extraction
backend/.mockery.public.yml Added mock configuration for flowexec package
backend/tests/mocks/flowexecmock/*.go Generated mocks for flow execution interfaces
backend/internal/oauth/oauth2/authz/handler_test.go Updated unit tests to inject mock flow exec service

@codecov
Copy link

codecov bot commented Oct 30, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 65.49%. Comparing base (9e66312) to head (8a413cd).
⚠️ Report is 4 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #624      +/-   ##
==========================================
+ Coverage   65.39%   65.49%   +0.10%     
==========================================
  Files         200      200              
  Lines       17752    17782      +30     
  Branches      255      250       -5     
==========================================
+ Hits        11609    11647      +38     
+ Misses       4798     4792       -6     
+ Partials     1345     1343       -2     
Flag Coverage Δ
backend-combined-postgres 55.05% <56.09%> (-0.03%) ⬇️
backend-combined-sqlite 55.05% <56.09%> (-0.03%) ⬇️
backend-integration-postgres 55.05% <56.09%> (-0.03%) ⬇️
backend-integration-sqlite 55.05% <56.09%> (-0.03%) ⬇️
backend-unit 28.00% <87.80%> (+0.60%) ⬆️
frontend-apps-develop-unit 28.00% <87.80%> (+0.60%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@senthalan senthalan force-pushed the improve-code-flow-engine branch from 836bb44 to 026a00b Compare October 30, 2025 19:04
Copilot AI review requested due to automatic review settings October 30, 2025 19:21
@senthalan senthalan force-pushed the improve-code-flow-engine branch from 026a00b to 171770a Compare October 30, 2025 19:21
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Copilot reviewed 18 out of 18 changed files in this pull request and generated 1 comment.

ctx.RuntimeData = make(map[string]string)
}
for key, value := range initContext.RuntimeData {
if _, ok := initContext.RuntimeData[key]; !ok {
Copy link

Copilot AI Oct 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The condition on line 401 is checking if the key exists in initContext.RuntimeData instead of ctx.RuntimeData. This will always be true since we're iterating over initContext.RuntimeData. The check should be if _, ok := ctx.RuntimeData[key]; !ok to prevent overwriting existing values in the engine context.

Suggested change
if _, ok := initContext.RuntimeData[key]; !ok {
if _, ok := ctx.RuntimeData[key]; !ok {

Copilot uses AI. Check for mistakes.
@senthalan senthalan force-pushed the improve-code-flow-engine branch from 171770a to 34ead24 Compare October 31, 2025 04:34
@senthalan senthalan requested a review from Copilot October 31, 2025 04:37
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Copilot reviewed 23 out of 23 changed files in this pull request and generated 2 comments.

@senthalan senthalan force-pushed the improve-code-flow-engine branch from 34ead24 to 8a35653 Compare October 31, 2025 04:44
@senthalan senthalan requested a review from Copilot October 31, 2025 04:50
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Copilot reviewed 23 out of 23 changed files in this pull request and generated 1 comment.

@senthalan senthalan force-pushed the improve-code-flow-engine branch from 8a35653 to a730701 Compare October 31, 2025 04:58
@senthalan senthalan requested a review from Copilot October 31, 2025 05:12
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Copilot reviewed 23 out of 23 changed files in this pull request and generated 1 comment.

Comment on lines 397 to 402
if ctx.RuntimeData == nil {
ctx.RuntimeData = make(map[string]string)
}
for key, value := range initContext.RuntimeData {
ctx.RuntimeData[key] = value
}
Copy link

Copilot AI Oct 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The runtime data merging logic could be simplified by using a utility function for map merging, especially if this pattern is used elsewhere in the codebase. This would improve consistency and maintainability.

Copilot uses AI. Check for mistakes.
@senthalan senthalan force-pushed the improve-code-flow-engine branch from a730701 to a42d853 Compare October 31, 2025 07:05
@senthalan senthalan requested a review from Copilot October 31, 2025 07:05
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Copilot reviewed 23 out of 23 changed files in this pull request and generated 2 comments.

Comments suppressed due to low confidence (1)

backend/internal/flow/flowmgt/service.go:45

  • The removal of the unexported init() error method from the interface is a breaking change to the interface definition. While this appears intentional, ensure that all implementations of FlowMgtServiceInterface have been updated accordingly and that initialization is now handled differently (e.g., in an Initialize function).
type FlowMgtServiceInterface interface {
	RegisterGraph(graphID string, g model.GraphInterface)
	GetGraph(graphID string) (model.GraphInterface, bool)
	IsValidGraphID(graphID string) bool
}

@senthalan senthalan force-pushed the improve-code-flow-engine branch from a42d853 to 266e30c Compare October 31, 2025 07:11
Copilot AI review requested due to automatic review settings October 31, 2025 08:23
@senthalan senthalan force-pushed the improve-code-flow-engine branch from 266e30c to 27e8de1 Compare October 31, 2025 08:23
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Copilot reviewed 24 out of 25 changed files in this pull request and generated 1 comment.

Files not reviewed (1)
  • frontend/pnpm-lock.yaml: Language not supported

flowInitCtx := &model.FlowInitContext{
ApplicationID: app.AppID,
FlowType: string(constants.FlowTypeAuthentication),
RuntimeData: nil,
Copy link

Copilot AI Oct 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Setting RuntimeData explicitly to nil is unnecessary since the zero value for a map is nil. Consider removing this line or initializing it as an empty map map[string]string{} if you intend to add OAuth-specific runtime data in the future.

Suggested change
RuntimeData: nil,

Copilot uses AI. Check for mistakes.
}

// ErrorInvalidFlowInitContext defines the error response for invalid flow init context.
var ErrorInvalidFlowInitContext = serviceerror.ServiceError{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shall we move this to line 82 where client errors are defined?

type FlowExecServiceInterface interface {
Execute(appID, flowID, actionID, flowType string, inputData map[string]string) (
*model.FlowStep, *serviceerror.ServiceError)
InitiateFlow(initContext *model.FlowInitContext) (string, *serviceerror.ServiceError)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having a separate function could be misleading for a outside component which uses this flow execution service right?
In a normal flow execution, we should call Execute() function to both initialize and continue an existing flow. However with InitiateFlow(), someone could confuse which function to call when calling the service. Ideally this should only be called from an internal component.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As discussed offline, we will be refactoring later

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets create an issue and track.. I would like to execute\initialize the flow, without needing the application id.

@senthalan senthalan force-pushed the improve-code-flow-engine branch from 27e8de1 to 20edcbc Compare November 1, 2025 07:22
@senthalan senthalan requested a review from Copilot November 1, 2025 07:24
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Copilot reviewed 24 out of 25 changed files in this pull request and generated 1 comment.

Files not reviewed (1)
  • frontend/pnpm-lock.yaml: Language not supported

Comment on lines 264 to 270
// ErrorSerializingRuntimeData defines the error response for errors while serializing runtime data.
var ErrorSerializingRuntimeData = serviceerror.ServiceError{
Code: "FES-5022",
Type: serviceerror.ServerErrorType,
Error: "Something went wrong",
ErrorDescription: "Error serializing runtime data to JSON",
}
Copy link

Copilot AI Nov 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error constant ErrorSerializingRuntimeData is defined but never used in the codebase. The InitiateFlow method doesn't perform any serialization of RuntimeData. Either implement the serialization logic that uses this error or remove the unused constant.

Suggested change
// ErrorSerializingRuntimeData defines the error response for errors while serializing runtime data.
var ErrorSerializingRuntimeData = serviceerror.ServiceError{
Code: "FES-5022",
Type: serviceerror.ServerErrorType,
Error: "Something went wrong",
ErrorDescription: "Error serializing runtime data to JSON",
}

Copilot uses AI. Check for mistakes.
@senthalan senthalan force-pushed the improve-code-flow-engine branch from 20edcbc to 430785e Compare November 1, 2025 07:27
@senthalan senthalan requested a review from Copilot November 1, 2025 07:28
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Copilot reviewed 24 out of 25 changed files in this pull request and generated 2 comments.

Files not reviewed (1)
  • frontend/pnpm-lock.yaml: Language not supported

Comment on lines 396 to 398
if len(initContext.RuntimeData) > 0 {
ctx.RuntimeData = initContext.RuntimeData
}
Copy link

Copilot AI Nov 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The condition len(initContext.RuntimeData) > 0 means that if initContext.RuntimeData is nil or empty, ctx.RuntimeData keeps its value from initContext(). Consider explicitly setting ctx.RuntimeData = nil in the else branch or before this check to ensure predictable behavior when RuntimeData is intentionally empty or nil.

Suggested change
if len(initContext.RuntimeData) > 0 {
ctx.RuntimeData = initContext.RuntimeData
}
ctx.RuntimeData = initContext.RuntimeData

Copilot uses AI. Check for mistakes.
}

// Initiate flow with OAuth context
logger := log.GetLogger().With(log.String(log.LoggerKeyComponentName, loggerComponentName))
Copy link

Copilot AI Nov 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logger is being created inside handleInitialAuthorizationRequest which is called for every authorization request. Consider initializing the logger once in the handler struct during construction to avoid repeated logger creation overhead.

Copilot uses AI. Check for mistakes.
@senthalan senthalan force-pushed the improve-code-flow-engine branch from 430785e to 0ed0522 Compare November 1, 2025 07:42
@senthalan senthalan requested a review from Copilot November 1, 2025 07:42
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Copilot reviewed 23 out of 23 changed files in this pull request and generated no new comments.

Copilot AI review requested due to automatic review settings November 1, 2025 08:27
@senthalan senthalan force-pushed the improve-code-flow-engine branch from 5851e9f to 1acdcb3 Compare November 1, 2025 08:27
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Copilot reviewed 24 out of 25 changed files in this pull request and generated 1 comment.

Files not reviewed (1)
  • frontend/pnpm-lock.yaml: Language not supported
Comments suppressed due to low confidence (1)

backend/internal/flow/flowexec/service_test.go:1

  • Taking the address of a package-level variable creates a pointer to the shared error constant. This should use the error value directly without taking its address: mockError := flowconstants.ErrorUpdatingContextInStore. Similarly, in handler_test.go line 350, the same issue exists.
/*

@senthalan senthalan force-pushed the improve-code-flow-engine branch from 1acdcb3 to 8a413cd Compare November 1, 2025 13:36
@senthalan senthalan merged commit 82f4e6a into asgardeo:main Nov 1, 2025
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

breaking change The feature/ improvement will alter the existing behaviour Type/Improvement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants