{"id":25776461,"url":"https://github.com/TNG/keycloak-mock","last_synced_at":"2025-02-27T06:06:33.009Z","repository":{"id":35404005,"uuid":"213330914","full_name":"TNG/keycloak-mock","owner":"TNG","description":"A Java library to test REST endpoints secured by Keycloak via OpenID connect.","archived":false,"fork":false,"pushed_at":"2024-07-08T07:52:57.000Z","size":2292,"stargazers_count":114,"open_issues_count":6,"forks_count":27,"subscribers_count":16,"default_branch":"main","last_synced_at":"2024-07-08T09:16:56.714Z","etag":null,"topics":["keycloak","mock","single-sign-on","testing"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/TNG.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-10-07T08:26:37.000Z","updated_at":"2024-07-08T07:52:58.000Z","dependencies_parsed_at":"2023-10-10T18:46:51.805Z","dependency_job_id":"95876031-0ff6-4615-b5b7-5f69cf6a8327","html_url":"https://github.com/TNG/keycloak-mock","commit_stats":null,"previous_names":[],"tags_count":19,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TNG%2Fkeycloak-mock","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TNG%2Fkeycloak-mock/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TNG%2Fkeycloak-mock/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TNG%2Fkeycloak-mock/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/TNG","download_url":"https://codeload.github.com/TNG/keycloak-mock/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240987435,"owners_count":19889334,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["keycloak","mock","single-sign-on","testing"],"created_at":"2025-02-27T06:01:24.544Z","updated_at":"2025-02-27T06:06:33.002Z","avatar_url":"https://github.com/TNG.png","language":"Java","funding_links":[],"categories":["Clients","测试"],"sub_categories":[],"readme":"[![Java CI](https://github.com/TNG/keycloak-mock/workflows/Java%20CI/badge.svg?branch=main)](https://github.com/TNG/keycloak-mock/actions?query=branch%3Amain)\n[![Github release date](https://img.shields.io/github/release-date/TNG/keycloak-mock.svg?logo=github)\n![Github release](https://img.shields.io/github/release/TNG/keycloak-mock.svg?logo=github)](https://github.com/TNG/keycloak-mock/releases)\n[![Maven release](https://img.shields.io/maven-central/v/com.tngtech.keycloakmock/mock?color=informational)](https://search.maven.org/search?q=com.tngtech.keycloakmock)\n[![Sonarcloud](https://img.shields.io/sonar/quality_gate/TNG_keycloak-mock?server=https%3A%2F%2Fsonarcloud.io)](https://sonarcloud.io/dashboard?id=TNG_keycloak-mock)\n\n# Keycloak Mock\n\n[Keycloak](https://www.keycloak.org) is a single sign-on solution that supports the\n[Open ID connect](https://openid.net/connect/) standard. However, it does not deliver any test\nsupport. This library is intended to fill that gap.\n\n## Recent changes\n\nHave a look at our [release notes](https://github.com/TNG/keycloak-mock/releases) for recent\nreleases and changes.\n\n## Usage\n\nAll artifacts are available on [Maven Central Repository](https://search.maven.org/) under the group\nID `com.tngtech.keycloakmock`.\n\n### Testing authenticated backend calls\n\nWhen testing a REST backend that is protected by a Keycloak adapter, the mock allows to generate\nvalid access tokens with configurable content (e.g. roles).\n\nYou can create and start the mock directly from the `mock` artifact using Maven\n\n```maven\n\u003cdependency\u003e\n    \u003cgroupId\u003ecom.tngtech.keycloakmock\u003c/groupId\u003e\n    \u003cartifactId\u003emock\u003c/artifactId\u003e\n    \u003cscope\u003etest\u003c/scope\u003e\n    \u003cversion\u003e0.17.0\u003c/version\u003e\n\u003c/dependency\u003e\n```\n\nor Gradle\n\n```gradle\ntestImplementation 'com.tngtech.keycloakmock:mock:0.17.0'\n```\n\nlike this:\n\n```java\nimport static com.tngtech.keycloakmock.api.ServerConfig.aServerConfig;\n\nimport com.tngtech.keycloakmock.api.KeycloakMock;\n\nclass Test {\n\n  void checkSomething() {\n    KeycloakMock mock = new KeycloakMock(aServerConfig().withPort(8000).withDefaultRealm(\"master\").build());\n    mock.start();\n\n    // do your test stuff\n\n    mock.stop();\n  }\n\n  void quarkusKeycloakMocks() {\n    // to mock Keycloak without context path (v18.0.0+)\n    KeycloakMock mockNoContextPath = new KeycloakMock(aServerConfig().withNoContextPath().build());\n    // or to use custom one\n    KeycloakMock mockCustomContextPath = new KeycloakMock(aServerConfig().withContextPath(\"/context-path\").build());\n    // if context path is not provided, '/auth' will be used as default due to backward compatibility reasons\n    KeycloakMock mockDefaultContextPath = new KeycloakMock(aServerConfig().build());\n    // ...\n  }\n}\n```\n\nYou can also use the convenience wrapper `mock-junit` for JUnit4\n\n```java\nimport com.tngtech.keycloakmock.junit.KeycloakMockRule;\n\npublic class Test {\n  @ClassRule\n  public static KeycloakMockRule mock = new KeycloakMockRule();\n\n  // ...\n\n}\n```\n\nor `mock-junit5` for JUnit5\n\n```java\nimport com.tngtech.keycloakmock.junit5.KeycloakMockExtension;\n\nclass Test {\n  @RegisterExtension\n  static KeycloakMockExtension mock = new KeycloakMockExtension();\n\n  // ...\n\n}\n```\n\nto let JUnit start the mock for you.\n\nYou can then generate a token of your choosing by providing a TokenConfig:\n\n```java\nimport static com.tngtech.keycloakmock.api.TokenConfig.aTokenConfig;\n\nclass Test {\n\n  String accessToken = mock.getAccessToken(aTokenConfig().withRole(\"ROLE_ADMIN\").build());\n\n  // ...\n\n}\n```\n\nFor a more in-detail test case, please have a look at\nthe [AuthenticationTest](example-backend/src/test/java/com/tngtech/keycloakmock/examplebackend/AuthenticationTest.java)\nin our example backend project.\n\nIn addition to generating and signing tokens programmatically, the mock also offers\n\n* user login (using implicit or authorization code flow, including support for redirect\n  to `http://localhost` and `urn:ietf:wg:oauth:2.0:oob` for desktop applications)\n  * instead of a password, you can enter the roles of the user\n* client credentials authentication\n* resource owner password credentials authentication (both for public and confidential clients)\n\nNote that as this is a mock, all flows are allowed for any client. For simplicity all successful\ncalls to the token endpoint return the same response including a refresh token, even for flows which\nshould not contain it according to the specifications.\n\n### Developing / testing frontends\n\nIt is also possible to run the mock server as a stand-alone application. Just get the (\nself-contained)\n`standalone` artifact, e.g.\nfrom [Maven Central](https://search.maven.org/artifact/com.tngtech.keycloakmock/standalone), and run\nit:\n\n```bash\n$ java -jar standalone.jar \u0026\n[main] INFO com.tngtech.keycloakmock.standalone.Main - Server is running on http://localhost:8000\n```\n\nThe stand-alone server can be configured using command line parameters. You can call it\nwith `--help` to get a list of all options.\n\nYou can even use it as a replacement in end-to-end tests, as the server is e.g. compatible with\n`cypress-keycloak`. Have a look at the [example-frontend-react](example-frontend-react) project on\nthis can be set up.\n\n## License\n\nThis project is licensed under the Apache 2.0 license (see [LICENSE](LICENSE)).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FTNG%2Fkeycloak-mock","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FTNG%2Fkeycloak-mock","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FTNG%2Fkeycloak-mock/lists"}