Various AT Protocol integrations with obsidian

fix auth for non bsky pds (#10)

authored by

treethought and committed by
GitHub
2d4b808a 3d9257d2

+21 -15
+1 -1
manifest.json
··· 1 1 { 2 2 "id": "atmosphere", 3 3 "name": "Atmosphere", 4 - "version": "0.1.11", 4 + "version": "0.1.12", 5 5 "minAppVersion": "0.15.0", 6 6 "description": "Various integrations with AT Protocol.", 7 7 "author": "treethought",
+1 -1
package.json
··· 1 1 { 2 2 "name": "obsidian-atmosphere", 3 - "version": "0.1.11", 3 + "version": "0.1.12", 4 4 "description": "Various integrations with AT Protocol.", 5 5 "main": "main.js", 6 6 "type": "module",
+19 -13
src/lib/client.ts
··· 5 5 6 6 const DEFAULT_SERVICE = "https://bsky.social"; 7 7 8 - // Custom fetch function using Obsidian's requestUrl to avoid CORS issues 9 8 export interface Credentials { 10 9 identifier: string; 11 10 password: string; ··· 22 21 } 23 22 24 23 get loggedIn(): boolean { 25 - return !!this.hh.cm.session?.did; 24 + return !!this.hh.cm?.session?.did; 26 25 } 27 26 get session() { 28 - return this.hh.cm.session; 27 + return this.hh.cm?.session; 29 28 } 30 29 31 30 async getActor(identifier: string): Promise<ResolvedActor> { ··· 35 34 36 35 export class Handler implements FetchHandlerObject { 37 36 creds?: Credentials; 38 - cm: CredentialManager; 37 + cm?: CredentialManager; 39 38 cache: Cache 40 39 41 40 constructor(creds?: Credentials) { 42 41 this.creds = creds; 43 42 this.cache = new Cache(5 * 60 * 1000); // 5 minutes TTL 44 - this.cm = new CredentialManager({ service: DEFAULT_SERVICE }); 43 + } 44 + 45 + async initAuth(): Promise<void> { 46 + if (this.cm?.session?.did || !this.creds) { 47 + return; 48 + } 49 + 50 + const actor = await this.getActor(this.creds.identifier); 51 + this.cm = new CredentialManager({ service: actor.pds }); 52 + await this.cm.login(this.creds); 45 53 } 46 54 47 55 async getActor(identifier: string): Promise<ResolvedActor> { ··· 71 79 if (!repo) { 72 80 return null 73 81 } 74 - const own = (repo === this.cm.session?.handle || repo === this.cm.session?.did); 82 + const own = (repo === this.cm?.session?.handle || repo === this.cm?.session?.did); 75 83 if (!own) { 76 84 const actor = await this.getActor(repo); 77 85 return actor.pds ··· 80 88 } 81 89 82 90 async handle(pathname: string, init: RequestInit): Promise<Response> { 83 - if (this.creds && !this.cm.session?.did) { 84 - await this.cm.login(this.creds); 85 - if (this.cm.session?.did) { 86 - void this.getActor(this.cm.session?.did) 87 - } 88 - } 91 + await this.initAuth(); 89 92 90 93 const cacheKey = `${init?.method || "GET"}:${pathname}`; 91 94 if (init?.method?.toLowerCase() === "get") { ··· 100 103 if (pds) { 101 104 const sfh = simpleFetchHandler({ service: pds }); 102 105 resp = await sfh(pathname, init); 106 + } else if (this.cm) { 107 + resp = await this.cm.handle(pathname, init); 103 108 } else { 104 - resp = await this.cm.handle(pathname, init); 109 + const sfh = simpleFetchHandler({ service: DEFAULT_SERVICE }); 110 + resp = await sfh(pathname, init); 105 111 } 106 112 107 113 if (init?.method?.toLowerCase() === "get" && resp.ok) {