{"id":21593405,"url":"https://github.com/zemirco/keycloak","last_synced_at":"2025-04-10T23:34:42.222Z","repository":{"id":43087109,"uuid":"342191716","full_name":"zemirco/keycloak","owner":"zemirco","description":"Go library for accessing the Keycloak API","archived":false,"fork":false,"pushed_at":"2023-04-07T19:03:43.000Z","size":100,"stargazers_count":35,"open_issues_count":4,"forks_count":13,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-03-24T20:22:35.401Z","etag":null,"topics":["go","hacktoberfest","keycloak"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/zemirco.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","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":"2021-02-25T09:31:46.000Z","updated_at":"2025-03-24T05:49:58.000Z","dependencies_parsed_at":"2024-06-19T00:12:27.656Z","dependency_job_id":"4df98c5a-91f5-4730-bf26-38952ce59ae2","html_url":"https://github.com/zemirco/keycloak","commit_stats":{"total_commits":44,"total_committers":5,"mean_commits":8.8,"dds":"0.13636363636363635","last_synced_commit":"ac3e94fc80a90d9206f35aa90babe0d1e984af03"},"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zemirco%2Fkeycloak","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zemirco%2Fkeycloak/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zemirco%2Fkeycloak/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zemirco%2Fkeycloak/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zemirco","download_url":"https://codeload.github.com/zemirco/keycloak/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248317348,"owners_count":21083519,"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":["go","hacktoberfest","keycloak"],"created_at":"2024-11-24T17:12:29.220Z","updated_at":"2025-04-10T23:34:42.190Z","avatar_url":"https://github.com/zemirco.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"\n# keycloak\n\n[![ci](https://github.com/zemirco/keycloak/workflows/ci/badge.svg)](https://github.com/zemirco/keycloak/actions/workflows/ci.yml)\n[![codeql](https://github.com/zemirco/keycloak/workflows/codeql/badge.svg)](https://github.com/zemirco/keycloak/actions/workflows/codeql.yml)\n[![Go Reference](https://pkg.go.dev/badge/github.com/zemirco/keycloak.svg)](https://pkg.go.dev/github.com/zemirco/keycloak)\n[![Go Report Card](https://goreportcard.com/badge/github.com/zemirco/keycloak)](https://goreportcard.com/report/github.com/zemirco/keycloak)\n[![Total alerts](https://img.shields.io/lgtm/alerts/g/zemirco/keycloak.svg?logo=lgtm\u0026logoWidth=18)](https://lgtm.com/projects/g/zemirco/keycloak/alerts/)\n\nkeycloak is a Go client library for accessing the [Keycloak API](https://www.keycloak.org/docs-api/12.0/rest-api/index.html).\n\n## Installation\n\n```bash\ngo get github.com/zemirco/keycloak/v2\n```\n\n## Usage\n\n```go\npackage main\n\nimport (\n    \"context\"\n\n    \"github.com/zemirco/keycloak/v2\"\n    \"golang.org/x/oauth2\"\n)\n\nfunc main() {\n    // create your oauth configuration\n    config := oauth2.Config{\n        ClientID: \"admin-cli\",\n        Endpoint: oauth2.Endpoint{\n            TokenURL: \"http://localhost:8080/realms/master/protocol/openid-connect/token\",\n        },\n    }\n\n    // get a valid token from keycloak\n    ctx := context.Background()\n    token, err := config.PasswordCredentialsToken(ctx, \"admin\", \"admin\")\n    if err != nil {\n        panic(err)\n    }\n\n    // create a new http client that uses the token on every request\n    client := config.Client(ctx, token)\n\n    // create a new keycloak instance and provide the http client\n    k, err := keycloak.NewKeycloak(client, \"http://localhost:8080/\")\n    if err != nil {\n        panic(err)\n    }\n\n    // start using the library and, for example, create a new realm\n    realm := \u0026keycloak.Realm{\n        Enabled: keycloak.Bool(true),\n        ID:      keycloak.String(\"myrealm\"),\n        Realm:   keycloak.String(\"myrealm\"),\n    }\n\n    res, err := k.Realms.Create(ctx, realm)\n    if err != nil {\n        panic(err)\n    }\n}\n```\n\n## Examples\n\n- [full example](https://github.com/zemirco/keycloak/blob/main/example_full_test.go): realm, client, users, resources, policies, permissions, evaluation\n- [ClientsService.Create](https://pkg.go.dev/github.com/zemirco/keycloak#example-ClientsService.Create): Create a new client\n- [GroupsService.Create](https://pkg.go.dev/github.com/zemirco/keycloak#example-GroupsService.Create): Create a new group\n- [NewKeycloak-Admin](https://pkg.go.dev/github.com/zemirco/keycloak#example-NewKeycloak-Admin): Create an admin instance\n- [NewKeycloak-User](https://pkg.go.dev/github.com/zemirco/keycloak#example-NewKeycloak-User): Create a user instance\n- [PoliciesService.CreateUserPolicy](https://pkg.go.dev/github.com/zemirco/keycloak#example-PoliciesService.CreateUserPolicy): Create a user policy\n- [RealmsService.Create](https://pkg.go.dev/github.com/zemirco/keycloak#example-RealmsService.Create): Create a new realm\n- [ResourcesService.Create](https://pkg.go.dev/github.com/zemirco/keycloak#example-ResourcesService.Create): Create a new resource\n- [RolesService.Create](https://pkg.go.dev/github.com/zemirco/keycloak#example-RolesService.Create): Create a new role\n- [ScopesService.Create](https://pkg.go.dev/github.com/zemirco/keycloak#example-ScopesService.Create): Create a new scope\n- [UsersService.Create](https://pkg.go.dev/github.com/zemirco/keycloak#example-UsersService.Create): Create a new user\n\n## Development\n\nUse `docker-compose` to start Keycloak locally.\n\n```bash\ndocker-compose up -d\n```\n\nKeycloak is running at http://localhost:8080/. The admin credentials are `admin` (username) and `admin` (password). If you want to change them simply edit the `docker-compose.yml`.\n\nKeycloak uses PostgreSQL and all data is kept across restarts.\n\nUse `down` if you want to stop the Keycloak server.\n\n```bash\ndocker-compose down\n```\n\n## Architecture\n\nThe main entry point is `keycloak.go`. This is where the Keycloak instance is created. It all starts in this file.\n\nWithin Keycloak we also have the concept of clients. They are the ones that connect to Keycloak for authentication and authorization purposes, e.g. our frontend and backend apps. That is why this library simply uses the `keycloak` instance of type `Keycloak` and not a `client` instance like [go-github](https://github.com/google/go-github). Although technically this library is a Keycloak client library for Go. However this distinction should make it clear what is meant when we talk about a client in our context.\n\n## Testing\n\nYou need to have Keycloak running on your local machine to execute the tests. Simply use `docker-compose` to start it.\n\nAll tests are independent from each other. Before each test we create a realm and after each test we delete it. You don't have to worry about it since the helper function `createRealm` does that automatically for you. Inside this realm you can do whatever you want. You don't have to clean up after yourself since everything is deleted automatically when the realm is deleted.\n\nRun all tests.\n\n```bash\ngo test -race -v ./...\n```\n\nCreate code coverage.\n\n```bash\ngo test -v ./... -coverprofile=coverage.out\ngo tool cover -html=coverage.out -o coverage.html\n```\n\nWe have also provided a simple `Makefile` that run both jobs automatically.\n\n```bash\nmake\n```\n\nOpen `coverage.html` with your browser.\n\n## Design goals\n\n1. Zero dependencies\n\n    It's just the Go standard library.\n\n    The only exception is [go-querystring](https://github.com/google/go-querystring) to easily handle query parameters.\n\n1. Idiomatic Go\n\n    Modelled after [go-github](https://github.com/google/go-github) and [go-jira](https://github.com/andygrunwald/go-jira).\n\n1. Keep authentication outside this library\n\n    This is the major difference to most of the other Go Keycloak libraries.\n\n    We leverage the brilliant [oauth2](https://github.com/golang/oauth2) package to deal with authentication. We have provided multiple examples to show you the workflow. It basically means we do not provide any methods to call the `/token` endpoint.\n\n1. Return struct and HTTP response\n\n    Whenever the Keycloak API returns JSON content you'll get a proper struct as well as the HTTP response.\n\n    ```go\n    func (s *ClientsService) Get(ctx context.Context, realm, id string) (*Client, *http.Response, error)\n    ```\n\n## Related work\n\n- https://github.com/Nerzal/gocloak\n- https://github.com/PhilippHeuer/go-keycloak\n- https://github.com/coreos/go-oidc\n- https://github.com/keycloak/kcinit\n- https://github.com/pulumi/pulumi-keycloak/tree/master/sdk/go/keycloak\n- https://github.com/airmap/go-keycloak\n- https://github.com/cloudtrust/keycloak-client\n- https://github.com/myENA/go-keycloak\n- https://github.com/threez/go-keycloak\n\n## License\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzemirco%2Fkeycloak","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzemirco%2Fkeycloak","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzemirco%2Fkeycloak/lists"}