StartAuthenticationServer starts an authentication server on a random port with users and clients specified in the TestAuthUser and TestAuthClientID constants.
()
| 15 | // StartAuthenticationServer starts an authentication server on a random port with |
| 16 | // users and clients specified in the TestAuthUser and TestAuthClientID constants. |
| 17 | func StartAuthenticationServer() (srv *oauth2.AuthorizationServer, port uint16, err error) { |
| 18 | var nl net.Listener |
| 19 | |
| 20 | // create a new socket for HTTP communication |
| 21 | nl, err = net.Listen("tcp", "localhost:0") |
| 22 | if err != nil { |
| 23 | return nil, 0, fmt.Errorf("could not listen: %w", err) |
| 24 | } |
| 25 | |
| 26 | port = nl.Addr().(*net.TCPAddr).AddrPort().Port() |
| 27 | |
| 28 | srv = oauth2.NewServer(fmt.Sprintf(":%d", port), |
| 29 | oauth2.WithClient("cli", "", "http://localhost:10000/callback"), |
| 30 | oauth2.WithClient(testdata.MockAuthClientID, testdata.MockAuthClientSecret, ""), |
| 31 | oauth2.WithPublicURL(fmt.Sprintf("http://localhost:%d", port)), |
| 32 | login.WithLoginPage( |
| 33 | login.WithUser(testdata.MockAuthUser, testdata.MockAuthPassword), |
| 34 | login.WithBaseURL("/v1/auth"), |
| 35 | ), |
| 36 | ) |
| 37 | |
| 38 | // simulate the /v1/auth endpoints |
| 39 | srv.Handler.(*http.ServeMux).Handle("/v1/auth/token", http.StripPrefix("/v1/auth", srv.Handler)) |
| 40 | srv.Handler.(*http.ServeMux).Handle("/v1/auth/certs", http.StripPrefix("/v1/auth", srv.Handler)) |
| 41 | |
| 42 | go func() { |
| 43 | _ = srv.Serve(nl) |
| 44 | }() |
| 45 | |
| 46 | return srv, port, nil |
| 47 | } |
| 48 | |
| 49 | func JWKSURL(port uint16) string { |
| 50 | return fmt.Sprintf("http://localhost:%d/v1/auth/certs", port) |