|
| 1 | +== Pac4j |
| 2 | + |
| 3 | +https://www.pac4j.org[Pac4j] security engine for Jooby. |
| 4 | + |
| 5 | +=== Usage |
| 6 | + |
| 7 | +1) Add the dependency: |
| 8 | + |
| 9 | +[dependency, artifactId="jooby-pac4j"] |
| 10 | +. |
| 11 | + |
| 12 | +2) Install Pac4j |
| 13 | + |
| 14 | +.Simple login form |
| 15 | +[source, java, role="primary"] |
| 16 | +---- |
| 17 | +import io.jooby.pac4j.Pac4jModule; |
| 18 | +
|
| 19 | +{ |
| 20 | + install(new Pac4jModule()); <1> |
| 21 | +
|
| 22 | + get("/", ctx -> { |
| 23 | + UserProfile user = ctx.getUser(); <2> |
| 24 | + return "Hello " + user.getId(); |
| 25 | + }); |
| 26 | +} |
| 27 | +---- |
| 28 | + |
| 29 | +.Kotlin |
| 30 | +[source, kt, role="secondary"] |
| 31 | +---- |
| 32 | +import io.jooby.pac4j.Pac4jModule |
| 33 | +
|
| 34 | +{ |
| 35 | + install(Pac4jModule()) <1> |
| 36 | + |
| 37 | + get("/") { |
| 38 | + "Hello $ctx.user.id" <2> |
| 39 | + } |
| 40 | +} |
| 41 | +---- |
| 42 | + |
| 43 | +<1> Install Pac4j with a simple login form |
| 44 | +<2> Access to authenticated user |
| 45 | + |
| 46 | +Once installed all routes defined below requires authentication. |
| 47 | + |
| 48 | +=== Clients |
| 49 | + |
| 50 | +A Client represents an authentication mechanism. It performs the login process and returns (if successful) a user profile. |
| 51 | +Clients are configured at bootstrap time using the Pac4j DSL: |
| 52 | + |
| 53 | +==== Google |
| 54 | + |
| 55 | +This example shows how to use Google. |
| 56 | + |
| 57 | +1) Add the dependency: |
| 58 | + |
| 59 | +[dependency, artifactId="pac4j-oidc"] |
| 60 | +. |
| 61 | + |
| 62 | +2) Generates clientId and secret keys or use the one provided by pac4j (**development only**): |
| 63 | + |
| 64 | +.application.conf |
| 65 | +[source, properties] |
| 66 | +---- |
| 67 | +oidc.clientId = 167480702619-8e1lo80dnu8bpk3k0lvvj27noin97vu9.apps.googleusercontent.com |
| 68 | +oidc.secret = MhMme_Ik6IH2JMnAT6MFIfee |
| 69 | +---- |
| 70 | + |
| 71 | +2) Configure client |
| 72 | + |
| 73 | +.Google |
| 74 | +[source, java, role="primary"] |
| 75 | +---- |
| 76 | +import io.jooby.pac4j.Pac4jModule; |
| 77 | +
|
| 78 | +{ |
| 79 | + install(new Pac4jModule() |
| 80 | + .client(conf -> { |
| 81 | + OidcConfiguration oidc = new OidcConfiguration(); |
| 82 | + oidc.setClientId(conf.getString("oidc.clientId")); |
| 83 | + oidc.setSecret(conf.getString("oidc.secret")); |
| 84 | + oidc.addCustomParam("prompt", "consent"); |
| 85 | + oidc.setUseNonce(true); |
| 86 | + return new GoogleOidcClient(oidc); |
| 87 | + }) |
| 88 | + ); |
| 89 | +
|
| 90 | + get("/", ctx -> { |
| 91 | + UserProfile user = ctx.getUser(); |
| 92 | + return "Hello " + user.getId(); |
| 93 | + }); |
| 94 | +} |
| 95 | +---- |
| 96 | + |
| 97 | +.Kotlin |
| 98 | +[source, kt, role="secondary"] |
| 99 | +---- |
| 100 | +import io.jooby.pac4j.Pac4jModule |
| 101 | +
|
| 102 | +{ |
| 103 | + install(Pac4jModule() |
| 104 | + .client { conf -> { |
| 105 | + val oidc = OidcConfiguration() |
| 106 | + oidc.clientId = conf.getString("oidc.clientId") |
| 107 | + oidc.secret = conf.getString("oidc.secret") |
| 108 | + oidc.addCustomParam("prompt", "consent") |
| 109 | + oidc.useNonce = true |
| 110 | + GoogleOidcClient(oidc) |
| 111 | + } |
| 112 | + ) |
| 113 | + |
| 114 | + get("/") { |
| 115 | + "Hello $ctx.user.id" |
| 116 | + } |
| 117 | +} |
| 118 | +---- |
| 119 | + |
| 120 | +==== Twitter |
| 121 | + |
| 122 | +This example shows how to use Twitter. |
| 123 | + |
| 124 | +1) Add the dependency: |
| 125 | + |
| 126 | +[dependency, artifactId="pac4j-oauth"] |
| 127 | +. |
| 128 | + |
| 129 | +2) Generates key and secret tokens or use the one provided by pac4j (**development only**): |
| 130 | + |
| 131 | +.application.conf |
| 132 | +[source, properties] |
| 133 | +---- |
| 134 | +twitter.key = CoxUiYwQOSFDReZYdjigBA |
| 135 | +twitter.secret = 2kAzunH5Btc4gRSaMr7D7MkyoJ5u1VzbOOzE8rBofs |
| 136 | +---- |
| 137 | + |
| 138 | +2) Configure client |
| 139 | + |
| 140 | +.Twitter |
| 141 | +[source, java, role="primary"] |
| 142 | +---- |
| 143 | +import io.jooby.pac4j.Pac4jModule; |
| 144 | +
|
| 145 | +{ |
| 146 | + install(new Pac4jModule() |
| 147 | + .client(conf -> { |
| 148 | + return new TwitterClient(conf.getString("twitter.key"), conf.getString("twitter.secret")); |
| 149 | + }) |
| 150 | + ); |
| 151 | +
|
| 152 | + get("/", ctx -> { |
| 153 | + UserProfile user = ctx.getUser(); |
| 154 | + return "Hello " + user.getId(); |
| 155 | + }); |
| 156 | +} |
| 157 | +---- |
| 158 | + |
| 159 | +.Kotlin |
| 160 | +[source, kt, role="secondary"] |
| 161 | +---- |
| 162 | +import io.jooby.pac4j.Pac4jModule |
| 163 | +
|
| 164 | +{ |
| 165 | + install(Pac4jModule() |
| 166 | + .client { conf -> |
| 167 | + TwitterClient(conf.getString("twitter.key"), conf.getString("twitter.secret") |
| 168 | + } |
| 169 | + ) |
| 170 | + |
| 171 | + get("/") { |
| 172 | + "Hello $ctx.user.id" |
| 173 | + } |
| 174 | +} |
| 175 | +---- |
| 176 | + |
| 177 | +==== JWT |
| 178 | + |
| 179 | +This example shows how to use JSON WEB TOKEN. |
| 180 | + |
| 181 | +1) Add the dependency: |
| 182 | + |
| 183 | +[dependency, artifactId="pac4j-jwt"] |
| 184 | +. |
| 185 | + |
| 186 | +2) Generates key and secret tokens or use the one provided by pac4j (**development only**): |
| 187 | + |
| 188 | +.application.conf |
| 189 | +[source, properties] |
| 190 | +---- |
| 191 | +jwt.salt = CoxUiYwQOSFDReZYdjigBA |
| 192 | +---- |
| 193 | + |
| 194 | +2) Configure client |
| 195 | + |
| 196 | +.JWT |
| 197 | +[source, java, role="primary"] |
| 198 | +---- |
| 199 | +import io.jooby.pac4j.Pac4jModule; |
| 200 | +
|
| 201 | +{ |
| 202 | + install(new Pac4jModule() |
| 203 | + .client(conf -> { |
| 204 | + ParameterClient client = new ParameterClient("token", |
| 205 | + new JwtAuthenticator(new SecretSignatureConfiguration(conf.getString("jwt.salt")))); |
| 206 | + client.setSupportGetRequest(true); |
| 207 | + client.setSupportPostRequest(true); |
| 208 | + return client; |
| 209 | + }) |
| 210 | + ); |
| 211 | +
|
| 212 | + get("/", ctx -> { |
| 213 | + UserProfile user = ctx.getUser(); |
| 214 | + return "Hello " + user.getId(); |
| 215 | + }); |
| 216 | +} |
| 217 | +---- |
| 218 | + |
| 219 | +.Kotlin |
| 220 | +[source, kt, role="secondary"] |
| 221 | +---- |
| 222 | +import io.jooby.pac4j.Pac4jModule |
| 223 | +
|
| 224 | +{ |
| 225 | + install(Pac4jModule() |
| 226 | + .client { conf -> |
| 227 | + val client = new ParameterClient("token", |
| 228 | + JwtAuthenticator(SecretSignatureConfiguration(conf.getString("jwt.salt")))) |
| 229 | + client.supportGetRequest = true |
| 230 | + client.supportPostRequest = true |
| 231 | + client |
| 232 | + } |
| 233 | + ) |
| 234 | + |
| 235 | + get("/") { |
| 236 | + "Hello $ctx.user.id" |
| 237 | + } |
| 238 | +} |
| 239 | +---- |
| 240 | + |
| 241 | +=== Protecting URLs |
| 242 | + |
| 243 | +By default Pac4j restrict access to all the routes defined after the Pac4j module. You can specify |
| 244 | +what url must be protected using a path pattern: |
| 245 | + |
| 246 | +.Java |
| 247 | +[source, java, role="primary"] |
| 248 | +---- |
| 249 | +import io.jooby.pac4j.Pac4jModule; |
| 250 | +
|
| 251 | +{ |
| 252 | + install(new Pac4jModule() |
| 253 | + .client("/admin/*", conf -> { |
| 254 | + return ...; |
| 255 | + }) |
| 256 | + ); |
| 257 | +} |
| 258 | +---- |
| 259 | + |
| 260 | +.Kotlin |
| 261 | +[source, kt, role="secondary"] |
| 262 | +---- |
| 263 | +import io.jooby.pac4j.Pac4jModule |
| 264 | +
|
| 265 | +{ |
| 266 | + install(Pac4jModule() |
| 267 | + .client("/admin/*") { conf -> |
| 268 | + ... |
| 269 | + } |
| 270 | + ) |
| 271 | +} |
| 272 | +---- |
| 273 | + |
| 274 | +All routes under `/admin` will be protected by Pac4j. |
| 275 | + |
| 276 | +=== Advanced Usage |
| 277 | + |
| 278 | +You can customize default options by using the javadoc:pac4j.Pac4jOptions[] and/or providing your |
| 279 | +own Pac4j configuration. |
| 280 | + |
| 281 | +.Java |
| 282 | +[source, java, role="primary"] |
| 283 | +---- |
| 284 | +import io.jooby.pac4j.Pac4jModule; |
| 285 | +import org.pac4j.core.config.Config; |
| 286 | +
|
| 287 | +{ |
| 288 | + Config pac4j = new Config(); |
| 289 | + pac4j.setSecurityLogic(...); |
| 290 | +
|
| 291 | + install(new Pac4jModule(pac4j)); |
| 292 | +} |
| 293 | +---- |
| 294 | + |
| 295 | +.Kotlin |
| 296 | +[source, kt, role="secondary"] |
| 297 | +---- |
| 298 | +import io.jooby.pac4j.Pac4jModule |
| 299 | +import org.pac4j.core.config.Config |
| 300 | +
|
| 301 | +{ |
| 302 | + val pac4j = Config() |
| 303 | + pac4j.securityLogic = ... |
| 304 | +
|
| 305 | + install(Pac4jModule(pa4j)) |
| 306 | +} |
| 307 | +---- |
0 commit comments