I wrote a quick and dirty email once about how to use keycloak with openid connect and py4web:
1. Install keycloak
2. Get passkey authentication in Keycloak to work:
3. For py4web go through the following steps to integrate keycloak with py4web for SSO and afterwards configure it in Keycloak that users that authenticate have to use passkey.
Create oauth2keycloak.py file in py4web/utils/auth_plugins/ and put in this:
from . import OAuth2
class OAuth2Keycloak(OAuth2):
name = "oauth2keycloak"
login_url = "http(s)://KEYCLOAKIPPORT/auth/realms/master/protocol/openid-connect/auth"
token_url = "http(s)://KEYCLOAKIPPORT/auth/realms/master/protocol/openid-connect/token"
userinfo_url = "http(s)://KEYCLOAKIPPORT/auth/realms/master/protocol/openid-connect/userinfo"
revoke_url = "http(s)://KEYCLOAKIPPORT/auth/realms/master/protocol/openid-connect/logout"
default_scope = "openid profile"
maps = {
"username": "email",
"email":"email",
"sso_id": "email",
"first_name": "given_name",
"last_name": "family_name",
}
In your app (probably based on scaffold) go to your static/components/auth.html and add a button for KeyCloak:
<a v-if="plugins.indexOf('oauth2keycloak')>=0"
class="button is-link"
v-bind:href="'../auth/plugin/oauth2keycloak/login?next='+next">Login with KeyCloak</a>
Also in your scaffoled app add the plugin to the auth.html file under templates:
<div class="column is-half is-offset-one-quarter" style="border : 1px solid #e1e1e1; border-radius: 10px">
<auth plugins="local,oauth2keycloak"></auth>
Finally for single logout add this line to your logout function in py4web/utils/auth.py
526 elif path == "logout":
527 self.session.clear()
528 redirect ('http(s)://KEYCLOAKIPPORT/auth/realms/master/protocol/openid-connect/logout?redirect_uri=*ENCODEDREDIRECTURIAFTERLOGOUT*
Of course you need to install keycloak and create 2 openid clients with secrets.