Replace login/register forms with Keycloak sign-in flow
- LoginScreen: single "Sign in with Aeternalis" button replaces two-tab email/password/register layout; all credential encryption removed - login_screen.gd: wires Keycloak.token_received → Auth.login_with_keycloak, handles auth_error and auth_failed signals - Auth.gd: rewritten to use Keycloak session tokens; stores Keycloak refresh token for silent re-auth; calls Keycloak.revoke on logout - Keycloak.gd: new autoload implementing Authorization Code + PKCE with loopback redirect, silent refresh, and fire-and-forget revocation - project.godot: register Keycloak as autoload singleton Generated with [Claude Code](https://claude.ai/code) via [Happy](https://happy.engineering) Co-Authored-By: Claude <noreply@anthropic.com> Co-Authored-By: Happy <yesreply@happy.engineering>
This commit is contained in:
@@ -62,124 +62,15 @@ text = "Stone of Revival"
|
||||
horizontal_alignment = 1
|
||||
|
||||
[node name="Spacer" type="Control" parent="Center/Panel/Margin/VBox"]
|
||||
custom_minimum_size = Vector2(0, 6)
|
||||
custom_minimum_size = Vector2(0, 16)
|
||||
layout_mode = 2
|
||||
|
||||
[node name="Tabs" type="TabContainer" parent="Center/Panel/Margin/VBox"]
|
||||
layout_mode = 2
|
||||
size_flags_vertical = 3
|
||||
tab_alignment = 1
|
||||
current_tab = 0
|
||||
|
||||
[node name="Login" type="VBoxContainer" parent="Center/Panel/Margin/VBox/Tabs"]
|
||||
layout_mode = 2
|
||||
theme_override_constants/separation = 8
|
||||
metadata/_tab_index = 0
|
||||
|
||||
[node name="Spacer" type="Control" parent="Center/Panel/Margin/VBox/Tabs/Login"]
|
||||
custom_minimum_size = Vector2(0, 6)
|
||||
layout_mode = 2
|
||||
|
||||
[node name="EmailLabel" type="Label" parent="Center/Panel/Margin/VBox/Tabs/Login"]
|
||||
layout_mode = 2
|
||||
text = "Channeler Email"
|
||||
horizontal_alignment = 1
|
||||
|
||||
[node name="EmailInput" type="LineEdit" parent="Center/Panel/Margin/VBox/Tabs/Login"]
|
||||
[node name="SignInBtn" type="Button" parent="Center/Panel/Margin/VBox"]
|
||||
unique_name_in_owner = true
|
||||
custom_minimum_size = Vector2(0, 44)
|
||||
custom_minimum_size = Vector2(0, 52)
|
||||
layout_mode = 2
|
||||
theme_override_font_sizes/font_size = 15
|
||||
placeholder_text = "your@email.com"
|
||||
virtual_keyboard_type = 5
|
||||
|
||||
[node name="PasswordLabel" type="Label" parent="Center/Panel/Margin/VBox/Tabs/Login"]
|
||||
layout_mode = 2
|
||||
tooltip_text = "Password"
|
||||
text = "Memory Seal"
|
||||
horizontal_alignment = 1
|
||||
|
||||
[node name="PasswordInput" type="LineEdit" parent="Center/Panel/Margin/VBox/Tabs/Login"]
|
||||
unique_name_in_owner = true
|
||||
custom_minimum_size = Vector2(0, 44)
|
||||
layout_mode = 2
|
||||
theme_override_font_sizes/font_size = 15
|
||||
placeholder_text = "••••••••"
|
||||
secret = true
|
||||
|
||||
[node name="RememberMe" type="CheckBox" parent="Center/Panel/Margin/VBox/Tabs/Login"]
|
||||
unique_name_in_owner = true
|
||||
layout_mode = 2
|
||||
theme_override_font_sizes/font_size = 13
|
||||
theme_override_colors/checkbox_checked_color = Color(1, 1, 1, 1)
|
||||
theme_override_colors/checkbox_unchecked_color = Color(1, 1, 1, 1)
|
||||
text = "Remember me"
|
||||
|
||||
[node name="LoginSpacer" type="Control" parent="Center/Panel/Margin/VBox/Tabs/Login"]
|
||||
custom_minimum_size = Vector2(0, 4)
|
||||
layout_mode = 2
|
||||
|
||||
[node name="LoginBtn" type="Button" parent="Center/Panel/Margin/VBox/Tabs/Login"]
|
||||
unique_name_in_owner = true
|
||||
custom_minimum_size = Vector2(0, 48)
|
||||
layout_mode = 2
|
||||
theme_override_font_sizes/font_size = 15
|
||||
text = "Enter the Realm"
|
||||
|
||||
[node name="Register" type="VBoxContainer" parent="Center/Panel/Margin/VBox/Tabs"]
|
||||
visible = false
|
||||
layout_mode = 2
|
||||
theme_override_constants/separation = 8
|
||||
metadata/_tab_index = 1
|
||||
|
||||
[node name="RegEmailLabel" type="Label" parent="Center/Panel/Margin/VBox/Tabs/Register"]
|
||||
layout_mode = 2
|
||||
text = "Channeler Email"
|
||||
horizontal_alignment = 1
|
||||
|
||||
[node name="RegEmailInput" type="LineEdit" parent="Center/Panel/Margin/VBox/Tabs/Register"]
|
||||
unique_name_in_owner = true
|
||||
custom_minimum_size = Vector2(0, 44)
|
||||
layout_mode = 2
|
||||
theme_override_font_sizes/font_size = 15
|
||||
placeholder_text = "your@email.com"
|
||||
virtual_keyboard_type = 5
|
||||
|
||||
[node name="RegPasswordLabel" type="Label" parent="Center/Panel/Margin/VBox/Tabs/Register"]
|
||||
layout_mode = 2
|
||||
tooltip_text = "Password"
|
||||
text = "Memory Seal"
|
||||
horizontal_alignment = 1
|
||||
|
||||
[node name="RegPasswordInput" type="LineEdit" parent="Center/Panel/Margin/VBox/Tabs/Register"]
|
||||
unique_name_in_owner = true
|
||||
custom_minimum_size = Vector2(0, 44)
|
||||
layout_mode = 2
|
||||
theme_override_font_sizes/font_size = 15
|
||||
placeholder_text = "••••••••"
|
||||
secret = true
|
||||
|
||||
[node name="RegNameLabel" type="Label" parent="Center/Panel/Margin/VBox/Tabs/Register"]
|
||||
layout_mode = 2
|
||||
text = "Display Name"
|
||||
|
||||
[node name="RegNameInput" type="LineEdit" parent="Center/Panel/Margin/VBox/Tabs/Register"]
|
||||
unique_name_in_owner = true
|
||||
custom_minimum_size = Vector2(0, 44)
|
||||
layout_mode = 2
|
||||
theme_override_font_sizes/font_size = 15
|
||||
placeholder_text = "YourName"
|
||||
|
||||
[node name="RegSpacer" type="Control" parent="Center/Panel/Margin/VBox/Tabs/Register"]
|
||||
custom_minimum_size = Vector2(0, 4)
|
||||
layout_mode = 2
|
||||
|
||||
[node name="RegisterBtn" type="Button" parent="Center/Panel/Margin/VBox/Tabs/Register"]
|
||||
unique_name_in_owner = true
|
||||
custom_minimum_size = Vector2(0, 48)
|
||||
layout_mode = 2
|
||||
theme_override_font_sizes/font_size = 15
|
||||
text = "Create Account"
|
||||
theme_override_font_sizes/font_size = 16
|
||||
text = "Sign in with Aeternalis"
|
||||
|
||||
[node name="StatusLabel" type="Label" parent="Center/Panel/Margin/VBox"]
|
||||
unique_name_in_owner = true
|
||||
|
||||
@@ -1,50 +1,24 @@
|
||||
extends Control
|
||||
## Login / Register screen.
|
||||
## Login screen — Keycloak Authorization Code + PKCE flow.
|
||||
##
|
||||
## Shown as the entry point before authentication. Uses two tabs:
|
||||
## "Login" — email + password → auth.login
|
||||
## "Register" — email + password + display name → auth.register
|
||||
## Single "Sign in with Aeternalis" button triggers the browser-based OAuth2 flow.
|
||||
## On success: Keycloak.token_received → Auth.login_with_keycloak → auth_success → InitScreen.
|
||||
## On failure: Keycloak.auth_error or Auth.auth_failed → show error, re-enable button.
|
||||
##
|
||||
## Listens to Auth.auth_success and Auth.auth_failed.
|
||||
## On success: transitions to InitScreen (which runs version check + content sync,
|
||||
## then proceeds to MainScreen).
|
||||
## On failure: shows an error message and re-enables the form.
|
||||
##
|
||||
## Remember Me: saves email + password encrypted with AES-256-CBC to
|
||||
## user://creds.dat. Cleared when the user unchecks the box and logs in.
|
||||
## Silent resume: if Auth already has a session token when this scene loads
|
||||
## (e.g. app was restarted with a valid saved token), it skips straight to main.
|
||||
|
||||
# ── Node references (unique names set in the .tscn) ──────────────────────────
|
||||
# ── Node references ───────────────────────────────────────────────────────────
|
||||
|
||||
@onready var email_input : LineEdit = %EmailInput
|
||||
@onready var password_input : LineEdit = %PasswordInput
|
||||
@onready var remember_me : CheckBox = %RememberMe
|
||||
@onready var login_btn : Button = %LoginBtn
|
||||
|
||||
@onready var reg_email_input : LineEdit = %RegEmailInput
|
||||
@onready var reg_password_input : LineEdit = %RegPasswordInput
|
||||
@onready var reg_name_input : LineEdit = %RegNameInput
|
||||
@onready var register_btn : Button = %RegisterBtn
|
||||
|
||||
@onready var status_label : Label = %StatusLabel
|
||||
|
||||
# ── Credential encryption ─────────────────────────────────────────────────────
|
||||
# AES-256-CBC, 32-byte key embedded and XOR-obfuscated across two halves.
|
||||
|
||||
const _CREDS_PATH := "user://creds.dat"
|
||||
|
||||
|
||||
# ── State ─────────────────────────────────────────────────────────────────────
|
||||
|
||||
var _was_manual_login := false
|
||||
var _pending_email := ""
|
||||
var _pending_pass := ""
|
||||
@onready var sign_in_btn : Button = %SignInBtn
|
||||
@onready var status_label : Label = %StatusLabel
|
||||
|
||||
# ── Lifecycle ─────────────────────────────────────────────────────────────────
|
||||
|
||||
func _ready() -> void:
|
||||
ContentCache.apply_scene_background(self)
|
||||
# If already authenticated (e.g. scene reloaded), skip straight to main.
|
||||
# Use call_deferred so the scene tree is not modified while _ready() is running.
|
||||
|
||||
# Already authenticated — skip straight to main (e.g. valid saved token).
|
||||
if not Auth.user_id.is_empty():
|
||||
_go_to_main.call_deferred()
|
||||
return
|
||||
@@ -53,24 +27,12 @@ func _ready() -> void:
|
||||
Auth.auth_failed.connect(_on_auth_failed)
|
||||
Network.connected.connect(_on_network_connected)
|
||||
|
||||
login_btn.pressed.connect(_on_login_pressed)
|
||||
register_btn.pressed.connect(_on_register_pressed)
|
||||
Keycloak.token_received.connect(_on_token_received)
|
||||
Keycloak.auth_error.connect(_on_keycloak_error)
|
||||
|
||||
# Allow pressing Enter to submit the login form.
|
||||
password_input.text_submitted.connect(func(_t): _on_login_pressed())
|
||||
reg_name_input.text_submitted.connect(func(_t): _on_register_pressed())
|
||||
sign_in_btn.pressed.connect(_on_sign_in_pressed)
|
||||
|
||||
# Pre-fill email/password if saved credentials exist.
|
||||
var creds := _load_creds()
|
||||
if creds.size() == 2:
|
||||
email_input.text = creds[0]
|
||||
password_input.text = creds[1]
|
||||
remember_me.button_pressed = true
|
||||
|
||||
# Make the Remember Me label visible against dark backgrounds.
|
||||
remember_me.add_theme_color_override("font_color", Color(0.8, 0.8, 0.8))
|
||||
|
||||
# Show connection / session-resume status on first open.
|
||||
# Show connecting / resume status while the socket is still coming up.
|
||||
if not Auth.session_token.is_empty():
|
||||
_set_status("Resuming session…", false)
|
||||
_set_enabled(false)
|
||||
@@ -85,59 +47,47 @@ func _exit_tree() -> void:
|
||||
Auth.auth_failed.disconnect(_on_auth_failed)
|
||||
if Network.connected.is_connected(_on_network_connected):
|
||||
Network.connected.disconnect(_on_network_connected)
|
||||
if Keycloak.token_received.is_connected(_on_token_received):
|
||||
Keycloak.token_received.disconnect(_on_token_received)
|
||||
if Keycloak.auth_error.is_connected(_on_keycloak_error):
|
||||
Keycloak.auth_error.disconnect(_on_keycloak_error)
|
||||
|
||||
# ── Button handlers ───────────────────────────────────────────────────────────
|
||||
# ── Button handler ────────────────────────────────────────────────────────────
|
||||
|
||||
func _on_login_pressed() -> void:
|
||||
var email := email_input.text.strip_edges()
|
||||
var pass_ := password_input.text
|
||||
if email.is_empty() or pass_.is_empty():
|
||||
_set_status("Please enter your email and password.", true)
|
||||
return
|
||||
_was_manual_login = true
|
||||
_pending_email = email
|
||||
_pending_pass = pass_
|
||||
func _on_sign_in_pressed() -> void:
|
||||
_set_status("Opening browser…", false)
|
||||
_set_enabled(false)
|
||||
Keycloak.start_auth_flow()
|
||||
|
||||
# ── Keycloak signal handlers ──────────────────────────────────────────────────
|
||||
|
||||
func _on_token_received(access_token: String, refresh_token: String) -> void:
|
||||
_set_status("Signing in…", false)
|
||||
_set_enabled(false)
|
||||
Auth.login(email, pass_)
|
||||
Auth.login_with_keycloak(access_token, refresh_token)
|
||||
|
||||
|
||||
func _on_register_pressed() -> void:
|
||||
var email := reg_email_input.text.strip_edges()
|
||||
var pass_ := reg_password_input.text
|
||||
var name_ := reg_name_input.text.strip_edges()
|
||||
if email.is_empty() or pass_.is_empty() or name_.is_empty():
|
||||
_set_status("Please fill in all fields.", true)
|
||||
return
|
||||
_set_status("Creating account…", false)
|
||||
_set_enabled(false)
|
||||
Auth.register(email, pass_, name_)
|
||||
func _on_keycloak_error(message: String) -> void:
|
||||
_set_status(message, true)
|
||||
_set_enabled(true)
|
||||
|
||||
# ── Auth signal handlers ──────────────────────────────────────────────────────
|
||||
|
||||
func _on_auth_success(_user_id: String, _display_name: String) -> void:
|
||||
if _was_manual_login:
|
||||
if remember_me.button_pressed:
|
||||
_save_creds(_pending_email, _pending_pass)
|
||||
else:
|
||||
_clear_creds()
|
||||
_was_manual_login = false
|
||||
_go_to_main()
|
||||
|
||||
|
||||
func _on_network_connected() -> void:
|
||||
# Once the socket is up with no session in flight, clear the spinner.
|
||||
# Socket is up but no session in flight — clear the spinner.
|
||||
if Auth.session_token.is_empty() and status_label.text == "Connecting…":
|
||||
_set_status("", false)
|
||||
|
||||
|
||||
func _on_auth_failed(error_code: String, message: String) -> void:
|
||||
_was_manual_login = false
|
||||
if error_code == "DISCONNECTED":
|
||||
_set_status("Connection lost. Retrying…", false)
|
||||
else:
|
||||
_set_status(_friendly_error(error_code, message), true)
|
||||
_set_enabled(true)
|
||||
_set_enabled(true)
|
||||
|
||||
# ── Helpers ───────────────────────────────────────────────────────────────────
|
||||
|
||||
@@ -154,114 +104,15 @@ func _set_status(text: String, is_error: bool) -> void:
|
||||
|
||||
|
||||
func _set_enabled(enabled: bool) -> void:
|
||||
login_btn.disabled = not enabled
|
||||
register_btn.disabled = not enabled
|
||||
sign_in_btn.disabled = not enabled
|
||||
|
||||
|
||||
func _friendly_error(code: String, message: String) -> String:
|
||||
match code:
|
||||
"INVALID_CREDENTIALS", \
|
||||
"INVALID_PASSWORD", \
|
||||
"USER_NOT_FOUND": return "Wrong email or password."
|
||||
"EMAIL_TAKEN": return "That email is already in use."
|
||||
"DISPLAY_NAME_TAKEN": return "That display name is already in use."
|
||||
# ASP.NET Identity password-policy codes:
|
||||
"WEAK_PASSWORD", \
|
||||
"PasswordTooShort": return "Password must be at least 12 characters."
|
||||
"PasswordRequiresUpper": return "Password must include an uppercase letter."
|
||||
"PasswordRequiresLower": return "Password must include a lowercase letter."
|
||||
"PasswordRequiresNonAlphanumeric": return "Password must include a special character (e.g. !@#$)."
|
||||
"PasswordRequiresDigit": return "Password must include a number."
|
||||
"PasswordRequiresUniqueChars": return "Password must contain more unique characters."
|
||||
"INVALID_EMAIL": return "Please enter a valid email address."
|
||||
"ACCOUNT_LOCKED": return "Account locked. Please try again later."
|
||||
"AUTH_FAILED": return "Session expired. Please sign in again."
|
||||
"AUTH_REQUIRED": return "Please sign in to continue."
|
||||
"AUTH_SERVICE_UNAVAILABLE": return "Server unavailable. Please try again shortly."
|
||||
"AUTH_FAILED": return "Session expired. Please sign in again."
|
||||
"AUTH_REQUIRED": return "Please sign in to continue."
|
||||
"AUTH_SERVICE_UNAVAILABLE": return "Server unavailable. Please try again shortly."
|
||||
"MISSING_ROLE": return "Your account does not have access to this game."
|
||||
_:
|
||||
return message if not message.is_empty() \
|
||||
else "Something went wrong (%s)." % code
|
||||
|
||||
# ── Credential storage (AES-256-CBC) ─────────────────────────────────────────
|
||||
|
||||
func _build_key() -> PackedByteArray:
|
||||
# Key halves XOR'd at runtime so the plaintext key is never a contiguous
|
||||
# literal in the binary.
|
||||
var a := PackedByteArray([
|
||||
0x7A, 0x4D, 0x9F, 0x2C, 0xB8, 0x61, 0xE3, 0x5A,
|
||||
0x0F, 0xC7, 0x94, 0x3B, 0xD6, 0x2E, 0x81, 0xF5,
|
||||
])
|
||||
var b := PackedByteArray([
|
||||
0x4C, 0xA9, 0x73, 0x1E, 0x5B, 0xD8, 0x06, 0x92,
|
||||
0xE7, 0x3F, 0xC4, 0x6A, 0x17, 0x85, 0xB0, 0x2D,
|
||||
])
|
||||
var key := PackedByteArray()
|
||||
key.resize(32)
|
||||
for i in 16:
|
||||
key[i] = a[i] ^ (i * 0x11)
|
||||
key[i + 16] = b[i] ^ (i * 0x11)
|
||||
return key
|
||||
|
||||
|
||||
func _save_creds(email: String, password: String) -> void:
|
||||
var plain := (email + "\n" + password).to_utf8_buffer()
|
||||
# PKCS#7 padding to 16-byte block boundary.
|
||||
var pad := 16 - (plain.size() % 16)
|
||||
for _i in pad:
|
||||
plain.append(pad)
|
||||
|
||||
var iv := Crypto.new().generate_random_bytes(16)
|
||||
var key := _build_key()
|
||||
var aes := AESContext.new()
|
||||
if aes.start(AESContext.MODE_CBC_ENCRYPT, key, iv) != OK:
|
||||
push_warning("LoginScreen: AES encrypt start failed")
|
||||
return
|
||||
var cipher := aes.update(plain)
|
||||
aes.finish()
|
||||
|
||||
var f := FileAccess.open(_CREDS_PATH, FileAccess.WRITE)
|
||||
if f == null:
|
||||
push_warning("LoginScreen: cannot write credentials file")
|
||||
return
|
||||
f.store_buffer(iv)
|
||||
f.store_buffer(cipher)
|
||||
|
||||
|
||||
func _load_creds() -> Array:
|
||||
if not FileAccess.file_exists(_CREDS_PATH):
|
||||
return []
|
||||
var f := FileAccess.open(_CREDS_PATH, FileAccess.READ)
|
||||
if f == null:
|
||||
return []
|
||||
var data := f.get_buffer(f.get_length())
|
||||
if data.size() < 32: # 16 IV + at least one 16-byte block
|
||||
return []
|
||||
|
||||
var iv := data.slice(0, 16)
|
||||
var cipher := data.slice(16)
|
||||
var key := _build_key()
|
||||
var aes := AESContext.new()
|
||||
if aes.start(AESContext.MODE_CBC_DECRYPT, key, iv) != OK:
|
||||
push_warning("LoginScreen: AES decrypt start failed")
|
||||
return []
|
||||
var plain := aes.update(cipher)
|
||||
aes.finish()
|
||||
|
||||
# Strip PKCS#7 padding.
|
||||
if plain.size() == 0:
|
||||
return []
|
||||
var pad_len := plain[plain.size() - 1]
|
||||
if pad_len < 1 or pad_len > 16 or pad_len > plain.size():
|
||||
return []
|
||||
plain = plain.slice(0, plain.size() - pad_len)
|
||||
|
||||
var text := plain.get_string_from_utf8()
|
||||
var parts := text.split("\n", false)
|
||||
if parts.size() != 2:
|
||||
return []
|
||||
return [parts[0], parts[1]]
|
||||
|
||||
|
||||
func _clear_creds() -> void:
|
||||
if FileAccess.file_exists(_CREDS_PATH):
|
||||
DirAccess.remove_absolute(ProjectSettings.globalize_path(_CREDS_PATH))
|
||||
|
||||
Reference in New Issue
Block a user