All checks were successful
continuous-integration/drone/pr Build is passing
ref #111
49 lines
2.0 KiB
TypeScript
49 lines
2.0 KiB
TypeScript
import * as jwt from "jsonwebtoken";
|
|
import { Get, JsonController, OnUndefined, Param } from 'routing-controllers';
|
|
import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi';
|
|
import { getConnectionManager, Repository } from 'typeorm';
|
|
import { config } from '../config';
|
|
import { InvalidCredentialsError } from '../errors/AuthError';
|
|
import { RunnerNotFoundError } from '../errors/RunnerErrors';
|
|
import { Runner } from '../models/entities/Runner';
|
|
import { ResponseSelfServiceRunner } from '../models/responses/ResponseSelfServiceRunner';
|
|
|
|
|
|
@JsonController('/runners')
|
|
export class RunnerSelfServiceController {
|
|
private runnerRepository: Repository<Runner>;
|
|
|
|
/**
|
|
* Gets the repository of this controller's model/entity.
|
|
*/
|
|
constructor() {
|
|
this.runnerRepository = getConnectionManager().get().getRepository(Runner);
|
|
}
|
|
|
|
@Get('/me/:jwt')
|
|
@ResponseSchema(ResponseSelfServiceRunner)
|
|
@ResponseSchema(RunnerNotFoundError, { statusCode: 404 })
|
|
@OnUndefined(RunnerNotFoundError)
|
|
@OpenAPI({ description: 'Lists all information about yourself. <br> Please provide your runner jwt(that code we gave you during registration) for auth. <br> If you lost your jwt/personalized link please contact support.' })
|
|
async get(@Param('jwt') token: string) {
|
|
return (new ResponseSelfServiceRunner(await this.getRunner(token)));
|
|
}
|
|
|
|
/**
|
|
* Get's a runner by a provided jwt token.
|
|
* @param token The runner jwt provided by the runner to identitfy themselves.
|
|
*/
|
|
private async getRunner(token: string): Promise<Runner> {
|
|
let jwtPayload = undefined
|
|
try {
|
|
jwtPayload = <any>jwt.verify(token, config.jwt_secret);
|
|
} catch (error) {
|
|
throw new InvalidCredentialsError();
|
|
}
|
|
|
|
const runner = await this.runnerRepository.findOne({ id: jwtPayload["id"] }, { relations: ['scans', 'group', 'scans.track', 'cards', 'distanceDonations', 'distanceDonations.donor', 'distanceDonations.runner', 'distanceDonations.runner.scans', 'distanceDonations.runner.scans.track'] });
|
|
if (!runner) { throw new RunnerNotFoundError() }
|
|
return runner;
|
|
}
|
|
|
|
} |