-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Nip 05 whitelist #22
base: master
Are you sure you want to change the base?
Nip 05 whitelist #22
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -65,6 +65,12 @@ export type Config = { | |||||||
enabled: boolean; | ||||||||
proxy: string; | ||||||||
}; | ||||||||
whitelist: { | ||||||||
enabled: boolean; | ||||||||
domain: string; | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
I would add a manual list of pubkeys in case someone wants a short hard-coded list. and I would rename |
||||||||
errorMessage: string; | ||||||||
fetchDelay: number; | ||||||||
}; | ||||||||
}; | ||||||||
|
||||||||
function loadYaml(filepath: string, content: string) { | ||||||||
|
@@ -92,6 +98,12 @@ const defaultConfig: Config = { | |||||||
media: { enabled: false, requireAuth: true, requirePubkeyInRule: false }, | ||||||||
list: { requireAuth: false, allowListOthers: false }, | ||||||||
tor: { enabled: false, proxy: "" }, | ||||||||
whitelist: { | ||||||||
enabled: true, | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Should default to false |
||||||||
domain: "", | ||||||||
errorMessage: "You are not authorized to upload.", | ||||||||
fetchDelay: 3600, | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could be renamed to something more intuitive like |
||||||||
}, | ||||||||
}; | ||||||||
|
||||||||
const searchPlaces = ["config.yaml", "config.yml", "config.json"]; | ||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import axios from 'axios'; | ||
import { config } from './config.js'; | ||
|
||
let whitelistCache: Set<string> = new Set(); | ||
let lastFetchTime = 0; | ||
|
||
export async function fetchWhitelist() { | ||
if (!config.whitelist.enabled) { | ||
whitelistCache.clear(); | ||
return whitelistCache; | ||
} | ||
|
||
const now = Date.now(); | ||
if (now - lastFetchTime < config.whitelist.fetchDelay * 1000) { | ||
return whitelistCache; | ||
} | ||
|
||
try { | ||
const response = await axios.get(`https://${config.whitelist.domain}/.well-known/nostr.json`); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. axios and be easily replaced here with the built-in const response = await fetch(`https://${config.whitelist.domain}/.well-known/nostr.json`).then(res => res.json()) |
||
const data = response.data; | ||
if (data && data.names) { | ||
whitelistCache = new Set(Object.values(data.names)); | ||
lastFetchTime = now; | ||
} | ||
} catch (error) { | ||
console.error("Failed to fetch whitelist:", error); | ||
} | ||
|
||
return whitelistCache; | ||
} | ||
|
||
export function isWhitelisted(pubkey: string): boolean { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would convert this method to |
||
if (!config.whitelist.enabled) return true; // Allow all if whitelist is disabled | ||
return whitelistCache.has(pubkey); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This code should be moved to the
checkUpload
method ( in the"check auth" block ) so it applies to both the
/uploadand
/media` endpointIt also wont have to check if
ctx.state.auth
exists then ( there are some configurations that allow uploads without auth )