Skip to content

Commit 19458af

Browse files
committed
Refactor OIDC code, fix linter issues, add documentation
1 parent 9f29991 commit 19458af

File tree

12 files changed

+392
-525
lines changed

12 files changed

+392
-525
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# OIDC Authentication
2+
3+
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.
4+
5+
## Overview
6+
7+
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.
8+
9+
## Setup
10+
11+
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.
12+
13+
## Usage
14+
15+
Here is an example of enabling OIDC authentication middleware in a GoFR application:
16+
17+
```go
18+
package main
19+
20+
import (
21+
"gofr.dev/gofr/pkg/gofr"
22+
"gofr.dev/gofr/pkg/gofr/http/middleware"
23+
)
24+
25+
func main() {
26+
app := gofr.New()
27+
28+
// Configure OIDC Auth Provider with your UserInfo endpoint
29+
oidcProvider := &middleware.OIDCAuthProvider{
30+
UserInfoEndpoint: "https://your-oidc-provider.com/userinfo",
31+
}
32+
33+
// Use the OIDC middleware for authentication
34+
app.Use(middleware.AuthMiddleware(oidcProvider))
35+
36+
// Define a protected route
37+
app.GET("/profile", func(c *gofr.Context) (any, error) {
38+
userClaims := c.UserInfo() // Access claims set by the middleware
39+
return userClaims, nil
40+
})
41+
42+
app.Run()
43+
}
44+
```
45+
46+
## Error Handling
47+
48+
The middleware handles common error scenarios including:
49+
50+
- Missing or empty Bearer tokens
51+
- Invalid or expired tokens
52+
- Failure to fetch or parse user info from the UserInfo endpoint
53+
54+
Appropriate HTTP 401 (Unauthorized) responses will be returned by the middleware in these cases.
55+
56+
## Tips
57+
58+
- Configure reasonable HTTP client timeouts in the middleware to avoid delays calling the UserInfo endpoint.
59+
- Consider caching user info responses if your application makes frequent authorization checks to improve performance.
60+
- Test your OIDC integration using tokens issued by your authorization server and confirm user claims are correctly propagated.
61+
62+
---
63+
64+
This integration enables robust and standardized authentication flows in GoFR applications using OpenID Connect.

docs/advanced-guide/oidc.md

Lines changed: 0 additions & 180 deletions
This file was deleted.

docs/navigation.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,11 @@ export const navigation = [
151151
title: 'Profiling in GoFr Applications',
152152
href: '/docs/advanced-guide/debugging',
153153
desc: "Discover GoFr auto-enables pprof profiling by leveraging its built-in configurations."
154+
},
155+
{
156+
title: 'OIDC Authentication',
157+
href: '/docs/advanced-guide/oidc-authentication',
158+
desc: 'Learn how to integrate OpenID Connect (OIDC) authentication using GoFR. Covers setup, configuration, and usage for secure authentication flows.'
154159
}
155160
],
156161
},

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@ go 1.24
55
require (
66
cloud.google.com/go/pubsub v1.49.0
77
github.com/DATA-DOG/go-sqlmock v1.5.2
8+
github.com/MicahParks/keyfunc v1.9.0
89
github.com/XSAM/otelsql v0.39.0
910
github.com/alicebob/miniredis/v2 v2.35.0
1011
github.com/dgraph-io/dgo/v210 v210.0.0-20230328113526-b66f8ae53a2d
1112
github.com/eclipse/paho.mqtt.golang v1.5.0
1213
github.com/go-redis/redismock/v9 v9.2.0
1314
github.com/go-sql-driver/mysql v1.9.3
1415
github.com/gogo/protobuf v1.3.2
16+
github.com/golang-jwt/jwt/v4 v4.5.2
1517
github.com/golang-jwt/jwt/v5 v5.3.0
1618
github.com/google/uuid v1.6.0
1719
github.com/gorilla/mux v1.8.1

go.sum

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4
2020
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
2121
github.com/DATA-DOG/go-sqlmock v1.5.2 h1:OcvFkGmslmlZibjAjaHm3L//6LiuBgolP7OputlJIzU=
2222
github.com/DATA-DOG/go-sqlmock v1.5.2/go.mod h1:88MAG/4G7SMwSE3CeA0ZKzrT5CiOU3OJ+JlNzwDqpNU=
23+
github.com/MicahParks/keyfunc v1.9.0 h1:lhKd5xrFHLNOWrDc4Tyb/Q1AJ4LCzQ48GVJyVIID3+o=
24+
github.com/MicahParks/keyfunc v1.9.0/go.mod h1:IdnCilugA0O/99dW+/MkvlyrsX8+L8+x95xuVNtM5jw=
2325
github.com/XSAM/otelsql v0.39.0 h1:4o374mEIMweaeevL7fd8Q3C710Xi2Jh/c8G4Qy9bvCY=
2426
github.com/XSAM/otelsql v0.39.0/go.mod h1:uMOXLUX+wkuAuP0AR3B45NXX7E9lJS2mERa8gqdU8R0=
2527
github.com/alicebob/miniredis/v2 v2.35.0 h1:QwLphYqCEAo1eu1TqPRN2jgVMPBweeQcR21jeqDCONI=
@@ -71,6 +73,9 @@ github.com/go-sql-driver/mysql v1.9.3/go.mod h1:qn46aNg1333BRMNU69Lq93t8du/dwxI6
7173
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
7274
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
7375
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
76+
github.com/golang-jwt/jwt/v4 v4.4.2/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0=
77+
github.com/golang-jwt/jwt/v4 v4.5.2 h1:YtQM7lnr8iZ+j5q71MGKkNw9Mn7AjHM68uc9g5fXeUI=
78+
github.com/golang-jwt/jwt/v4 v4.5.2/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0=
7479
github.com/golang-jwt/jwt/v5 v5.3.0 h1:pv4AsKCKKZuqlgs5sUmn4x8UlGa0kEVt/puTpKx9vvo=
7580
github.com/golang-jwt/jwt/v5 v5.3.0/go.mod h1:fxCRLWMO43lRc8nhHWY6LGqRcf+1gQWArsqaEUEa5bE=
7681
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=

go.work.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1296,6 +1296,7 @@ golang.org/x/net v0.33.0/go.mod h1:HXLR5J+9DxmrqMwG9qjGCxZ+zKXxBru04zlTvWlWuN4=
12961296
golang.org/x/net v0.34.0/go.mod h1:di0qlW3YNM5oh6GqDGQr92MyTozJPmybPK4Ev/Gm31k=
12971297
golang.org/x/net v0.35.0/go.mod h1:EglIi67kWsHKlRzzVMUD93VMSWGFOMSZgxFjparz1Qk=
12981298
golang.org/x/net v0.37.0/go.mod h1:ivrbrMbzFq5J41QOQh0siUuly180yBYtLp+CKbEaFx8=
1299+
golang.org/x/net v0.41.0/go.mod h1:B/K4NNqkfmg07DQYrbwvSluqCJOOXwUjeb/5lOisjbA=
12991300
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
13001301
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
13011302
golang.org/x/oauth2 v0.0.0-20191202225959-858c2ad4c8b6/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
@@ -1360,6 +1361,7 @@ golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
13601361
golang.org/x/sys v0.32.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
13611362
golang.org/x/telemetry v0.0.0-20240521205824-bda55230c457 h1:zf5N6UOrA487eEFacMePxjXAJctxKmyjKUsjA11Uzuk=
13621363
golang.org/x/telemetry v0.0.0-20240521205824-bda55230c457/go.mod h1:pRgIJT+bRLFKnoM1ldnzKoxTIn14Yxz928LQRYYgIN0=
1364+
golang.org/x/telemetry v0.0.0-20250710130107-8d8967aff50b/go.mod h1:4ZwOYna0/zsOKwuR5X/m0QFOJpSZvAxFfkQT+Erd9D4=
13631365
golang.org/x/term v0.31.0/go.mod h1:R4BeIy7D95HzImkxGkTW1UQTtP54tio2RyHz7PwK0aw=
13641366
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
13651367
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=

0 commit comments

Comments
 (0)