Skip to content

Commit

Permalink
support access for implicit accounts
Browse files Browse the repository at this point in the history
  • Loading branch information
petersalomonsen committed Dec 26, 2023
1 parent ac5ca36 commit c8df0d6
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 21 deletions.
13 changes: 4 additions & 9 deletions nearcontract/buildanddeploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,7 @@ RUSTFLAGS='-C link-arg=-s' cargo build --target=wasm32-unknown-unknown --release
# Remove unneeded WebAssembly exports
mkdir -p out
cp ./target/wasm32-unknown-unknown/release/rust_simple_access_control.wasm out/main.wasm
if [ -z "$1" ]
then
echo "Deploying to DEV account"
near dev-deploy out/main.wasm
else
# Deploy to account given in argument to this script
echo "Deploying to $1"
near deploy $1 out/nft.wasm
fi
echo "Deploying to DEV account"
near dev-deploy out/main.wasm
near call `cat neardev/dev-account` --accountId=`cat neardev/dev-account` init

28 changes: 17 additions & 11 deletions server/checkpermission.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,17 +80,23 @@ export async function checkPermission(repository, token) {
}

const account = await near.account(msgobj.accountId);
const accesskeys = await account.getAccessKeys();

const publicKeys = accesskeys.map(key =>
nearApi.utils.PublicKey.fromString(key.public_key)
);

const pubkey = publicKeys.find(pk =>
nacl.sign.detached.verify(new Uint8Array(sha256.array(msgbytes)),
new Uint8Array(signature), new Uint8Array(pk.data))
);

let pubkey;
if (msgobj.accountId.length == 64) {
pubkey = new Uint8Array(32);
for (let i = 0; i < pubkey.length; i++) {
pubkey[i] = parseInt(msgobj.accountId.substr(i * 2, 2), 16);
}
} else {
const accesskeys = await account.getAccessKeys();
const publicKeys = accesskeys.map(key =>
nearApi.utils.PublicKey.fromString(key.public_key)
);

pubkey = publicKeys.find(pk =>
nacl.sign.detached.verify(new Uint8Array(sha256.array(msgbytes)),
new Uint8Array(signature), new Uint8Array(pk.data))
);
}
if (pubkey) {
const contract = new nearApi.Contract(account,
nearconfig.contractName,
Expand Down
26 changes: 25 additions & 1 deletion server/checkpermission.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { checkPermission, PERMISSION_READER, PERMISSION_CONTRIBUTOR, use_testnet, PERMISSION_OWNER } from './checkpermission.js';
import { default as nearApi } from 'near-api-js';
import { default as nacl } from 'tweetnacl';
import { homedir } from 'os';
import * as assert from 'assert';
import { readFileSync } from 'fs';
Expand All @@ -26,14 +27,15 @@ async function setUpTestConnection() {

describe('checkpermission', function () {
this.timeout(20000);
const reponame = 'test2';

it('should allow anonymous reads for the default test repository', async () => {
assert.equal((await checkPermission('test', 'ANONYMOUS')).permission, PERMISSION_READER);
});
it('should become owner of repository', async () => {
const near = await setUpTestConnection();
const testAccount = await near.account(testAccountName);

const reponame = 'test2';
await testAccount.functionCall({
contractId: testAccount.accountId,
methodName: 'set_permission',
Expand All @@ -52,4 +54,26 @@ describe('checkpermission', function () {
const accessToken = tokenMessage + '.' + Buffer.from(signature.signature).toString('base64');
assert.equal((await checkPermission(reponame, accessToken)).permission, PERMISSION_OWNER);
});
it('should check access for implicit accounts', async () => {
const implicitAccountId = '3e393bf74cf63cf8e3851ea0fe6a92e56595f506947d7c66310e6ceac97a3e9f';
const near = await setUpTestConnection();
const testAccount = await near.account(testAccountName);
await testAccount.functionCall({
contractId: testAccount.accountId,
methodName: 'set_permission',
args: {
path: reponame,
account_id: implicitAccountId,
permission: PERMISSION_CONTRIBUTOR
},
attachedDeposit: '100000000000000000000000'
});
const privateKey = new Uint8Array([254, 114, 130, 212, 33, 69, 193, 93, 12, 15, 108, 76, 19, 198, 118, 148, 193, 62, 78, 4, 9, 157, 188, 191, 132, 137, 188, 31, 54, 103, 246, 191, 62, 57, 59, 247, 76, 246, 60, 248, 227, 133, 30, 160, 254, 106, 146, 229, 101, 149, 245, 6, 148, 125, 124, 102, 49, 14, 108, 234, 201, 122, 62, 159]);

const tokenMessage = btoa(JSON.stringify({ accountId: implicitAccountId, iat: new Date().getTime() }));
const signature = nacl.sign.detached(new TextEncoder().encode(tokenMessage), privateKey);
const accessToken = tokenMessage + '.' + btoa(String.fromCharCode(...signature));

assert.equal((await checkPermission(reponame, accessToken)).permission, PERMISSION_CONTRIBUTOR);
});
});

0 comments on commit c8df0d6

Please sign in to comment.