@@ -13,6 +13,7 @@ This mock server recreates Google's OAuth2 flow without requiring an internet co
1313- ` /authorize ` - Authorization endpoint where users are redirected to authenticate
1414- ` /token ` - Token exchange endpoint to obtain access tokens
1515- ` /userinfo ` - User profile information endpoint
16+ - ` /.well-known/openid-configuration ` - OpenID Connect discovery endpoint
1617
1718## Use Cases
1819- Develop OAuth2 clients offline
@@ -47,8 +48,14 @@ go build -o mock-oauth2-server ./cmd/server
4748# Specify a custom port using the command-line flag (highest priority)
4849./mock-oauth2-server --port 9088
4950
51+ # Specify a custom hostname for the server URLs
52+ ./mock-oauth2-server --host http://mock-oauth2-server:9088
53+
5054# Specify a custom port using environment variable (used if no command-line flag is provided)
5155MOCK_OAUTH_PORT=9088 ./mock-oauth2-server
56+
57+ # Specify a custom issuer URL using environment variable (useful in containerized environments)
58+ MOCK_ISSUER_URL=http://mock-oauth2:8080 ./mock-oauth2-server
5259```
5360
5461## Running with Docker
@@ -123,6 +130,7 @@ services:
123130124131 - MOCK_USER_NAME=Test User
125132 - MOCK_TOKEN_EXPIRY=3600
133+ - MOCK_ISSUER_URL=http://mock-oauth2:8080
126134 # Mount a volume for custom fixtures if needed
127135 # volumes:
128136 # - ./test/fixtures:/app/test/fixtures
@@ -185,6 +193,8 @@ golang-mock-oauth2-server/
185193│ │ ├── token_test.go # Test token handler
186194│ │ ├── userinfo.go
187195│ │ ├── userinfo_test.go # Test userinfo handler
196+ │ │ ├── openid_config.go # OpenID Connect discovery endpoint
197+ │ │ ├── openid_config_test.go # Test OpenID Connect discovery
188198│ │ ├── config.go
189199│ │ └── config_test.go # Test config handler
190200│ ├── middleware/
@@ -227,6 +237,7 @@ Handler functions process incoming HTTP requests for each OAuth2 endpoint:
227237- ` authorize.go ` - Handles user authentication and generates authorization codes
228238- ` token.go ` - Exchanges authorization codes for access tokens
229239- ` userinfo.go ` - Returns user profile information
240+ - ` openid_config.go ` - Provides OpenID Connect discovery metadata
230241- ` config.go ` - Manages dynamic configuration for testing
231242
232243#### Configuration (` internal/config/ ` )
@@ -312,6 +323,33 @@ Retrieves mock user profile information.
312323}
313324```
314325
326+ #### OpenID Connect Discovery Endpoint (` /.well-known/openid-configuration ` )
327+
328+ Provides OpenID Connect (OIDC) configuration metadata for client auto-configuration.
329+
330+ ** Method** : GET
331+
332+ ** Response** : A JSON document with standard OIDC configuration
333+
334+ ``` json
335+ {
336+ "issuer" : " http://localhost:8080" ,
337+ "authorization_endpoint" : " http://localhost:8080/authorize" ,
338+ "token_endpoint" : " http://localhost:8080/token" ,
339+ "userinfo_endpoint" : " http://localhost:8080/userinfo" ,
340+ "jwks_uri" : " http://localhost:8080/jwks" ,
341+ "response_types_supported" : [" code" ],
342+ "subject_types_supported" : [" public" ],
343+ "id_token_signing_alg_values_supported" : [" RS256" ],
344+ "scopes_supported" : [" openid" , " email" , " profile" ],
345+ "token_endpoint_auth_methods_supported" : [" client_secret_post" , " client_secret_basic" ],
346+ "claims_supported" : [
347+ " sub" , " iss" , " name" , " given_name" ,
348+ " family_name" , " email" , " email_verified" , " picture"
349+ ]
350+ }
351+ ```
352+
315353#### Configuration Endpoint
316354
317355##### Dynamic Configuration Endpoint (` /config ` )
@@ -376,36 +414,60 @@ Available configuration options:
376414 - Environment: ` MOCK_OAUTH_PORT=9088 `
377415 - Default: ` 8080 `
378416
417+ - Issuer URL:
418+ - Command-line: ` --host http://custom-hostname:9088 `
419+ - Environment: ` MOCK_ISSUER_URL=http://mock-oauth2:9088 `
420+ - Default: ` http://localhost:[port] `
421+
379422- Other settings (environment variables only):
380423 - ` MOCK_USER_EMAIL ` - Email for the mock user (default:
[email protected] )
381424 - ` MOCK_USER_NAME ` - Name for the mock user (default: Test User)
382425 - ` MOCK_TOKEN_EXPIRY ` - Token expiry in seconds (default: 3600)
383426
427+ The issuer URL is particularly important in containerized environments where the service name differs from "localhost". It affects the URLs returned in the OpenID Connect discovery document and needs to match what your OAuth client is configured to use.
428+
384429### Example Usage
385430
386431In your OAuth2 client application:
387432
388- ``` bash
433+ ``` go
434+ // For a Go application using the golang.org/x/oauth2 package
435+ import (
436+ " context"
437+ " golang.org/x/oauth2"
438+ )
439+
389440const (
390441 clientID = " test-client-id"
391442 clientSecret = " test-client-secret"
392443 redirectURL = " http://localhost:8081/callback"
393- authURL = " http://localhost:8080/authorize"
394- tokenURL = " http://localhost:8080/token"
395- userInfoURL = " http://localhost:8080/userinfo"
396444)
397445
398- // Configure OAuth2 client to use the mock server
446+ // You can either specify endpoints manually:
399447oauth2Config := &oauth2.Config {
400448 ClientID : clientID,
401449 ClientSecret : clientSecret,
402450 RedirectURL : redirectURL,
403451 Scopes : []string {" openid" , " email" , " profile" },
404452 Endpoint : oauth2.Endpoint {
405- AuthURL: authURL ,
406- TokenURL: tokenURL ,
453+ AuthURL: " http://localhost:8080/authorize " ,
454+ TokenURL: " http://localhost:8080/token " ,
407455 },
408456}
457+
458+ // Or use the OpenID Connect discovery document:
459+ provider , err := oidc.NewProvider (context.Background (), " http://localhost:8080" )
460+ if err != nil {
461+ // handle error
462+ }
463+
464+ oauth2Config := &oauth2.Config {
465+ ClientID : clientID,
466+ ClientSecret : clientSecret,
467+ RedirectURL : redirectURL,
468+ Scopes : []string {" openid" , " email" , " profile" },
469+ Endpoint : provider.Endpoint (),
470+ }
409471```
410472
411473## Testing Strategies
0 commit comments