mirror of
https://github.com/we-promise/sure.git
synced 2026-04-07 14:31:25 +00:00
* Add Google SSO onboarding flow for Flutter mobile app Previously, mobile users attempting Google SSO without a linked OIDC identity received an error telling them to link from the web app first. This adds the same account linking/creation flow that exists on the PWA. Backend changes: - sessions_controller: Cache pending OIDC auth with a linking code and redirect back to the app instead of returning an error - api/v1/auth_controller: Add sso_link endpoint to link Google identity to an existing account via email/password, and sso_create_account endpoint to create a new SSO-only account (respects JIT config) - routes: Add POST auth/sso_link and auth/sso_create_account Flutter changes: - auth_service: Detect account_not_linked callback status, add ssoLink and ssoCreateAccount API methods - auth_provider: Track SSO onboarding state, expose linking/creation methods and cancelSsoOnboarding - sso_onboarding_screen: New screen with tabs to link existing account or create new account, pre-filled with Google profile data - main.dart: Show SsoOnboardingScreen when ssoOnboardingPending is true https://claude.ai/code/session_011ag1qSfriUg6j7TqFgbS5c * Fix broken SSO tests: use MemoryStore cache and correct redirect param - Sessions test: check `status` param instead of `error` since handle_mobile_sso_onboarding sends linking info with status key - API auth tests: swap null_store for MemoryStore so cache-based linking code validation works in test environment https://claude.ai/code/session_011ag1qSfriUg6j7TqFgbS5c * Delay linking-code consumption until SSO link/create succeeds Split validate_and_consume_linking_code into validate_linking_code (read-only) and consume_linking_code! (delete). The code is now only consumed after password verification (sso_link) or successful user save (sso_create_account), so recoverable errors no longer burn the one-time code and force a full Google SSO roundtrip. https://claude.ai/code/session_011ag1qSfriUg6j7TqFgbS5c * Make linking-code consumption atomic to prevent race conditions Move consume_linking_code! (backed by Rails.cache.delete) to after recoverable checks (bad password, policy rejection) but before side-effecting operations (identity/user creation). Only the first caller to delete the cache key gets true, so concurrent requests with the same code cannot both succeed. - sso_link: consume after password auth, before OidcIdentity creation - sso_create_account: consume after allow_account_creation check, before User creation - Bad password still preserves the code for retry - Add single-use regression tests for both endpoints https://claude.ai/code/session_011ag1qSfriUg6j7TqFgbS5c * Add missing sso_create_account test coverage for blank code and validation failure - Test blank linking_code returns 400 (bad_request) with proper error - Test duplicate email triggers user.save failure → 422 with validation errors https://claude.ai/code/session_011ag1qSfriUg6j7TqFgbS5c * Verify cache payload in mobile SSO onboarding test with MemoryStore The test environment uses :null_store which silently discards cache writes, so handle_mobile_sso_onboarding's Rails.cache.write was never verified. Swap in a MemoryStore for this test and assert the full cached payload (provider, uid, email, name, device_info, allow_account_creation) at the linking_code key from the redirect URL. https://claude.ai/code/session_011ag1qSfriUg6j7TqFgbS5c * Add rswag/OpenAPI specs for sso_link and sso_create_account endpoints POST /api/v1/auth/sso_link: documents linking_code + email/password params, 200 (tokens), 400 (missing code), 401 (invalid creds/expired). POST /api/v1/auth/sso_create_account: documents linking_code + optional first_name/last_name params, 200 (tokens), 400 (missing code), 401 (expired code), 403 (creation disabled), 422 (validation errors). Note: RAILS_ENV=test bundle exec rake rswag:specs:swaggerize should be run to regenerate docs/api/openapi.yaml once the runtime environment matches the Gemfile Ruby version. https://claude.ai/code/session_011ag1qSfriUg6j7TqFgbS5c * Preserve OIDC issuer through mobile SSO onboarding flow handle_mobile_sso_onboarding now caches the issuer from auth.extra.raw_info.iss so it survives the linking-code round trip. build_omniauth_hash populates extra.raw_info.iss from the cached issuer so OidcIdentity.create_from_omniauth stores it correctly. Previously the issuer was always nil for mobile SSO-created identities because build_omniauth_hash passed an empty raw_info OpenStruct. https://claude.ai/code/session_011ag1qSfriUg6j7TqFgbS5c * Block MFA users from bypassing second factor via sso_link sso_link authenticated with email/password but never checked user.otp_required?, allowing MFA users to obtain tokens without a second factor. The mobile SSO callback already rejects MFA users with "mfa_not_supported"; apply the same guard in sso_link before consuming the linking code or creating an identity. Returns 401 with mfa_required: true, consistent with the login action's MFA response shape. https://claude.ai/code/session_011ag1qSfriUg6j7TqFgbS5c * Fix NoMethodError in SSO link MFA test Replace non-existent User.generate_otp_secret class method with ROTP::Base32.random(32), matching the pattern used in User#setup_mfa!. https://claude.ai/code/session_011ag1qSfriUg6j7TqFgbS5c * Assert linking code survives rejected SSO create account Add cache persistence assertion to "should reject SSO create account when not allowed" test, verifying the linking code is not consumed on the 403 path. This mirrors the pattern used in the invalid-password sso_link test. The other rejection tests (expired/missing linking code) don't have a valid cached code to check, so no assertion is needed there. https://claude.ai/code/session_011ag1qSfriUg6j7TqFgbS5c --------- Co-authored-by: Claude <noreply@anthropic.com>
374 lines
13 KiB
Ruby
374 lines
13 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
require 'swagger_helper'
|
|
|
|
RSpec.describe 'API V1 Auth', type: :request do
|
|
path '/api/v1/auth/signup' do
|
|
post 'Sign up a new user' do
|
|
tags 'Auth'
|
|
consumes 'application/json'
|
|
produces 'application/json'
|
|
parameter name: :body, in: :body, required: true, schema: {
|
|
type: :object,
|
|
properties: {
|
|
user: {
|
|
type: :object,
|
|
properties: {
|
|
email: { type: :string, format: :email, description: 'User email address' },
|
|
password: { type: :string, description: 'Password (min 8 chars, mixed case, number, special char)' },
|
|
first_name: { type: :string },
|
|
last_name: { type: :string }
|
|
},
|
|
required: %w[email password]
|
|
},
|
|
device: {
|
|
type: :object,
|
|
properties: {
|
|
device_id: { type: :string, description: 'Unique device identifier' },
|
|
device_name: { type: :string, description: 'Human-readable device name' },
|
|
device_type: { type: :string, description: 'Device type (e.g. ios, android)' },
|
|
os_version: { type: :string },
|
|
app_version: { type: :string }
|
|
},
|
|
required: %w[device_id device_name device_type os_version app_version]
|
|
},
|
|
invite_code: { type: :string, nullable: true, description: 'Invite code (required when invites are enforced)' }
|
|
},
|
|
required: %w[user device]
|
|
}
|
|
|
|
response '201', 'user created' do
|
|
schema type: :object,
|
|
properties: {
|
|
access_token: { type: :string },
|
|
refresh_token: { type: :string },
|
|
token_type: { type: :string },
|
|
expires_in: { type: :integer },
|
|
created_at: { type: :integer },
|
|
user: {
|
|
type: :object,
|
|
properties: {
|
|
id: { type: :string, format: :uuid },
|
|
email: { type: :string },
|
|
first_name: { type: :string },
|
|
last_name: { type: :string },
|
|
ui_layout: { type: :string, enum: %w[dashboard intro] },
|
|
ai_enabled: { type: :boolean }
|
|
}
|
|
}
|
|
}
|
|
run_test!
|
|
end
|
|
|
|
response '422', 'validation error' do
|
|
schema '$ref' => '#/components/schemas/ErrorResponse'
|
|
run_test!
|
|
end
|
|
|
|
response '403', 'invite code required or invalid' do
|
|
schema '$ref' => '#/components/schemas/ErrorResponse'
|
|
run_test!
|
|
end
|
|
end
|
|
end
|
|
|
|
path '/api/v1/auth/login' do
|
|
post 'Log in with email and password' do
|
|
tags 'Auth'
|
|
consumes 'application/json'
|
|
produces 'application/json'
|
|
parameter name: :body, in: :body, required: true, schema: {
|
|
type: :object,
|
|
properties: {
|
|
email: { type: :string, format: :email },
|
|
password: { type: :string },
|
|
otp_code: { type: :string, nullable: true, description: 'TOTP code if MFA is enabled' },
|
|
device: {
|
|
type: :object,
|
|
properties: {
|
|
device_id: { type: :string },
|
|
device_name: { type: :string },
|
|
device_type: { type: :string },
|
|
os_version: { type: :string },
|
|
app_version: { type: :string }
|
|
},
|
|
required: %w[device_id device_name device_type os_version app_version]
|
|
}
|
|
},
|
|
required: %w[email password device]
|
|
}
|
|
|
|
response '200', 'login successful' do
|
|
schema type: :object,
|
|
properties: {
|
|
access_token: { type: :string },
|
|
refresh_token: { type: :string },
|
|
token_type: { type: :string },
|
|
expires_in: { type: :integer },
|
|
created_at: { type: :integer },
|
|
user: {
|
|
type: :object,
|
|
properties: {
|
|
id: { type: :string, format: :uuid },
|
|
email: { type: :string },
|
|
first_name: { type: :string },
|
|
last_name: { type: :string },
|
|
ui_layout: { type: :string, enum: %w[dashboard intro] },
|
|
ai_enabled: { type: :boolean }
|
|
}
|
|
}
|
|
}
|
|
run_test!
|
|
end
|
|
|
|
response '401', 'invalid credentials or MFA required' do
|
|
schema '$ref' => '#/components/schemas/ErrorResponse'
|
|
run_test!
|
|
end
|
|
end
|
|
end
|
|
|
|
path '/api/v1/auth/sso_exchange' do
|
|
post 'Exchange mobile SSO authorization code for tokens' do
|
|
tags 'Auth'
|
|
consumes 'application/json'
|
|
produces 'application/json'
|
|
description 'Exchanges a one-time authorization code (received via deep link after mobile SSO) for OAuth tokens. The code is single-use and expires after 5 minutes.'
|
|
parameter name: :body, in: :body, required: true, schema: {
|
|
type: :object,
|
|
properties: {
|
|
code: { type: :string, description: 'One-time authorization code from mobile SSO callback' }
|
|
},
|
|
required: %w[code]
|
|
}
|
|
|
|
response '200', 'tokens issued' do
|
|
schema type: :object,
|
|
properties: {
|
|
access_token: { type: :string },
|
|
refresh_token: { type: :string },
|
|
token_type: { type: :string },
|
|
expires_in: { type: :integer },
|
|
created_at: { type: :integer },
|
|
user: {
|
|
type: :object,
|
|
properties: {
|
|
id: { type: :string, format: :uuid },
|
|
email: { type: :string },
|
|
first_name: { type: :string },
|
|
last_name: { type: :string },
|
|
ui_layout: { type: :string, enum: %w[dashboard intro] },
|
|
ai_enabled: { type: :boolean }
|
|
}
|
|
}
|
|
}
|
|
run_test!
|
|
end
|
|
|
|
response '401', 'invalid or expired code' do
|
|
schema '$ref' => '#/components/schemas/ErrorResponse'
|
|
run_test!
|
|
end
|
|
end
|
|
end
|
|
|
|
path '/api/v1/auth/refresh' do
|
|
post 'Refresh an access token' do
|
|
tags 'Auth'
|
|
consumes 'application/json'
|
|
produces 'application/json'
|
|
parameter name: :body, in: :body, required: true, schema: {
|
|
type: :object,
|
|
properties: {
|
|
refresh_token: { type: :string, description: 'The refresh token from a previous login or refresh' },
|
|
device: {
|
|
type: :object,
|
|
properties: {
|
|
device_id: { type: :string }
|
|
},
|
|
required: %w[device_id]
|
|
}
|
|
},
|
|
required: %w[refresh_token device]
|
|
}
|
|
|
|
response '200', 'token refreshed' do
|
|
schema type: :object,
|
|
properties: {
|
|
access_token: { type: :string },
|
|
refresh_token: { type: :string },
|
|
token_type: { type: :string },
|
|
expires_in: { type: :integer },
|
|
created_at: { type: :integer }
|
|
}
|
|
run_test!
|
|
end
|
|
|
|
response '401', 'invalid refresh token' do
|
|
schema '$ref' => '#/components/schemas/ErrorResponse'
|
|
run_test!
|
|
end
|
|
|
|
response '400', 'missing refresh token' do
|
|
schema '$ref' => '#/components/schemas/ErrorResponse'
|
|
run_test!
|
|
end
|
|
end
|
|
end
|
|
|
|
path '/api/v1/auth/sso_link' do
|
|
post 'Link an existing account via SSO' do
|
|
tags 'Auth'
|
|
consumes 'application/json'
|
|
produces 'application/json'
|
|
description 'Authenticates with email/password and links the SSO identity from a previously issued linking code. Creates an OidcIdentity, logs the link via SsoAuditLog, and issues mobile OAuth tokens.'
|
|
parameter name: :body, in: :body, required: true, schema: {
|
|
type: :object,
|
|
properties: {
|
|
linking_code: { type: :string, description: 'One-time linking code from mobile SSO onboarding redirect' },
|
|
email: { type: :string, format: :email, description: 'Email of the existing account to link' },
|
|
password: { type: :string, description: 'Password for the existing account' }
|
|
},
|
|
required: %w[linking_code email password]
|
|
}
|
|
|
|
response '200', 'account linked and tokens issued' do
|
|
schema type: :object,
|
|
properties: {
|
|
access_token: { type: :string },
|
|
refresh_token: { type: :string },
|
|
token_type: { type: :string },
|
|
expires_in: { type: :integer },
|
|
created_at: { type: :integer },
|
|
user: {
|
|
type: :object,
|
|
properties: {
|
|
id: { type: :string, format: :uuid },
|
|
email: { type: :string },
|
|
first_name: { type: :string },
|
|
last_name: { type: :string },
|
|
ui_layout: { type: :string, enum: %w[dashboard intro] },
|
|
ai_enabled: { type: :boolean }
|
|
}
|
|
}
|
|
}
|
|
run_test!
|
|
end
|
|
|
|
response '400', 'missing linking code' do
|
|
schema '$ref' => '#/components/schemas/ErrorResponse'
|
|
run_test!
|
|
end
|
|
|
|
response '401', 'invalid credentials or expired linking code' do
|
|
schema '$ref' => '#/components/schemas/ErrorResponse'
|
|
run_test!
|
|
end
|
|
end
|
|
end
|
|
|
|
path '/api/v1/auth/sso_create_account' do
|
|
post 'Create a new account via SSO' do
|
|
tags 'Auth'
|
|
consumes 'application/json'
|
|
produces 'application/json'
|
|
description 'Creates a new user and family from a previously issued linking code. Links the SSO identity via OidcIdentity, logs the JIT account creation via SsoAuditLog, and issues mobile OAuth tokens. The linking code must have allow_account_creation enabled.'
|
|
parameter name: :body, in: :body, required: true, schema: {
|
|
type: :object,
|
|
properties: {
|
|
linking_code: { type: :string, description: 'One-time linking code from mobile SSO onboarding redirect' },
|
|
first_name: { type: :string, description: 'First name (overrides value from SSO provider if provided)' },
|
|
last_name: { type: :string, description: 'Last name (overrides value from SSO provider if provided)' }
|
|
},
|
|
required: %w[linking_code]
|
|
}
|
|
|
|
response '200', 'account created and tokens issued' do
|
|
schema type: :object,
|
|
properties: {
|
|
access_token: { type: :string },
|
|
refresh_token: { type: :string },
|
|
token_type: { type: :string },
|
|
expires_in: { type: :integer },
|
|
created_at: { type: :integer },
|
|
user: {
|
|
type: :object,
|
|
properties: {
|
|
id: { type: :string, format: :uuid },
|
|
email: { type: :string },
|
|
first_name: { type: :string },
|
|
last_name: { type: :string },
|
|
ui_layout: { type: :string, enum: %w[dashboard intro] },
|
|
ai_enabled: { type: :boolean }
|
|
}
|
|
}
|
|
}
|
|
run_test!
|
|
end
|
|
|
|
response '400', 'missing linking code' do
|
|
schema '$ref' => '#/components/schemas/ErrorResponse'
|
|
run_test!
|
|
end
|
|
|
|
response '401', 'invalid or expired linking code' do
|
|
schema '$ref' => '#/components/schemas/ErrorResponse'
|
|
run_test!
|
|
end
|
|
|
|
response '403', 'account creation disabled' do
|
|
schema '$ref' => '#/components/schemas/ErrorResponse'
|
|
run_test!
|
|
end
|
|
|
|
response '422', 'user validation error' do
|
|
schema type: :object,
|
|
properties: {
|
|
errors: {
|
|
type: :array,
|
|
items: { type: :string }
|
|
}
|
|
}
|
|
run_test!
|
|
end
|
|
end
|
|
end
|
|
|
|
path '/api/v1/auth/enable_ai' do
|
|
patch 'Enable AI features for the authenticated user' do
|
|
tags 'Auth'
|
|
consumes 'application/json'
|
|
produces 'application/json'
|
|
security [ { apiKeyAuth: [] } ]
|
|
|
|
response '200', 'ai enabled' do
|
|
schema type: :object,
|
|
properties: {
|
|
user: {
|
|
type: :object,
|
|
properties: {
|
|
id: { type: :string, format: :uuid },
|
|
email: { type: :string },
|
|
first_name: { type: :string, nullable: true },
|
|
last_name: { type: :string, nullable: true },
|
|
ui_layout: { type: :string, enum: %w[dashboard intro] },
|
|
ai_enabled: { type: :boolean }
|
|
}
|
|
}
|
|
}
|
|
run_test!
|
|
end
|
|
|
|
response '401', 'unauthorized' do
|
|
schema '$ref' => '#/components/schemas/ErrorResponse'
|
|
run_test!
|
|
end
|
|
|
|
response '403', 'insufficient scope' do
|
|
schema '$ref' => '#/components/schemas/ErrorResponse'
|
|
run_test!
|
|
end
|
|
end
|
|
end
|
|
end
|