Refactor
I would start by not calling props.getProperty("app.auth.idp.strategy")
repeatedly. Call it once, and you immediately have less reason to split any lines.
strategy = props.getProperty("app.auth.idp.strategy")
if not strategy or strategy == 'saml/simpleSAMLphp' and PROXYING_MECHANISM == "ngrok":
IDP_STRATEGY = "saml/simpleSAMLphp"
elif strategy == 'saml/gsuite':
IDP_STRATEGY = "saml/gsuite"
elif strategy == 'saml/remote-simpleSAMLphp':
IDP_STRATEGY = "saml/remote-simpleSAMLphp"
else:
IDP_STRATEGY = "saml"
Line continuation
For the first long line, your options are line continuation, either explicit:
if not strategy or \
strategy == 'saml/simpleSAMLphp' and \
PROXYING_MECHANISM == "ngrok":
or implicit, inside parentheses:
if (not strategy or
strategy == 'saml/simpleSAMLphp' and
PROXYING_MECHANISM == "ngrok"):
Use lookup, not repeated comparisons
An even better option is to replace a long string of comparisons with a dict
lookup:
strategies = {
"saml/gsuite": "saml/gsuite",
"saml/remote-simpleSAMLphp": "saml/remote-simpleSAMLphp",
}
if PROXYING_MECHANISM == "ngrok":
strategies['saml/simpleSAMLphp'] = 'saml/simpleSAMLphp'
IDP_STRATEGY = strategies.get(props.getProperty("app.auth.idp.strategy"), "saml")
And because each key of the dict
is just mapped to itself, you can replace that with a simple set
lookup.
strategies = {
"saml/gsuite",
"saml/remote-simpleSAMLphp",
}
if PROXYING_MECHANISM == "ngrok":
strategies.add('saml/simpleSAMLphp')
IDP_STRATEGY = "saml"
strategy = props.getProperty("app.auth.idp.strategy")
if strategy in strategies:
IDP_STRATEGY = strategy
Take your pick which of the last two you find more readable. The dict
is more redundant in its definition, but allows a single assignment to IDP_STRATEGY
.