@@ -4024,66 +4024,85 @@ extension NodeAppModel {
40244024 bootstrapToken ? . trimmingCharacters ( in : . whitespacesAndNewlines ) . isEmpty == false
40254025 }
40264026
4027+ private enum GatewayCredentialHandoffError : Error {
4028+ case missingRoles ( Set < String > )
4029+ case persistenceFailed
4030+ }
4031+
40274032 private func completeSuccessfulGatewayAuthHandoff (
40284033 stableID : String ,
40294034 routeGeneration : UInt64 ,
4030- issuedRoles : Set < String > ,
4031- nodeOptions : GatewayConnectOptions ) throws -> GatewayConnectOptions ?
4035+ authRoles : ( received : Set < String > , persisted : Set < String > ) ,
4036+ nodeOptions : GatewayConnectOptions ) async -> GatewayConnectOptions ?
40324037 {
40334038 guard self . isCurrentGatewayRoute ( generation : routeGeneration , stableID : stableID ) else { return nil }
4034-
4035- // Bootstrap authentication is single-use. Do not keep a consumed bootstrap
4036- // route alive unless both replacement sessions can authenticate from secure storage.
4037- guard issuedRoles . isSuperset ( of : [ " node " , " operator " ] ) else {
4038- return nodeOptions . allowStoredDeviceAuth ? nodeOptions : nil
4039- }
4040- guard let config = activeGatewayConnectConfig ,
4041- GatewayStableIdentifier . matches ( config . effectiveStableID , stableID )
4042- else { return nil }
4043- let instanceID = GatewaySettingsStore . currentInstanceID ( )
4044- let deviceAuthGatewayID = nodeOptions . deviceAuthGatewayID ?? stableID
4045- if let metadata = GatewaySettingsStore . loadGatewayCredentialMetadata (
4046- instanceId : instanceID ,
4047- gatewayStableID : deviceAuthGatewayID ) ,
4048- metadata . suppressStoredDeviceAuth
4049- {
4050- guard try GatewaySettingsStore . completeGatewayCredentialHandoff (
4051- instanceId : instanceID ,
4052- gatewayStableID : deviceAuthGatewayID )
4053- else { return nil }
4054- }
4055- var reconnectOptions = nodeOptions
4056- reconnectOptions . allowStoredDeviceAuth = true
4057- self . activeGatewayConnectConfig = GatewayConnectConfig (
4058- url : config . url ,
4059- stableID : config . stableID ,
4060- tls : config . tls ,
4061- token : config . token ,
4062- bootstrapToken : nil ,
4063- password : config . password ,
4064- nodeOptions : reconnectOptions )
4065-
4066- if self . operatorGatewayTask == nil ,
4067- self . shouldStartOperatorGatewayLoop (
4068- token : config . token ,
4069- bootstrapToken : nil ,
4070- password : config . password ,
4071- deviceAuthGatewayID : deviceAuthGatewayID ,
4072- allowStoredDeviceAuth : true )
4073- {
4074- let sessionBox = config . tls . map {
4075- WebSocketSessionBox ( session : GatewayTLSPinningSession ( params : $0 ) )
4039+ do {
4040+ // Bootstrap authentication is single-use. Do not keep a consumed bootstrap
4041+ // route alive unless both replacement sessions can authenticate from secure storage.
4042+ let requiredRoles : Set = [ " node " , " operator " ]
4043+ guard authRoles . persisted . isSuperset ( of : requiredRoles ) else {
4044+ if nodeOptions . allowStoredDeviceAuth {
4045+ return nodeOptions
4046+ }
4047+ let missingRoles = requiredRoles . subtracting ( authRoles . received )
4048+ throw missingRoles . isEmpty
4049+ ? GatewayCredentialHandoffError . persistenceFailed
4050+ : GatewayCredentialHandoffError . missingRoles ( missingRoles )
40764051 }
4077- self . startOperatorGatewayLoop (
4052+ guard let config = activeGatewayConnectConfig ,
4053+ GatewayStableIdentifier . matches ( config . effectiveStableID , stableID )
4054+ else { return nil }
4055+ let instanceID = GatewaySettingsStore . currentInstanceID ( )
4056+ let deviceAuthGatewayID = nodeOptions . deviceAuthGatewayID ?? stableID
4057+ if let metadata = GatewaySettingsStore . loadGatewayCredentialMetadata (
4058+ instanceId : instanceID ,
4059+ gatewayStableID : deviceAuthGatewayID ) ,
4060+ metadata . suppressStoredDeviceAuth
4061+ {
4062+ guard try GatewaySettingsStore . completeGatewayCredentialHandoff (
4063+ instanceId : instanceID ,
4064+ gatewayStableID : deviceAuthGatewayID )
4065+ else { throw GatewayCredentialHandoffError . persistenceFailed }
4066+ }
4067+ var reconnectOptions = nodeOptions
4068+ reconnectOptions . allowStoredDeviceAuth = true
4069+ self . activeGatewayConnectConfig = GatewayConnectConfig (
40784070 url : config . url ,
4079- stableID : stableID ,
4071+ stableID : config . stableID ,
4072+ tls : config . tls ,
40804073 token : config . token ,
40814074 bootstrapToken : nil ,
40824075 password : config . password ,
4083- nodeOptions : reconnectOptions ,
4084- sessionBox : sessionBox )
4076+ nodeOptions : reconnectOptions )
4077+
4078+ if self . operatorGatewayTask == nil ,
4079+ self . shouldStartOperatorGatewayLoop (
4080+ token : config . token ,
4081+ bootstrapToken : nil ,
4082+ password : config . password ,
4083+ deviceAuthGatewayID : deviceAuthGatewayID ,
4084+ allowStoredDeviceAuth : true )
4085+ {
4086+ let sessionBox = config . tls . map {
4087+ WebSocketSessionBox ( session : GatewayTLSPinningSession ( params : $0 ) )
4088+ }
4089+ self . startOperatorGatewayLoop (
4090+ url : config . url ,
4091+ stableID : stableID ,
4092+ token : config . token ,
4093+ bootstrapToken : nil ,
4094+ password : config . password ,
4095+ nodeOptions : reconnectOptions ,
4096+ sessionBox : sessionBox )
4097+ }
4098+ return reconnectOptions
4099+ } catch {
4100+ await self . handleGatewayCredentialHandoffFailure (
4101+ stableID : stableID ,
4102+ routeGeneration : routeGeneration ,
4103+ error : error )
4104+ return nil
40854105 }
4086- return reconnectOptions
40874106 }
40884107
40894108 private func gatewayOptionsAfterSuccessfulConnection (
@@ -4100,35 +4119,18 @@ extension NodeAppModel {
41004119 else {
41014120 return nodeOptions
41024121 }
4103- let issuedRoles = await nodeGateway . currentIssuedDeviceAuthRoles ( )
4104- guard self . isCurrentGatewayRoute ( generation : routeGeneration , stableID : stableID ) else { return nil }
4105- let reconnectOptions : GatewayConnectOptions ?
4106- do {
4107- reconnectOptions = try self . completeSuccessfulGatewayAuthHandoff (
4108- stableID : stableID ,
4109- routeGeneration : routeGeneration ,
4110- issuedRoles : issuedRoles ,
4111- nodeOptions : nodeOptions )
4112- } catch {
4113- await self . handleGatewayCredentialHandoffPersistenceFailure (
4114- stableID : stableID ,
4115- routeGeneration : routeGeneration ,
4116- error : error )
4117- return nil
4118- }
4119- guard let reconnectOptions else {
4120- await self . handleGatewayCredentialHandoffPersistenceFailure (
4121- stableID : stableID ,
4122- routeGeneration : routeGeneration )
4123- return nil
4124- }
4125- return reconnectOptions
4122+ let authRoles = await nodeGateway . currentDeviceAuthRoles ( )
4123+ return await self . completeSuccessfulGatewayAuthHandoff (
4124+ stableID : stableID ,
4125+ routeGeneration : routeGeneration ,
4126+ authRoles : authRoles ,
4127+ nodeOptions : nodeOptions )
41264128 }
41274129
4128- private func handleGatewayCredentialHandoffPersistenceFailure (
4130+ private func handleGatewayCredentialHandoffFailure (
41294131 stableID : String ,
41304132 routeGeneration : UInt64 ,
4131- error : Error ? = nil ) async
4133+ error : Error ) async
41324134 {
41334135 guard self . isCurrentGatewayRoute ( generation : routeGeneration , stableID : stableID ) else { return }
41344136 guard self . credentialHandoffFailureGeneration != routeGeneration else { return }
@@ -4141,18 +4143,39 @@ extension NodeAppModel {
41414143 await self . nodeGateway . disconnect ( )
41424144 await self . operatorGateway . disconnect ( )
41434145 guard self . isCurrentGatewayRoute ( generation : routeGeneration , stableID : stableID ) else { return }
4144- let technicalDetails = error . map {
4145- " Gateway credential handoff persistence failed: \( $0 . localizedDescription ) "
4146- } ?? " Gateway credential handoff persistence failed. "
4146+ let problem : GatewayConnectionProblem
4147+ let technicalDetails : String
4148+ switch error {
4149+ case let GatewayCredentialHandoffError . missingRoles ( roles ) :
4150+ technicalDetails = " Gateway credential handoff missing roles: \( roles . sorted ( ) . joined ( separator : " , " ) ) . "
4151+ problem = GatewayConnectionProblem (
4152+ kind : . unknown ,
4153+ owner : . gateway ,
4154+ title : " Gateway setup incomplete " ,
4155+ message : """
4156+ The Gateway did not provide all credentials required by this iPhone. \
4157+ Generate a new iPhone setup code on the Gateway, then scan it in Settings → Gateway. \
4158+ Automatic reconnect is paused until you retry setup.
4159+ """ ,
4160+ docsURL : URL ( string : " https://docs.openclaw.ai/platforms/ios " ) ,
4161+ retryable : true ,
4162+ pauseReconnect : true ,
4163+ technicalDetails : technicalDetails )
4164+ default :
4165+ technicalDetails = error is GatewayCredentialHandoffError
4166+ ? " Gateway credential handoff persistence failed. "
4167+ : " Gateway credential handoff persistence failed: \( error . localizedDescription ) "
4168+ problem = GatewayConnectionProblem (
4169+ kind : . unknown ,
4170+ owner : . iphone ,
4171+ title : " Credential save failed " ,
4172+ message : " OpenClaw disconnected because it could not securely save the new gateway credential. " ,
4173+ retryable : true ,
4174+ pauseReconnect : true ,
4175+ technicalDetails : technicalDetails )
4176+ }
41474177 GatewayDiagnostics . log ( technicalDetails )
4148- self . applyGatewayConnectionProblem ( GatewayConnectionProblem (
4149- kind : . unknown ,
4150- owner : . iphone ,
4151- title : " Credential save failed " ,
4152- message : " OpenClaw disconnected because it could not securely save the new gateway credential. " ,
4153- retryable : true ,
4154- pauseReconnect : true ,
4155- technicalDetails : technicalDetails ) )
4178+ self . applyGatewayConnectionProblem ( problem )
41564179 }
41574180
41584181 private func refreshBackgroundReconnectSuppressionIfNeeded ( source : String ) {
@@ -4296,27 +4319,13 @@ extension NodeAppModel {
42964319 bootstrapToken : auth . bootstrapToken ,
42974320 password : auth . password )
42984321 if usedBootstrapToken {
4299- let issuedRoles = await nodeGateway . currentIssuedDeviceAuthRoles ( )
4300- guard self . isCurrentGatewayRoute ( generation : routeGeneration , stableID : stableID ) else { return }
4301- do {
4302- guard try self . completeSuccessfulGatewayAuthHandoff (
4303- stableID : stableID ,
4304- routeGeneration : routeGeneration ,
4305- issuedRoles : issuedRoles ,
4306- nodeOptions : nodeOptions ) != nil
4307- else {
4308- await self . handleGatewayCredentialHandoffPersistenceFailure (
4309- stableID : stableID ,
4310- routeGeneration : routeGeneration )
4311- return
4312- }
4313- } catch {
4314- await self . handleGatewayCredentialHandoffPersistenceFailure (
4315- stableID : stableID ,
4316- routeGeneration : routeGeneration ,
4317- error : error )
4318- return
4319- }
4322+ let authRoles = await nodeGateway . currentDeviceAuthRoles ( )
4323+ guard await self . completeSuccessfulGatewayAuthHandoff (
4324+ stableID : stableID ,
4325+ routeGeneration : routeGeneration ,
4326+ authRoles : authRoles ,
4327+ nodeOptions : nodeOptions ) != nil
4328+ else { return }
43204329 }
43214330
43224331 self . clearGatewayConnectionProblem ( )
@@ -10628,14 +10637,14 @@ extension NodeAppModel {
1062810637 }
1062910638
1063010639 func _test_completeSuccessfulGatewayAuthHandoff (
10631- issuedRoles : Set < String > ,
10632- nodeOptions : GatewayConnectOptions ) throws -> GatewayConnectOptions ?
10640+ authRoles : ( received : Set < String > , persisted : Set < String > ) ,
10641+ nodeOptions : GatewayConnectOptions ) async -> GatewayConnectOptions ?
1063310642 {
1063410643 guard let stableID = activeGatewayConnectConfig ? . effectiveStableID else { return nil }
10635- return try self . completeSuccessfulGatewayAuthHandoff (
10644+ return await self . completeSuccessfulGatewayAuthHandoff (
1063610645 stableID : stableID ,
1063710646 routeGeneration : self . gatewayRouteGeneration ,
10638- issuedRoles : issuedRoles ,
10647+ authRoles : authRoles ,
1063910648 nodeOptions : nodeOptions )
1064010649 }
1064110650
0 commit comments