Skip to content

Add verification functions #40

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
/_site/
.netlify/


## Generic files to ignore
*~
*.DS_Store
Expand Down Expand Up @@ -33,6 +32,7 @@ codekit-config.json
dwsync.xml
node_modules
npm-debug.log
.vscode/


## Compiled source
Expand Down
19 changes: 19 additions & 0 deletions functions/common/errors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export class UnverifiedHostnameError extends Error {
constructor(message, hostname, args) {
super(`${message}: ${JSON.stringify({
hostname,
...args
}, null, 2)}`)

Error.captureStackTrace(this, UnverifiedHostnameError)
}
}

export class RedirectError extends Error {
constructor(message, metadata = null) {
const msg = (metadata) ? `${message}: ${JSON.stringify(metadata, null, 2)}` : message
super(msg)

Error.captureStackTrace(this, RedirectError)
}
}
40 changes: 40 additions & 0 deletions functions/verify.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import fetch from 'node-fetch'
import members from '../data/members'
import {RedirectError} from './common/errors'

members.push({
url: 'https://httpbin.org/status/302'
})

export const handler = async (event, context) => {
const errors = []

for (const member of members) {
const url = member.url

try {
const res = await fetch(url, {
redirect: 'manual'
})

if (!res.ok) {
// TODO: figure out what to do to remove the URL from the webring
errors.push(new RedirectError(`did not receive a 2xx response from ${url}`, {
status: res.status,
statusText: res.statusText,
headers: res.headers.raw()
}))
}
} catch (err) {
errors.push(err)
}
}

if (errors.length > 0) {
errors.forEach((e) => console.log(e))
}

return {
statusCode: 200,
}
}
50 changes: 50 additions & 0 deletions functions/verifyDNS.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import {Resolver} from 'node:dns/promises'
import members from '../data/members'
import {UnverifiedHostnameError} from './common/errors'

const TXTPREFIX = 'a11y-webring-club-verification'

const resolver = new Resolver()

export const handler = async (event, context) => {
const errors = []

for (const member of members) {
// if we haven't defined a key for a member we won't verify
if (!('key' in member)) {
continue
}

const url = new URL(member.url)

try {
const res = await resolver.resolveTxt(url.hostname)

res.flat().forEach((el) => {
if (!el.startsWith(TXTPREFIX)) {
return
}

const key = el.split('=').pop()
if (key !== member.key) {
// TODO: figure out what to do to remove the URL from the webring
errors.push(new UnverifiedHostnameError(`could not verify: ${member.url}`, url.hostname, {
memberKey: member.key,
providedKey: key,
dnsRecords: res.flat()
}))
}
})
} catch (err) {
errors.push(new UnverifiedHostnameError(err, url.hostname))
}
}

if (errors.length > 0) {
errors.forEach((e) => console.log(e))
}

return {
statusCode: 200,
}
}
6 changes: 6 additions & 0 deletions netlify.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
[functions]
directory = "functions"

[functions."verify"]
schedule = "@daily"

[functions."verifyDNS"]
schedule = "@weekly

[[headers]]
for = "/*"
[headers.values]
Expand Down
Loading