@@ -261,6 +261,8 @@ public actor GatewayChannelActor {
261261 let clientDisplayName = options . clientDisplayName ?? InstanceIdentity . displayName
262262 let clientId = options . clientId
263263 let clientMode = options . clientMode
264+ let role = options . role
265+ let scopes = options . scopes
264266
265267 let reqId = UUID ( ) . uuidString
266268 var client : [ String : ProtoAnyCodable ] = [
@@ -283,33 +285,36 @@ public actor GatewayChannelActor {
283285 " caps " : ProtoAnyCodable ( options . caps ) ,
284286 " locale " : ProtoAnyCodable ( primaryLocale ) ,
285287 " userAgent " : ProtoAnyCodable ( ProcessInfo . processInfo . operatingSystemVersionString ) ,
286- " role " : ProtoAnyCodable ( options . role ) ,
287- " scopes " : ProtoAnyCodable ( options . scopes ) ,
288+ " role " : ProtoAnyCodable ( role ) ,
289+ " scopes " : ProtoAnyCodable ( scopes ) ,
288290 ]
289291 if ! options . commands . isEmpty {
290292 params [ " commands " ] = ProtoAnyCodable ( options . commands )
291293 }
292294 if ! options . permissions . isEmpty {
293295 params [ " permissions " ] = ProtoAnyCodable ( options . permissions )
294296 }
295- if let token = self . token {
296- params [ " auth " ] = ProtoAnyCodable ( [ " token " : ProtoAnyCodable ( token ) ] )
297+ let identity = DeviceIdentityStore . loadOrCreate ( )
298+ let storedToken = DeviceAuthStore . loadToken ( deviceId : identity . deviceId , role : role ) ? . token
299+ let authToken = storedToken ?? self . token
300+ let canFallbackToShared = storedToken != nil && self . token != nil
301+ if let authToken {
302+ params [ " auth " ] = ProtoAnyCodable ( [ " token " : ProtoAnyCodable ( authToken ) ] )
297303 } else if let password = self . password {
298304 params [ " auth " ] = ProtoAnyCodable ( [ " password " : ProtoAnyCodable ( password ) ] )
299305 }
300- let identity = DeviceIdentityStore . loadOrCreate ( )
301306 let signedAtMs = Int ( Date ( ) . timeIntervalSince1970 * 1000 )
302307 let connectNonce = try await self . waitForConnectChallenge ( )
303- let scopes = options . scopes . joined ( separator : " , " )
308+ let scopesValue = scopes . joined ( separator : " , " )
304309 var payloadParts = [
305310 connectNonce == nil ? " v1 " : " v2 " ,
306311 identity . deviceId ,
307312 clientId ,
308313 clientMode ,
309- options . role ,
310- scopes ,
314+ role ,
315+ scopesValue ,
311316 String ( signedAtMs ) ,
312- self . token ?? " " ,
317+ authToken ?? " " ,
313318 ]
314319 if let connectNonce {
315320 payloadParts . append ( connectNonce )
@@ -336,11 +341,22 @@ public actor GatewayChannelActor {
336341 params : ProtoAnyCodable ( params ) )
337342 let data = try self . encoder . encode ( frame )
338343 try await self . task ? . send ( . data ( data ) )
339- let response = try await self . waitForConnectResponse ( reqId : reqId )
340- try await self . handleConnectResponse ( response )
344+ do {
345+ let response = try await self . waitForConnectResponse ( reqId : reqId )
346+ try await self . handleConnectResponse ( response , identity : identity , role : role )
347+ } catch {
348+ if canFallbackToShared {
349+ DeviceAuthStore . clearToken ( deviceId : identity . deviceId , role : role )
350+ }
351+ throw error
352+ }
341353 }
342354
343- private func handleConnectResponse ( _ res : ResponseFrame ) async throws {
355+ private func handleConnectResponse (
356+ _ res : ResponseFrame ,
357+ identity : DeviceIdentity ,
358+ role : String
359+ ) async throws {
344360 if res . ok == false {
345361 let msg = ( res . error ? [ " message " ] ? . value as? String ) ?? " gateway connect failed "
346362 throw NSError ( domain : " Gateway " , code : 1008 , userInfo : [ NSLocalizedDescriptionKey : msg ] )
@@ -358,6 +374,17 @@ public actor GatewayChannelActor {
358374 } else if let tick = ok . policy [ " tickIntervalMs " ] ? . value as? Int {
359375 self . tickIntervalMs = Double ( tick )
360376 }
377+ if let auth = ok . auth ,
378+ let deviceToken = auth [ " deviceToken " ] ? . value as? String {
379+ let authRole = auth [ " role " ] ? . value as? String ?? role
380+ let scopes = ( auth [ " scopes " ] ? . value as? [ ProtoAnyCodable ] ) ?
381+ . compactMap { $0 . value as? String } ?? [ ]
382+ _ = DeviceAuthStore . storeToken (
383+ deviceId : identity . deviceId ,
384+ role : authRole ,
385+ token : deviceToken ,
386+ scopes : scopes )
387+ }
361388 self . lastTick = Date ( )
362389 self . tickTask ? . cancel ( )
363390 self . tickTask = Task { [ weak self ] in
0 commit comments