Skip to content

Refactor authentication services to use di pattern#673

Merged
ThaminduDilshan merged 1 commit intoasgardeo:mainfrom
ThaminduDilshan:thamindu-authn-impr
Nov 5, 2025
Merged

Refactor authentication services to use di pattern#673
ThaminduDilshan merged 1 commit intoasgardeo:mainfrom
ThaminduDilshan:thamindu-authn-impr

Conversation

@ThaminduDilshan
Copy link
Copy Markdown
Contributor

Purpose

$subject

Copilot AI review requested due to automatic review settings November 4, 2025 15:29
Copy link
Copy Markdown
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 implements dependency injection pattern for authentication services across the backend, replacing direct instantiation with constructor-based injection. The changes introduce proper service initialization functions, deprecate the old "New" constructors, and update service registration in the main application bootstrap.

  • Refactored authentication services to use dependency injection pattern with private new* constructors and public Initialize() functions
  • Updated service wiring in servicemanager.go to properly inject dependencies between authentication services
  • Added mock setup for AuthAssertGeneratorInterface in tests to ensure proper test coverage of the new pattern

Reviewed Changes

Copilot reviewed 33 out of 33 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
backend/tests/mocks/authn/oidcmock/OIDCAuthnCoreServiceInterface_mock.go Added GetOAuthClientConfig mock method to support new interface requirements
backend/tests/mocks/authn/oauthmock/OAuthAuthnCoreServiceInterface_mock.go Added GetOAuthClientConfig mock method to OAuth authenticator mock
backend/tests/mocks/authn/googlemock/GoogleOIDCAuthnServiceInterface_mock.go Added GetOAuthClientConfig mock method to Google OIDC authenticator mock
backend/tests/mocks/authn/githubmock/GithubOAuthAuthnServiceInterface_mock.go Added GetOAuthClientConfig mock method to GitHub OAuth authenticator mock
backend/internal/notification/init.go Updated to return both NotificationSenderMgtSvcInterface and OTPServiceInterface for proper dependency injection
backend/internal/executor/oidcauth/oidcauthexecutor.go Refactored to directly instantiate dependencies instead of nesting service constructors
backend/internal/executor/oauth/oauthexecutor.go Refactored OAuth executor to use flattened dependency instantiation
backend/internal/executor/googleauth/googleauthexecutor.go Simplified Google auth executor by removing nested service creation and using direct dependency injection
backend/internal/executor/githubauth/githubauthexecutor.go Refactored GitHub executor to use direct service instantiation with proper dependencies
backend/internal/authn/service_test.go Added mock for AuthAssertGeneratorInterface and updated test setup to use struct literal initialization
backend/internal/authn/service.go Changed NewAuthenticationService to private newAuthenticationService with explicit dependency parameters
backend/internal/authn/otp/service_test.go Updated tests to use private newOTPAuthnService constructor
backend/internal/authn/otp/service.go Added private newOTPAuthnService constructor and deprecated public NewOTPAuthnService
backend/internal/authn/otp/init.go Added Initialize() function to create OTP authentication service with injected dependencies
backend/internal/authn/oidc/service_test.go Updated tests to use new constructor pattern with all required dependencies
backend/internal/authn/oidc/service.go Refactored to use private newOIDCAuthnService with full dependency injection and deprecated old constructor
backend/internal/authn/oidc/init.go Added Initialize() function for OIDC authentication service
backend/internal/authn/oauth/service.go Changed to use private newOAuthAuthnService constructor with explicit dependencies and deprecated old constructor
backend/internal/authn/oauth/init.go Added Initialize() function for OAuth authentication service
backend/internal/authn/init.go Transformed from service wrapper to proper initialization function with dependency injection
backend/internal/authn/handler_test.go Updated to reference private authenticationHandler type
backend/internal/authn/handler.go Changed handler to use private type and constructor accepting injected service
backend/internal/authn/google/service.go Refactored Google OIDC service to use private constructor with dependencies and added GetOAuthClientConfig method
backend/internal/authn/google/init.go Added Initialize() function for Google OIDC authentication service
backend/internal/authn/github/service.go Refactored GitHub OAuth service with private constructor and added GetOAuthClientConfig method
backend/internal/authn/github/init.go Added Initialize() function for GitHub OAuth authentication service
backend/internal/authn/credentials/service_test.go Updated tests to use private newCredentialsAuthnService constructor
backend/internal/authn/credentials/service.go Added private newCredentialsAuthnService and deprecated public constructor
backend/internal/authn/credentials/init.go Added Initialize() function for credentials authentication service
backend/internal/authn/assert/init.go Added Initialize() function for auth assert generator
backend/internal/authn/assert/generator_test.go Updated test to use private constructor
backend/internal/authn/assert/generator.go Added private newAuthAssertGenerator constructor and deprecated public one
backend/cmd/server/servicemanager.go Updated service registration to use new dependency injection pattern with proper ordering

Comment thread backend/internal/authn/service_test.go
Comment thread backend/internal/authn/oidc/service_test.go
Comment thread backend/internal/authn/oidc/service_test.go
@codecov
Copy link
Copy Markdown

codecov Bot commented Nov 4, 2025

Codecov Report

❌ Patch coverage is 81.22066% with 40 lines in your changes missing coverage. Please review.
⚠️ Please upload report for BASE (main@6cf9b17). Learn more about missing BASE report.
⚠️ Report is 3 commits behind head on main.

Files with missing lines Patch % Lines
backend/internal/authn/init.go 85.13% 8 Missing and 3 partials ⚠️
backend/internal/authn/github/service.go 62.50% 4 Missing and 2 partials ⚠️
backend/internal/authn/google/service.go 68.42% 4 Missing and 2 partials ⚠️
backend/internal/authn/oauth/service.go 40.00% 3 Missing and 3 partials ⚠️
backend/internal/authn/oidc/service.go 53.84% 3 Missing and 3 partials ⚠️
...internal/executor/googleauth/googleauthexecutor.go 50.00% 4 Missing ⚠️
backend/internal/authn/otp/service.go 85.71% 1 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main     #673   +/-   ##
=======================================
  Coverage        ?   69.52%           
=======================================
  Files           ?      229           
  Lines           ?    20179           
  Branches        ?      262           
=======================================
  Hits            ?    14030           
  Misses          ?     4684           
  Partials        ?     1465           
Flag Coverage Δ
backend-combined-postgres 55.55% <79.34%> (?)
backend-combined-sqlite 55.55% <79.34%> (?)
backend-integration-postgres 55.55% <79.34%> (?)
backend-integration-sqlite 55.55% <79.34%> (?)
backend-unit 35.41% <24.41%> (?)
frontend-apps-develop-unit 35.41% <24.41%> (?)

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.

Copilot AI review requested due to automatic review settings November 4, 2025 16:30
Copy link
Copy Markdown
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 38 out of 38 changed files in this pull request and generated no new comments.

Comments suppressed due to low confidence (3)

backend/internal/authn/init.go:97

  • Inconsistent handler usage: This OPTIONS handler uses an inline anonymous function while other OPTIONS handlers in the same function use optionsNoContentHandler. For consistency and maintainability, use optionsNoContentHandler here as well.
    backend/internal/authn/init.go:109
  • Inconsistent handler usage: This OPTIONS handler uses an inline anonymous function while other OPTIONS handlers in the same function use optionsNoContentHandler. For consistency and maintainability, use optionsNoContentHandler here as well.
    backend/internal/authn/init.go:121
  • Inconsistent handler usage: This OPTIONS handler uses an inline anonymous function while other OPTIONS handlers in the same function use optionsNoContentHandler. For consistency and maintainability, use optionsNoContentHandler here as well.

Comment thread backend/cmd/server/servicemanager.go Outdated
idpService, jwtService,
assertGenerator, credentialsAuthnService, otpAuthnService,
oAuthAuthnService, oidcAuthnService, googleAuthnService, githubAuthnService,
)
Copy link
Copy Markdown
Contributor

@senthalan senthalan Nov 4, 2025

Choose a reason for hiding this comment

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

Rather than creating all these Service in the servicemanager level, can't we do this

In the servicemanager.go, we will Initialize the authentication service with all the needed services

# servicemanager.go
authnService, executorProvider = authn.Initialize(
		mux,, userService
		idpService, jwtService
	)

....

_ := flowExec.Initialize(mux, executorProvider)

And we will have a authn/init.go, we will Initialize each of these authenticaiton package which will return authenticaiton service and corresponding executor. Using that we can then create authnService and executorProvider(I am not sure whether this name) which will keep the map of executors which will be used for the flowexec

# authn/init.go
   ....
	oauthAuthnSvc, oAuthExectuor := authnoauth.Initialize(idpService, userService)
	oidcAuthnSvc, oidcExectuor := authnoidc.Initialize(idpService, userService, jwtService)
   ......
    authnService := newAuthenticationService(
		...
		oauthAuthnSvc,
		oidcAuthnSvc,
		....
	)
   ...
   
   executorProvider := ...

  return authnService, executorProvider

In the authn/oauth/init.go, we can Initialize each of authenticaiton service and corresponding executor as well and return both.

# authn/oauth/init.go
 oauthAuthnSvc := newOAuthAuthnService(...)
 oauthAuthExceutor := newOAuthAuthExceutor(oauthAuthnSvc)
return oauthAuthnSvc, oauthAuthExceutor

So with this change there won't be need for the executor package.

I like this approach because,

  1. It keeps the main servicemanager clean, it doesn't need to know about individual authentication service
  2. It's better if we can keep the authentication service and corresponding executor under the same package
  3. This will give us add recoveryservice and it's executors in another package and feed into the flowExce.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Then isn't this introducing a cyclic dependency when we try to construct executor from the internal/authn/oauth package?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The directory structure I am suggesting will be like this

authn/
    oauth2/ 
        service.go
        oauthexecutor.go
        init.go
    github/ 
        service.go
        githubexecutor.go
        init.go
   service.go
   handler.go
   init.go

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Let's track this as an improvement as discussed offline. This will require refactoring the executors which will conflict with my other pending changes

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Created an issue to track this: #674.

Will push the other suggestion we discussed offline

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@ThaminduDilshan ThaminduDilshan merged commit 7092be0 into asgardeo:main Nov 5, 2025
8 checks passed
@ThaminduDilshan ThaminduDilshan deleted the thamindu-authn-impr branch November 5, 2025 05:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants