That's what you would expect from httpbin.org, right?
Restricting access: Swagger Security and x-flat-jwt
Now, you don't want anyone except authorized users to use this proxy. This is typically achieved with access tokens. Some tokens are JSON Web Tokens (JWT), while others are opaque.
Swagger has a two-part feature to describe protected access to routes: securityDefinitions (what sort of protection is applied …) and security (… to which routes), e.g.:
This defines a security scheme object (named JWTCookie), meaning that some sort of cookie is needed to access certain routes. This is applied to the wildcard path/httpbin/**.
This documentation feature, with some extensions, is used to make FLAT actually permit access to the route only if a valid JWT token is presented.
First, we define the name of the cookie expected to accompany the API request:
Then we specify the configuration for decoding the JWT token:
The specified key is a public key, read from the file pubkey.pem, e.g.:
That's all. Now FLAT will only permit requests if they supply a token that bear an RS256 signature that was created with the private key that matches the given public key.
Usually, you would get the key and algorithm from your identity provider (e.g. an OAuth2 authorization server). That service would be responsible for issuing JWT tokens for your users.
For this tutorial we have prepared a couple of JWT tokens for you to try out different situations:
Let's give it a try:
Ah, yes, we forgot to present a token in the authtoken cookie. But we see, that the protection works.
Let's try again with the first token:
Hmm, expired. So this one is too old. (Access tokens typically have a restricted period of use.)
OK, let's use the other token:
Tada!
By the way, apart from cookies, this also works similarly with the Authorization: Bearer … header:
You can try that with
But there are two additional features that can be quite handy: out-header and out-var.
Sending JWT claims upstream: out-header
With out-header you can send the whole set of claims from the JWT upstream:
Accessing JWT claims: out-var
With out-var you can specify the name of a variable where FLAT will store the JSON claims encoded in the JWT, in order to make them available for further processing. E.g.
…
securityDefinitions:
JWTCookie:
type: apiKey
in: header
name: Cookie
x-flat-cookiename: authtoken # ⬅ specify the cookie name
…
…
JWTCookie:
type: apiKey
in: header
name: Cookie
x-flat-cookiename: authtoken
x-flat-jwt: # ⬅ our JWT configuration:
key: # ⬅ the key to decode the JWT …
file: pubkey.pem # ⬅ … is read from the file pubkey.pem
alg: RS256 # ⬅ the signing algorithm is RS256
…
-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDGSd+sSTss2uOuVJKpumpFAaml
t1CWLMTAZNAabF71Ur0P6u833RhAIjXDSA/QeVitzvqvCZpNtbOJVegaREqLMJqv
FOUkFdLNRP3f9XjYFFvubo09tcjX6oGEREKDqLG2MfZ2Z8LVzuJc6SwZMgVFk/63
rdAOci3W9u3zOSGj4QIDAQAB
-----END PUBLIC KEY-----
-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDGSd+sSTss2uOuVJKpumpFAaml
t1CWLMTAZNAabF71Ur0P6u833RhAIjXDSA/QeVitzvqvCZpNtbOJVegaREqLMJqv
FOUkFdLNRP3f9XjYFFvubo09tcjX6oGEREKDqLG2MfZ2Z8LVzuJc6SwZMgVFk/63
rdAOci3W9u3zOSGj4QIDAQAB
-----END PUBLIC KEY-----