https://github.com/giuseppegrieco/loopback4-kratos
A simple Ory Kratos integration in loopback4 applications
https://github.com/giuseppegrieco/loopback4-kratos
kratos loopback4 loopback4-kratos
Last synced: about 2 months ago
JSON representation
A simple Ory Kratos integration in loopback4 applications
- Host: GitHub
- URL: https://github.com/giuseppegrieco/loopback4-kratos
- Owner: giuseppegrieco
- License: gpl-3.0
- Created: 2022-09-17T21:48:09.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2022-12-25T23:56:59.000Z (over 2 years ago)
- Last Synced: 2025-02-26T08:49:20.522Z (2 months ago)
- Topics: kratos, loopback4, loopback4-kratos
- Language: TypeScript
- Homepage:
- Size: 306 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-ory - Ory Kratos Loopback4 integration
- awesome-ory - Ory Kratos Loopback4 integration
README
# loopback4-kratos
[](https://github.com/giuseppegrieco/loopback4-kratos/actions/workflows/npm.yml)A simple Ory Kratos integration in loopback4 applications.
[![LoopBack]()](http://loopback.io/)
## Installation
Install KratosComponent using `npm`;
```sh
$ [npm install | yarn add] loopback4-kratos
```## Basic Use
```ts
import {AuthenticationComponent} from '@loopback/authentication';import {
KratosComponentBindings,
KratosComponent,
} from 'loopback4-kratos';// ...
export class MyApplication extends BootMixin(
ServiceMixin(RepositoryMixin(RestApplication)),
) {
constructor(options: ApplicationConfig = {}) {
// ...this.component(AuthenticationComponent);
this.component(KratosComponent);
this.bind(KratosComponentBindings.CONFIG).to({
baseUrl: 'http://kratos_url'
});
// To register a custom user service
this.bind(KratosComponentBindings.USER_SERVICE.key).toClass(
MyUserService
);// ...
}// ...
}
```It is therefore necessary to define a new user service:
```ts
import {UserProfile} from '@loopback/security';import {
KratosUserService
} from 'loopback4-kratos';
import {Session} from '@ory/kratos-client';// ...
export class MyUserService extends KratosUserService {
convertToUserProfile(response: Session): UserProfile {
const ans = super.convertToUserProfile(response);
// Implement your strategy ...
return ans;
}// ...
}
```After this, you can just use Kratos as authentication strategy across application.
```ts
import {authenticate} from '@loopback/authentication';
import {get} from '@loopback/rest';// ...
export class YourController {
@get('/foo')
@authenticate('kratos')
foo() {
// this request is protected by kratos authentication
}// ...
}
```