diff --git a/packages/api-gateway/src/auth-service/gen/api_key.ts b/packages/api-gateway/src/auth-service/gen/api_key.ts index 49bee97..4a6e60c 100644 --- a/packages/api-gateway/src/auth-service/gen/api_key.ts +++ b/packages/api-gateway/src/auth-service/gen/api_key.ts @@ -1,68 +1,64 @@ /* eslint-disable */ -import { GrpcMethod, GrpcStreamMethod } from '@nestjs/microservices'; -import { Observable } from 'rxjs'; +import { GrpcMethod, GrpcStreamMethod } from "@nestjs/microservices"; +import { Observable } from "rxjs"; -export const protobufPackage = 'authservice.api_key'; +export const protobufPackage = "authservice.api_key"; -export interface IssueApiKeyRequest {} +export interface IssueApiKeyRequest { +} + +export interface IssueApiKeyResponse { +} + +export interface RevokeApiKeyRequest { +} -export interface IssueApiKeyResponse {} +export interface RevokeApiKeyResponse { +} -export interface RevokeApiKeyRequest {} +export interface IsValidApiKeyRequest { +} -export interface RevokeApiKeyResponse {} +export interface IsValidApiKeyResponse { +} -export const AUTHSERVICE_API_KEY_PACKAGE_NAME = 'authservice.api_key'; +export const AUTHSERVICE_API_KEY_PACKAGE_NAME = "authservice.api_key"; export interface ApiKeyServiceClient { issueApiKey(request: IssueApiKeyRequest): Observable; revokeApiKey(request: RevokeApiKeyRequest): Observable; + + isValidApiKey(request: IsValidApiKeyRequest): Observable; } export interface ApiKeyServiceController { issueApiKey( request: IssueApiKeyRequest, - ): - | Promise - | Observable - | IssueApiKeyResponse; + ): Promise | Observable | IssueApiKeyResponse; revokeApiKey( request: RevokeApiKeyRequest, - ): - | Promise - | Observable - | RevokeApiKeyResponse; + ): Promise | Observable | RevokeApiKeyResponse; + + isValidApiKey( + request: IsValidApiKeyRequest, + ): Promise | Observable | IsValidApiKeyResponse; } export function ApiKeyServiceControllerMethods() { return function (constructor: Function) { - const grpcMethods: string[] = ['issueApiKey', 'revokeApiKey']; + const grpcMethods: string[] = ["issueApiKey", "revokeApiKey", "isValidApiKey"]; for (const method of grpcMethods) { - const descriptor: any = Reflect.getOwnPropertyDescriptor( - constructor.prototype, - method, - ); - GrpcMethod('ApiKeyService', method)( - constructor.prototype[method], - method, - descriptor, - ); + const descriptor: any = Reflect.getOwnPropertyDescriptor(constructor.prototype, method); + GrpcMethod("ApiKeyService", method)(constructor.prototype[method], method, descriptor); } const grpcStreamMethods: string[] = []; for (const method of grpcStreamMethods) { - const descriptor: any = Reflect.getOwnPropertyDescriptor( - constructor.prototype, - method, - ); - GrpcStreamMethod('ApiKeyService', method)( - constructor.prototype[method], - method, - descriptor, - ); + const descriptor: any = Reflect.getOwnPropertyDescriptor(constructor.prototype, method); + GrpcStreamMethod("ApiKeyService", method)(constructor.prototype[method], method, descriptor); } }; } -export const API_KEY_SERVICE_NAME = 'ApiKeyService'; +export const API_KEY_SERVICE_NAME = "ApiKeyService"; diff --git a/packages/api-gateway/src/auth-service/gen/jwt.ts b/packages/api-gateway/src/auth-service/gen/jwt.ts index e432e0e..0b2915e 100644 --- a/packages/api-gateway/src/auth-service/gen/jwt.ts +++ b/packages/api-gateway/src/auth-service/gen/jwt.ts @@ -1,18 +1,30 @@ /* eslint-disable */ -import { GrpcMethod, GrpcStreamMethod } from '@nestjs/microservices'; -import { Observable } from 'rxjs'; +import { GrpcMethod, GrpcStreamMethod } from "@nestjs/microservices"; +import { Observable } from "rxjs"; -export const protobufPackage = 'authservice.jwt'; +export const protobufPackage = "authservice.jwt"; -export interface CreateJwtRequest {} +export interface CreateJwtRequestHeader { + XApiKey: string; +} + +export interface CreateJwtRequest { + header: CreateJwtRequestHeader | undefined; +} -export interface CreateJwtResponse {} +export interface CreateJwtResponse { + success: boolean; + error: string; + jwt?: string | undefined; +} -export interface ValidateJwtRequest {} +export interface ValidateJwtRequest { +} -export interface ValidateJwtResponse {} +export interface ValidateJwtResponse { +} -export const AUTHSERVICE_JWT_PACKAGE_NAME = 'authservice.jwt'; +export const AUTHSERVICE_JWT_PACKAGE_NAME = "authservice.jwt"; export interface JwtServiceClient { createJwt(request: CreateJwtRequest): Observable; @@ -21,48 +33,26 @@ export interface JwtServiceClient { } export interface JwtServiceController { - createJwt( - request: CreateJwtRequest, - ): - | Promise - | Observable - | CreateJwtResponse; + createJwt(request: CreateJwtRequest): Promise | Observable | CreateJwtResponse; validateJwt( request: ValidateJwtRequest, - ): - | Promise - | Observable - | ValidateJwtResponse; + ): Promise | Observable | ValidateJwtResponse; } export function JwtServiceControllerMethods() { return function (constructor: Function) { - const grpcMethods: string[] = ['createJwt', 'validateJwt']; + const grpcMethods: string[] = ["createJwt", "validateJwt"]; for (const method of grpcMethods) { - const descriptor: any = Reflect.getOwnPropertyDescriptor( - constructor.prototype, - method, - ); - GrpcMethod('JwtService', method)( - constructor.prototype[method], - method, - descriptor, - ); + const descriptor: any = Reflect.getOwnPropertyDescriptor(constructor.prototype, method); + GrpcMethod("JwtService", method)(constructor.prototype[method], method, descriptor); } const grpcStreamMethods: string[] = []; for (const method of grpcStreamMethods) { - const descriptor: any = Reflect.getOwnPropertyDescriptor( - constructor.prototype, - method, - ); - GrpcStreamMethod('JwtService', method)( - constructor.prototype[method], - method, - descriptor, - ); + const descriptor: any = Reflect.getOwnPropertyDescriptor(constructor.prototype, method); + GrpcStreamMethod("JwtService", method)(constructor.prototype[method], method, descriptor); } }; } -export const JWT_SERVICE_NAME = 'JwtService'; +export const JWT_SERVICE_NAME = "JwtService"; diff --git a/packages/auth-service/src/gen/api_key.ts b/packages/auth-service/src/gen/api_key.ts index 49bee97..4a6e60c 100644 --- a/packages/auth-service/src/gen/api_key.ts +++ b/packages/auth-service/src/gen/api_key.ts @@ -1,68 +1,64 @@ /* eslint-disable */ -import { GrpcMethod, GrpcStreamMethod } from '@nestjs/microservices'; -import { Observable } from 'rxjs'; +import { GrpcMethod, GrpcStreamMethod } from "@nestjs/microservices"; +import { Observable } from "rxjs"; -export const protobufPackage = 'authservice.api_key'; +export const protobufPackage = "authservice.api_key"; -export interface IssueApiKeyRequest {} +export interface IssueApiKeyRequest { +} + +export interface IssueApiKeyResponse { +} + +export interface RevokeApiKeyRequest { +} -export interface IssueApiKeyResponse {} +export interface RevokeApiKeyResponse { +} -export interface RevokeApiKeyRequest {} +export interface IsValidApiKeyRequest { +} -export interface RevokeApiKeyResponse {} +export interface IsValidApiKeyResponse { +} -export const AUTHSERVICE_API_KEY_PACKAGE_NAME = 'authservice.api_key'; +export const AUTHSERVICE_API_KEY_PACKAGE_NAME = "authservice.api_key"; export interface ApiKeyServiceClient { issueApiKey(request: IssueApiKeyRequest): Observable; revokeApiKey(request: RevokeApiKeyRequest): Observable; + + isValidApiKey(request: IsValidApiKeyRequest): Observable; } export interface ApiKeyServiceController { issueApiKey( request: IssueApiKeyRequest, - ): - | Promise - | Observable - | IssueApiKeyResponse; + ): Promise | Observable | IssueApiKeyResponse; revokeApiKey( request: RevokeApiKeyRequest, - ): - | Promise - | Observable - | RevokeApiKeyResponse; + ): Promise | Observable | RevokeApiKeyResponse; + + isValidApiKey( + request: IsValidApiKeyRequest, + ): Promise | Observable | IsValidApiKeyResponse; } export function ApiKeyServiceControllerMethods() { return function (constructor: Function) { - const grpcMethods: string[] = ['issueApiKey', 'revokeApiKey']; + const grpcMethods: string[] = ["issueApiKey", "revokeApiKey", "isValidApiKey"]; for (const method of grpcMethods) { - const descriptor: any = Reflect.getOwnPropertyDescriptor( - constructor.prototype, - method, - ); - GrpcMethod('ApiKeyService', method)( - constructor.prototype[method], - method, - descriptor, - ); + const descriptor: any = Reflect.getOwnPropertyDescriptor(constructor.prototype, method); + GrpcMethod("ApiKeyService", method)(constructor.prototype[method], method, descriptor); } const grpcStreamMethods: string[] = []; for (const method of grpcStreamMethods) { - const descriptor: any = Reflect.getOwnPropertyDescriptor( - constructor.prototype, - method, - ); - GrpcStreamMethod('ApiKeyService', method)( - constructor.prototype[method], - method, - descriptor, - ); + const descriptor: any = Reflect.getOwnPropertyDescriptor(constructor.prototype, method); + GrpcStreamMethod("ApiKeyService", method)(constructor.prototype[method], method, descriptor); } }; } -export const API_KEY_SERVICE_NAME = 'ApiKeyService'; +export const API_KEY_SERVICE_NAME = "ApiKeyService"; diff --git a/packages/auth-service/src/gen/jwt.ts b/packages/auth-service/src/gen/jwt.ts index e432e0e..0b2915e 100644 --- a/packages/auth-service/src/gen/jwt.ts +++ b/packages/auth-service/src/gen/jwt.ts @@ -1,18 +1,30 @@ /* eslint-disable */ -import { GrpcMethod, GrpcStreamMethod } from '@nestjs/microservices'; -import { Observable } from 'rxjs'; +import { GrpcMethod, GrpcStreamMethod } from "@nestjs/microservices"; +import { Observable } from "rxjs"; -export const protobufPackage = 'authservice.jwt'; +export const protobufPackage = "authservice.jwt"; -export interface CreateJwtRequest {} +export interface CreateJwtRequestHeader { + XApiKey: string; +} + +export interface CreateJwtRequest { + header: CreateJwtRequestHeader | undefined; +} -export interface CreateJwtResponse {} +export interface CreateJwtResponse { + success: boolean; + error: string; + jwt?: string | undefined; +} -export interface ValidateJwtRequest {} +export interface ValidateJwtRequest { +} -export interface ValidateJwtResponse {} +export interface ValidateJwtResponse { +} -export const AUTHSERVICE_JWT_PACKAGE_NAME = 'authservice.jwt'; +export const AUTHSERVICE_JWT_PACKAGE_NAME = "authservice.jwt"; export interface JwtServiceClient { createJwt(request: CreateJwtRequest): Observable; @@ -21,48 +33,26 @@ export interface JwtServiceClient { } export interface JwtServiceController { - createJwt( - request: CreateJwtRequest, - ): - | Promise - | Observable - | CreateJwtResponse; + createJwt(request: CreateJwtRequest): Promise | Observable | CreateJwtResponse; validateJwt( request: ValidateJwtRequest, - ): - | Promise - | Observable - | ValidateJwtResponse; + ): Promise | Observable | ValidateJwtResponse; } export function JwtServiceControllerMethods() { return function (constructor: Function) { - const grpcMethods: string[] = ['createJwt', 'validateJwt']; + const grpcMethods: string[] = ["createJwt", "validateJwt"]; for (const method of grpcMethods) { - const descriptor: any = Reflect.getOwnPropertyDescriptor( - constructor.prototype, - method, - ); - GrpcMethod('JwtService', method)( - constructor.prototype[method], - method, - descriptor, - ); + const descriptor: any = Reflect.getOwnPropertyDescriptor(constructor.prototype, method); + GrpcMethod("JwtService", method)(constructor.prototype[method], method, descriptor); } const grpcStreamMethods: string[] = []; for (const method of grpcStreamMethods) { - const descriptor: any = Reflect.getOwnPropertyDescriptor( - constructor.prototype, - method, - ); - GrpcStreamMethod('JwtService', method)( - constructor.prototype[method], - method, - descriptor, - ); + const descriptor: any = Reflect.getOwnPropertyDescriptor(constructor.prototype, method); + GrpcStreamMethod("JwtService", method)(constructor.prototype[method], method, descriptor); } }; } -export const JWT_SERVICE_NAME = 'JwtService'; +export const JWT_SERVICE_NAME = "JwtService"; diff --git a/packages/auth-service/src/modules/api_key/api_key.controller.ts b/packages/auth-service/src/modules/api_key/api_key.controller.ts index 12b127c..d62e58b 100644 --- a/packages/auth-service/src/modules/api_key/api_key.controller.ts +++ b/packages/auth-service/src/modules/api_key/api_key.controller.ts @@ -2,6 +2,8 @@ import { Controller } from '@nestjs/common'; import { ApiKeyServiceController, ApiKeyServiceControllerMethods, + IsValidApiKeyRequest, + IsValidApiKeyResponse, IssueApiKeyRequest, IssueApiKeyResponse, RevokeApiKeyRequest, @@ -19,4 +21,9 @@ export class ApiKeyController implements ApiKeyServiceController { console.log(`request: ${request}`); throw new Error('Method not implemented.'); } + + async isValidApiKey(request: IsValidApiKeyRequest): Promise { + + return true + } } diff --git a/packages/auth-service/src/modules/jwt/jwt.controller.ts b/packages/auth-service/src/modules/jwt/jwt.controller.ts index 5c60762..10fd335 100644 --- a/packages/auth-service/src/modules/jwt/jwt.controller.ts +++ b/packages/auth-service/src/modules/jwt/jwt.controller.ts @@ -1,7 +1,10 @@ -import { Controller } from '@nestjs/common'; +import { Controller, Inject, Post } from '@nestjs/common'; +import { ClientGrpc } from '@nestjs/microservices'; +import { API_KEY_SERVICE_NAME, ApiKeyServiceClient } from 'src/gen/api_key'; import { CreateJwtRequest, CreateJwtResponse, + JWT_SERVICE_NAME, JwtServiceController, JwtServiceControllerMethods, ValidateJwtRequest, @@ -11,9 +14,25 @@ import { @Controller('jwt') @JwtServiceControllerMethods() export class JWTController implements JwtServiceController { + private apiKeyService: ApiKeyServiceClient; + + constructor(private apiKeyClient: ClientGrpc) { + this.apiKeyService = apiKeyClient.getService(API_KEY_SERVICE_NAME); + } + + @Post('authorize') async createJwt(request: CreateJwtRequest): Promise { - console.log(`request: ${request}`); - return {}; + const { header } = request; + const apiKey = header.XApiKey; + + if (!this.apiKeyService.isValidApiKey(apiKey)) { + return { + success: false, + error: "Provided API key is not valid" + } + } + + return } validateJwt(request: ValidateJwtRequest): Promise { console.log(`request: ${request}`); diff --git a/packages/proto/auth-service/api_key.proto b/packages/proto/auth-service/api_key.proto index c858e0c..89816ee 100644 --- a/packages/proto/auth-service/api_key.proto +++ b/packages/proto/auth-service/api_key.proto @@ -5,6 +5,7 @@ package authservice.api_key; service ApiKeyService { rpc issueApiKey(IssueApiKeyRequest) returns (IssueApiKeyResponse); rpc revokeApiKey(RevokeApiKeyRequest) returns (RevokeApiKeyResponse); + rpc isValidApiKey(IsValidApiKeyRequest) returns (IsValidApiKeyResponse); } message IssueApiKeyRequest {} @@ -12,3 +13,6 @@ message IssueApiKeyResponse {} message RevokeApiKeyRequest {} message RevokeApiKeyResponse {} + +message IsValidApiKeyRequest{} +message IsValidApiKeyResponse{} diff --git a/packages/proto/auth-service/jwt.proto b/packages/proto/auth-service/jwt.proto index 7294f19..08ae852 100644 --- a/packages/proto/auth-service/jwt.proto +++ b/packages/proto/auth-service/jwt.proto @@ -7,8 +7,22 @@ service JwtService { rpc validateJwt(ValidateJwtRequest) returns (ValidateJwtResponse); } -message CreateJwtRequest {} -message CreateJwtResponse {} +message CreateJwtRequestHeader{ + string X_Api_Key = 1 [json_name = "X-Api-Key"]; +} + +message CreateJwtRequest { + CreateJwtRequestHeader header = 1; +} +message CreateJwtResponse { + bool success = 1; + oneof error_optional{ + string error = 3; + } + oneof jwt_optional{ + string jwt = 3; + } +} message ValidateJwtRequest {} message ValidateJwtResponse {}