CrossClj

0.1.2 docs

SourceDocs



RECENT

    authorization-troll

    Clojars

    Jul 30, 2015


    OWNER
    Funding Circle
    www.fundingcircle.com

    Readme

    Index of all namespaces


    The README below is fetched from the published project artifact. Some relative links may be broken.

    authorization-troll

    A Liberator compliant library to check if a user is authorized to access a resource.

    Installation

    In your project.clj: [authorization-troll "0.1.0-SNAPSHOT"]

    Usage

    Middleware

    The function extract-user-from-header expects that there will be a user map defined in the x-user header. It will extract the x-user header and assoc the user map into the request.

    (ns myapp.core
      (:require [authorization-troll.middleware :refer [extract-user-from-header])
    
    (def app (-> root-handler
                 (extract-user-from-header))  
    

    Liberator decisions

    The authorization-troll.authorization namespace provides functions that can be used in Liberator-defined resources. These functions assume the presence of a user map in the request object. The user map can have a key/value pair for roles and/or a key/value pair for a user-id:

    ;;; Sample user maps
    {:user {:roles ["foo" "bar"] }}
    {:user {:user-id 42 :roles ["baz"] }}
    {:user {:user-id 42 }}
    
    (defn- check-for-roles [context]
      (let [requestor (get-in context [:request :user])]
        (authorized-with-any-roles requestor :admin :manager)))
        
    (defresource foo
      :authorized? check-for-roles)
    

    Test helpers

    The authorization-troll.test-helper namespace provides a function, set-user for writing tests. Set-user adds an x-user header to the request.

    (let [user {:user {:roles [:foo :bar]}}
            request (-> (mock/request :get "/")
                        (set-user user))])
    
    

    License

    Copyright © 2015 Funding Circle

    Distributed under the BSD 3-Clause License.