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:
…securityDefinitions:JWTCookie:type:apiKeyin:headername:Cookiex-flat-cookiename:authtoken# ⬅ specify the cookie name…
Then we specify the configuration for decoding the JWT token:
…JWTCookie:type:apiKeyin:headername:Cookiex-flat-cookiename:authtokenx-flat-jwt:# ⬅ our JWT configuration:key:# ⬅ the key to decode the JWT …file:pubkey.pem# ⬅ … is read from the file pubkey.pemalg:RS256# ⬅ the signing algorithm is RS256…
The specified key is a public key, read from the file pubkey.pem, e.g.:
-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDGSd+sSTss2uOuVJKpumpFAaml
t1CWLMTAZNAabF71Ur0P6u833RhAIjXDSA/QeVitzvqvCZpNtbOJVegaREqLMJqv
FOUkFdLNRP3f9XjYFFvubo09tcjX6oGEREKDqLG2MfZ2Z8LVzuJc6SwZMgVFk/63
rdAOci3W9u3zOSGj4QIDAQAB
-----END PUBLIC KEY-----
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:
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.
-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDGSd+sSTss2uOuVJKpumpFAaml
t1CWLMTAZNAabF71Ur0P6u833RhAIjXDSA/QeVitzvqvCZpNtbOJVegaREqLMJqv
FOUkFdLNRP3f9XjYFFvubo09tcjX6oGEREKDqLG2MfZ2Z8LVzuJc6SwZMgVFk/63
rdAOci3W9u3zOSGj4QIDAQAB
-----END PUBLIC KEY-----