Skip to content
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

revert: src/infra/ucan.js to last working checkpoint #71

Merged
merged 1 commit into from
Apr 29, 2024
Merged
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
101 changes: 33 additions & 68 deletions src/infra/ucan.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,88 +8,53 @@ const serviceDID = config.SERVICE_DID;
let verify = (req, res, next) => {
req.requestId = uuidv4();
console.log('req.requestId: ', req.requestId);

let token = req.headers['authorization']; // Express headers are auto converted to lowercase
if (token && token.startsWith('Bearer ')) {
// Remove Bearer from string
token = token.slice(7, token.length);
}
const contractAddress = req.headers?.contract;
const invokerAddress = req.headers?.invoker;
const chainId = req.headers?.chain;
const contractAddress = req.headers && req.headers.contract;
const invokerAddress = req.headers && req.headers.invoker;
const chainId = req.headers && req.headers.chain;
req.isAuthenticated = false;
req.invokerAddress = invokerAddress;
req.contractAddress = contractAddress;
req.chainId = chainId;

// request remains not authenticated if no token is provided
if (!token) {
next()
return
}

if (contractAddress) {
collaboratorKey({
contractAddress, invokerAddress, chainId
}).then((invokerDID) => {
if (invokerDID) {
ucans.verify(token, {
// to make sure we're the intended recipient of this UCAN
audience: serviceDID,
// capabilities required for this invocation & which owner we expect for each capability
requiredCapabilities: [
{
capability: {
with: { scheme: "storage", hierPart: contractAddress.toLowerCase() },
can: { namespace: "file", segments: ["CREATE"] }
},
rootIssuer: invokerDID,
if (token && contractAddress) {
collaboratorKey({ contractAddress, invokerAddress, chainId })
.then((invokerDID) => {
if (invokerDID) {
ucans.verify(token, {
// to make sure we're the intended recipient of this UCAN
audience: serviceDID,
// capabilities required for this invocation & which owner we expect for each capability
requiredCapabilities: [
{
capability: {
with: { scheme: "storage", hierPart: contractAddress.toLowerCase() },
can: { namespace: "file", segments: ["CREATE"] }
},
rootIssuer: invokerDID,
}
],
}).then((result) => {
console.log(result);
if (result.ok) {
req.isAuthenticated = true;
}
],
}).then((result) => {
console.log(result);
if (result.ok) {
req.isAuthenticated = true;
}
next();
});
} else {
next();
});
} else {
}
})
.catch((error) => {
console.log(error);
next();
}
}).catch((error) => {
console.log(error);
next();
});
});
} else {
/* handle temp user auth
* here the invoker address is expected to be public DID of the temp user
* and the ucan token should be issued by the service DID
*/
ucans.verify(token, {
audience: serviceDID,
requiredCapabilities: [
{
capability: {
with: { scheme: "storage", hierPart: invokerAddress.toLowerCase() },
can: { namespace: "file", segments: ["CREATE"] }
},
rootIssuer: invokerAddress.toLowerCase(),
}
],
}).then((result) => {
console.log(result);
if (result.ok) {
req.isAuthenticated = true;
}
}).catch((error) => {
console.log(error);
});
next();
}


next();
};



module.exports = { verify };