{"id":22547519,"url":"https://github.com/firmanjs/sso-keycloak-php","last_synced_at":"2025-04-10T01:12:29.110Z","repository":{"id":50681144,"uuid":"238639020","full_name":"firmanJS/sso-keycloak-php","owner":"firmanJS","description":"example integrate keycloak sso with php and javascript","archived":false,"fork":false,"pushed_at":"2020-02-19T12:37:17.000Z","size":7,"stargazers_count":35,"open_issues_count":0,"forks_count":10,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-10T01:12:24.653Z","etag":null,"topics":["docker","docker-compose","javascript","keycloak","php","sso"],"latest_commit_sha":null,"homepage":"","language":"Hack","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/firmanJS.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-02-06T08:15:43.000Z","updated_at":"2025-04-03T11:55:24.000Z","dependencies_parsed_at":"2022-09-23T14:30:41.255Z","dependency_job_id":null,"html_url":"https://github.com/firmanJS/sso-keycloak-php","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/firmanJS%2Fsso-keycloak-php","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/firmanJS%2Fsso-keycloak-php/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/firmanJS%2Fsso-keycloak-php/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/firmanJS%2Fsso-keycloak-php/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/firmanJS","download_url":"https://codeload.github.com/firmanJS/sso-keycloak-php/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248137890,"owners_count":21053775,"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":["docker","docker-compose","javascript","keycloak","php","sso"],"created_at":"2024-12-07T15:12:13.546Z","updated_at":"2025-04-10T01:12:29.093Z","avatar_url":"https://github.com/firmanJS.png","language":"Hack","readme":"[![Maintainability](https://api.codeclimate.com/v1/badges/3b4d9bda0531bdccf536/maintainability)](https://codeclimate.com/github/firmanJS/sso-keycloak-php/maintainability)\n\n# Docker Keycloak Example With PHP\n\nhow to set keycloak by using the docker and an example of application implmentation with php languages\n\n## Test Running Application\n\ncopy environment\n```sh\ncp .env-sample .env\n```\n\ntest with logging docker\n```sh\ndocker-compose up --build\n```\n\ntest with logging docker in background\n```sh\ndocker-compose up --build -d\n```\n\n## Step 1: Keycloak Setup\n\nThe first step is to use the keycloak admin console to manage client registration and set role permissions.\n\nOpen with your favorite browser like Chrome or Mozilla\n\n* http://localhost:2080/\n\nlogin with username and password :\n```txt\nusername: admin\npassword: password\n```\n\n### Create New Realm in Keycloak\n\nFollow steps below:\n\n1. Click `Add realm` button on the top left of the admin dashboard. Create a new realm with this data:\n   * Name = `demo-realm`\n1. Click `Create`\n1. Click `Login` tab, then configure this value:\n   * User registration = `ON`\n1. Click `Save`\n\n### Create New Client in Keycloak\n\nFollow steps below:\n\n1. Click on `Clients` in the left menu\n1. Click on \"Create\", then configure these values:\n   * Client ID = `demo_client`\n1. Click `Save`\n1. Edit this field:\n   * Access Type = `public`\n   * Valid Redirect URIs = `http://localhost:3000/*`\n\n### Create Permission to Client\n\nFollow steps below:\n\n1. Click on `Clients` in the left menu\n1. Click `Edit` button next to `demo_client`\n1. Click `Roles` tab and click button `Add Role` example Role Name = access_view\n1. Click `Mappers` on tab and click button `Add Builtin` checklist `client roles` and click save\n1. Click `edit` `client roles` in `Token Claim Name` change roles to `permission` and click save\n\nNow you have successfully finished the keycloak configuration for the new client application.\n\n### Roles and Permission\n\nFollow steps below:\n\n1. Click on `Roles` in the left menu\n1. Click `Add Role` Example :\n  * Name = `Administrator`\n  * Set Composite Roles = `ON`  \n  * in Composite Roles Select Client Roles `demo_client`\n  * in Alvailable Roles select permission `access_view` and click `Add selected` \n  * click tab `Default Roles` in top `Roles` page\n  * in `Realm Roles` select `Available Roles` `Administrator` example for default roles user register app\n\n### Get Keycloak JSON setup for app\n\nFollow steps below:\n\n1. Click on `Clients` in the left menu\n2. Click `demo_client` \n3. Click on `Installation` in top menu\n4. in `Format Option` select a format `Keycloak OIDC JSON` and click Download\n5. move `keycloak.json` in the root folder app\n\n### Setup keycloak.json in app PHP\n\nmove keycloak.json to root app directory PHP create file index.php and add code like this\n\n```javascript\n\u003cscript src=\"http://localhost:2080/auth/js/keycloak.js\"\u003e\u003c/script\u003e\n\u003cscript src=\"https://cdn.jsdelivr.net/npm/js-cookie@beta/dist/js.cookie.min.js\"\u003e\u003c/script\u003e\n\u003cscript type=\"text/javascript\"\u003e\n    const keycloak = Keycloak('http://localhost:3000/keycloak.json')\n    const initOptions = {\n        responseMode: 'fragment',\n        flow: 'standard',\n        onLoad: 'login-required'\n    };\n    function logout(){\n        Cookies.remove('token');\n        Cookies.remove('callback');\n        keycloak.logout();\n    }\n    keycloak.init(initOptions).success(function(authenticated) {\n        Cookies.set('token', keycloak.token);\n        Cookies.set('callback',JSON.stringify(keycloak.tokenParsed.resource_access.php_service.permission));\n        var arr = JSON.parse(Cookies.get('callback'));\n        arr = arr.reduce((index,value) =\u003e (index[value] = true, index), {});\n        (arr.access_create === true ? document.getElementById(\"create\").disabled = false : document.getElementById(\"create\").disabled = true);\n        (arr.access_edit === true ? document.getElementById(\"edit\").disabled = false : document.getElementById(\"edit\").disabled = true);\n        (arr.access_delete === true ? document.getElementById(\"delete\").disabled = false : document.getElementById(\"delete\").disabled = true);\n        (arr.access_view === true ? document.getElementById(\"read\").disabled = false : document.getElementById(\"read\").disabled = true);\n        document.getElementById(\"test\").innerHTML = Cookies.get('token');\n        // console.log('Init Success (' + (authenticated ? 'Authenticated token : '+JSON.stringify(keycloak) : 'Not Authenticated') + ')');\n    }).error(function() {\n        console.log('Init Error');\n    });\n\u003c/script\u003e\n```\n\n### Testing User Self-Registration\n\nPlease start in Firefox or chrome a \"New Private Window\" and connect to the following URL\n\nhttp://localhost:2080/auth/realms/demo-realm/account\n\n\nFollow steps below:\n\n1. Click `register` in the bottom login page\n\n\u003c!-- ### User Role Mappings\n\nFollow steps below:\n\n1. Click on \"User\" in the left menu\n1. Click tab `Role Mappings`\n1. in `Realm Roles` Select Available Roles `Administrator` and click `Add selected` --\u003e\n\n\n### Accees to Keycloak Dabatabse\n\nUse this credentials to access\n\n* Host: `keycloak_db_sso`\n* Port: 5432\n* Database: `keycloak_sso`\n* User: `keycloak_sso`\n* Password: `password`\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffirmanjs%2Fsso-keycloak-php","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffirmanjs%2Fsso-keycloak-php","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffirmanjs%2Fsso-keycloak-php/lists"}