-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #124 from protokol/feat/credentialOffer
Feat/credential offer
- Loading branch information
Showing
99 changed files
with
6,697 additions
and
2,274 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.gitignor# Add directories or file patterns to ignore during indexing (e.g. foo/ or *.csv) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { Controller, Post, Body, Get, Delete, Param, UseGuards } from '@nestjs/common'; | ||
import { ApiKeyService } from './api-key.service'; | ||
import { ApiTags, ApiBearerAuth } from '@nestjs/swagger'; | ||
|
||
@Controller('api-keys') | ||
@ApiTags('API Keys') | ||
@ApiBearerAuth() | ||
export class ApiKeyController { | ||
constructor(private apiKeyService: ApiKeyService) { } | ||
|
||
@Post() | ||
async createApiKey( | ||
@Body() body: { name: string; allowedCredentialTypes: string[] } | ||
) { | ||
return await this.apiKeyService.createApiKey( | ||
body.name, | ||
body.allowedCredentialTypes | ||
); | ||
} | ||
|
||
@Get() | ||
async listApiKeys() { | ||
return await this.apiKeyService.listApiKeys(); | ||
} | ||
|
||
@Delete(':id') | ||
async revokeApiKey(@Param('id') id: string) { | ||
return await this.apiKeyService.revokeApiKey(parseInt(id)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { Injectable, CanActivate, ExecutionContext, UnauthorizedException } from '@nestjs/common'; | ||
import { InjectRepository } from '@nestjs/typeorm'; | ||
import { Repository } from 'typeorm'; | ||
import { ApiKey } from '../entities/api-key.entity'; | ||
import { Request } from 'express'; | ||
|
||
@Injectable() | ||
export class ApiKeyGuard implements CanActivate { | ||
constructor( | ||
@InjectRepository(ApiKey) | ||
private apiKeyRepository: Repository<ApiKey>, | ||
) { } | ||
|
||
async canActivate(context: ExecutionContext): Promise<boolean> { | ||
const request = context.switchToHttp().getRequest<Request>(); | ||
const apiKey = request.headers['x-api-key']; | ||
|
||
if (!apiKey) { | ||
throw new UnauthorizedException('API key is missing'); | ||
} | ||
|
||
const apiKeyEntity = await this.apiKeyRepository.findOne({ | ||
where: { key: apiKey as string, isActive: true } | ||
}); | ||
|
||
if (!apiKeyEntity) { | ||
throw new UnauthorizedException('Invalid API key'); | ||
} | ||
|
||
if (apiKeyEntity.expires_at && apiKeyEntity.expires_at < new Date()) { | ||
throw new UnauthorizedException('API key has expired'); | ||
} | ||
|
||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { Module } from "@nestjs/common"; | ||
import { TypeOrmModule } from "@nestjs/typeorm"; | ||
import { Student } from "../entities/student.entity"; | ||
import { VerifiableCredential } from "@entities/verifiableCredential.entity"; | ||
import { Did } from "../entities/did.entity"; | ||
import { ApiKeyService } from "./api-key.service"; | ||
import { ApiKeyController } from "./api-key.controller"; | ||
import { ApiKey } from "@entities/api-key.entity"; | ||
import { ApiKeyGuard } from "./api-key.guard"; | ||
|
||
@Module({ | ||
imports: [ | ||
TypeOrmModule.forFeature([ApiKey]) | ||
], | ||
providers: [ApiKeyService, ApiKeyGuard], | ||
controllers: [ApiKeyController], | ||
exports: [ApiKeyService, ApiKeyGuard, TypeOrmModule] | ||
}) | ||
export class ApiKeyModule { } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { Injectable, NotFoundException } from '@nestjs/common'; | ||
import { InjectRepository } from '@nestjs/typeorm'; | ||
import { Repository } from 'typeorm'; | ||
import { ApiKey } from '../entities/api-key.entity'; | ||
import { randomBytes } from 'crypto'; | ||
@Injectable() | ||
export class ApiKeyService { | ||
constructor( | ||
@InjectRepository(ApiKey) | ||
private apiKeyRepository: Repository<ApiKey>, | ||
) { } | ||
|
||
async createApiKey(name: string, allowedCredentialTypes: string[]): Promise<ApiKey> { | ||
const key = randomBytes(32).toString('hex'); | ||
|
||
const apiKey = this.apiKeyRepository.create({ | ||
key, | ||
name, | ||
allowedCredentialTypes | ||
}); | ||
|
||
return await this.apiKeyRepository.save(apiKey); | ||
} | ||
|
||
async listApiKeys(): Promise<ApiKey[]> { | ||
return await this.apiKeyRepository.find({ | ||
select: ['id', 'name', 'created_at', 'expires_at', 'isActive', 'allowedCredentialTypes', 'key'] | ||
}); | ||
} | ||
|
||
async revokeApiKey(id: number): Promise<void> { | ||
const apiKey = await this.apiKeyRepository.findOne({ where: { id } }); | ||
if (!apiKey) { | ||
throw new NotFoundException('API key not found'); | ||
} | ||
|
||
apiKey.isActive = false; | ||
await this.apiKeyRepository.save(apiKey); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
3cf9d25
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Deploy preview for ebsi-vector-frontend ready!
✅ Preview
https://ebsi-vector-frontend-5b6vywhlc-protokol.vercel.app
Built with commit 3cf9d25.
This pull request is being automatically deployed with vercel-action