{"id":18565047,"url":"https://github.com/greenbone/keycloak-client-golang","last_synced_at":"2025-05-15T18:35:13.178Z","repository":{"id":169383789,"uuid":"566446234","full_name":"greenbone/keycloak-client-golang","owner":"greenbone","description":"Reusable keycloak client for golang","archived":false,"fork":false,"pushed_at":"2024-04-15T07:56:25.000Z","size":275,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":7,"default_branch":"main","last_synced_at":"2024-04-17T16:11:17.094Z","etag":null,"topics":["3rdgen","backend","extended","go","opensight","opensight-asset"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"agpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/greenbone.png","metadata":{"files":{"readme":"README.md","changelog":"changelog.toml","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":".github/CODEOWNERS","security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2022-11-15T17:34:08.000Z","updated_at":"2024-06-05T13:20:32.501Z","dependencies_parsed_at":"2023-10-11T18:22:42.337Z","dependency_job_id":"8a246a4a-ed4d-4592-ab7f-afffc779df28","html_url":"https://github.com/greenbone/keycloak-client-golang","commit_stats":null,"previous_names":["greenbone/user-management-api"],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/greenbone%2Fkeycloak-client-golang","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/greenbone%2Fkeycloak-client-golang/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/greenbone%2Fkeycloak-client-golang/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/greenbone%2Fkeycloak-client-golang/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/greenbone","download_url":"https://codeload.github.com/greenbone/keycloak-client-golang/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239293948,"owners_count":19615043,"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":["3rdgen","backend","extended","go","opensight","opensight-asset"],"created_at":"2024-11-06T22:17:30.525Z","updated_at":"2025-02-17T13:15:03.061Z","avatar_url":"https://github.com/greenbone.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"![Greenbone Logo](https://www.greenbone.net/wp-content/uploads/gb_new-logo_horizontal_rgb_small.png)\n\n[![GitHub releases](https://img.shields.io/github/release/greenbone/keycloak-client-golang.svg)](https://github.com/greenbone/keycloak-client-golang/releases)\n\n# Golang Keycloak Client\n\nThis repository contains a reusable connector for Keycloak.\n\n## Authorization\n\nSee [auth/example_test.go](auth/example_test.go) for example usage or snippet below:\n\n```go\nimport \"github.com/greenbone/keycloak-client-golang/auth\"\n\nfunc main() {\n    realmInfo := auth.KeycloakRealmInfo{\n        RealmId:               \"user-management\",             // keycloak realm name\n        AuthServerInternalUrl: \"http://keycloak:8080/auth\",   // keycloak server internal url\n        AuthServerPublicUrl:   \"http://localhost:28080/auth\", // keycloak server public url (jwt issuer)\n    }\n    \n    authorizer, err := auth.NewKeycloakAuthorizer(realmInfo)\n    if err != nil {\n        log.Fatal(fmt.Errorf(\"error creating keycloak token authorizer: %w\", err))\n        return\n    }\n\n    authMiddleware, err := auth.NewGinAuthMiddleware(authorizer.ParseRequest)\n    if err != nil {\n        log.Fatal(fmt.Errorf(\"error creating keycloak auth middleware: %w\", err))\n        return\n    }\n\n    gin.SetMode(gin.TestMode)\n    router := gin.Default()\n    router.Use(authMiddleware) // wire up auth middleware\n\n    router.GET(\"/test\", func(c *gin.Context) {\n        userContext, err := auth.GetUserContext(c)\n        if err != nil {\n            _ = c.AbortWithError(http.StatusInternalServerError, err)\n            return\n        }\n\n        c.String(http.StatusOK, fmt.Sprintf(\"%#v\", userContext))\n        // Output:\n        //\n        // \u0026auth.UserContext{\n        //     Realm: \"user-management\", \n        //     UserID: \"1927ed8a-3f1f-4846-8433-db290ea5ff90\", \n        //     UserName: \"initial\", \n        //     EmailAddress: \"initial@host.local\", \n        //     Roles: []string{\"\"offline_access\", \"uma_authorization\", \"user\", \"default-roles-user-management\"}, \n        //     Groups: []string{\"user-management-initial\"}, \n        //     AllowedOrigins: []string{\"http://localhost:3000\"},\n        // }\n    })\n}\n```\n\n*Steps:*\n\n- create a realm info struct with realm id and keycloak internal url (inside docker/k8s) from environment variables,\n- create keycloak authorizer via `auth.NewKeycloakAuthorizer` and pass the realm info,\n- create gin middleware via `auth.NewGinAuthMiddleware` with `ParseRequest` method of the authorizer. It will check `Authorization` header for the bearer token and `Origin` header for an allowed origin. It will put decoded claims into gin context\n- wire up auth middleware to routes you decide\n- inside routes use `auth.GetUserContext` to get decoded token claims as a user context object from gin context\n\n\n## Maintainer\n\nThis project is maintained by [Greenbone AG][Greenbone Networks]\n\n## Contributing\n\nYour contributions are highly appreciated. Please\n[create a pull request](https://github.com/greenbone/keycloak-client-golang/pulls)\non GitHub. Bigger changes need to be discussed with the development team via the\n[issues section at GitHub](https://github.com/greenbone/keycloak-client-golang/issues)\nfirst.\n\n## License\n\nCopyright (C) 2020-2023 [Greenbone AG][Greenbone Networks]\n\nLicensed under the [GNU General Public License v3.0 or later](LICENSE).\n\n[Greenbone Networks]: https://www.greenbone.net/\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgreenbone%2Fkeycloak-client-golang","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgreenbone%2Fkeycloak-client-golang","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgreenbone%2Fkeycloak-client-golang/lists"}