File tree Expand file tree Collapse file tree 2 files changed +445
-0
lines changed Expand file tree Collapse file tree 2 files changed +445
-0
lines changed Original file line number Diff line number Diff line change @@ -287,6 +287,34 @@ response, you could write it as below:
287
287
type UserAPI10 = " users" :> Get '[JSON] (Headers ' [Header "User-Count" Integer] [User])
288
288
`` `
289
289
290
+ ### Basic Authentication
291
+
292
+ Once you've established the basic routes and semantics of your API, it's time
293
+ to consider protecting parts of it. Authentication and authorization are broad
294
+ and nuanced topics; as servant began to explore this space we started small
295
+ with one of HTTP's earliest authentication schemes: [Basic
296
+ Authentication](https://en.wikipedia.org/wiki/Basic_access_authentication).
297
+
298
+ When protecting endpoints with basic authentication, we need to specify two items:
299
+
300
+ 1. The **realm** of authentication as per the Basic Authentictaion spec.
301
+ 2. The datatype returned by the server after authentication is verified. This
302
+ is usually a `User` or `Customer` type datatype.
303
+
304
+ With those two items in mind, *servant* provides the following combinator:
305
+
306
+ `` ` haskell ignore
307
+ data BasicAuth (realm :: Symbol) (userData :: *)
308
+ `` `
309
+
310
+ Which is used like so:
311
+
312
+ `` ` haskell
313
+ type ProtectedAPI12
314
+ = UserAPI -- this is public
315
+ :<|> BasicAuth " my-real" User :> UserAPI2 -- this is protected by auth
316
+ `` `
317
+
290
318
### Interoperability with `wai`: `Raw`
291
319
292
320
Finally, we also include a combinator named `Raw` that provides an escape hatch
You can’t perform that action at this time.
0 commit comments