-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Add OIDC middleware #2138
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
Open
meher745
wants to merge
6
commits into
gofr-dev:development
Choose a base branch
from
meher745:add-oidc-middleware
base: development
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
Add OIDC middleware #2138
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d084809
feat: add OpenID Connect middleware with dynamic discovery and userin…
meher745 6f16546
refactor: update OIDC middleware & discovery per review feedback
meher745 5955516
Merge branch 'development' into add-oidc-middleware
meher745 9f29991
Merge branch 'development' into add-oidc-middleware
meher745 19458af
Refactor OIDC code, fix linter issues, add documentation
meher745 1babe5c
Merge branch 'development' of github.com:meher745/gofr into add-oidc-…
meher745 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 hidden or 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,64 @@ | ||
# OIDC Authentication | ||
|
||
OpenID Connect (OIDC) is an identity layer built on top of OAuth 2.0 that enables secure user authentication and transmission of user profile information. It allows clients to verify end-user identities based on authentication performed by an authorization server. | ||
|
||
## Overview | ||
|
||
Authentication is a critical part of securing web applications by ensuring only authorized users can access protected resources. GoFR supports OIDC integration through middleware that validates Bearer tokens and fetches user information from the OIDC provider. | ||
|
||
## Setup | ||
|
||
To enable OIDC authentication in GoFR, configure the middleware with your OIDC provider’s UserInfo endpoint. This endpoint is used to validate access tokens and retrieve user claims. | ||
|
||
## Usage | ||
|
||
Here is an example of enabling OIDC authentication middleware in a GoFR application: | ||
|
||
```go | ||
package main | ||
|
||
import ( | ||
"gofr.dev/gofr/pkg/gofr" | ||
"gofr.dev/gofr/pkg/gofr/http/middleware" | ||
) | ||
|
||
func main() { | ||
app := gofr.New() | ||
|
||
// Configure OIDC Auth Provider with your UserInfo endpoint | ||
oidcProvider := &middleware.OIDCAuthProvider{ | ||
UserInfoEndpoint: "https://your-oidc-provider.com/userinfo", | ||
} | ||
|
||
// Use the OIDC middleware for authentication | ||
app.Use(middleware.AuthMiddleware(oidcProvider)) | ||
|
||
// Define a protected route | ||
app.GET("/profile", func(c *gofr.Context) (any, error) { | ||
userClaims := c.UserInfo() // Access claims set by the middleware | ||
return userClaims, nil | ||
}) | ||
|
||
app.Run() | ||
} | ||
``` | ||
|
||
## Error Handling | ||
|
||
The middleware handles common error scenarios including: | ||
|
||
- Missing or empty Bearer tokens | ||
- Invalid or expired tokens | ||
- Failure to fetch or parse user info from the UserInfo endpoint | ||
|
||
Appropriate HTTP 401 (Unauthorized) responses will be returned by the middleware in these cases. | ||
|
||
## Tips | ||
|
||
- Configure reasonable HTTP client timeouts in the middleware to avoid delays calling the UserInfo endpoint. | ||
- Consider caching user info responses if your application makes frequent authorization checks to improve performance. | ||
- Test your OIDC integration using tokens issued by your authorization server and confirm user claims are correctly propagated. | ||
|
||
--- | ||
|
||
This integration enables robust and standardized authentication flows in GoFR applications using OpenID Connect. |
This file contains hidden or 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 hidden or 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 hidden or 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
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.
I don't get the version downgrade here