{"id":15713723,"url":"https://github.com/henrikac/kemal-authorizer","last_synced_at":"2025-03-30T18:47:18.749Z","repository":{"id":140270751,"uuid":"381163242","full_name":"henrikac/kemal-authorizer","owner":"henrikac","description":"A shard that makes it easy to make specific routes in a Kemal application only accessible to either anonymous, authenticated or authorized users.","archived":false,"fork":false,"pushed_at":"2021-06-29T19:12:14.000Z","size":16,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-02-05T21:27:18.235Z","etag":null,"topics":["crystal","crystal-lang","crystal-language"],"latest_commit_sha":null,"homepage":"","language":"Crystal","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/henrikac.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"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":"2021-06-28T21:15:51.000Z","updated_at":"2024-03-02T16:25:54.000Z","dependencies_parsed_at":null,"dependency_job_id":"9e1574f9-178a-47ff-abe5-af0e1d378c32","html_url":"https://github.com/henrikac/kemal-authorizer","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/henrikac%2Fkemal-authorizer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/henrikac%2Fkemal-authorizer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/henrikac%2Fkemal-authorizer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/henrikac%2Fkemal-authorizer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/henrikac","download_url":"https://codeload.github.com/henrikac/kemal-authorizer/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246365640,"owners_count":20765546,"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":["crystal","crystal-lang","crystal-language"],"created_at":"2024-10-03T21:33:06.851Z","updated_at":"2025-03-30T18:47:18.513Z","avatar_url":"https://github.com/henrikac.png","language":"Crystal","funding_links":[],"categories":[],"sub_categories":[],"readme":"# kemal-authorizer\n\nThis is a shard that makes it easy to make specific routes in a Kemal application only accessible to either anonymous, authenticated or authorized (administrators) users.\n\n## Installation\n\n1. Add the dependency to your `shard.yml`:\n\n   ```yaml\n   dependencies:\n     kemal-authorizer:\n       github: henrikac/kemal-authorizer\n   ```\n\n2. Run `shards install`\n\n## Usage\n\n```crystal\nrequire \"kemal\"\nrequire \"kemal-session\"\nrequire \"kemal-authorizer\"\n\nKemal::Session.config do |config|\n  config.secret = \"some_secret\"\nend\n\n# Only anonymous users can access these routes.\n# authenticated users will be redirected to \"/\" (default route).\nadd_handler Kemal::Authorizer::AnonymousHandler.new({\n  \"/login\" =\u003e [\"GET\", \"POST\"],\n  \"/signup\" =\u003e [\"GET\", \"POST\"]\n})\n\n# Only authenticated users can access these routes.\n# Unauthenticated users will be redirected to \"/login?next=...\" (default route).\nadd_handler Kemal::Authorizer::AuthenticationHandler.new({\n  \"/dashboard\" =\u003e [\"GET\"],\n  \"/logout\" =\u003e [\"POST\"]\n})\n\n# Only authenticated users that `is_admin` can access this route.\n# Unauthenticated users will be redirected to \"/login?next=...\" (default route).\n# If the user is authenticated but not an admin then the status code will be set to 401.\nadd_handler Kemal::Authorizer::AuthorizationHandler.new({\n  \"/admin\" =\u003e [\"GET\"]\n})\n\nget \"/\" do |env|\n  user = Kemal::Authorizer::UserStorableObject.new(1, \"user@mail.com\", true) # id, mail, is_admin\n  env.session.object(\"user\", user)\n  \"Home\"\nend\n\nget \"/login\" do |env|\n  \"Login\"\nend\n\nget \"/admin\" do |env|\n  \"Admin\"\nend\n\nKemal.run\n```\n\n#### Configuration\n`Kemal::Authorizer` has a few default configurations that can changed if needed.\n\n```crystal\nKemal::Authorizer.config do |config|\n  config.anonymous_url = \"/\"\n  config.login_url = \"/login\"\n  config.user_obj_name = \"user\" # name of the session object env.session.object(user_obj_name, obj)\n  config.user_type = Kemal::Authorizer::UserStorableObject\nend\n```\n\n#### Custom Handlers\nYou can create custom handlers by inheriting from `Kemal::Authorizer::BaseHandler`.\n\n```crystal\nclass CustomHandler \u003c Kemal::Authorizer::BaseHandler\n  def call(context)\n    # add custom logic\n    call_next context\n  end\nend\n\nadd_handler CustomHandler.new({\"/my/route\", [\"GET\", \"POST\", \"PUT\"]})\n```\n\n#### Custom StorableUser\nIf the built-in `UserStorableObject` is not sufficient enough then it is possible to make\na custom type and then set `config.user_type` to the new type. New StorableUser types must\ninherit from `Kemal::Authorizer::StorableUser`.  \n\n`Kemal::Authorizer::StorableUser` is a class with a single property `is_admin` that is set to `false` by default.\n\n```crystal\nrequire \"json\"\n\nclass MyStorableUserType \u003c Kemal::Authorizer::StorableUser\n  include JSON::Serializable\n  include Kemal::Session::StorableObject\n\n  property id : Int32\n  property name : String\n\n  def initialize(@id : Int32, @name : String); end\nend\n\n# and then\n\nKemal::Authorizer.config do |config|\n  config.user_type = MyStorableUserType\nend\n\nuser = MyStorableUserType.new(1, \"Alice\")\nuser.id       # =\u003e 1\nuser.name     # =\u003e Alice\nuser.is_admin # =\u003e false\n```\n\n## Contributing\n\n1. Fork it (\u003chttps://github.com/henrikac/kemal-authorizer/fork\u003e)\n2. Create your feature branch (`git checkout -b my-new-feature`)\n3. Commit your changes (`git commit -am 'Add some feature'`)\n4. Push to the branch (`git push origin my-new-feature`)\n5. Create a new Pull Request\n\n## Contributors\n\n- [Henrik Christensen](https://github.com/henrikac) - creator and maintainer\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhenrikac%2Fkemal-authorizer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhenrikac%2Fkemal-authorizer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhenrikac%2Fkemal-authorizer/lists"}