@atproto/ozone
Version:
Backend service for moderating the Bluesky network.
128 lines • 4.83 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.VerificationIssuer = void 0;
const api_1 = require("@atproto/api");
const HANDLE_INVALID = 'handle.invalid';
class VerificationIssuer {
constructor(verifierConfig) {
Object.defineProperty(this, "verifierConfig", {
enumerable: true,
configurable: true,
writable: true,
value: verifierConfig
});
Object.defineProperty(this, "session", {
enumerable: true,
configurable: true,
writable: true,
value: new api_1.CredentialSession(new URL(this.verifierConfig.url))
});
Object.defineProperty(this, "agent", {
enumerable: true,
configurable: true,
writable: true,
value: new api_1.Agent(this.session)
});
}
static creator() {
return (verifierConfig) => new VerificationIssuer(verifierConfig);
}
async getAgent() {
if (!this.session.hasSession) {
await this.session.login({
identifier: this.verifierConfig.did,
password: this.verifierConfig.password,
});
}
// Trigger a test request to check if the session is still valid, if not, we will login again
try {
await this.agent.com.atproto.server.getSession();
}
catch (err) {
if (err.status === 401) {
await this.session.login({
identifier: this.verifierConfig.did,
password: this.verifierConfig.password,
});
}
}
return this.agent;
}
async verify(verifications) {
const grantedVerifications = [];
const failedVerifications = [];
const now = new Date().toISOString();
const agent = await this.getAgent();
await Promise.allSettled(verifications.map(async ({ displayName, handle, subject, createdAt }) => {
if (handle.toLowerCase() === HANDLE_INVALID) {
failedVerifications.push({
$type: 'tools.ozone.verification.grantVerifications#grantError',
error: 'Cannot verify with invalid handle',
subject,
});
return;
}
try {
const verificationRecord = {
createdAt: createdAt || now,
issuer: this.verifierConfig.did,
displayName,
handle,
subject,
};
const { data: { uri, cid }, } = await agent.com.atproto.repo.createRecord({
repo: this.verifierConfig.did,
record: verificationRecord,
collection: 'app.bsky.graph.verification',
});
grantedVerifications.push({
...verificationRecord,
uri,
cid,
revokedAt: null,
updatedAt: now,
revokedBy: null,
revokeReason: null,
});
}
catch (err) {
failedVerifications.push({
$type: 'tools.ozone.verification.grantVerifications#grantError',
error: err.message,
subject,
});
return;
}
}));
return { grantedVerifications, failedVerifications };
}
async revoke({ uris }) {
const revokedVerifications = [];
const failedRevocations = [];
const agent = await this.getAgent();
await Promise.allSettled(uris.map(async (uri) => {
try {
const atUri = new api_1.AtUri(uri);
if (atUri.collection !== 'app.bsky.graph.verification') {
throw new Error(`Only verification records can be revoked`);
}
if (atUri.host !== this.verifierConfig.did) {
throw new Error(`Cannot revoke verification record ${uri} not issued by ${this.verifierConfig.did}`);
}
await agent.com.atproto.repo.deleteRecord({
collection: atUri.collection,
repo: this.verifierConfig.did,
rkey: atUri.rkey,
});
revokedVerifications.push(uri);
}
catch (err) {
failedRevocations.push({ uri, error: err.message });
return;
}
}));
return { revokedVerifications, failedRevocations };
}
}
exports.VerificationIssuer = VerificationIssuer;
//# sourceMappingURL=issuer.js.map