Speakr is a personal, self-hosted web application designed for transcribing audio recordings
Python
Add SSO unlink feature for linked accounts
Murtaza Nasir27a509f1acfa
diff --git a/docs/admin-guide/sso-setup.md b/docs/admin-guide/sso-setup.mdindex 5a31b064..354decdf 100644--- a/docs/admin-guide/sso-setup.md+++ b/docs/admin-guide/sso-setup.md@@ -92,10 +92,16 @@ For self-hosted deployments where you control both Speakr and the IdP, this is g- In **Account > Single Sign-On**, click **Link {PROVIDER} account** while logged in.- If the SSO subject is already linked to another user, the link is rejected.+## Unlinking SSO++Users can unlink their SSO account from **Account > Single Sign-On** by clicking **Unlink {PROVIDER} account**. This removes the SSO association while keeping the local account intact.++**Important:** Users who created their account via SSO (and have no password set) must first set a password before unlinking. Otherwise they would be locked out of their account.+## Troubleshooting- **Login fails immediately**: verify `SSO_DISCOVERY_URL`, client credentials, and that the redirect URI matches exactly.- **User created without email**: some IdPs do not return `email`; user is created with a placeholder email based on `sub`.- **Domain rejected**: confirm `SSO_ALLOWED_DOMAINS` and that the IdP returns an `email` claim.-- **Already linked**: ensure each SSO subject is unique; unlinking is not yet implemented.+- **Already linked**: ensure each SSO subject is unique; users can unlink from Account settings to re-link to a different account.diff --git a/src/api/auth.py b/src/api/auth.pyindex 3c8af0f2..11beb4b4 100644--- a/src/api/auth.py+++ b/src/api/auth.py@@ -270,6 +270,24 @@ def sso_link():return redirect(url_for('auth.sso_login'))+@auth_bp.route('/auth/sso/unlink', methods=['POST'])+@login_required+def sso_unlink():+ if not current_user.sso_subject:+ flash('Your account is not linked to SSO.', 'warning')+ return redirect(url_for('auth.account'))++ if not current_user.password:+ flash('Cannot unlink SSO - you have no password set. Please set a password first.', 'danger')+ return redirect(url_for('auth.account'))++ current_user.sso_provider = None+ current_user.sso_subject = None+ db.session.commit()+ flash('SSO account unlinked successfully.', 'success')+ return redirect(url_for('auth.account'))++@auth_bp.route('/logout')@csrf_exemptdef logout():@@ -384,7 +402,8 @@ def account():sso_enabled=sso_enabled,sso_provider_name=sso_config.get('provider_name', 'SSO'),sso_linked=sso_linked,- sso_subject=current_user.sso_subject)+ sso_subject=current_user.sso_subject,+ has_password=bool(current_user.password))@auth_bp.route('/change_password', methods=['POST'])diff --git a/templates/account.html b/templates/account.htmlindex 45e2957b..b37d7b3a 100644--- a/templates/account.html+++ b/templates/account.html@@ -614,6 +614,18 @@<div class="text-xs text-[var(--text-muted)] break-all"><span class="font-medium text-[var(--text-secondary)]">Subject:</span> {{ sso_subject }}</div>+ {% if has_password %}+ <form method="POST" action="{{ url_for('auth.sso_unlink') }}" class="mt-2">+ <input type="hidden" name="csrf_token" value="{{ csrf_token() }}"/>+ <button type="submit" onclick="return confirm('Are you sure you want to unlink your SSO account? You will need to use your email and password to sign in.')" class="w-full inline-flex items-center justify-center px-4 py-2 border border-[var(--border-secondary)] text-[var(--text-secondary)] rounded-md hover:bg-[var(--bg-tertiary)] hover:text-[var(--text-danger)] focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-[var(--border-focus)] transition-colors duration-200">+ <i class="fas fa-unlink mr-2"></i> Unlink {{ sso_provider_name }} account+ </button>+ </form>+ {% else %}+ <div class="mt-2 text-xs text-[var(--text-muted)]">+ <i class="fas fa-info-circle mr-1"></i> To unlink SSO, you must first set a password.+ </div>+ {% endif %}{% else %}<form method="POST" action="{{ url_for('auth.sso_link') }}" class="mt-2"><input type="hidden" name="csrf_token" value="{{ csrf_token() }}"/>