hydra 客户端包装器
这个图书馆是一个包装 周围的 奥里海德拉 客户端 - @ ory/ 客户它规定:
OryOidcModule: 与 Ory Hydra Oidc( 公共) API 互动的模块OryOAuth2Module: 与 Ory Hydra OAuth2( admin) API 互动的模块
安装
npm install @getlarge/hydra-client-wrapper
使用量
导入应用程序中的模块 :
import {
OryIdentitiesModule,
OryFrontendModule,
} from '@getlarge/kratos-client-wrapper';
import { Module } from '@nestjs/common';
@Module({
imports: [
OryIdentitiesModule.forRoot({
basePath: 'http://localhost:4434',
accessToken: 'my-access-token',
}),
OryFrontendModule.forRootAsync({
useFactory: () => ({
baseUrl: 'http://localhost:4433',
}),
}),
],
})
export class YourModule {}
在您的提供者中注入服务 :
import { OryOAuth2Service } from '@getlarge/hydra-client-wrapper';
import { Injectable } from '@nestjs/common';
@Injectable()
export class YourService {
constructor(private readonly oryOAuth2Service: OryOAuth2Service) {}
async createClient(ownerId: string, scope = 'offline') {
return this.oryOAuth2Service.createOAuth2Client({
oAuth2Client: {
grant_types: ['client_credentials'],
access_token_strategy: 'opaque',
owner: ownerId,
scope,
},
});
}
}
利用警卫保护你的路线:
import { OryOAuth2AuthenticationGuard } from '@getlarge/hydra-client-wrapper';
import { Controller, Get, Logger, UseGuards } from '@nestjs/common';
@Controller()
export class YourController {
@UseGuards(
OryOAuth2AuthenticationGuard({
async postValidationHook(ctx, token) {
const req = ctx.switchToHttp().getRequest();
if (!token.client_id) {
throw new Error('Client ID not found in token');
}
const { data: client } = await this.oryService.getOAuth2Client({
id: token.client_id,
});
req.client = client;
},
accessTokenResolver: (ctx) =>
ctx
.switchToHttp()
.getRequest()
?.headers?.authorization?.replace('Bearer ', ''),
})
)
@Get()
async get() {
return 'Hello World';
}
}
发展
大楼
运行 nx build hydra-client-wrapper 来建造图书馆.
运行单元测试
运行 nx test hydra-client-wrapper 通过 欢乐。 。 。 .