惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
V2EX
IT之家
IT之家
博客园 - 叶小钗
雷峰网
雷峰网
T
Tailwind CSS Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
S
SegmentFault 最新的问题
Apple Machine Learning Research
Apple Machine Learning Research
爱范儿
爱范儿
博客园 - 【当耐特】
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
大猫的无限游戏
大猫的无限游戏
Last Week in AI
Last Week in AI
月光博客
月光博客
酷 壳 – CoolShell
酷 壳 – CoolShell
Jina AI
Jina AI
博客园 - Franky
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
宝玉的分享
宝玉的分享
阮一峰的网络日志
阮一峰的网络日志
Hugging Face - Blog
Hugging Face - Blog
博客园 - 司徒正美

Recent Commits to openclaw:main

test: merge chat side-result checks · openclaw/openclaw@ddd2c2a test: merge cron history checks · openclaw/openclaw@f7eb746 test: merge responsive navigation shell checks · openclaw/openclaw@c2e4b47 docs(changelog): add codex oauth fixes · openclaw/openclaw@628e6cd test: merge navigation routing cases · openclaw/openclaw@5d8cecb Tests: mock channel registry bundled fallback · openclaw/openclaw@2b08233 Secrets: avoid broad web search discovery for single plugin config · openclaw/openclaw@a464f59 test: merge config view browser checks · openclaw/openclaw@20cf511 fix(status): align oauth health with runtime · openclaw/openclaw@eed7116 feat: add macOS screen snapshots for monitor preview (#67954) thanks … · openclaw/openclaw@f377db1 fix: report shared auth scopes in hello-ok (#67810) thanks @BunsDev · openclaw/openclaw@0b6c39b Auto-reply: avoid eager bundled route fallback · openclaw/openclaw@3ea1bf4 Tests: narrow session binding contract setup · openclaw/openclaw@54e4e16 fix(macOS): enable undo/redo in webchat composer text input (#34962) · openclaw/openclaw@00951dc Tests: speed up channel setup promotion · openclaw/openclaw@82b529a Docs: refresh agent instructions · openclaw/openclaw@5775fe2 fix(auth): serialize OAuth refresh across agents to fix #26322 (#67876) · openclaw/openclaw@8e79080 test: allow ollama public surface boundary test · openclaw/openclaw@7d4f1a6 Docs: add test performance guardrails · openclaw/openclaw@89706d3 Tests: restore context-engine usage proof · openclaw/openclaw@e4c4f95 Tests: slim context engine runtime coverage · openclaw/openclaw@74c198f ci: retry failed custom checkouts · openclaw/openclaw@0ee5baf test: trim duplicate provider auth onboarding cases · openclaw/openclaw@1ffc02e matrix: fix sessions_spawn --thread subagent session spawning (#67643) · openclaw/openclaw@1ce2596 test: reduce auth choice fixture churn · openclaw/openclaw@857b9cd test: mock health status config boundaries · openclaw/openclaw@9d5ab4a test: mock onboard config io boundary · openclaw/openclaw@299694d test: mock legacy state plugin boundaries · openclaw/openclaw@2713089 test: mock channel install boundaries · openclaw/openclaw@b945248 test: mock doctor preview channel boundaries · openclaw/openclaw@b1a3ad4
fix ios share extension device identity · openclaw/opencl...
joshavant · 2026-06-19 · via Recent Commits to openclaw:main
11

import CryptoKit

22

import Foundation

334+

public enum GatewayDeviceIdentityProfile: String, Sendable {

5+

case primary

6+

case shareExtension

7+8+

var identityFileName: String {

9+

switch self {

10+

case .primary:

11+

"device.json"

12+

case .shareExtension:

13+

"share-device.json"

14+

}

15+

}

16+17+

var authFileName: String {

18+

switch self {

19+

case .primary:

20+

"device-auth.json"

21+

case .shareExtension:

22+

"share-device-auth.json"

23+

}

24+

}

25+

}

26+427

public struct DeviceIdentity: Codable, Sendable {

528

public var deviceId: String

629

public var publicKey: String

@@ -17,8 +40,40 @@ public struct DeviceIdentity: Codable, Sendable {

17401841

enum DeviceIdentityPaths {

1942

private static let stateDirEnv = ["OPENCLAW_STATE_DIR"]

43+

static let legacyMigrationMarkerFileName = ".legacy-migrated"

20442145

static func stateDirURL() -> URL {

46+

self.stateDirURL(

47+

overrideURL: self.stateDirOverrideURL(),

48+

legacyStateDirURL: self.legacyStateDirURL(),

49+

appGroupStateDirURL: self.appGroupStateDirURL(),

50+

temporaryDirectory: FileManager.default.temporaryDirectory)

51+

}

52+53+

static func stateDirURL(

54+

overrideURL: URL?,

55+

legacyStateDirURL: URL?,

56+

appGroupStateDirURL: URL?,

57+

temporaryDirectory: URL) -> URL

58+

{

59+

if let overrideURL {

60+

return overrideURL

61+

}

62+

if let appGroupStateDirURL {

63+

if let legacyStateDirURL {

64+

self.migrateLegacyIdentityDirectoryIfNeeded(

65+

from: legacyStateDirURL,

66+

to: appGroupStateDirURL)

67+

}

68+

return appGroupStateDirURL

69+

}

70+

if let legacyStateDirURL {

71+

return legacyStateDirURL

72+

}

73+

return temporaryDirectory.appendingPathComponent("openclaw", isDirectory: true)

74+

}

75+76+

private static func stateDirOverrideURL() -> URL? {

2277

for key in self.stateDirEnv {

2378

if let raw = getenv(key) {

2479

let value = String(cString: raw).trimmingCharacters(in: .whitespacesAndNewlines)

@@ -27,34 +82,93 @@ enum DeviceIdentityPaths {

2782

}

2883

}

2984

}

85+

return nil

86+

}

308788+

private static func legacyStateDirURL() -> URL? {

3189

if let appSupport = FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask).first {

3290

return appSupport.appendingPathComponent("OpenClaw", isDirectory: true)

3391

}

92+

return nil

93+

}

349435-

return FileManager.default.temporaryDirectory.appendingPathComponent("openclaw", isDirectory: true)

95+

private static func appGroupStateDirURL() -> URL? {

96+

guard

97+

let containerURL = FileManager.default

98+

.containerURL(forSecurityApplicationGroupIdentifier: OpenClawAppGroup.identifier)

99+

else {

100+

return nil

101+

}

102+

return containerURL.appendingPathComponent("OpenClaw", isDirectory: true)

103+

}

104+105+

private static func identityDirectoryURL(base: URL) -> URL {

106+

base.appendingPathComponent("identity", isDirectory: true)

107+

}

108+109+

private static func migrateLegacyIdentityDirectoryIfNeeded(from legacyStateDirURL: URL, to sharedStateDirURL: URL) {

110+

let fileManager = FileManager.default

111+

let legacyIdentityURL = self.identityDirectoryURL(base: legacyStateDirURL)

112+

let legacyDeviceURL = legacyIdentityURL.appendingPathComponent("device.json", isDirectory: false)

113+

let sharedIdentityURL = self.identityDirectoryURL(base: sharedStateDirURL)

114+

let sharedDeviceURL = sharedIdentityURL.appendingPathComponent("device.json", isDirectory: false)

115+

let markerURL = sharedIdentityURL.appendingPathComponent(self.legacyMigrationMarkerFileName, isDirectory: false)

116+

guard

117+

!fileManager.fileExists(atPath: markerURL.path) || !fileManager.fileExists(atPath: sharedDeviceURL.path),

118+

fileManager.fileExists(atPath: legacyDeviceURL.path)

119+

else {

120+

return

121+

}

122+123+

do {

124+

// The share extension can create app-group identity before the app migrates.

125+

// Until this marker exists, the app's legacy paired identity remains authoritative.

126+

try fileManager.createDirectory(at: sharedIdentityURL, withIntermediateDirectories: true)

127+

let entries = try fileManager.contentsOfDirectory(at: legacyIdentityURL, includingPropertiesForKeys: nil)

128+

var copiedNames = Set<String>()

129+

for entry in entries {

130+

let destination = sharedIdentityURL.appendingPathComponent(entry.lastPathComponent, isDirectory: false)

131+

if fileManager.fileExists(atPath: destination.path) {

132+

try fileManager.removeItem(at: destination)

133+

}

134+

try fileManager.copyItem(at: entry, to: destination)

135+

copiedNames.insert(entry.lastPathComponent)

136+

}

137+

for staleName in ["device.json", "device-auth.json"] where !copiedNames.contains(staleName) {

138+

let staleURL = sharedIdentityURL.appendingPathComponent(staleName, isDirectory: false)

139+

if fileManager.fileExists(atPath: staleURL.path) {

140+

try fileManager.removeItem(at: staleURL)

141+

}

142+

}

143+

try Data("1\n".utf8).write(to: markerURL, options: [.atomic])

144+

} catch {

145+

// Device identity migration is best-effort; callers will create fresh state if no readable identity exists.

146+

}

36147

}

37148

}

3814939150

public enum DeviceIdentityStore {

40-

private static let fileName = "device.json"

41151

private static let ed25519SPKIPrefix = Data([

42-

0x30, 0x2a, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65,

152+

0x30, 0x2A, 0x30, 0x05, 0x06, 0x03, 0x2B, 0x65,

43153

0x70, 0x03, 0x21, 0x00,

44154

])

45155

private static let ed25519PKCS8PrivatePrefix = Data([

46-

0x30, 0x2e, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06,

47-

0x03, 0x2b, 0x65, 0x70, 0x04, 0x22, 0x04, 0x20,

156+

0x30, 0x2E, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06,

157+

0x03, 0x2B, 0x65, 0x70, 0x04, 0x22, 0x04, 0x20,

48158

])

4915950160

public static func loadOrCreate() -> DeviceIdentity {

51-

self.loadOrCreate(fileURL: self.fileURL())

161+

self.loadOrCreate(profile: .primary)

162+

}

163+164+

public static func loadOrCreate(profile: GatewayDeviceIdentityProfile) -> DeviceIdentity {

165+

self.loadOrCreate(fileURL: self.fileURL(profile: profile))

52166

}

5316754168

static func loadOrCreate(fileURL url: URL) -> DeviceIdentity {

55169

if let data = try? Data(contentsOf: url) {

56170

switch self.decodeStoredIdentity(data) {

57-

case .identity(let decoded):

171+

case let .identity(decoded):

58172

return decoded

59173

case .recognizedInvalid:

60174

return self.generate()

@@ -143,7 +257,7 @@ public enum DeviceIdentityStore {

143257

let privateKeyData = Data(base64Encoded: identity.privateKey)

144258

else { return nil }

145259146-

guard publicKeyData.count == 32 && privateKeyData.count == 32,

260+

guard publicKeyData.count == 32, privateKeyData.count == 32,

147261

self.keyPairMatches(publicKeyData: publicKeyData, privateKeyData: privateKeyData)

148262

else { return nil }

149263

return DeviceIdentity(

@@ -211,11 +325,11 @@ public enum DeviceIdentityStore {

211325

}

212326

}

213327214-

private static func fileURL() -> URL {

328+

private static func fileURL(profile: GatewayDeviceIdentityProfile) -> URL {

215329

let base = DeviceIdentityPaths.stateDirURL()

216330

return base

217331

.appendingPathComponent("identity", isDirectory: true)

218-

.appendingPathComponent(self.fileName, isDirectory: false)

332+

.appendingPathComponent(profile.identityFileName, isDirectory: false)

219333

}

220334

}

221335