Skip to content

Commit

Permalink
Support for OpenShift OAuth (#1454)
Browse files Browse the repository at this point in the history
After successful authentication against the OpenShift built-in
OAuth provider, retrive the username.

Example OAuth Provider configuration, assuming that the Flask
application is running in the container on OpenShift:

OAUTH_PROVIDERS = [
    {
        "name": "openshift",
        "icon": "fa-circle-o",
        "token_key": "access_token",
        "remote_app": {
            "client_id": "system:serviceaccount:mynamespace:mysa",
            "client_secret": "<mysa serviceaccount token here>",
            "api_base_url": "https://openshift.default.svc.cluster.local:443",
            "client_kwargs": {"scope": "user:info"},
            "redirect_uri": "https://myapp-mynamespace.apps.<cluster_domain>",
            "access_token_url": "https://oauth-openshift.apps.<cluster_domain>/oauth/token",
            "authorize_url": "https://oauth-openshift.apps.<cluster_domain>/oauth/authorize",
            "token_endpoint_auth_method": "client_secret_post"
        }
    }
]

See also:
Using a service account as an OAuth client
https://docs.openshift.com/container-platform/4.5/authentication/using-service-accounts-as-oauth-client.html
  • Loading branch information
noseka1 authored Sep 10, 2020
1 parent c2634ba commit 9f1f64d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
15 changes: 14 additions & 1 deletion docs/security.rst
Original file line number Diff line number Diff line change
Expand Up @@ -486,14 +486,27 @@ key is just the configuration for authlib::
'request_token_url':None,
'access_token_url':'https://accounts.google.com/o/oauth2/token',
'authorize_url':'https://accounts.google.com/o/oauth2/auth'}
},
{'name':'openshift', 'icon':'fa-circle-o', 'token_key':'access_token',
'remote_app': {
'client_id':'system:serviceaccount:mynamespace:mysa',
'client_secret':'<mysa serviceaccount token here>',
'api_base_url':'https://openshift.default.svc.cluster.local:443',
'client_kwargs':{
'scope': 'user:info'
},
'redirect_uri':'https://myapp-mynamespace.apps.<cluster_domain>',
'access_token_url':'https://oauth-openshift.apps.<cluster_domain>/oauth/token',
'authorize_url':'https://oauth-openshift.apps.<cluster_domain>/oauth/authorize',
'token_endpoint_auth_method':'client_secret_post'}
}
]

This needs a small explanation, you basically have five special keys:

:name: The name of the provider, you can choose whatever you want. But the framework as some
builtin logic to retrieve information about a user that you can make use of if you choose:
'twitter', 'google', 'github','linkedin'.
'twitter', 'google', 'github', 'linkedin', 'openshift'.

:icon: The font-awesome icon for this provider.
:token_key: The token key name that this provider uses, google and github uses *'access_token'*,
Expand Down
8 changes: 8 additions & 0 deletions flask_appbuilder/security/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,14 @@ def get_oauth_user_info(self, provider, resp):
"id": me["oid"],
"username": me["oid"],
}
# for OpenShift
if provider == "openshift":
me = self.appbuilder.sm.oauth_remotes[provider].get(
"apis/user.openshift.io/v1/users/~"
)
data = me.json()
log.debug("User info from OpenShift: {0}".format(data))
return {"username": "openshift_" + data.get("metadata").get("name")}
else:
return {}

Expand Down

2 comments on commit 9f1f64d

@NickLarsenNZ
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@noseka1, are you able to explain why the username prefixed with openshift_, and not being transparent about the actual username?

@NickLarsenNZ
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh actually, I now see most of the provider implementation prefix it. Except Keycloak :/

Please sign in to comment.