
Server-to-server authentication through oauth2-proxy has no obvious solution when using the Google provider. There is no built-in mechanism for API clients to authenticate without a browser-based OAuth2 flow. The workaround: exchange Google Service Account credentials for a JWT token that oauth2-proxy can validate directly.
Overview
In order to do server to server auth through oauth2-proxy when using Google Provider, you have to do the following:
- Setup a new Google Service Account
- Update OAuth2 Proxy settings
- Exchange SA credentials for JWT token
Google Service Account
Creating a Service Account is pretty standard fare, and no special permissions are needed for it. Grab the service-account.json file as you will need it to exchange for JWT Token
Update OAuth2 Proxy settings
When deploying oauth2 proxy, in addition to Google provider settings you need to set the following options:
--oidc-issuer-url=https://accounts.google.com, without this setting, as of v7.2.1,oauth2-proxycannot validate jwt tokens when using the Google provider.--skip-jwt-bearer-tokens=true- this tells oauth2 proxy not to do the exchange ifAuthorization: Bearer ey....header is set.
We also set the --authenticated-emails-file=/path/to/file setting, where you will need to add the SA email address that you created in the first step.
Exchange SA credentials for JWT token
Last thing you will need to do is have a valid JWT token, which requires using the SA to get it from Google. Here’s some code that gives you the token:
| |
While get_token looks complicated, its not. All its doing is a valid jwt token request and you can read more about it here. Key details are the client_email which must match allowed email address and target_audience which must match the client id in OAuth2 Proxy.
And thats it! Now, in addition to secure, short lived access for your users, you can have service accounts exchange their credentials for a valid JWT token.