|
| 1 | +# StippleUsers |
| 2 | + |
| 3 | +A middle layer to provide consistent user authorisation independent of the methods of authentication and authorisation. |
| 4 | + |
| 5 | +The package exports: |
| 6 | + |
| 7 | +- `default_user()` - the default username, if only a single user is required (mainly used to keep the app functional without any specialisation) |
| 8 | +- `default_role()` - the default role that is attributed to each user, default: `user` |
| 9 | +- `get_user()` - obtain the current user, needs to be specialised (can be any type), default: `default_user()` |
| 10 | +- `get_roles()` - obtain the roles of the current user, needs to be specialised, default: `user !== nothing ? [default_role(App)] : String[]` |
| 11 | +- `is_authorised(; role = default_role())` - return whether the current user has the role or any of the roles defined by the `role` keyword, can be specialised |
| 12 | + |
| 13 | +Customization is typically done by specialising `get_user()` and `get_roles()`, it is also possible to specialise `is_authorised()` but not recommended. |
| 14 | +As the middle layer is designed to allow for app-dependent authorisation, specialisation needs provide the app type it is meant to work with, e.g. |
| 15 | + |
| 16 | +#### with GenieAuthentication |
| 17 | +```julia |
| 18 | +using GenieAuthentication |
| 19 | +StippleUsers.get_user(::AllApps) = get_authentication() |
| 20 | +``` |
| 21 | +to provide the user id of GenieAuthentication for all apps. Note that `const AllApps = Typ{<:ReactiveModel}` has been defined for convenience. |
| 22 | +#### with Snowpark Container Services |
| 23 | +```julia |
| 24 | +StippleUsers.get_user(::Type{SnowApp}) = get(headers(), "sf-context-current-user", "not_authorised") |
| 25 | +StippleUsers.get_roles(::Type{SnowApp}, user::String) = user == "not_authorised" ? String[] : [default_role()] |
| 26 | +``` |
| 27 | +to provide the Snowpark user for the (explicit) app `SnowApp` |
| 28 | + |
| 29 | +### Example 1 - Numbered Users and Admins for a all Apps |
| 30 | +```julia |
| 31 | +using Stipple, Stipple.ReactiveTools, StippleUsers |
| 32 | + |
| 33 | +@app MyApp begin |
| 34 | + @in x = 1 |
| 35 | +end |
| 36 | + |
| 37 | +# define roles as the first part of the string |
| 38 | +StippleUsers.get_roles(::AllApps, user::String) = String[split(user, '_')[1]] |
| 39 | + |
| 40 | +# case 1: normal user |
| 41 | +StippleUsers.get_user(::AllApps) = "user_1" |
| 42 | + |
| 43 | +get_roles() |
| 44 | +# ["user"] |
| 45 | + |
| 46 | +is_authorised() |
| 47 | +# true |
| 48 | + |
| 49 | +is_authorised(role = "admin") |
| 50 | +# false |
| 51 | + |
| 52 | +# case 2: Admin |
| 53 | +StippleUsers.get_user(::AllApps) = "admin_1" |
| 54 | + |
| 55 | +get_roles() |
| 56 | +# ["admin"] |
| 57 | + |
| 58 | +is_authorised() |
| 59 | +# false |
| 60 | + |
| 61 | +is_authorised(role = "admin") |
| 62 | +# true |
| 63 | +``` |
| 64 | + |
| 65 | +### Example 2 - User List via Roles for a single App |
| 66 | +```julia |
| 67 | +using Stipple, Stipple.ReactiveTools, StippleUsers |
| 68 | + |
| 69 | +@app MyApp begin |
| 70 | + @in x = 1 |
| 71 | +end |
| 72 | + |
| 73 | +# make roles identical to user |
| 74 | +StippleUsers.get_roles(::Type{MyApp}, user::String) = [user] |
| 75 | + |
| 76 | +# case 1: normal user |
| 77 | +StippleUsers.get_user(::Type{MyApp}) = "user_1" |
| 78 | + |
| 79 | +get_roles(MyApp) |
| 80 | +# ["user_1"] |
| 81 | + |
| 82 | +is_authorised(MyApp) |
| 83 | +# false |
| 84 | + |
| 85 | +userlist() = ["user_1", "admin_1"] |
| 86 | +is_authorised(MyApp, role = userlist()) |
| 87 | +# true |
| 88 | + |
| 89 | +# case 2: Admin |
| 90 | +StippleUsers.get_user(::Type{MyApp}) = "admin_1" |
| 91 | + |
| 92 | +get_roles(MyApp) |
| 93 | +# ["admin_1"] |
| 94 | + |
| 95 | +is_authorised(MyApp, role = userlist()) |
| 96 | +# true |
| 97 | +``` |
| 98 | + |
| 99 | +### Example 3 - Overwriting `is_authorised()` for a specific implicit model |
| 100 | +```julia |
| 101 | +using Stipple, Stipple.ReactiveTools, StippleUsers |
| 102 | + |
| 103 | +# define an implicit model |
| 104 | +@app begin |
| 105 | + @in x = 1 |
| 106 | +end |
| 107 | + |
| 108 | +userlist() = ["user_1", "admin_1"] |
| 109 | + |
| 110 | +# define `App` as the module's implicit model |
| 111 | +App = Stipple.@type |
| 112 | + |
| 113 | +# define `is_authorised()` for the implicit App |
| 114 | +# Note that it is important to allow for kwargs, because internal handling will call `is_authorised()` with the `role` kwarg. |
| 115 | +StippleUsers.is_authorised(::Type{App}, user::String; kwargs...) = user ∈ userlist() |
| 116 | + |
| 117 | +StippleUsers.get_user(::Type{App}) = "user_1" |
| 118 | +is_authorised(App) |
| 119 | +# true |
| 120 | + |
| 121 | +StippleUsers.get_user(::Type{App}) = "user_2" |
| 122 | +is_authorised(App) |
| 123 | +# false |
| 124 | + |
| 125 | +StippleUsers.get_user(::Type{App}) = "admin_1" |
| 126 | +is_authorised(App) |
| 127 | +# true |
| 128 | +``` |
| 129 | + |
| 130 | +### Example 4 - applying authorisation to a Genie web application |
| 131 | + |
| 132 | +```julia |
| 133 | +using Stipple, Stipple.ReactiveTools, StippleUsers |
| 134 | + |
| 135 | +@app MyApp begin |
| 136 | + @in x = 1 |
| 137 | +end |
| 138 | + |
| 139 | +# create a random user or admin |
| 140 | +StippleUsers.get_user(::Type{MyApp}) = "$(rand(("user", "admin")))_$(rand(1:100))" |
| 141 | + |
| 142 | +# define the role as first part of the string |
| 143 | +StippleUsers.get_roles(::Type{MyApp}, user::String) = String[split(user, '_')[1]] |
| 144 | + |
| 145 | +# define an admin check which returns nothing in case of success and a not-found-page in case of failure |
| 146 | +admin_check() = is_authorised(MyApp, role = "admin") ? nothing : not_found() |
| 147 | + |
| 148 | +ui() = "Hello Admin!" |
| 149 | +@page("/", ui, model = MyApp, pre = admin_check) |
| 150 | + |
| 151 | +up(open_browser = true) |
| 152 | +# reload the page several times; depending on whether you are a (random) admin, the page will be shown or not |
| 153 | +``` |
0 commit comments