justtrackio / gosoline

Gosoline is our framework which fuels all of our Golang applications
MIT License
93 stars 50 forks source link

allow restricting available auth options through config #1139

Closed bt-justtrack closed 2 months ago

bt-justtrack commented 2 months ago

The idea here is that we can restrict in the config.dist.yml what authentication options are available. The same can be done with env vars, or we can layer env vars over the config.dist.yml to allow again all (eg disallow one method by default and for certain environments it could be turned on again).

In code we would specify our auth handler like this:

auth.NewChainHandler(auth.OnlyConfiguredAuthenticators(config, "default", map[string]auth.Authenticator{
        auth.ByApiKey:    apiKeyAuthHandler,
        auth.ByGoogle:    googleAuthHandler,
        auth.ByBasicAuth: emailAuthHandler,
    }))

and could restrict them to eg just Google and BasicAuth like this in the config.dist.yml:

httpserver:
  default:
    auth:
      allowedAuthenticators:
        - google
        - basicAuth

All APIKey auths would then result in 401 Unauthorized. This can then be extended to allowing different auth options on different environments through either different config.dist.yml files or env vars.