Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
228 commits
Select commit Hold shift + click to select a range
98a506c
Listing 2.1 - update POM with our required dependencies.
Apr 15, 2019
d606a49
Listing 2.2 Add the database schema SQL file
Apr 15, 2019
2c87eb5
Remove Maven-generated App/AppTest classes
Apr 15, 2019
4037d97
Listing 2.3 Setting up the connection pool
Apr 15, 2019
564ebfb
Listing 2.4 The SpaceController class
Apr 15, 2019
aa3b0c1
Listing 2.5 Wiring up the Natter API endpoints
Apr 15, 2019
f274036
Listing 2.6 Using prepared statements
Apr 15, 2019
de2fcc3
Listing 2.7 Create restricted database user
Apr 15, 2019
a0fb8e7
Listing 2.8 Using the Dalesbred library
Apr 15, 2019
1691cfa
Listing 2.9 Validating inputs
Apr 15, 2019
ba78299
Listing 2.10 Handling exceptions
Apr 15, 2019
0f32bf3
Remove information leakage
Apr 15, 2019
067b05a
Exploiting XSS against the Natter API
Apr 15, 2019
dd8a4b3
Listing 2.12 Preventing XSS attacks
Apr 15, 2019
4d58ffe
Listing 2.13 Posting a new message
Apr 15, 2019
8323e7d
Listing 2.14 Reading a single message
Apr 15, 2019
c624e3a
Listing 2.15 Searching for recent messages
Apr 15, 2019
a1ddb72
Listing 2.16 The Moderator API
Apr 15, 2019
8969df7
Add Guava dependency
May 14, 2019
14755bd
Listing 3.1 Add rate-limiting with Guava
May 14, 2019
4dff119
Add scrypt dependency
May 14, 2019
2cec9e3
Add users table
May 14, 2019
fbd26da
Listing 3.2 Registering a new user
May 14, 2019
b4feb61
Add the user registration endpoint
May 14, 2019
6ffbc81
Grant database user permissions on user table
May 14, 2019
d7998b1
Listing 3.3 Authenticating a request
May 14, 2019
04e6ea0
Add authentication checks
May 14, 2019
1b6c554
Listing 3.4 Enabling HTTPS
May 14, 2019
ea2fd92
Add audit log table
May 14, 2019
1e55fbc
Listing 3.5 The audit log controller
May 14, 2019
490adcd
Wire up audit logging
May 14, 2019
58e8d18
Require authentication
May 14, 2019
4b0d0ee
Add permissions table and ensure space owner has full perms
May 14, 2019
99b64c0
Listing 3.6 Checking permissions in a filter
May 14, 2019
7fc02ce
Listing 3.7 Add authorization filters
May 14, 2019
06f4760
Listing 3.8 Adding users to a space
May 14, 2019
ff20f66
Avoid integrity constraint error in schema
May 14, 2019
41168cc
Wire up addMember operation
May 14, 2019
45da5af
Avoid privilege escalation attack
May 14, 2019
eca74f1
Listing 4.1 Calling the Natter API from JavaScript
May 14, 2019
ba11bad
Listing 4.2 The Natter HTML UI
May 14, 2019
75ac975
Listing 4.3 Intercepting form submission
May 14, 2019
854b6f3
Serve HTML static files from the same origin as the API
May 14, 2019
cc83b30
Listing 4.4 CORS filter
May 14, 2019
217ea80
Wire up CORS filter
May 14, 2019
52c2b95
Listing 4.5 Producing a session cookie
May 14, 2019
9def0e0
Listing 4.6 Avoid session fixation attacks
May 14, 2019
ede0665
Listing 4.7 Validating a session cookie
May 14, 2019
4e6e622
Wire up session validation
May 14, 2019
f202947
Listing 4.8 Calling the login endpoint from JavaScript
May 14, 2019
b6a44bc
Listing 4.9 The login form HTML
May 14, 2019
a57a297
Listing 4.10 Redirecting to the login page
May 14, 2019
c8a3617
Listing 4.11 Removing the WWW-Authenticate challenge
May 14, 2019
8ff9a46
Listing 4.12 Preventing Flash-based CSRF bypass
May 14, 2019
d2be221
Listing 4.13 CSRF protection filter
May 14, 2019
36b2f8e
Listing 4.14 Returning the anti-CSRF token after login
May 14, 2019
c18d0a9
Listing 4.15 Storing the anti-CSRF token in localStorage
May 14, 2019
2030443
Listing 4.16 Passing the X-CSRF-Token header
May 14, 2019
27eadbe
Add X-CSRF-Token to CORS allowed headers
May 14, 2019
e127a4a
Remove X-Requested-With filter in favour of CsrfFilter
May 14, 2019
645600f
Update code to reflect revised chapter 4
May 21, 2019
d6df75b
Add logout
Jun 2, 2019
0d6bf79
Allow running on a different port
May 22, 2019
f92c92f
Listing 5.1 CORS filter
May 22, 2019
01e29b7
Enable the CORS filter
May 22, 2019
e6a7e37
The DatabaseTokenStore
May 22, 2019
6d86a47
Bearer authentication scheme
May 22, 2019
7be9d0e
switch to bearer
Jun 2, 2019
d5c9438
Delete expired tokens
May 22, 2019
4a58b4c
Switch to scheduleAtFixedRate
Jun 2, 2019
a89bbdc
Switch to Bearer auth in the UI
May 22, 2019
f29b970
Use localStorage for tokens
May 22, 2019
c5d1ea6
Tighten up the CORS settings
May 22, 2019
fef727a
Client-side stateless tokens
May 23, 2019
2cfb52f
JsonTokenStore revocation
Jun 2, 2019
badd9c7
Add encryption to client-side tokens
May 24, 2019
6a5c8e4
Encrypted token revocation
Jun 2, 2019
6f070e9
Move to ChaCha20-Poly1305 authenticated encryption
May 24, 2019
d2cba59
Verify the audience as a JWT.
May 24, 2019
2d5d506
Produce valid HS256 JWTs
May 25, 2019
f65b0a7
Produce encrypted JWTs with nimbus-jose
May 25, 2019
33d2839
JWT token revocation
Jun 2, 2019
2b36be3
Tidy up hand-rolled JWT validation code.
May 25, 2019
6e33227
Correct Date import
May 25, 2019
b4ecafb
Fix JWT processing
May 25, 2019
b3e7df9
Extract JWT header into a separate wrapper store
May 25, 2019
5ce08b7
JwtHeaderTokenStore revocation
Jun 2, 2019
1dd7553
Switch back to the JwtTokenStore
May 25, 2019
6f1e3e7
Add types to enforce security properties
May 27, 2019
b40cde3
Add the HmacTokenStore
May 23, 2019
1fe4f46
Revert change to database permissions
Jun 4, 2019
728125d
Switch back to using the JwtTokenStore
Jun 4, 2019
de2d8f8
Implement token whitelisting for JWTs
Jun 4, 2019
a26536d
Update README with links to chapter branches
May 27, 2019
e1a2dad
Add support for scoped tokens.
May 31, 2019
eab5726
OAuth2TokenStore
Jun 2, 2019
ee59391
OAuth2 token revocation
Jun 2, 2019
68c2550
Updated README
Jun 2, 2019
d089512
Improve OAuth2TokenStore
Jul 1, 2019
2b5f672
Switch to "TLS" SSLContext to avoid blocking TLS1.3
Jul 2, 2019
683f414
Add SignedJwtAccessTokenStore
Jul 3, 2019
7af8009
Fix compilation error
Jul 3, 2019
cacec3a
Add a minimal OAuth 2 Authorization Server (ROPC only)
Jul 5, 2019
0ea1bbd
Example filter for validating an ID token based on an access token.
Jul 5, 2019
c9b2cac
Update .gitignore
Aug 1, 2019
e68d312
Use static import for now()
Aug 5, 2019
25b6bdf
Update .gitignore
Aug 1, 2019
67a1754
Update .gitignore
Aug 1, 2019
70627f0
Update .gitignore
Aug 1, 2019
492f423
Update .gitignore
Aug 1, 2019
613be24
Update .gitignore
Aug 1, 2019
2235316
Update .gitignore
Aug 1, 2019
de85494
Update dependencies to latest stable versions
Sep 10, 2019
765774e
Update dependencies to latest stable versions
Sep 10, 2019
e9fef50
Update dependencies to latest stable versions
Sep 10, 2019
e5db6ec
Update dependencies to latest stable versions
Sep 10, 2019
3ae56eb
Update dependencies to latest stable versions
Sep 10, 2019
8ee0540
Update dependencies to latest stable versions
Sep 10, 2019
6537bd2
Update default security headers
Aug 1, 2019
af7163a
Update default security headers
Aug 1, 2019
9770aac
Update default security headers
Aug 1, 2019
6134974
Add OAuth2TokenStore
Sep 11, 2019
384e4e0
Secure HTTPS client settings
Sep 11, 2019
5ac796e
Add simple app for revoking access tokens
Sep 11, 2019
dd608c5
Add SignedJwtAccessTokenStore
Sep 11, 2019
8102eba
Add simple group support
Sep 14, 2019
6e0e11a
Add LdapUserController that authenticates and looks up groups from LDAP
Sep 14, 2019
74ef806
Revert to original UserController
Sep 14, 2019
5e7008b
Implement RBAC
Sep 14, 2019
f8e6a80
Allow users to specify a role when creating a session
Sep 14, 2019
22c49df
Implement ABAC with Drools
Sep 16, 2019
717d3ce
Update README for chapters 8 and 9
Oct 5, 2019
74422ee
Top-up with changes from chapters 7 and 8
Oct 5, 2019
0624d99
Update README to point to MEAP
Oct 5, 2019
8f13b7d
Split out lookup of permissions from enforcement
Oct 8, 2019
324588e
Initial capability URI implementation
Oct 9, 2019
3905e4a
Fix NPE bug in permission check
Oct 9, 2019
e75a1e7
Rearrange authentication in UserController
Oct 9, 2019
62a4991
Flesh out capabilities for other API methods.
Oct 9, 2019
663fd1f
Switch to stateless capability tokens
Oct 10, 2019
ef77e20
Add a simple capability-based message browser
Oct 12, 2019
c151969
Fix stray quote
Oct 12, 2019
00be059
Use Authorization header for capability tokens
Oct 12, 2019
8dcaebd
Remove test code
Oct 12, 2019
7bfc5f8
Use cookies for authN, capabilities for authZ
Oct 13, 2019
3e594da
Implement MacaroonTokenStore
Oct 14, 2019
11f7149
Only require authentication when looking up permissions.
Oct 17, 2019
8ca1945
Fix schema loading code to also work within Docker
Nov 4, 2019
59e33b2
Build basic Docker image with Google JIB
Nov 4, 2019
cceb7a5
Remove FK constraint from tokens to users
Nov 5, 2019
da0a2da
Add Dockerfile for H2 database
Nov 5, 2019
0ac1d62
Disable TLS and make JDBC URL configurable
Nov 5, 2019
36e4224
Use older version of H2 to avoid bug
Nov 5, 2019
13480e9
Remove stray comma
Nov 5, 2019
1697766
Create Kubernetes microservice deployments
Nov 8, 2019
60d1bbb
Add network policies
Nov 9, 2019
b06254c
Expose certs as secrets
Nov 11, 2019
97ca77b
Enable TLS across the cluster
Nov 12, 2019
2ba658b
Disable TLS again and switch to link preview microservice example
Nov 17, 2019
aaac44f
Avoid SSRF and DNS rebinding attacks
Nov 21, 2019
ac90504
Delete network policies
Nov 21, 2019
87592c3
Enable Linkerd service mesh
Nov 22, 2019
09e1fc0
Update valid host headers
Nov 22, 2019
af4836b
Add ingress controller
Nov 25, 2019
b4d11a5
Add sample private key and certificate PEMs
Nov 25, 2019
a67ea21
Merge changes after chapter 10
Nov 27, 2019
b8b9027
Update README for chapter 10
Nov 27, 2019
92d4090
Use password component for capability token
Nov 27, 2019
79ba222
Increase visibility of sha256 method for reuse
Dec 14, 2019
8548a88
Top-up from chapter04-end
Dec 14, 2019
8d45f23
Top-up merge from ch4
Dec 14, 2019
b3db2f7
Pull out base64 encoder field
Dec 14, 2019
5253fd1
Hash tokens in the DatabaseTokenStore
Dec 14, 2019
0677416
Top-up merge from chapter05-end
Dec 16, 2019
aa4d60b
Top-up merge from chapter05-end
Dec 16, 2019
ea93985
Extract Base64url utility class
Dec 16, 2019
007850d
Top-up merge of chapter04-end to pull in Base64url
Dec 16, 2019
fb6aafb
Merge branch 'chapter05' into chapter05-end
Dec 16, 2019
7994db5
Use Base64url utility class
Dec 16, 2019
35326f1
Pull in Base64url changes
Dec 16, 2019
b57d678
Use Base64url utility class
Dec 16, 2019
26d5d60
More Base64url cleanup
Dec 16, 2019
3d1c7bc
Chapter 6 revisions
Jan 9, 2020
73d78c7
More chapter 6 revisions
Jan 19, 2020
a3ad732
Top-up merge branch 'chapter06-end' into chapter07
Jan 19, 2020
bb814be
Top-up merge from ch6
Jan 19, 2020
40e83ba
Top-up merge branch 'chapter08' into chapter08-end
Jan 19, 2020
66e27f4
Correct type error
Jan 19, 2020
aba3f26
Top-up merge from ch8
Jan 19, 2020
b1b5f14
Top-up merge from ch9
Jan 19, 2020
42ec583
Fix broken HmacTokenStore usage
Jan 20, 2020
86371c6
Sample JWT bearer client
Jan 20, 2020
ae50e0d
Add TLS client certificate authentication
Jan 27, 2020
5c7b873
Allow password authentication to be disabled
Jan 27, 2020
6b7ff66
Enforce mTLS certificate-bound access tokens
Feb 3, 2020
199c139
Read database password from Kubernetes secrets
Feb 5, 2020
387638b
Use HKDF to derive keys without storing them
Feb 7, 2020
0210ab9
Merge changes from chapter11-end
Feb 7, 2020
f17999c
Update README.md for chapter 11
Feb 7, 2020
06961b0
Ensure client cert auth succeeded
Feb 10, 2020
8550ce4
Rationalize use of the term "service" to avoid confusion
Feb 17, 2020
02684a7
Top-up merge branch 'chapter11' into chapter11-end
Feb 17, 2020
c69de80
Top-up merge branch 'chapter11-end'
Feb 17, 2020
5b9ef21
Fix scope of constant
Feb 25, 2020
ca806c1
A simple UDP client and server
Feb 25, 2020
816e8ca
Initial DTLS implementation (WiP)
Feb 26, 2020
8e0c172
Rename classes
Mar 4, 2020
fdcd1e4
Debugged DTLS implementation
Mar 5, 2020
6b84116
Use ChaCha20-Poly1305 cipher suites
Mar 9, 2020
18f6c5c
BouncyCastle TLS PSK example
Mar 9, 2020
4b5b8ec
Convert BC PSK example to use DTLS
Mar 9, 2020
2f6cbf6
Send an empty initial packet to kick-start the handshake
Mar 9, 2020
c7fdb87
Use raw PSK cipher suites
Mar 9, 2020
9984d6c
Add example of COSE encryption with HKDF
Mar 12, 2020
319b9c4
Add decryption example
Mar 13, 2020
8646ac2
Add NaCl CryptoBox example
Mar 16, 2020
047c86f
Add AES-SIV example
Mar 23, 2020
bea6ad2
Add example of ratcheting for forward secrecy
Apr 7, 2020
e7b926a
Add device database
May 3, 2020
ba517c6
Updated README for chapters 12 and 13
May 3, 2020
f883df6
Add DeviceIdentityManager and method to retrieve deviceId
May 18, 2020
fa4704a
Add example of replay protection using ETags
May 18, 2020
c70aa0b
Tweaks for readability.
May 18, 2020
751d611
Add OAuth2 device authorization grant example
May 21, 2020
3c61b6c
Add OSCORE implementation
May 22, 2020
fe2af7f
Merge final code from chapter 13
Jun 4, 2020
062baef
Remove code from master branch (see per-chapter branches)
Aug 5, 2020
447e057
Update README for publication
Nov 20, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
.idea/
*.iml
target/
*.zip
.DS_Store
110 changes: 104 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,114 @@
# API Security in Action

This repository contains source code to accompany the upcoming book
API Security in Action, written by Neil Madden and to be published by
Manning Publications some time next year. If you have stumbled across
this repository by accident, it is unlikely to make much sense on its
own at this stage. Please see *TBC* for early access.
This repository contains source code that accompanies the book
*API Security in Action*, written by Neil Madden and published by
Manning Publications in November 2020.
Please see [Manning's website](https://www.manning.com/books/api-security-in-action?a_aid=api_security_in_action&a_bid=6806e3b6)
for information on purchasing a copy, or its available from Amazon
and other retailers.

**Note: there is no source code on the main branch.** You need to check
out the branch for the chapter you are reading.

The git repo is organized with a separate branch for each chapter,
starting with Chapter 2. Actually there are two (or more) branches
starting with Chapter 2. Actually there are two branches
per chapter. The branches called "chapter02", "chapter03" etc will
give you the source code as needed for starting out on the given chapter.
The branches named "chapter02-end", "chapter03-end" etc give the
final source code after all the alterations in that chapter. Typically
the source code at the end of a chapter is also identical to the start
of the next chapter.

**I strongly recommend working through the code listings from the book.**

The source code can also be downloaded as a zip file from Manning's website.

## Prerequisites

The following are needed to run the code examples:

- Java 11 or later. See https://adoptopenjdk.net for installers.
- A recent version of [Apache Maven](https://maven.apache.org) - I use 3.6.1.
- For testing, [curl](https://curl.haxx.se). On Mac OS X you should install
a version of curl linked against OpenSSL rather than Secure Transport, otherwise
you may need to adjust the examples in the book.
- I highly recommend installing [mkcert](https://github.com/FiloSottile/mkcert)
for working with SSL certificates from chapter 3 onwards.

The API server for each chapter can be started using the command

mvn clean compile exec:java

This will start the Spark/Jetty server running on port 4567. See chapter
descriptions for HTTP requests that can be used.

Chapter 10 and onwards have more detailed requirements to run the sample code.
Please consult the book for exact instructions.

## Postman

I've created a [Postman](https://www.postman.com) collection to help you perform operations using the API developed
during the book as an alternative to curl. You can import the collection from this url:
https://www.postman.com/collections/ef49c7f5cba0737ecdfd

## Chapters

### Chapter 2 - Secure API development

- [Starting Point](https://github.com/NeilMadden/apisecurityinaction/tree/chapter02)
- [Finished Code](https://github.com/NeilMadden/apisecurityinaction/tree/chapter02-end)

### Chapter 3 - Securing the Natter API

- [Starting Point](https://github.com/NeilMadden/apisecurityinaction/tree/chapter03)
- [Finished Code](https://github.com/NeilMadden/apisecurityinaction/tree/chapter03-end)

### Chapter 4 - Session cookie authentication

- [Starting Point](https://github.com/NeilMadden/apisecurityinaction/tree/chapter04)
- [Finished Code](https://github.com/NeilMadden/apisecurityinaction/tree/chapter04-end)

### Chapter 5 - Modern token-based authentication

- [Starting Point](https://github.com/NeilMadden/apisecurityinaction/tree/chapter05)
- [Finished Code](https://github.com/NeilMadden/apisecurityinaction/tree/chapter05-end)

### Chapter 6 - Self-contained tokens and JWTs

- [Starting Point](https://github.com/NeilMadden/apisecurityinaction/tree/chapter06)
- [Finished Code](https://github.com/NeilMadden/apisecurityinaction/tree/chapter06-end)

### Chapter 7 - OAuth 2 and OpenID Connect

- [Starting Point](https://github.com/NeilMadden/apisecurityinaction/tree/chapter07)
- [Finished Code](https://github.com/NeilMadden/apisecurityinaction/tree/chapter07-end)

### Chapter 8 - Identity-based access control

- [Starting Point](https://github.com/NeilMadden/apisecurityinaction/tree/chapter08)
- [Finished Code](https://github.com/NeilMadden/apisecurityinaction/tree/chapter08-end)

### Chapter 9 - Capability security and Macaroons

- [Starting Point](https://github.com/NeilMadden/apisecurityinaction/tree/chapter09)
- [Finished Code](https://github.com/NeilMadden/apisecurityinaction/tree/chapter09-end)

### Chapter 10 - Microservice APIs in Kubernetes

- [Starting Point](https://github.com/NeilMadden/apisecurityinaction/tree/chapter10)
- [Finished Code](https://github.com/NeilMadden/apisecurityinaction/tree/chapter10-end)

### Chapter 11 - Securing service to service APIs

- [Starting Point](https://github.com/NeilMadden/apisecurityinaction/tree/chapter11)
- [Finished Code](https://github.com/NeilMadden/apisecurityinaction/tree/chapter11-end)

### Chapter 12 - Securing IoT communications

- [Starting Point](https://github.com/NeilMadden/apisecurityinaction/tree/chapter12)
- [Finished Code](https://github.com/NeilMadden/apisecurityinaction/tree/chapter12-end)

### Chapter 13 - Securing IoT APIs

- [Starting Point](https://github.com/NeilMadden/apisecurityinaction/tree/chapter13)
- [Finished Code](https://github.com/NeilMadden/apisecurityinaction/tree/chapter13-end)
75 changes: 0 additions & 75 deletions natter-api/pom.xml

This file was deleted.

13 changes: 0 additions & 13 deletions natter-api/src/main/java/com/manning/apisecurityinaction/App.java

This file was deleted.

This file was deleted.