Skip to content

Commit

Permalink
Merge pull request #37 from spknetwork/don't-require-posting-authorit…
Browse files Browse the repository at this point in the history
…y-to-sign-in

don't require posting auth to sign in
  • Loading branch information
sisygoboom authored Nov 18, 2024
2 parents a77d8f3 + c1fa638 commit 1f8d66c
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 43 deletions.
26 changes: 26 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,32 @@
"localRoot": "${workspaceFolder}",
"remoteRoot": "/usr/app",
"skipFiles": ["<node_internals>/**"]
},
{
"type": "node",
"request": "launch",
"name": "Debug Jest Tests",
"program": "${workspaceFolder}/node_modules/jest/bin/jest",
"args": [
"--runInBand"
],
"cwd": "${workspaceFolder}",
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"disableOptimisticBPs": true,
"windows": {
"program": "${workspaceFolder}/node_modules/jest/bin/jest"
},
"env": {
"NODE_ENV": "test"
},
"sourceMaps": true,
"runtimeArgs": [
"--experimental-vm-modules"
],
"outFiles": [
"${workspaceFolder}/dist/**/*.js"
]
}
]
}
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"type": "module",
"dependencies": {
"@hiveio/dhive": "^1.1.0",
"@hiveio/hive-js": "^2.0.7",
"@hiveio/hive-js": "^2.0.8",
"@magic-sdk/admin": "^2.1.0",
"@nestjs/common": "^10.0.0",
"@nestjs/config": "^3.2.2",
Expand Down
12 changes: 12 additions & 0 deletions src/repositories/hive-chain/hive-chain.repository.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,4 +349,16 @@ describe('HiveRepository', () => {
expect(hiveRepository.verifyPostingAuth(account)).toBe(false);
})
})

describe('hivePostExists', () => {
it('Returns true if the post exists', async () => {
const exists = await hiveRepository.hivePostExists({ author: 'sisygoboom', permlink: 'my-prayers-have-been-answered' });
expect(exists).toBe(true);
})

it('Returns false if the post does not exist', async () => {
const exists = await hiveRepository.hivePostExists({ author: 'fake', permlink: 'pretend post' });
expect(exists).toBe(false);
})
})
});
3 changes: 2 additions & 1 deletion src/repositories/hive-chain/hive-chain.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export class HiveChainRepository {
readonly _hiveJs = hiveJsPackage;
readonly _hive: Client = new Client(
process.env.HIVE_HOST?.split(',') || [
'https://hive-api.web3telekom.xyz',
'https://anyx.io',
'https://hived.privex.io',
'https://rpc.ausbit.dev',
Expand Down Expand Up @@ -62,7 +63,7 @@ export class HiveChainRepository {

async hivePostExists({ author, permlink }: AuthorPerm): Promise<boolean> {
const fetchContent = async (): Promise<boolean> => {
const content = await this._hiveJs.api.getContent(author, permlink);
const content = await this._hive.database.call('get_content', [author, permlink]);
return typeof content === 'object' && !!content.body;
};

Expand Down
25 changes: 0 additions & 25 deletions src/services/auth/auth.controller.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,31 +206,6 @@ describe('AuthController', () => {
})
})

it('Fails to log in when the user does not have posting authority', async () => {
const privateKey = PrivateKey.fromSeed(crypto.randomBytes(32).toString("hex"));
const message = { account: 'ned', ts: Date.now() };
const signature = privateKey.sign(crypto.createHash('sha256').update(JSON.stringify(message)).digest());

process.env.TEST_PUBLIC_KEY = privateKey.createPublic().toString();

const body = {
authority_type: 'posting',
proof_payload: message,
proof: signature.toString(),
}

return request(app.getHttpServer())
.post('/v1/auth/login/singleton/hive')
.send(body)
.expect(401)
.then(response => {
expect(response.body).toEqual({
errorType: "MISSING_POSTING_AUTHORITY",
reason: "Hive Account @ned has not granted posting authority to @threespeak"
})
})
})

it('Fails to log in when the proof is out of date', async () => {
const privateKey = PrivateKey.fromSeed(crypto.randomBytes(32).toString("hex"));
const message = { account: 'starkerz', ts: 1984 };
Expand Down
10 changes: 0 additions & 10 deletions src/services/auth/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,16 +119,6 @@ export class AuthController {
);
}

if (!this.hiveRepository.verifyPostingAuth(accountDetails)) {
throw new HttpException(
{
reason: `Hive Account @${body.proof_payload.account} has not granted posting authority to @threespeak`,
errorType: 'MISSING_POSTING_AUTHORITY',
},
HttpStatus.UNAUTHORIZED,
);
}

return await this.authService.authenticateUser('singleton', body.proof_payload.account, 'hive');
}

Expand Down
14 changes: 13 additions & 1 deletion src/services/uploader/uploading.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { BadRequestException, Injectable, NotFoundException } from '@nestjs/common';
import {
BadRequestException,
Injectable,
Logger,
LoggerService,
NotFoundException,
} from '@nestjs/common';
import { VideoRepository } from '../../repositories/video/video.repository';
import { UploadRepository } from '../../repositories/upload/upload.repository';
import { PublishingService } from '../../services/publishing/publishing.service';
Expand All @@ -12,6 +18,8 @@ import { HiveService } from '../hive/hive.service';

@Injectable()
export class UploadingService {
readonly #logger: LoggerService = new Logger(UploadingService.name);

constructor(
private readonly uploadRepository: UploadRepository,
private readonly videoRepository: VideoRepository,
Expand Down Expand Up @@ -109,6 +117,7 @@ export class UploadingService {
type: 'video',
});
if (!uploadJob) {
this.#logger.error('The upload job could not be located');
throw new NotFoundException('The upload job could not be located');
}
if (uploadJob.immediatePublish) {
Expand Down Expand Up @@ -149,12 +158,15 @@ export class UploadingService {

async handleTusdCallback(uploadMetaData: Upload) {
if (uploadMetaData.authorization === 'TESTING') {
this.#logger.error('TestAuthorizationError');
throw new Error('TestAuthorizationError');
}
if (uploadMetaData.Size >= 5000000000) {
this.#logger.error('File too big to be uploaded');
throw new Error('File too big to be uploaded');
}
if (!uploadMetaData.Storage) {
this.#logger.error('Storage is undefined');
throw new Error('Storage is undefined');
}
const info: ffmpeg.FfprobeData = await new Promise((resolve, reject) => {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/exponentialBackoff.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export async function exponentialBackoff<T>(
fn: () => Promise<T>,
retries = 3,
delay = 1000,
delay = 500,
): Promise<T> {
for (let i = 0; i < retries; i++) {
try {
Expand Down

0 comments on commit 1f8d66c

Please sign in to comment.