Introduction
Understand Keycloak for identity and access management on Azure Cobalt 100-based virtual machines
Create an Azure Cobalt 100-based Arm64 virtual machine
Allow access to Keycloak and the Flask application on Azure
Deploy Keycloak on an Azure Cobalt 100-based Arm64 virtual machine
Integrate a Flask OAuth2 application with Keycloak on an Azure Cobalt 100-based Arm64 virtual machine
Next Steps
In this section, you’ll configure Keycloak realms, users, and OAuth2/OpenID Connect clients, then integrate a Flask application with Keycloak authentication and test the workflow.
Create a dedicated realm for the Flask OAuth2 demo application.
In the admin console:
demo-realm as the realm name.
Keycloak Create Realm configuration page
Create a user in the demo-realm for testing OAuth2 authentication:
testuser and select Create.
Keycloak user creation page for demo-realm
You’ll now configure Keycloak as an OAuth2/OpenID Connect provider and build a Flask application that authenticates users through Keycloak.
Create a Keycloak client for the Flask application:
Client type: OpenID Connect
Client ID: flask-demo
Client authentication: Off
Authorization: Off
http://YOUR_PUBLIC_IP:5000/*
Keycloak OpenID Connect client configuration for Flask demo application
Create a project directory for the Flask OAuth2 application:
mkdir ~/flask-keycloak-demo
cd ~/flask-keycloak-demo
Create and activate a Python virtual environment for dependency isolation:
python3 -m venv venv
Activate the virtual environment:
source venv/bin/activate
Install Flask and OAuth-related Python packages:
pip install flask authlib requests
Create a Flask application that implements the OAuth2 authorization code flow with PKCE using authlib. The application connects to Keycloak using the OpenID Connect discovery endpoint.
Replace YOUR_PUBLIC_IP in the app with the public IP address of your Azure VM:
cat > app.py <<'EOF'
import os
from flask import Flask, redirect, url_for, session
from authlib.integrations.flask_client import OAuth
app = Flask(__name__)
app.secret_key = os.urandom(24)
KEYCLOAK_URL = os.environ.get('KEYCLOAK_URL', 'http://YOUR_PUBLIC_IP:8080')
REALM = 'demo-realm'
oauth = OAuth(app)
keycloak = oauth.register(
name='keycloak',
client_id='flask-demo',
client_secret=None,
server_metadata_url=f'{KEYCLOAK_URL}/realms/{REALM}/.well-known/openid-configuration',
client_kwargs={
'scope': 'openid email profile',
'code_challenge_method': 'S256',
},
)
@app.route('/')
def home():
user = session.get('user')
if user:
return f'Logged in as: {user.get("preferred_username", user.get("sub"))}'
return '<a href="/login">Log in with Keycloak</a>'
@app.route('/login')
def login():
redirect_uri = url_for('auth', _external=True)
return keycloak.authorize_redirect(redirect_uri)
@app.route('/auth')
def auth():
token = keycloak.authorize_access_token()
user = token.get('userinfo')
session['user'] = user
return redirect(url_for('home'))
@app.route('/logout')
def logout():
session.pop('user', None)
return redirect(url_for('home'))
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)
EOF
Start the Flask application with the Keycloak URL pointing to localhost:
KEYCLOAK_URL=http://localhost:8080 python app.py
The Flask application connects to Keycloak locally on the VM. The browser redirects use the public IP automatically, because Keycloak’s hostname setting controls the URLs returned in the discovery document.
Open the application in your browser. Replace YOUR_PUBLIC_IP with the public IP address of your Azure VM:
http://YOUR_PUBLIC_IP:5000
The home page shows a login link. Select the link to be redirected to the Keycloak login page. After authenticating as testuser, you’re redirected back to the Flask application and the page displays the logged-in username.
Logged in as: testuser
Flask OAuth2 demo application authenticated through Keycloak
The following are solutions to some issues that you might encounter when you try to run the demo application:
Recreate temporary directories and restart Keycloak:
sudo mkdir -p /opt/keycloak/data/tmp
sudo chown -R keycloak:keycloak /opt/keycloak/data
sudo systemctl restart keycloak
If the browser shows an HTTPS required error after logging in, disable SSL enforcement for demo-realm. Keycloak enables SSL enforcement per realm by default, so you need to apply this fix to each realm you create.
Log in to the Keycloak database:
sudo -u postgres psql -d keycloak
Disable SSL enforcement for demo-realm:
UPDATE realm
SET ssl_required = 'NONE'
WHERE name = 'demo-realm';
Exit PostgreSQL:
\q
Restart Keycloak:
sudo systemctl restart keycloak
You’ve now completed a full OAuth2/OpenID Connect integration on an Azure Cobalt 100-based Arm64 VM.
The Flask application authenticates users through Keycloak using the authorization code flow with PKCE, exchanging authorization codes for access tokens and retrieving user identity from the OpenID Connect userinfo endpoint.
You can build on this foundation by adding role-based access control in Keycloak, integrating additional applications into the same realm, or replacing the database SSL fix with a proper TLS certificate for production deployments.